xref: /linux/drivers/cpuidle/cpuidle.c (revision c1d51f684c72b5eb2aecbbd47be3a2977a2dc903)
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,
78*c1d51f68SRafael J. Wysocki 			      u64 max_latency_ns,
790d94039fSRafael J. Wysocki 			      unsigned int forbidden_flags,
8028ba086eSRafael J. Wysocki 			      bool s2idle)
81a6220fc1SRafael J. Wysocki {
82*c1d51f68SRafael 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 ||
89*c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns <= latency_req ||
90*c1d51f68SRafael 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 
95*c1d51f68SRafael 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 /**
1020e7414b7SRafael J. Wysocki  * cpuidle_use_deepest_state - Set/clear governor override flag.
1030e7414b7SRafael J. Wysocki  * @enable: New value of the flag.
1040e7414b7SRafael J. Wysocki  *
1050e7414b7SRafael J. Wysocki  * Set/unset the current CPU to use the deepest idle state (override governors
1060e7414b7SRafael J. Wysocki  * going forward if set).
1070e7414b7SRafael J. Wysocki  */
108bb8313b6SJacob Pan void cpuidle_use_deepest_state(bool enable)
109bb8313b6SJacob Pan {
110bb8313b6SJacob Pan 	struct cpuidle_device *dev;
111bb8313b6SJacob Pan 
112bb8313b6SJacob Pan 	preempt_disable();
113bb8313b6SJacob Pan 	dev = cpuidle_get_device();
11441dc750eSLi, Fei 	if (dev)
115bb8313b6SJacob Pan 		dev->use_deepest_state = enable;
116bb8313b6SJacob Pan 	preempt_enable();
117bb8313b6SJacob Pan }
118bb8313b6SJacob Pan 
119ef2b22acSRafael J. Wysocki /**
120ef2b22acSRafael J. Wysocki  * cpuidle_find_deepest_state - Find the deepest available idle state.
121ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
122ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
123ef2b22acSRafael J. Wysocki  */
124ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
125ef2b22acSRafael J. Wysocki 			       struct cpuidle_device *dev)
126ef2b22acSRafael J. Wysocki {
127*c1d51f68SRafael J. Wysocki 	return find_deepest_state(drv, dev, U64_MAX, 0, false);
128ef2b22acSRafael J. Wysocki }
129ef2b22acSRafael J. Wysocki 
130bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND
13128ba086eSRafael J. Wysocki static void enter_s2idle_proper(struct cpuidle_driver *drv,
132124cf911SRafael J. Wysocki 				struct cpuidle_device *dev, int index)
133124cf911SRafael J. Wysocki {
13464bdff69SRafael J. Wysocki 	ktime_t time_start, time_end;
13564bdff69SRafael J. Wysocki 
13664bdff69SRafael J. Wysocki 	time_start = ns_to_ktime(local_clock());
13764bdff69SRafael J. Wysocki 
138ae0afb4fSRafael J. Wysocki 	/*
139ae0afb4fSRafael J. Wysocki 	 * trace_suspend_resume() called by tick_freeze() for the last CPU
140ae0afb4fSRafael J. Wysocki 	 * executing it contains RCU usage regarded as invalid in the idle
141ae0afb4fSRafael J. Wysocki 	 * context, so tell RCU about that.
142ae0afb4fSRafael J. Wysocki 	 */
143ae0afb4fSRafael J. Wysocki 	RCU_NONIDLE(tick_freeze());
144124cf911SRafael J. Wysocki 	/*
145124cf911SRafael J. Wysocki 	 * The state used here cannot be a "coupled" one, because the "coupled"
146124cf911SRafael J. Wysocki 	 * cpuidle mechanism enables interrupts and doing that with timekeeping
147124cf911SRafael J. Wysocki 	 * suspended is generally unsafe.
148124cf911SRafael J. Wysocki 	 */
14963caae84SLucas Stach 	stop_critical_timings();
15028ba086eSRafael J. Wysocki 	drv->states[index].enter_s2idle(dev, drv, index);
151124cf911SRafael J. Wysocki 	WARN_ON(!irqs_disabled());
152124cf911SRafael J. Wysocki 	/*
153124cf911SRafael J. Wysocki 	 * timekeeping_resume() that will be called by tick_unfreeze() for the
154ae0afb4fSRafael J. Wysocki 	 * first CPU executing it calls functions containing RCU read-side
155124cf911SRafael J. Wysocki 	 * critical sections, so tell RCU about that.
156124cf911SRafael J. Wysocki 	 */
157124cf911SRafael J. Wysocki 	RCU_NONIDLE(tick_unfreeze());
15863caae84SLucas Stach 	start_critical_timings();
15964bdff69SRafael J. Wysocki 
16064bdff69SRafael J. Wysocki 	time_end = ns_to_ktime(local_clock());
16164bdff69SRafael J. Wysocki 
16264bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
16364bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_usage++;
164124cf911SRafael J. Wysocki }
165124cf911SRafael J. Wysocki 
166a6220fc1SRafael J. Wysocki /**
16728ba086eSRafael J. Wysocki  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
168ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
169ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
17038106313SRafael J. Wysocki  *
17128ba086eSRafael J. Wysocki  * If there are states with the ->enter_s2idle callback, find the deepest of
172ef2b22acSRafael J. Wysocki  * them and enter it with frozen tick.
17338106313SRafael J. Wysocki  */
17428ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
17538106313SRafael J. Wysocki {
17638106313SRafael J. Wysocki 	int index;
17738106313SRafael J. Wysocki 
178124cf911SRafael J. Wysocki 	/*
17928ba086eSRafael J. Wysocki 	 * Find the deepest state with ->enter_s2idle present, which guarantees
180124cf911SRafael J. Wysocki 	 * that interrupts won't be enabled when it exits and allows the tick to
181124cf911SRafael J. Wysocki 	 * be frozen safely.
182124cf911SRafael J. Wysocki 	 */
183*c1d51f68SRafael J. Wysocki 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
1846f16886bSSudeep Holla 	if (index > 0)
18528ba086eSRafael J. Wysocki 		enter_s2idle_proper(drv, dev, index);
186124cf911SRafael J. Wysocki 
187ef2b22acSRafael J. Wysocki 	return index;
18838106313SRafael J. Wysocki }
18987e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
19038106313SRafael J. Wysocki 
19138106313SRafael J. Wysocki /**
19256cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
19356cfbf74SColin Cross  * @dev: cpuidle device for this cpu
19456cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
1957312280bSRafael J. Wysocki  * @index: index into the states table in @drv of the state to enter
19656cfbf74SColin Cross  */
19756cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
198554c06baSDaniel Lezcano 			int index)
19956cfbf74SColin Cross {
20056cfbf74SColin Cross 	int entered_state;
20156cfbf74SColin Cross 
202554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
203df8d9eeaSRafael J. Wysocki 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
204dbd1b8eaSShreyas B. Prabhu 	ktime_t time_start, time_end;
205554c06baSDaniel Lezcano 
206df8d9eeaSRafael J. Wysocki 	/*
207df8d9eeaSRafael J. Wysocki 	 * Tell the time framework to switch to a broadcast timer because our
208df8d9eeaSRafael J. Wysocki 	 * local timer will be shut down.  If a local timer is used from another
209df8d9eeaSRafael J. Wysocki 	 * CPU as a broadcast timer, this call may fail if it is not available.
210df8d9eeaSRafael J. Wysocki 	 */
211827a5aefSRafael J. Wysocki 	if (broadcast && tick_broadcast_enter()) {
212*c1d51f68SRafael J. Wysocki 		index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
2130d94039fSRafael J. Wysocki 					   CPUIDLE_FLAG_TIMER_STOP, false);
2140d94039fSRafael J. Wysocki 		if (index < 0) {
215827a5aefSRafael J. Wysocki 			default_idle_call();
216df8d9eeaSRafael J. Wysocki 			return -EBUSY;
217827a5aefSRafael J. Wysocki 		}
2180d94039fSRafael J. Wysocki 		target_state = &drv->states[index];
219f187851bSNicholas Piggin 		broadcast = false;
2200d94039fSRafael J. Wysocki 	}
221df8d9eeaSRafael J. Wysocki 
222faad3849SRafael J. Wysocki 	/* Take note of the planned idle state. */
223faad3849SRafael J. Wysocki 	sched_idle_set_state(target_state);
224faad3849SRafael J. Wysocki 
22530fe6884SSandeep Tripathy 	trace_cpu_idle_rcuidle(index, dev->cpu);
226dbd1b8eaSShreyas B. Prabhu 	time_start = ns_to_ktime(local_clock());
227554c06baSDaniel Lezcano 
22863caae84SLucas Stach 	stop_critical_timings();
229554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
23063caae84SLucas Stach 	start_critical_timings();
231554c06baSDaniel Lezcano 
232f9fccdb9SPeter Zijlstra 	sched_clock_idle_wakeup_event();
233dbd1b8eaSShreyas B. Prabhu 	time_end = ns_to_ktime(local_clock());
23430fe6884SSandeep Tripathy 	trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
235554c06baSDaniel Lezcano 
236faad3849SRafael J. Wysocki 	/* The cpu is no longer idle or about to enter idle. */
237faad3849SRafael J. Wysocki 	sched_idle_set_state(NULL);
238faad3849SRafael J. Wysocki 
239df8d9eeaSRafael J. Wysocki 	if (broadcast) {
240df8d9eeaSRafael J. Wysocki 		if (WARN_ON_ONCE(!irqs_disabled()))
241df8d9eeaSRafael J. Wysocki 			local_irq_disable();
242df8d9eeaSRafael J. Wysocki 
243df8d9eeaSRafael J. Wysocki 		tick_broadcast_exit();
244df8d9eeaSRafael J. Wysocki 	}
245df8d9eeaSRafael J. Wysocki 
246e7387da5SDaniel Lezcano 	if (!cpuidle_state_is_coupled(drv, index))
247554c06baSDaniel Lezcano 		local_irq_enable();
248554c06baSDaniel Lezcano 
2497037b43eSFieah Lim 	if (entered_state >= 0) {
250*c1d51f68SRafael J. Wysocki 		s64 diff, delay = drv->states[entered_state].exit_latency_ns;
25104dab58aSRafael J. Wysocki 		int i;
25204dab58aSRafael J. Wysocki 
2537037b43eSFieah Lim 		/*
2547037b43eSFieah Lim 		 * Update cpuidle counters
2557037b43eSFieah Lim 		 * This can be moved to within driver enter routine,
2567037b43eSFieah Lim 		 * but that results in multiple copies of same code.
2577037b43eSFieah Lim 		 */
258*c1d51f68SRafael J. Wysocki 		diff = ktime_sub(time_end, time_start);
259554c06baSDaniel Lezcano 
260*c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = diff;
261*c1d51f68SRafael J. Wysocki 		dev->states_usage[entered_state].time_ns += diff;
26256cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
26304dab58aSRafael J. Wysocki 
264*c1d51f68SRafael J. Wysocki 		if (diff < drv->states[entered_state].target_residency_ns) {
26504dab58aSRafael J. Wysocki 			for (i = entered_state - 1; i >= 0; i--) {
26699e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
26704dab58aSRafael J. Wysocki 					continue;
26804dab58aSRafael J. Wysocki 
26904dab58aSRafael J. Wysocki 				/* Shallower states are enabled, so update. */
27004dab58aSRafael J. Wysocki 				dev->states_usage[entered_state].above++;
27104dab58aSRafael J. Wysocki 				break;
27204dab58aSRafael J. Wysocki 			}
27304dab58aSRafael J. Wysocki 		} else if (diff > delay) {
27404dab58aSRafael J. Wysocki 			for (i = entered_state + 1; i < drv->state_count; i++) {
27599e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
27604dab58aSRafael J. Wysocki 					continue;
27704dab58aSRafael J. Wysocki 
27804dab58aSRafael J. Wysocki 				/*
27904dab58aSRafael J. Wysocki 				 * Update if a deeper state would have been a
28004dab58aSRafael J. Wysocki 				 * better match for the observed idle duration.
28104dab58aSRafael J. Wysocki 				 */
282*c1d51f68SRafael J. Wysocki 				if (diff - delay >= drv->states[i].target_residency_ns)
28304dab58aSRafael J. Wysocki 					dev->states_usage[entered_state].below++;
28404dab58aSRafael J. Wysocki 
28504dab58aSRafael J. Wysocki 				break;
28604dab58aSRafael J. Wysocki 			}
28704dab58aSRafael J. Wysocki 		}
28856cfbf74SColin Cross 	} else {
289*c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = 0;
29056cfbf74SColin Cross 	}
29156cfbf74SColin Cross 
29256cfbf74SColin Cross 	return entered_state;
29356cfbf74SColin Cross }
29456cfbf74SColin Cross 
29556cfbf74SColin Cross /**
296907e30f1SDaniel Lezcano  * cpuidle_select - ask the cpuidle framework to choose an idle state
2974f86d3a8SLen Brown  *
298907e30f1SDaniel Lezcano  * @drv: the cpuidle driver
299907e30f1SDaniel Lezcano  * @dev: the cpuidle device
30045f1ff59SRafael J. Wysocki  * @stop_tick: indication on whether or not to stop the tick
301907e30f1SDaniel Lezcano  *
30251164251SRafael J. Wysocki  * Returns the index of the idle state.  The return value must not be negative.
30345f1ff59SRafael J. Wysocki  *
30445f1ff59SRafael J. Wysocki  * The memory location pointed to by @stop_tick is expected to be written the
30545f1ff59SRafael J. Wysocki  * 'false' boolean value if the scheduler tick should not be stopped before
30645f1ff59SRafael J. Wysocki  * entering the returned state.
3074f86d3a8SLen Brown  */
30845f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
30945f1ff59SRafael J. Wysocki 		   bool *stop_tick)
3104f86d3a8SLen Brown {
31145f1ff59SRafael J. Wysocki 	return cpuidle_curr_governor->select(drv, dev, stop_tick);
312246eb7f0SKevin Hilman }
313246eb7f0SKevin Hilman 
314907e30f1SDaniel Lezcano /**
315907e30f1SDaniel Lezcano  * cpuidle_enter - enter into the specified idle state
316907e30f1SDaniel Lezcano  *
317907e30f1SDaniel Lezcano  * @drv:   the cpuidle driver tied with the cpu
318907e30f1SDaniel Lezcano  * @dev:   the cpuidle device
319907e30f1SDaniel Lezcano  * @index: the index in the idle state table
320907e30f1SDaniel Lezcano  *
321907e30f1SDaniel Lezcano  * Returns the index in the idle state, < 0 in case of error.
322907e30f1SDaniel Lezcano  * The error code depends on the backend driver
323907e30f1SDaniel Lezcano  */
324907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
325907e30f1SDaniel Lezcano 		  int index)
326907e30f1SDaniel Lezcano {
3276f9b83acSUlf Hansson 	int ret = 0;
3286f9b83acSUlf Hansson 
3296f9b83acSUlf Hansson 	/*
3306f9b83acSUlf Hansson 	 * Store the next hrtimer, which becomes either next tick or the next
3316f9b83acSUlf Hansson 	 * timer event, whatever expires first. Additionally, to make this data
3326f9b83acSUlf Hansson 	 * useful for consumers outside cpuidle, we rely on that the governor's
3336f9b83acSUlf Hansson 	 * ->select() callback have decided, whether to stop the tick or not.
3346f9b83acSUlf Hansson 	 */
3356f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
3366f9b83acSUlf Hansson 
3374c1ed5a6SXunlei Pang 	if (cpuidle_state_is_coupled(drv, index))
3386f9b83acSUlf Hansson 		ret = cpuidle_enter_state_coupled(dev, drv, index);
3396f9b83acSUlf Hansson 	else
3406f9b83acSUlf Hansson 		ret = cpuidle_enter_state(dev, drv, index);
3416f9b83acSUlf Hansson 
3426f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, 0);
3436f9b83acSUlf Hansson 	return ret;
344907e30f1SDaniel Lezcano }
345fb11c9c6SViresh Kumar 
346907e30f1SDaniel Lezcano /**
347907e30f1SDaniel Lezcano  * cpuidle_reflect - tell the underlying governor what was the state
348907e30f1SDaniel Lezcano  * we were in
349907e30f1SDaniel Lezcano  *
350907e30f1SDaniel Lezcano  * @dev  : the cpuidle device
351907e30f1SDaniel Lezcano  * @index: the index in the idle state table
352907e30f1SDaniel Lezcano  *
353907e30f1SDaniel Lezcano  */
354907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index)
355907e30f1SDaniel Lezcano {
356a802ea96SRafael J. Wysocki 	if (cpuidle_curr_governor->reflect && index >= 0)
357907e30f1SDaniel Lezcano 		cpuidle_curr_governor->reflect(dev, index);
3584f86d3a8SLen Brown }
3594f86d3a8SLen Brown 
3604f86d3a8SLen Brown /**
361259231a0SMarcelo Tosatti  * cpuidle_poll_time - return amount of time to poll for,
362259231a0SMarcelo Tosatti  * governors can override dev->poll_limit_ns if necessary
363259231a0SMarcelo Tosatti  *
364259231a0SMarcelo Tosatti  * @drv:   the cpuidle driver tied with the cpu
365259231a0SMarcelo Tosatti  * @dev:   the cpuidle device
366259231a0SMarcelo Tosatti  *
367259231a0SMarcelo Tosatti  */
368259231a0SMarcelo Tosatti u64 cpuidle_poll_time(struct cpuidle_driver *drv,
369259231a0SMarcelo Tosatti 		      struct cpuidle_device *dev)
370259231a0SMarcelo Tosatti {
371259231a0SMarcelo Tosatti 	int i;
372259231a0SMarcelo Tosatti 	u64 limit_ns;
373259231a0SMarcelo Tosatti 
374259231a0SMarcelo Tosatti 	if (dev->poll_limit_ns)
375259231a0SMarcelo Tosatti 		return dev->poll_limit_ns;
376259231a0SMarcelo Tosatti 
377259231a0SMarcelo Tosatti 	limit_ns = TICK_NSEC;
378259231a0SMarcelo Tosatti 	for (i = 1; i < drv->state_count; i++) {
37999e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable)
380259231a0SMarcelo Tosatti 			continue;
381259231a0SMarcelo Tosatti 
382*c1d51f68SRafael J. Wysocki 		limit_ns = (u64)drv->states[i].target_residency_ns;
383259231a0SMarcelo Tosatti 	}
384259231a0SMarcelo Tosatti 
385259231a0SMarcelo Tosatti 	dev->poll_limit_ns = limit_ns;
386259231a0SMarcelo Tosatti 
387259231a0SMarcelo Tosatti 	return dev->poll_limit_ns;
388259231a0SMarcelo Tosatti }
389259231a0SMarcelo Tosatti 
390259231a0SMarcelo Tosatti /**
3914f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
3924f86d3a8SLen Brown  */
3934f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
3944f86d3a8SLen Brown {
395a0bfa137SLen Brown 	if (enabled_devices) {
3964f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
3974f86d3a8SLen Brown 		smp_wmb();
398a0bfa137SLen Brown 		initialized = 1;
3994f86d3a8SLen Brown 	}
4004f86d3a8SLen Brown }
4014f86d3a8SLen Brown 
4024f86d3a8SLen Brown /**
4034f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
4044f86d3a8SLen Brown  */
4054f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
4064f86d3a8SLen Brown {
407a0bfa137SLen Brown 	if (enabled_devices) {
408a0bfa137SLen Brown 		initialized = 0;
4092ed903c5SChuansheng Liu 		wake_up_all_idle_cpus();
4104f86d3a8SLen Brown 	}
411442bf3aaSDaniel Lezcano 
412442bf3aaSDaniel Lezcano 	/*
413442bf3aaSDaniel Lezcano 	 * Make sure external observers (such as the scheduler)
414442bf3aaSDaniel Lezcano 	 * are done looking at pointed idle states.
415442bf3aaSDaniel Lezcano 	 */
416442bf3aaSDaniel Lezcano 	synchronize_rcu();
4174f86d3a8SLen Brown }
4184f86d3a8SLen Brown 
4194f86d3a8SLen Brown /**
4204f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
4214f86d3a8SLen Brown  */
4224f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
4234f86d3a8SLen Brown {
4244f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
4254f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
4264f86d3a8SLen Brown }
4274f86d3a8SLen Brown 
4284f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
4294f86d3a8SLen Brown 
4304f86d3a8SLen Brown /**
4314f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
4324f86d3a8SLen Brown  */
4334f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
4344f86d3a8SLen Brown {
4354f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4364f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4374f86d3a8SLen Brown }
4384f86d3a8SLen Brown 
4394f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
4404f86d3a8SLen Brown 
4418651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
4428651f97bSPreeti U Murthy void cpuidle_pause(void)
4438651f97bSPreeti U Murthy {
4448651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4458651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
4468651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4478651f97bSPreeti U Murthy }
4488651f97bSPreeti U Murthy 
4498651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
4508651f97bSPreeti U Murthy void cpuidle_resume(void)
4518651f97bSPreeti U Murthy {
4528651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4538651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
4548651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4558651f97bSPreeti U Murthy }
4568651f97bSPreeti U Murthy 
4574f86d3a8SLen Brown /**
4584f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
4594f86d3a8SLen Brown  * @dev: the CPU
4604f86d3a8SLen Brown  *
4614f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
4624f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
4634f86d3a8SLen Brown  */
4644f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
4654f86d3a8SLen Brown {
4665df0aa73SDaniel Lezcano 	int ret;
467bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
4684f86d3a8SLen Brown 
4691b0a0e9aSSrivatsa S. Bhat 	if (!dev)
4701b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
4711b0a0e9aSSrivatsa S. Bhat 
4724f86d3a8SLen Brown 	if (dev->enabled)
4734f86d3a8SLen Brown 		return 0;
474bf4d1b5dSDaniel Lezcano 
475e7b06a09SGaurav Jindal 	if (!cpuidle_curr_governor)
476e7b06a09SGaurav Jindal 		return -EIO;
477e7b06a09SGaurav Jindal 
478bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
479bf4d1b5dSDaniel Lezcano 
480e7b06a09SGaurav Jindal 	if (!drv)
4814f86d3a8SLen Brown 		return -EIO;
482bf4d1b5dSDaniel Lezcano 
48310b9d3f8SDaniel Lezcano 	if (!dev->registered)
48410b9d3f8SDaniel Lezcano 		return -EINVAL;
48510b9d3f8SDaniel Lezcano 
486bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
487bf4d1b5dSDaniel Lezcano 	if (ret)
4884f86d3a8SLen Brown 		return ret;
4894f86d3a8SLen Brown 
4903fc74bd8SGaurav Jindal 	if (cpuidle_curr_governor->enable) {
4913fc74bd8SGaurav Jindal 		ret = cpuidle_curr_governor->enable(drv, dev);
4923fc74bd8SGaurav Jindal 		if (ret)
4934f86d3a8SLen Brown 			goto fail_sysfs;
4943fc74bd8SGaurav Jindal 	}
4954f86d3a8SLen Brown 
4964f86d3a8SLen Brown 	smp_wmb();
4974f86d3a8SLen Brown 
4984f86d3a8SLen Brown 	dev->enabled = 1;
4994f86d3a8SLen Brown 
5004f86d3a8SLen Brown 	enabled_devices++;
5014f86d3a8SLen Brown 	return 0;
5024f86d3a8SLen Brown 
5034f86d3a8SLen Brown fail_sysfs:
504bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5054f86d3a8SLen Brown 
5064f86d3a8SLen Brown 	return ret;
5074f86d3a8SLen Brown }
5084f86d3a8SLen Brown 
5094f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
5104f86d3a8SLen Brown 
5114f86d3a8SLen Brown /**
5124f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
5134f86d3a8SLen Brown  * @dev: the CPU
5144f86d3a8SLen Brown  *
5154f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5164f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5174f86d3a8SLen Brown  */
5184f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
5194f86d3a8SLen Brown {
520bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
521bf4d1b5dSDaniel Lezcano 
522cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
5234f86d3a8SLen Brown 		return;
524bf4d1b5dSDaniel Lezcano 
525bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
5264f86d3a8SLen Brown 		return;
5274f86d3a8SLen Brown 
5284f86d3a8SLen Brown 	dev->enabled = 0;
5294f86d3a8SLen Brown 
5304f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
531bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
5324f86d3a8SLen Brown 
533bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5344f86d3a8SLen Brown 	enabled_devices--;
5354f86d3a8SLen Brown }
5364f86d3a8SLen Brown 
5374f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
5384f86d3a8SLen Brown 
539f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev)
540f6bb51a5SDaniel Lezcano {
541f6bb51a5SDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
542f6bb51a5SDaniel Lezcano 
543f6bb51a5SDaniel Lezcano 	list_del(&dev->device_list);
544f6bb51a5SDaniel Lezcano 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
545f6bb51a5SDaniel Lezcano 	module_put(drv->owner);
546c998c078SDave Gerlach 
547c998c078SDave Gerlach 	dev->registered = 0;
548f6bb51a5SDaniel Lezcano }
549f6bb51a5SDaniel Lezcano 
550267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev)
5515df0aa73SDaniel Lezcano {
5525df0aa73SDaniel Lezcano 	memset(dev->states_usage, 0, sizeof(dev->states_usage));
553*c1d51f68SRafael J. Wysocki 	dev->last_residency_ns = 0;
5546f9b83acSUlf Hansson 	dev->next_hrtimer = 0;
5555df0aa73SDaniel Lezcano }
5565df0aa73SDaniel Lezcano 
5574f86d3a8SLen Brown /**
558dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
559dcb84f33SVenkatesh Pallipadi  * and enable routines
5604f86d3a8SLen Brown  * @dev: the cpu
561dcb84f33SVenkatesh Pallipadi  *
562dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
5634f86d3a8SLen Brown  */
564dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
5654f86d3a8SLen Brown {
566bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
56799e98d3fSRafael J. Wysocki 	int i, ret;
5684f86d3a8SLen Brown 
569bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
5704f86d3a8SLen Brown 		return -EINVAL;
5714f86d3a8SLen Brown 
57299e98d3fSRafael J. Wysocki 	for (i = 0; i < drv->state_count; i++)
57399e98d3fSRafael J. Wysocki 		if (drv->states[i].disabled)
57499e98d3fSRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
57599e98d3fSRafael J. Wysocki 
5764f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
5774f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
5784f86d3a8SLen Brown 
5794126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
58047182668SViresh Kumar 	if (ret)
581f6bb51a5SDaniel Lezcano 		__cpuidle_unregister_device(dev);
58247182668SViresh Kumar 	else
583dcb84f33SVenkatesh Pallipadi 		dev->registered = 1;
58447182668SViresh Kumar 
58547182668SViresh Kumar 	return ret;
586dcb84f33SVenkatesh Pallipadi }
587dcb84f33SVenkatesh Pallipadi 
588dcb84f33SVenkatesh Pallipadi /**
589dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
590dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
591dcb84f33SVenkatesh Pallipadi  */
592dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
593dcb84f33SVenkatesh Pallipadi {
594c878a52dSDaniel Lezcano 	int ret = -EBUSY;
595dcb84f33SVenkatesh Pallipadi 
5961b0a0e9aSSrivatsa S. Bhat 	if (!dev)
5971b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
5981b0a0e9aSSrivatsa S. Bhat 
599dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
600dcb84f33SVenkatesh Pallipadi 
601c878a52dSDaniel Lezcano 	if (dev->registered)
602c878a52dSDaniel Lezcano 		goto out_unlock;
603c878a52dSDaniel Lezcano 
604267d4bf8SViresh Kumar 	__cpuidle_device_init(dev);
6055df0aa73SDaniel Lezcano 
606f6bb51a5SDaniel Lezcano 	ret = __cpuidle_register_device(dev);
607f6bb51a5SDaniel Lezcano 	if (ret)
608f6bb51a5SDaniel Lezcano 		goto out_unlock;
609f6bb51a5SDaniel Lezcano 
610f6bb51a5SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
611f6bb51a5SDaniel Lezcano 	if (ret)
612f6bb51a5SDaniel Lezcano 		goto out_unregister;
613dcb84f33SVenkatesh Pallipadi 
61410b9d3f8SDaniel Lezcano 	ret = cpuidle_enable_device(dev);
615f6bb51a5SDaniel Lezcano 	if (ret)
616f6bb51a5SDaniel Lezcano 		goto out_sysfs;
61710b9d3f8SDaniel Lezcano 
6184f86d3a8SLen Brown 	cpuidle_install_idle_handler();
6194f86d3a8SLen Brown 
620f6bb51a5SDaniel Lezcano out_unlock:
6214f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
6224f86d3a8SLen Brown 
623f6bb51a5SDaniel Lezcano 	return ret;
624f6bb51a5SDaniel Lezcano 
625f6bb51a5SDaniel Lezcano out_sysfs:
626f6bb51a5SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
627f6bb51a5SDaniel Lezcano out_unregister:
628f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
629f6bb51a5SDaniel Lezcano 	goto out_unlock;
6304f86d3a8SLen Brown }
6314f86d3a8SLen Brown 
6324f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
6334f86d3a8SLen Brown 
6344f86d3a8SLen Brown /**
6354f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
6364f86d3a8SLen Brown  * @dev: the cpu
6374f86d3a8SLen Brown  */
6384f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
6394f86d3a8SLen Brown {
640813e8e3dSKonrad Rzeszutek Wilk 	if (!dev || dev->registered == 0)
641dcb84f33SVenkatesh Pallipadi 		return;
642dcb84f33SVenkatesh Pallipadi 
6434f86d3a8SLen Brown 	cpuidle_pause_and_lock();
6444f86d3a8SLen Brown 
6454f86d3a8SLen Brown 	cpuidle_disable_device(dev);
6464f86d3a8SLen Brown 
6471aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
648f6bb51a5SDaniel Lezcano 
649f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
6504f86d3a8SLen Brown 
6514126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
6524126c019SColin Cross 
6534f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
6544f86d3a8SLen Brown }
6554f86d3a8SLen Brown 
6564f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
6574f86d3a8SLen Brown 
6581c192d04SDaniel Lezcano /**
6594c637b21SDaniel Lezcano  * cpuidle_unregister: unregister a driver and the devices. This function
6604c637b21SDaniel Lezcano  * can be used only if the driver has been previously registered through
6614c637b21SDaniel Lezcano  * the cpuidle_register function.
6624c637b21SDaniel Lezcano  *
6634c637b21SDaniel Lezcano  * @drv: a valid pointer to a struct cpuidle_driver
6644c637b21SDaniel Lezcano  */
6654c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv)
6664c637b21SDaniel Lezcano {
6674c637b21SDaniel Lezcano 	int cpu;
6684c637b21SDaniel Lezcano 	struct cpuidle_device *device;
6694c637b21SDaniel Lezcano 
67082467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
6714c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
6724c637b21SDaniel Lezcano 		cpuidle_unregister_device(device);
6734c637b21SDaniel Lezcano 	}
6744c637b21SDaniel Lezcano 
6754c637b21SDaniel Lezcano 	cpuidle_unregister_driver(drv);
6764c637b21SDaniel Lezcano }
6774c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister);
6784c637b21SDaniel Lezcano 
6794c637b21SDaniel Lezcano /**
6804c637b21SDaniel Lezcano  * cpuidle_register: registers the driver and the cpu devices with the
6814c637b21SDaniel Lezcano  * coupled_cpus passed as parameter. This function is used for all common
6824c637b21SDaniel Lezcano  * initialization pattern there are in the arch specific drivers. The
6834c637b21SDaniel Lezcano  * devices is globally defined in this file.
6844c637b21SDaniel Lezcano  *
6854c637b21SDaniel Lezcano  * @drv         : a valid pointer to a struct cpuidle_driver
6864c637b21SDaniel Lezcano  * @coupled_cpus: a cpumask for the coupled states
6874c637b21SDaniel Lezcano  *
6884c637b21SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
6894c637b21SDaniel Lezcano  */
6904c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv,
6914c637b21SDaniel Lezcano 		     const struct cpumask *const coupled_cpus)
6924c637b21SDaniel Lezcano {
6934c637b21SDaniel Lezcano 	int ret, cpu;
6944c637b21SDaniel Lezcano 	struct cpuidle_device *device;
6954c637b21SDaniel Lezcano 
6964c637b21SDaniel Lezcano 	ret = cpuidle_register_driver(drv);
6974c637b21SDaniel Lezcano 	if (ret) {
6984c637b21SDaniel Lezcano 		pr_err("failed to register cpuidle driver\n");
6994c637b21SDaniel Lezcano 		return ret;
7004c637b21SDaniel Lezcano 	}
7014c637b21SDaniel Lezcano 
70282467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7034c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7044c637b21SDaniel Lezcano 		device->cpu = cpu;
7054c637b21SDaniel Lezcano 
7064c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
7074c637b21SDaniel Lezcano 		/*
708caf4a36eSViresh Kumar 		 * On multiplatform for ARM, the coupled idle states could be
7094c637b21SDaniel Lezcano 		 * enabled in the kernel even if the cpuidle driver does not
7104c637b21SDaniel Lezcano 		 * use it. Note, coupled_cpus is a struct copy.
7114c637b21SDaniel Lezcano 		 */
7124c637b21SDaniel Lezcano 		if (coupled_cpus)
7134c637b21SDaniel Lezcano 			device->coupled_cpus = *coupled_cpus;
7144c637b21SDaniel Lezcano #endif
7154c637b21SDaniel Lezcano 		ret = cpuidle_register_device(device);
7164c637b21SDaniel Lezcano 		if (!ret)
7174c637b21SDaniel Lezcano 			continue;
7184c637b21SDaniel Lezcano 
7194c637b21SDaniel Lezcano 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
7204c637b21SDaniel Lezcano 
7214c637b21SDaniel Lezcano 		cpuidle_unregister(drv);
7224c637b21SDaniel Lezcano 		break;
7234c637b21SDaniel Lezcano 	}
7244c637b21SDaniel Lezcano 
7254c637b21SDaniel Lezcano 	return ret;
7264c637b21SDaniel Lezcano }
7274c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register);
7284c637b21SDaniel Lezcano 
7294f86d3a8SLen Brown #ifdef CONFIG_SMP
7304f86d3a8SLen Brown 
7314f86d3a8SLen Brown /*
7324f86d3a8SLen Brown  * This function gets called when a part of the kernel has a new latency
7334f86d3a8SLen Brown  * requirement.  This means we need to get all processors out of their C-state,
7344f86d3a8SLen Brown  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
7354f86d3a8SLen Brown  * wakes them all right up.
7364f86d3a8SLen Brown  */
7374f86d3a8SLen Brown static int cpuidle_latency_notify(struct notifier_block *b,
7384f86d3a8SLen Brown 		unsigned long l, void *v)
7394f86d3a8SLen Brown {
7402ed903c5SChuansheng Liu 	wake_up_all_idle_cpus();
7414f86d3a8SLen Brown 	return NOTIFY_OK;
7424f86d3a8SLen Brown }
7434f86d3a8SLen Brown 
7444f86d3a8SLen Brown static struct notifier_block cpuidle_latency_notifier = {
7454f86d3a8SLen Brown 	.notifier_call = cpuidle_latency_notify,
7464f86d3a8SLen Brown };
7474f86d3a8SLen Brown 
748d82b3518SMark Gross static inline void latency_notifier_init(struct notifier_block *n)
749d82b3518SMark Gross {
750d82b3518SMark Gross 	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
751d82b3518SMark Gross }
7524f86d3a8SLen Brown 
7534f86d3a8SLen Brown #else /* CONFIG_SMP */
7544f86d3a8SLen Brown 
7554f86d3a8SLen Brown #define latency_notifier_init(x) do { } while (0)
7564f86d3a8SLen Brown 
7574f86d3a8SLen Brown #endif /* CONFIG_SMP */
7584f86d3a8SLen Brown 
7594f86d3a8SLen Brown /**
7604f86d3a8SLen Brown  * cpuidle_init - core initializer
7614f86d3a8SLen Brown  */
7624f86d3a8SLen Brown static int __init cpuidle_init(void)
7634f86d3a8SLen Brown {
7644f86d3a8SLen Brown 	int ret;
7654f86d3a8SLen Brown 
76662027aeaSLen Brown 	if (cpuidle_disabled())
76762027aeaSLen Brown 		return -ENODEV;
76862027aeaSLen Brown 
7698a25a2fdSKay Sievers 	ret = cpuidle_add_interface(cpu_subsys.dev_root);
7704f86d3a8SLen Brown 	if (ret)
7714f86d3a8SLen Brown 		return ret;
7724f86d3a8SLen Brown 
7734f86d3a8SLen Brown 	latency_notifier_init(&cpuidle_latency_notifier);
7744f86d3a8SLen Brown 
7754f86d3a8SLen Brown 	return 0;
7764f86d3a8SLen Brown }
7774f86d3a8SLen Brown 
77862027aeaSLen Brown module_param(off, int, 0444);
77961cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
7804f86d3a8SLen Brown core_initcall(cpuidle_init);
781