xref: /linux/drivers/cpuidle/cpuidle.c (revision bb7b11258561e47abbacebf76e3ce4092953dfdf)
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>
174f86d3a8SLen Brown #include <linux/notifier.h>
18e8db0be1SJean Pihet #include <linux/pm_qos.h>
194f86d3a8SLen Brown #include <linux/cpu.h>
204f86d3a8SLen Brown #include <linux/cpuidle.h>
219a0b8415Svenkatesh.pallipadi@intel.com #include <linux/ktime.h>
222e94d1f7SArjan van de Ven #include <linux/hrtimer.h>
23884b17e1SPaul Gortmaker #include <linux/module.h>
2438106313SRafael J. Wysocki #include <linux/suspend.h>
25124cf911SRafael J. Wysocki #include <linux/tick.h>
26bf9282dcSPeter Zijlstra #include <linux/mmu_context.h>
27e67198ccSFrederic Weisbecker #include <linux/context_tracking.h>
28288f023eSArjan van de Ven #include <trace/events/power.h>
294f86d3a8SLen Brown 
304f86d3a8SLen Brown #include "cpuidle.h"
314f86d3a8SLen Brown 
324f86d3a8SLen Brown DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
334c637b21SDaniel Lezcano DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
344f86d3a8SLen Brown 
354f86d3a8SLen Brown DEFINE_MUTEX(cpuidle_lock);
364f86d3a8SLen Brown LIST_HEAD(cpuidle_detected_devices);
374f86d3a8SLen Brown 
384f86d3a8SLen Brown static int enabled_devices;
3962027aeaSLen Brown static int off __read_mostly;
40a0bfa137SLen Brown static int initialized __read_mostly;
4162027aeaSLen Brown 
4262027aeaSLen Brown int cpuidle_disabled(void)
4362027aeaSLen Brown {
4462027aeaSLen Brown 	return off;
4562027aeaSLen Brown }
46d91ee586SLen Brown void disable_cpuidle(void)
47d91ee586SLen Brown {
48d91ee586SLen Brown 	off = 1;
49d91ee586SLen Brown }
504f86d3a8SLen Brown 
51ef2b22acSRafael J. Wysocki bool cpuidle_not_available(struct cpuidle_driver *drv,
5231a34090SRafael J. Wysocki 			   struct cpuidle_device *dev)
5331a34090SRafael J. Wysocki {
5431a34090SRafael J. Wysocki 	return off || !initialized || !drv || !dev || !dev->enabled;
5531a34090SRafael J. Wysocki }
5631a34090SRafael J. Wysocki 
574f86d3a8SLen Brown /**
581a022e3fSBoris Ostrovsky  * cpuidle_play_dead - cpu off-lining
591a022e3fSBoris Ostrovsky  *
60ee01e663SToshi Kani  * Returns in case of an error or no driver
611a022e3fSBoris Ostrovsky  */
621a022e3fSBoris Ostrovsky int cpuidle_play_dead(void)
631a022e3fSBoris Ostrovsky {
641a022e3fSBoris Ostrovsky 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
65bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
668aef33a7SDaniel Lezcano 	int i;
671a022e3fSBoris Ostrovsky 
68ee01e663SToshi Kani 	if (!drv)
69ee01e663SToshi Kani 		return -ENODEV;
70ee01e663SToshi Kani 
711a022e3fSBoris Ostrovsky 	/* Find lowest-power state that supports long-term idle */
727d51d979SRafael J. Wysocki 	for (i = drv->state_count - 1; i >= 0; i--)
738aef33a7SDaniel Lezcano 		if (drv->states[i].enter_dead)
748aef33a7SDaniel Lezcano 			return drv->states[i].enter_dead(dev, i);
751a022e3fSBoris Ostrovsky 
761a022e3fSBoris Ostrovsky 	return -ENODEV;
771a022e3fSBoris Ostrovsky }
781a022e3fSBoris Ostrovsky 
79ef2b22acSRafael J. Wysocki static int find_deepest_state(struct cpuidle_driver *drv,
800d94039fSRafael J. Wysocki 			      struct cpuidle_device *dev,
81c1d51f68SRafael J. Wysocki 			      u64 max_latency_ns,
820d94039fSRafael J. Wysocki 			      unsigned int forbidden_flags,
8328ba086eSRafael J. Wysocki 			      bool s2idle)
84a6220fc1SRafael J. Wysocki {
85c1d51f68SRafael J. Wysocki 	u64 latency_req = 0;
8651164251SRafael J. Wysocki 	int i, ret = 0;
87a6220fc1SRafael J. Wysocki 
8851164251SRafael J. Wysocki 	for (i = 1; i < drv->state_count; i++) {
89a6220fc1SRafael J. Wysocki 		struct cpuidle_state *s = &drv->states[i];
90a6220fc1SRafael J. Wysocki 
9199e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable ||
92c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns <= latency_req ||
93c1d51f68SRafael J. Wysocki 		    s->exit_latency_ns > max_latency_ns ||
9499e98d3fSRafael J. Wysocki 		    (s->flags & forbidden_flags) ||
9599e98d3fSRafael J. Wysocki 		    (s2idle && !s->enter_s2idle))
96a6220fc1SRafael J. Wysocki 			continue;
97a6220fc1SRafael J. Wysocki 
98c1d51f68SRafael J. Wysocki 		latency_req = s->exit_latency_ns;
99a6220fc1SRafael J. Wysocki 		ret = i;
100a6220fc1SRafael J. Wysocki 	}
101a6220fc1SRafael J. Wysocki 	return ret;
102a6220fc1SRafael J. Wysocki }
103a6220fc1SRafael J. Wysocki 
1040e7414b7SRafael J. Wysocki /**
105c55b51a0SDaniel Lezcano  * cpuidle_use_deepest_state - Set/unset governor override mode.
106c55b51a0SDaniel Lezcano  * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
1070e7414b7SRafael J. Wysocki  *
108c55b51a0SDaniel Lezcano  * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
109c55b51a0SDaniel Lezcano  * state with exit latency within @latency_limit_ns (override governors going
110c55b51a0SDaniel Lezcano  * forward), or do not override governors if it is zero.
1110e7414b7SRafael J. Wysocki  */
112c55b51a0SDaniel Lezcano void cpuidle_use_deepest_state(u64 latency_limit_ns)
113bb8313b6SJacob Pan {
114bb8313b6SJacob Pan 	struct cpuidle_device *dev;
115bb8313b6SJacob Pan 
116bb8313b6SJacob Pan 	preempt_disable();
117bb8313b6SJacob Pan 	dev = cpuidle_get_device();
11841dc750eSLi, Fei 	if (dev)
119c55b51a0SDaniel Lezcano 		dev->forced_idle_latency_limit_ns = latency_limit_ns;
120bb8313b6SJacob Pan 	preempt_enable();
121bb8313b6SJacob Pan }
122bb8313b6SJacob Pan 
123ef2b22acSRafael J. Wysocki /**
124ef2b22acSRafael J. Wysocki  * cpuidle_find_deepest_state - Find the deepest available idle state.
125ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
126ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
127cefb9409SBenjamin Gaignard  * @latency_limit_ns: Idle state exit latency limit
128cefb9409SBenjamin Gaignard  *
129cefb9409SBenjamin Gaignard  * Return: the index of the deepest available idle state.
130ef2b22acSRafael J. Wysocki  */
131ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
1325aa9ba63SDaniel Lezcano 			       struct cpuidle_device *dev,
1335aa9ba63SDaniel Lezcano 			       u64 latency_limit_ns)
134ef2b22acSRafael J. Wysocki {
1355aa9ba63SDaniel Lezcano 	return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
136ef2b22acSRafael J. Wysocki }
137ef2b22acSRafael J. Wysocki 
138bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND
13928ba086eSRafael J. Wysocki static void enter_s2idle_proper(struct cpuidle_driver *drv,
140124cf911SRafael J. Wysocki 				struct cpuidle_device *dev, int index)
141124cf911SRafael J. Wysocki {
14264bdff69SRafael J. Wysocki 	ktime_t time_start, time_end;
1438747f202SPeter Zijlstra 	struct cpuidle_state *target_state = &drv->states[index];
14464bdff69SRafael J. Wysocki 
14564bdff69SRafael J. Wysocki 	time_start = ns_to_ktime(local_clock());
14664bdff69SRafael J. Wysocki 
1471098582aSPeter Zijlstra 	tick_freeze();
148124cf911SRafael J. Wysocki 	/*
149124cf911SRafael J. Wysocki 	 * The state used here cannot be a "coupled" one, because the "coupled"
150124cf911SRafael J. Wysocki 	 * cpuidle mechanism enables interrupts and doing that with timekeeping
151124cf911SRafael J. Wysocki 	 * suspended is generally unsafe.
152124cf911SRafael J. Wysocki 	 */
15363caae84SLucas Stach 	stop_critical_timings();
1548747f202SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
155e67198ccSFrederic Weisbecker 		ct_idle_enter();
1568747f202SPeter Zijlstra 	target_state->enter_s2idle(dev, drv, index);
15749d9c593SPeter Zijlstra 	if (WARN_ON_ONCE(!irqs_disabled()))
15849d9c593SPeter Zijlstra 		local_irq_disable();
1598747f202SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
160e67198ccSFrederic Weisbecker 		ct_idle_exit();
1611098582aSPeter Zijlstra 	tick_unfreeze();
16263caae84SLucas Stach 	start_critical_timings();
16364bdff69SRafael J. Wysocki 
16464bdff69SRafael J. Wysocki 	time_end = ns_to_ktime(local_clock());
16564bdff69SRafael J. Wysocki 
16664bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
16764bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_usage++;
168124cf911SRafael J. Wysocki }
169124cf911SRafael J. Wysocki 
170a6220fc1SRafael J. Wysocki /**
17128ba086eSRafael J. Wysocki  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
172ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
173ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
17438106313SRafael J. Wysocki  *
17528ba086eSRafael J. Wysocki  * If there are states with the ->enter_s2idle callback, find the deepest of
176ef2b22acSRafael J. Wysocki  * them and enter it with frozen tick.
17738106313SRafael J. Wysocki  */
17828ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
17938106313SRafael J. Wysocki {
18038106313SRafael J. Wysocki 	int index;
18138106313SRafael J. Wysocki 
182124cf911SRafael J. Wysocki 	/*
18328ba086eSRafael J. Wysocki 	 * Find the deepest state with ->enter_s2idle present, which guarantees
184124cf911SRafael J. Wysocki 	 * that interrupts won't be enabled when it exits and allows the tick to
185124cf911SRafael J. Wysocki 	 * be frozen safely.
186124cf911SRafael J. Wysocki 	 */
187c1d51f68SRafael J. Wysocki 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
18810e8b11eSRafael J. Wysocki 	if (index > 0) {
18928ba086eSRafael J. Wysocki 		enter_s2idle_proper(drv, dev, index);
19010e8b11eSRafael J. Wysocki 		local_irq_enable();
19110e8b11eSRafael J. Wysocki 	}
192ef2b22acSRafael J. Wysocki 	return index;
19338106313SRafael J. Wysocki }
19487e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
19538106313SRafael J. Wysocki 
19638106313SRafael J. Wysocki /**
19756cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
19856cfbf74SColin Cross  * @dev: cpuidle device for this cpu
19956cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
2007312280bSRafael J. Wysocki  * @index: index into the states table in @drv of the state to enter
20156cfbf74SColin Cross  */
20256cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
203554c06baSDaniel Lezcano 			int index)
20456cfbf74SColin Cross {
20556cfbf74SColin Cross 	int entered_state;
20656cfbf74SColin Cross 
207554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
208df8d9eeaSRafael J. Wysocki 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
209dbd1b8eaSShreyas B. Prabhu 	ktime_t time_start, time_end;
210554c06baSDaniel Lezcano 
211df8d9eeaSRafael J. Wysocki 	/*
212df8d9eeaSRafael J. Wysocki 	 * Tell the time framework to switch to a broadcast timer because our
213df8d9eeaSRafael J. Wysocki 	 * local timer will be shut down.  If a local timer is used from another
214df8d9eeaSRafael J. Wysocki 	 * CPU as a broadcast timer, this call may fail if it is not available.
215df8d9eeaSRafael J. Wysocki 	 */
216827a5aefSRafael J. Wysocki 	if (broadcast && tick_broadcast_enter()) {
217c1d51f68SRafael J. Wysocki 		index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
2180d94039fSRafael J. Wysocki 					   CPUIDLE_FLAG_TIMER_STOP, false);
2190d94039fSRafael J. Wysocki 		if (index < 0) {
220827a5aefSRafael J. Wysocki 			default_idle_call();
221df8d9eeaSRafael J. Wysocki 			return -EBUSY;
222827a5aefSRafael J. Wysocki 		}
2230d94039fSRafael J. Wysocki 		target_state = &drv->states[index];
224f187851bSNicholas Piggin 		broadcast = false;
2250d94039fSRafael J. Wysocki 	}
226df8d9eeaSRafael J. Wysocki 
227bf9282dcSPeter Zijlstra 	if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
228bf9282dcSPeter Zijlstra 		leave_mm(dev->cpu);
229bf9282dcSPeter Zijlstra 
230faad3849SRafael J. Wysocki 	/* Take note of the planned idle state. */
231faad3849SRafael J. Wysocki 	sched_idle_set_state(target_state);
232faad3849SRafael J. Wysocki 
2331098582aSPeter Zijlstra 	trace_cpu_idle(index, dev->cpu);
234dbd1b8eaSShreyas B. Prabhu 	time_start = ns_to_ktime(local_clock());
235554c06baSDaniel Lezcano 
23663caae84SLucas Stach 	stop_critical_timings();
2378747f202SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
238e67198ccSFrederic Weisbecker 		ct_idle_enter();
239*bb7b1125SPeter Zijlstra 
240554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
241*bb7b1125SPeter Zijlstra 	if (WARN_ONCE(!irqs_disabled(), "%ps leaked IRQ state", target_state->enter))
242*bb7b1125SPeter Zijlstra 		raw_local_irq_disable();
243*bb7b1125SPeter Zijlstra 
2448747f202SPeter Zijlstra 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
245e67198ccSFrederic Weisbecker 		ct_idle_exit();
24663caae84SLucas Stach 	start_critical_timings();
247554c06baSDaniel Lezcano 
248f9fccdb9SPeter Zijlstra 	sched_clock_idle_wakeup_event();
249dbd1b8eaSShreyas B. Prabhu 	time_end = ns_to_ktime(local_clock());
2501098582aSPeter Zijlstra 	trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
251554c06baSDaniel Lezcano 
252faad3849SRafael J. Wysocki 	/* The cpu is no longer idle or about to enter idle. */
253faad3849SRafael J. Wysocki 	sched_idle_set_state(NULL);
254faad3849SRafael J. Wysocki 
255*bb7b1125SPeter Zijlstra 	if (broadcast)
256df8d9eeaSRafael J. Wysocki 		tick_broadcast_exit();
257df8d9eeaSRafael J. Wysocki 
258e7387da5SDaniel Lezcano 	if (!cpuidle_state_is_coupled(drv, index))
259554c06baSDaniel Lezcano 		local_irq_enable();
260554c06baSDaniel Lezcano 
2617037b43eSFieah Lim 	if (entered_state >= 0) {
262c1d51f68SRafael J. Wysocki 		s64 diff, delay = drv->states[entered_state].exit_latency_ns;
26304dab58aSRafael J. Wysocki 		int i;
26404dab58aSRafael J. Wysocki 
2657037b43eSFieah Lim 		/*
2667037b43eSFieah Lim 		 * Update cpuidle counters
2677037b43eSFieah Lim 		 * This can be moved to within driver enter routine,
2687037b43eSFieah Lim 		 * but that results in multiple copies of same code.
2697037b43eSFieah Lim 		 */
270c1d51f68SRafael J. Wysocki 		diff = ktime_sub(time_end, time_start);
271554c06baSDaniel Lezcano 
272c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = diff;
273c1d51f68SRafael J. Wysocki 		dev->states_usage[entered_state].time_ns += diff;
27456cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
27504dab58aSRafael J. Wysocki 
276c1d51f68SRafael J. Wysocki 		if (diff < drv->states[entered_state].target_residency_ns) {
27704dab58aSRafael J. Wysocki 			for (i = entered_state - 1; i >= 0; i--) {
27899e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
27904dab58aSRafael J. Wysocki 					continue;
28004dab58aSRafael J. Wysocki 
28104dab58aSRafael J. Wysocki 				/* Shallower states are enabled, so update. */
28204dab58aSRafael J. Wysocki 				dev->states_usage[entered_state].above++;
2836ab4b199SKajetan Puchalski 				trace_cpu_idle_miss(dev->cpu, entered_state, false);
28404dab58aSRafael J. Wysocki 				break;
28504dab58aSRafael J. Wysocki 			}
28604dab58aSRafael J. Wysocki 		} else if (diff > delay) {
28704dab58aSRafael J. Wysocki 			for (i = entered_state + 1; i < drv->state_count; i++) {
28899e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
28904dab58aSRafael J. Wysocki 					continue;
29004dab58aSRafael J. Wysocki 
29104dab58aSRafael J. Wysocki 				/*
29204dab58aSRafael J. Wysocki 				 * Update if a deeper state would have been a
29304dab58aSRafael J. Wysocki 				 * better match for the observed idle duration.
29404dab58aSRafael J. Wysocki 				 */
2956ab4b199SKajetan Puchalski 				if (diff - delay >= drv->states[i].target_residency_ns) {
29604dab58aSRafael J. Wysocki 					dev->states_usage[entered_state].below++;
2976ab4b199SKajetan Puchalski 					trace_cpu_idle_miss(dev->cpu, entered_state, true);
2986ab4b199SKajetan Puchalski 				}
29904dab58aSRafael J. Wysocki 
30004dab58aSRafael J. Wysocki 				break;
30104dab58aSRafael J. Wysocki 			}
30204dab58aSRafael J. Wysocki 		}
30356cfbf74SColin Cross 	} else {
304c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = 0;
305f49735f4SLina Iyer 		dev->states_usage[index].rejected++;
30656cfbf74SColin Cross 	}
30756cfbf74SColin Cross 
30856cfbf74SColin Cross 	return entered_state;
30956cfbf74SColin Cross }
31056cfbf74SColin Cross 
31156cfbf74SColin Cross /**
312907e30f1SDaniel Lezcano  * cpuidle_select - ask the cpuidle framework to choose an idle state
3134f86d3a8SLen Brown  *
314907e30f1SDaniel Lezcano  * @drv: the cpuidle driver
315907e30f1SDaniel Lezcano  * @dev: the cpuidle device
31645f1ff59SRafael J. Wysocki  * @stop_tick: indication on whether or not to stop the tick
317907e30f1SDaniel Lezcano  *
31851164251SRafael J. Wysocki  * Returns the index of the idle state.  The return value must not be negative.
31945f1ff59SRafael J. Wysocki  *
32045f1ff59SRafael J. Wysocki  * The memory location pointed to by @stop_tick is expected to be written the
32145f1ff59SRafael J. Wysocki  * 'false' boolean value if the scheduler tick should not be stopped before
32245f1ff59SRafael J. Wysocki  * entering the returned state.
3234f86d3a8SLen Brown  */
32445f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
32545f1ff59SRafael J. Wysocki 		   bool *stop_tick)
3264f86d3a8SLen Brown {
32745f1ff59SRafael J. Wysocki 	return cpuidle_curr_governor->select(drv, dev, stop_tick);
328246eb7f0SKevin Hilman }
329246eb7f0SKevin Hilman 
330907e30f1SDaniel Lezcano /**
331907e30f1SDaniel Lezcano  * cpuidle_enter - enter into the specified idle state
332907e30f1SDaniel Lezcano  *
333907e30f1SDaniel Lezcano  * @drv:   the cpuidle driver tied with the cpu
334907e30f1SDaniel Lezcano  * @dev:   the cpuidle device
335907e30f1SDaniel Lezcano  * @index: the index in the idle state table
336907e30f1SDaniel Lezcano  *
337907e30f1SDaniel Lezcano  * Returns the index in the idle state, < 0 in case of error.
338907e30f1SDaniel Lezcano  * The error code depends on the backend driver
339907e30f1SDaniel Lezcano  */
340907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
341907e30f1SDaniel Lezcano 		  int index)
342907e30f1SDaniel Lezcano {
3436f9b83acSUlf Hansson 	int ret = 0;
3446f9b83acSUlf Hansson 
3456f9b83acSUlf Hansson 	/*
3466f9b83acSUlf Hansson 	 * Store the next hrtimer, which becomes either next tick or the next
3476f9b83acSUlf Hansson 	 * timer event, whatever expires first. Additionally, to make this data
3486f9b83acSUlf Hansson 	 * useful for consumers outside cpuidle, we rely on that the governor's
3496f9b83acSUlf Hansson 	 * ->select() callback have decided, whether to stop the tick or not.
3506f9b83acSUlf Hansson 	 */
3516f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
3526f9b83acSUlf Hansson 
3534c1ed5a6SXunlei Pang 	if (cpuidle_state_is_coupled(drv, index))
3546f9b83acSUlf Hansson 		ret = cpuidle_enter_state_coupled(dev, drv, index);
3556f9b83acSUlf Hansson 	else
3566f9b83acSUlf Hansson 		ret = cpuidle_enter_state(dev, drv, index);
3576f9b83acSUlf Hansson 
3586f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, 0);
3596f9b83acSUlf Hansson 	return ret;
360907e30f1SDaniel Lezcano }
361fb11c9c6SViresh Kumar 
362907e30f1SDaniel Lezcano /**
363907e30f1SDaniel Lezcano  * cpuidle_reflect - tell the underlying governor what was the state
364907e30f1SDaniel Lezcano  * we were in
365907e30f1SDaniel Lezcano  *
366907e30f1SDaniel Lezcano  * @dev  : the cpuidle device
367907e30f1SDaniel Lezcano  * @index: the index in the idle state table
368907e30f1SDaniel Lezcano  *
369907e30f1SDaniel Lezcano  */
370907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index)
371907e30f1SDaniel Lezcano {
372a802ea96SRafael J. Wysocki 	if (cpuidle_curr_governor->reflect && index >= 0)
373907e30f1SDaniel Lezcano 		cpuidle_curr_governor->reflect(dev, index);
3744f86d3a8SLen Brown }
3754f86d3a8SLen Brown 
3767a25759eSMel Gorman /*
3777a25759eSMel Gorman  * Min polling interval of 10usec is a guess. It is assuming that
3787a25759eSMel Gorman  * for most users, the time for a single ping-pong workload like
3797a25759eSMel Gorman  * perf bench pipe would generally complete within 10usec but
3807a25759eSMel Gorman  * this is hardware dependant. Actual time can be estimated with
3817a25759eSMel Gorman  *
3827a25759eSMel Gorman  * perf bench sched pipe -l 10000
3837a25759eSMel Gorman  *
3847a25759eSMel Gorman  * Run multiple times to avoid cpufreq effects.
3857a25759eSMel Gorman  */
3867a25759eSMel Gorman #define CPUIDLE_POLL_MIN 10000
3877a25759eSMel Gorman #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
3887a25759eSMel Gorman 
3894f86d3a8SLen Brown /**
390259231a0SMarcelo Tosatti  * cpuidle_poll_time - return amount of time to poll for,
391259231a0SMarcelo Tosatti  * governors can override dev->poll_limit_ns if necessary
392259231a0SMarcelo Tosatti  *
393259231a0SMarcelo Tosatti  * @drv:   the cpuidle driver tied with the cpu
394259231a0SMarcelo Tosatti  * @dev:   the cpuidle device
395259231a0SMarcelo Tosatti  *
396259231a0SMarcelo Tosatti  */
397259231a0SMarcelo Tosatti u64 cpuidle_poll_time(struct cpuidle_driver *drv,
398259231a0SMarcelo Tosatti 		      struct cpuidle_device *dev)
399259231a0SMarcelo Tosatti {
400259231a0SMarcelo Tosatti 	int i;
401259231a0SMarcelo Tosatti 	u64 limit_ns;
402259231a0SMarcelo Tosatti 
4037a25759eSMel Gorman 	BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
4047a25759eSMel Gorman 
405259231a0SMarcelo Tosatti 	if (dev->poll_limit_ns)
406259231a0SMarcelo Tosatti 		return dev->poll_limit_ns;
407259231a0SMarcelo Tosatti 
4087a25759eSMel Gorman 	limit_ns = CPUIDLE_POLL_MAX;
409259231a0SMarcelo Tosatti 	for (i = 1; i < drv->state_count; i++) {
4107a25759eSMel Gorman 		u64 state_limit;
4117a25759eSMel Gorman 
41299e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable)
413259231a0SMarcelo Tosatti 			continue;
414259231a0SMarcelo Tosatti 
4157a25759eSMel Gorman 		state_limit = drv->states[i].target_residency_ns;
4167a25759eSMel Gorman 		if (state_limit < CPUIDLE_POLL_MIN)
4177a25759eSMel Gorman 			continue;
4187a25759eSMel Gorman 
4197a25759eSMel Gorman 		limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
42036fcb429SMarcelo Tosatti 		break;
421259231a0SMarcelo Tosatti 	}
422259231a0SMarcelo Tosatti 
423259231a0SMarcelo Tosatti 	dev->poll_limit_ns = limit_ns;
424259231a0SMarcelo Tosatti 
425259231a0SMarcelo Tosatti 	return dev->poll_limit_ns;
426259231a0SMarcelo Tosatti }
427259231a0SMarcelo Tosatti 
428259231a0SMarcelo Tosatti /**
4294f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
4304f86d3a8SLen Brown  */
4314f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
4324f86d3a8SLen Brown {
433a0bfa137SLen Brown 	if (enabled_devices) {
4344f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
4354f86d3a8SLen Brown 		smp_wmb();
436a0bfa137SLen Brown 		initialized = 1;
4374f86d3a8SLen Brown 	}
4384f86d3a8SLen Brown }
4394f86d3a8SLen Brown 
4404f86d3a8SLen Brown /**
4414f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
4424f86d3a8SLen Brown  */
4434f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
4444f86d3a8SLen Brown {
445a0bfa137SLen Brown 	if (enabled_devices) {
446a0bfa137SLen Brown 		initialized = 0;
4472ed903c5SChuansheng Liu 		wake_up_all_idle_cpus();
4484f86d3a8SLen Brown 	}
449442bf3aaSDaniel Lezcano 
450442bf3aaSDaniel Lezcano 	/*
451442bf3aaSDaniel Lezcano 	 * Make sure external observers (such as the scheduler)
452442bf3aaSDaniel Lezcano 	 * are done looking at pointed idle states.
453442bf3aaSDaniel Lezcano 	 */
454442bf3aaSDaniel Lezcano 	synchronize_rcu();
4554f86d3a8SLen Brown }
4564f86d3a8SLen Brown 
4574f86d3a8SLen Brown /**
4584f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
4594f86d3a8SLen Brown  */
4604f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
4614f86d3a8SLen Brown {
4624f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
4634f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
4644f86d3a8SLen Brown }
4654f86d3a8SLen Brown 
4664f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
4674f86d3a8SLen Brown 
4684f86d3a8SLen Brown /**
4694f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
4704f86d3a8SLen Brown  */
4714f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
4724f86d3a8SLen Brown {
4734f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4744f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4754f86d3a8SLen Brown }
4764f86d3a8SLen Brown 
4774f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
4784f86d3a8SLen Brown 
4798651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
4808651f97bSPreeti U Murthy void cpuidle_pause(void)
4818651f97bSPreeti U Murthy {
4828651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4838651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
4848651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4858651f97bSPreeti U Murthy }
4868651f97bSPreeti U Murthy 
4878651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
4888651f97bSPreeti U Murthy void cpuidle_resume(void)
4898651f97bSPreeti U Murthy {
4908651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4918651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
4928651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4938651f97bSPreeti U Murthy }
4948651f97bSPreeti U Murthy 
4954f86d3a8SLen Brown /**
4964f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
4974f86d3a8SLen Brown  * @dev: the CPU
4984f86d3a8SLen Brown  *
4994f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5004f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5014f86d3a8SLen Brown  */
5024f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
5034f86d3a8SLen Brown {
5045df0aa73SDaniel Lezcano 	int ret;
505bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
5064f86d3a8SLen Brown 
5071b0a0e9aSSrivatsa S. Bhat 	if (!dev)
5081b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
5091b0a0e9aSSrivatsa S. Bhat 
5104f86d3a8SLen Brown 	if (dev->enabled)
5114f86d3a8SLen Brown 		return 0;
512bf4d1b5dSDaniel Lezcano 
513e7b06a09SGaurav Jindal 	if (!cpuidle_curr_governor)
514e7b06a09SGaurav Jindal 		return -EIO;
515e7b06a09SGaurav Jindal 
516bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
517bf4d1b5dSDaniel Lezcano 
518e7b06a09SGaurav Jindal 	if (!drv)
5194f86d3a8SLen Brown 		return -EIO;
520bf4d1b5dSDaniel Lezcano 
52110b9d3f8SDaniel Lezcano 	if (!dev->registered)
52210b9d3f8SDaniel Lezcano 		return -EINVAL;
52310b9d3f8SDaniel Lezcano 
524bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
525bf4d1b5dSDaniel Lezcano 	if (ret)
5264f86d3a8SLen Brown 		return ret;
5274f86d3a8SLen Brown 
5283fc74bd8SGaurav Jindal 	if (cpuidle_curr_governor->enable) {
5293fc74bd8SGaurav Jindal 		ret = cpuidle_curr_governor->enable(drv, dev);
5303fc74bd8SGaurav Jindal 		if (ret)
5314f86d3a8SLen Brown 			goto fail_sysfs;
5323fc74bd8SGaurav Jindal 	}
5334f86d3a8SLen Brown 
5344f86d3a8SLen Brown 	smp_wmb();
5354f86d3a8SLen Brown 
5364f86d3a8SLen Brown 	dev->enabled = 1;
5374f86d3a8SLen Brown 
5384f86d3a8SLen Brown 	enabled_devices++;
5394f86d3a8SLen Brown 	return 0;
5404f86d3a8SLen Brown 
5414f86d3a8SLen Brown fail_sysfs:
542bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5434f86d3a8SLen Brown 
5444f86d3a8SLen Brown 	return ret;
5454f86d3a8SLen Brown }
5464f86d3a8SLen Brown 
5474f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
5484f86d3a8SLen Brown 
5494f86d3a8SLen Brown /**
5504f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
5514f86d3a8SLen Brown  * @dev: the CPU
5524f86d3a8SLen Brown  *
5534f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5544f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5554f86d3a8SLen Brown  */
5564f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
5574f86d3a8SLen Brown {
558bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
559bf4d1b5dSDaniel Lezcano 
560cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
5614f86d3a8SLen Brown 		return;
562bf4d1b5dSDaniel Lezcano 
563bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
5644f86d3a8SLen Brown 		return;
5654f86d3a8SLen Brown 
5664f86d3a8SLen Brown 	dev->enabled = 0;
5674f86d3a8SLen Brown 
5684f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
569bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
5704f86d3a8SLen Brown 
571bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5724f86d3a8SLen Brown 	enabled_devices--;
5734f86d3a8SLen Brown }
5744f86d3a8SLen Brown 
5754f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
5764f86d3a8SLen Brown 
577f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev)
578f6bb51a5SDaniel Lezcano {
579f6bb51a5SDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
580f6bb51a5SDaniel Lezcano 
581f6bb51a5SDaniel Lezcano 	list_del(&dev->device_list);
582f6bb51a5SDaniel Lezcano 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
583f6bb51a5SDaniel Lezcano 	module_put(drv->owner);
584c998c078SDave Gerlach 
585c998c078SDave Gerlach 	dev->registered = 0;
586f6bb51a5SDaniel Lezcano }
587f6bb51a5SDaniel Lezcano 
588267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev)
5895df0aa73SDaniel Lezcano {
5905df0aa73SDaniel Lezcano 	memset(dev->states_usage, 0, sizeof(dev->states_usage));
591c1d51f68SRafael J. Wysocki 	dev->last_residency_ns = 0;
5926f9b83acSUlf Hansson 	dev->next_hrtimer = 0;
5935df0aa73SDaniel Lezcano }
5945df0aa73SDaniel Lezcano 
5954f86d3a8SLen Brown /**
596dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
597dcb84f33SVenkatesh Pallipadi  * and enable routines
5984f86d3a8SLen Brown  * @dev: the cpu
599dcb84f33SVenkatesh Pallipadi  *
600dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
6014f86d3a8SLen Brown  */
602dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
6034f86d3a8SLen Brown {
604bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
60599e98d3fSRafael J. Wysocki 	int i, ret;
6064f86d3a8SLen Brown 
607bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
6084f86d3a8SLen Brown 		return -EINVAL;
6094f86d3a8SLen Brown 
61075a80267SRafael J. Wysocki 	for (i = 0; i < drv->state_count; i++) {
611ba1e78a1SRafael J. Wysocki 		if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
61299e98d3fSRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
61399e98d3fSRafael J. Wysocki 
61475a80267SRafael J. Wysocki 		if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
61575a80267SRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
61675a80267SRafael J. Wysocki 	}
61775a80267SRafael J. Wysocki 
6184f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
6194f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
6204f86d3a8SLen Brown 
6214126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
62247182668SViresh Kumar 	if (ret)
623f6bb51a5SDaniel Lezcano 		__cpuidle_unregister_device(dev);
62447182668SViresh Kumar 	else
625dcb84f33SVenkatesh Pallipadi 		dev->registered = 1;
62647182668SViresh Kumar 
62747182668SViresh Kumar 	return ret;
628dcb84f33SVenkatesh Pallipadi }
629dcb84f33SVenkatesh Pallipadi 
630dcb84f33SVenkatesh Pallipadi /**
631dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
632dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
633dcb84f33SVenkatesh Pallipadi  */
634dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
635dcb84f33SVenkatesh Pallipadi {
636c878a52dSDaniel Lezcano 	int ret = -EBUSY;
637dcb84f33SVenkatesh Pallipadi 
6381b0a0e9aSSrivatsa S. Bhat 	if (!dev)
6391b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
6401b0a0e9aSSrivatsa S. Bhat 
641dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
642dcb84f33SVenkatesh Pallipadi 
643c878a52dSDaniel Lezcano 	if (dev->registered)
644c878a52dSDaniel Lezcano 		goto out_unlock;
645c878a52dSDaniel Lezcano 
646267d4bf8SViresh Kumar 	__cpuidle_device_init(dev);
6475df0aa73SDaniel Lezcano 
648f6bb51a5SDaniel Lezcano 	ret = __cpuidle_register_device(dev);
649f6bb51a5SDaniel Lezcano 	if (ret)
650f6bb51a5SDaniel Lezcano 		goto out_unlock;
651f6bb51a5SDaniel Lezcano 
652f6bb51a5SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
653f6bb51a5SDaniel Lezcano 	if (ret)
654f6bb51a5SDaniel Lezcano 		goto out_unregister;
655dcb84f33SVenkatesh Pallipadi 
65610b9d3f8SDaniel Lezcano 	ret = cpuidle_enable_device(dev);
657f6bb51a5SDaniel Lezcano 	if (ret)
658f6bb51a5SDaniel Lezcano 		goto out_sysfs;
65910b9d3f8SDaniel Lezcano 
6604f86d3a8SLen Brown 	cpuidle_install_idle_handler();
6614f86d3a8SLen Brown 
662f6bb51a5SDaniel Lezcano out_unlock:
6634f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
6644f86d3a8SLen Brown 
665f6bb51a5SDaniel Lezcano 	return ret;
666f6bb51a5SDaniel Lezcano 
667f6bb51a5SDaniel Lezcano out_sysfs:
668f6bb51a5SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
669f6bb51a5SDaniel Lezcano out_unregister:
670f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
671f6bb51a5SDaniel Lezcano 	goto out_unlock;
6724f86d3a8SLen Brown }
6734f86d3a8SLen Brown 
6744f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
6754f86d3a8SLen Brown 
6764f86d3a8SLen Brown /**
6774f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
6784f86d3a8SLen Brown  * @dev: the cpu
6794f86d3a8SLen Brown  */
6804f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
6814f86d3a8SLen Brown {
682813e8e3dSKonrad Rzeszutek Wilk 	if (!dev || dev->registered == 0)
683dcb84f33SVenkatesh Pallipadi 		return;
684dcb84f33SVenkatesh Pallipadi 
6854f86d3a8SLen Brown 	cpuidle_pause_and_lock();
6864f86d3a8SLen Brown 
6874f86d3a8SLen Brown 	cpuidle_disable_device(dev);
6884f86d3a8SLen Brown 
6891aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
690f6bb51a5SDaniel Lezcano 
691f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
6924f86d3a8SLen Brown 
6934126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
6944126c019SColin Cross 
6954f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
6964f86d3a8SLen Brown }
6974f86d3a8SLen Brown 
6984f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
6994f86d3a8SLen Brown 
7001c192d04SDaniel Lezcano /**
7014c637b21SDaniel Lezcano  * cpuidle_unregister: unregister a driver and the devices. This function
7024c637b21SDaniel Lezcano  * can be used only if the driver has been previously registered through
7034c637b21SDaniel Lezcano  * the cpuidle_register function.
7044c637b21SDaniel Lezcano  *
7054c637b21SDaniel Lezcano  * @drv: a valid pointer to a struct cpuidle_driver
7064c637b21SDaniel Lezcano  */
7074c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv)
7084c637b21SDaniel Lezcano {
7094c637b21SDaniel Lezcano 	int cpu;
7104c637b21SDaniel Lezcano 	struct cpuidle_device *device;
7114c637b21SDaniel Lezcano 
71282467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7134c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7144c637b21SDaniel Lezcano 		cpuidle_unregister_device(device);
7154c637b21SDaniel Lezcano 	}
7164c637b21SDaniel Lezcano 
7174c637b21SDaniel Lezcano 	cpuidle_unregister_driver(drv);
7184c637b21SDaniel Lezcano }
7194c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister);
7204c637b21SDaniel Lezcano 
7214c637b21SDaniel Lezcano /**
7224c637b21SDaniel Lezcano  * cpuidle_register: registers the driver and the cpu devices with the
7234c637b21SDaniel Lezcano  * coupled_cpus passed as parameter. This function is used for all common
7244c637b21SDaniel Lezcano  * initialization pattern there are in the arch specific drivers. The
7254c637b21SDaniel Lezcano  * devices is globally defined in this file.
7264c637b21SDaniel Lezcano  *
7274c637b21SDaniel Lezcano  * @drv         : a valid pointer to a struct cpuidle_driver
7284c637b21SDaniel Lezcano  * @coupled_cpus: a cpumask for the coupled states
7294c637b21SDaniel Lezcano  *
7304c637b21SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
7314c637b21SDaniel Lezcano  */
7324c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv,
7334c637b21SDaniel Lezcano 		     const struct cpumask *const coupled_cpus)
7344c637b21SDaniel Lezcano {
7354c637b21SDaniel Lezcano 	int ret, cpu;
7364c637b21SDaniel Lezcano 	struct cpuidle_device *device;
7374c637b21SDaniel Lezcano 
7384c637b21SDaniel Lezcano 	ret = cpuidle_register_driver(drv);
7394c637b21SDaniel Lezcano 	if (ret) {
7404c637b21SDaniel Lezcano 		pr_err("failed to register cpuidle driver\n");
7414c637b21SDaniel Lezcano 		return ret;
7424c637b21SDaniel Lezcano 	}
7434c637b21SDaniel Lezcano 
74482467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7454c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7464c637b21SDaniel Lezcano 		device->cpu = cpu;
7474c637b21SDaniel Lezcano 
7484c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
7494c637b21SDaniel Lezcano 		/*
750caf4a36eSViresh Kumar 		 * On multiplatform for ARM, the coupled idle states could be
7514c637b21SDaniel Lezcano 		 * enabled in the kernel even if the cpuidle driver does not
7524c637b21SDaniel Lezcano 		 * use it. Note, coupled_cpus is a struct copy.
7534c637b21SDaniel Lezcano 		 */
7544c637b21SDaniel Lezcano 		if (coupled_cpus)
7554c637b21SDaniel Lezcano 			device->coupled_cpus = *coupled_cpus;
7564c637b21SDaniel Lezcano #endif
7574c637b21SDaniel Lezcano 		ret = cpuidle_register_device(device);
7584c637b21SDaniel Lezcano 		if (!ret)
7594c637b21SDaniel Lezcano 			continue;
7604c637b21SDaniel Lezcano 
7614c637b21SDaniel Lezcano 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
7624c637b21SDaniel Lezcano 
7634c637b21SDaniel Lezcano 		cpuidle_unregister(drv);
7644c637b21SDaniel Lezcano 		break;
7654c637b21SDaniel Lezcano 	}
7664c637b21SDaniel Lezcano 
7674c637b21SDaniel Lezcano 	return ret;
7684c637b21SDaniel Lezcano }
7694c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register);
7704c637b21SDaniel Lezcano 
7714f86d3a8SLen Brown /**
7724f86d3a8SLen Brown  * cpuidle_init - core initializer
7734f86d3a8SLen Brown  */
7744f86d3a8SLen Brown static int __init cpuidle_init(void)
7754f86d3a8SLen Brown {
77662027aeaSLen Brown 	if (cpuidle_disabled())
77762027aeaSLen Brown 		return -ENODEV;
77862027aeaSLen Brown 
7793a4a0042SRafael J. Wysocki 	return cpuidle_add_interface(cpu_subsys.dev_root);
7804f86d3a8SLen Brown }
7814f86d3a8SLen Brown 
78262027aeaSLen Brown module_param(off, int, 0444);
78361cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
7844f86d3a8SLen Brown core_initcall(cpuidle_init);
785