xref: /linux/drivers/cpuidle/cpuidle.c (revision 1098582a0f6c4e8fd28da0a6305f9233d02c9c1d)
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.
124cefb9409SBenjamin Gaignard  * @latency_limit_ns: Idle state exit latency limit
125cefb9409SBenjamin Gaignard  *
126cefb9409SBenjamin Gaignard  * Return: the index of the deepest available idle state.
127ef2b22acSRafael J. Wysocki  */
128ef2b22acSRafael J. Wysocki int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
1295aa9ba63SDaniel Lezcano 			       struct cpuidle_device *dev,
1305aa9ba63SDaniel Lezcano 			       u64 latency_limit_ns)
131ef2b22acSRafael J. Wysocki {
1325aa9ba63SDaniel Lezcano 	return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
133ef2b22acSRafael J. Wysocki }
134ef2b22acSRafael J. Wysocki 
135bb8313b6SJacob Pan #ifdef CONFIG_SUSPEND
13628ba086eSRafael J. Wysocki static void enter_s2idle_proper(struct cpuidle_driver *drv,
137124cf911SRafael J. Wysocki 				struct cpuidle_device *dev, int index)
138124cf911SRafael J. Wysocki {
13964bdff69SRafael J. Wysocki 	ktime_t time_start, time_end;
14064bdff69SRafael J. Wysocki 
14164bdff69SRafael J. Wysocki 	time_start = ns_to_ktime(local_clock());
14264bdff69SRafael J. Wysocki 
143ae0afb4fSRafael J. Wysocki 	/*
144ae0afb4fSRafael J. Wysocki 	 * trace_suspend_resume() called by tick_freeze() for the last CPU
145ae0afb4fSRafael J. Wysocki 	 * executing it contains RCU usage regarded as invalid in the idle
146ae0afb4fSRafael J. Wysocki 	 * context, so tell RCU about that.
147ae0afb4fSRafael J. Wysocki 	 */
148*1098582aSPeter Zijlstra 	tick_freeze();
149124cf911SRafael J. Wysocki 	/*
150124cf911SRafael J. Wysocki 	 * The state used here cannot be a "coupled" one, because the "coupled"
151124cf911SRafael J. Wysocki 	 * cpuidle mechanism enables interrupts and doing that with timekeeping
152124cf911SRafael J. Wysocki 	 * suspended is generally unsafe.
153124cf911SRafael J. Wysocki 	 */
15463caae84SLucas Stach 	stop_critical_timings();
155*1098582aSPeter Zijlstra 	rcu_idle_enter();
15628ba086eSRafael J. Wysocki 	drv->states[index].enter_s2idle(dev, drv, index);
15749d9c593SPeter Zijlstra 	if (WARN_ON_ONCE(!irqs_disabled()))
15849d9c593SPeter Zijlstra 		local_irq_disable();
159124cf911SRafael J. Wysocki 	/*
160124cf911SRafael J. Wysocki 	 * timekeeping_resume() that will be called by tick_unfreeze() for the
161ae0afb4fSRafael J. Wysocki 	 * first CPU executing it calls functions containing RCU read-side
162124cf911SRafael J. Wysocki 	 * critical sections, so tell RCU about that.
163124cf911SRafael J. Wysocki 	 */
164*1098582aSPeter Zijlstra 	rcu_idle_exit();
165*1098582aSPeter Zijlstra 	tick_unfreeze();
16663caae84SLucas Stach 	start_critical_timings();
16764bdff69SRafael J. Wysocki 
16864bdff69SRafael J. Wysocki 	time_end = ns_to_ktime(local_clock());
16964bdff69SRafael J. Wysocki 
17064bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
17164bdff69SRafael J. Wysocki 	dev->states_usage[index].s2idle_usage++;
172124cf911SRafael J. Wysocki }
173124cf911SRafael J. Wysocki 
174a6220fc1SRafael J. Wysocki /**
17528ba086eSRafael J. Wysocki  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
176ef2b22acSRafael J. Wysocki  * @drv: cpuidle driver for the given CPU.
177ef2b22acSRafael J. Wysocki  * @dev: cpuidle device for the given CPU.
17838106313SRafael J. Wysocki  *
17928ba086eSRafael J. Wysocki  * If there are states with the ->enter_s2idle callback, find the deepest of
180ef2b22acSRafael J. Wysocki  * them and enter it with frozen tick.
18138106313SRafael J. Wysocki  */
18228ba086eSRafael J. Wysocki int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
18338106313SRafael J. Wysocki {
18438106313SRafael J. Wysocki 	int index;
18538106313SRafael J. Wysocki 
186124cf911SRafael J. Wysocki 	/*
18728ba086eSRafael J. Wysocki 	 * Find the deepest state with ->enter_s2idle present, which guarantees
188124cf911SRafael J. Wysocki 	 * that interrupts won't be enabled when it exits and allows the tick to
189124cf911SRafael J. Wysocki 	 * be frozen safely.
190124cf911SRafael J. Wysocki 	 */
191c1d51f68SRafael J. Wysocki 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
19210e8b11eSRafael J. Wysocki 	if (index > 0) {
19328ba086eSRafael J. Wysocki 		enter_s2idle_proper(drv, dev, index);
19410e8b11eSRafael J. Wysocki 		local_irq_enable();
19510e8b11eSRafael J. Wysocki 	}
196ef2b22acSRafael J. Wysocki 	return index;
19738106313SRafael J. Wysocki }
19887e9b9f1SRafael J. Wysocki #endif /* CONFIG_SUSPEND */
19938106313SRafael J. Wysocki 
20038106313SRafael J. Wysocki /**
20156cfbf74SColin Cross  * cpuidle_enter_state - enter the state and update stats
20256cfbf74SColin Cross  * @dev: cpuidle device for this cpu
20356cfbf74SColin Cross  * @drv: cpuidle driver for this cpu
2047312280bSRafael J. Wysocki  * @index: index into the states table in @drv of the state to enter
20556cfbf74SColin Cross  */
20656cfbf74SColin Cross int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
207554c06baSDaniel Lezcano 			int index)
20856cfbf74SColin Cross {
20956cfbf74SColin Cross 	int entered_state;
21056cfbf74SColin Cross 
211554c06baSDaniel Lezcano 	struct cpuidle_state *target_state = &drv->states[index];
212df8d9eeaSRafael J. Wysocki 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
213dbd1b8eaSShreyas B. Prabhu 	ktime_t time_start, time_end;
214554c06baSDaniel Lezcano 
215df8d9eeaSRafael J. Wysocki 	/*
216df8d9eeaSRafael J. Wysocki 	 * Tell the time framework to switch to a broadcast timer because our
217df8d9eeaSRafael J. Wysocki 	 * local timer will be shut down.  If a local timer is used from another
218df8d9eeaSRafael J. Wysocki 	 * CPU as a broadcast timer, this call may fail if it is not available.
219df8d9eeaSRafael J. Wysocki 	 */
220827a5aefSRafael J. Wysocki 	if (broadcast && tick_broadcast_enter()) {
221c1d51f68SRafael J. Wysocki 		index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
2220d94039fSRafael J. Wysocki 					   CPUIDLE_FLAG_TIMER_STOP, false);
2230d94039fSRafael J. Wysocki 		if (index < 0) {
224827a5aefSRafael J. Wysocki 			default_idle_call();
225df8d9eeaSRafael J. Wysocki 			return -EBUSY;
226827a5aefSRafael J. Wysocki 		}
2270d94039fSRafael J. Wysocki 		target_state = &drv->states[index];
228f187851bSNicholas Piggin 		broadcast = false;
2290d94039fSRafael J. Wysocki 	}
230df8d9eeaSRafael J. Wysocki 
231faad3849SRafael J. Wysocki 	/* Take note of the planned idle state. */
232faad3849SRafael J. Wysocki 	sched_idle_set_state(target_state);
233faad3849SRafael J. Wysocki 
234*1098582aSPeter Zijlstra 	trace_cpu_idle(index, dev->cpu);
235dbd1b8eaSShreyas B. Prabhu 	time_start = ns_to_ktime(local_clock());
236554c06baSDaniel Lezcano 
23763caae84SLucas Stach 	stop_critical_timings();
238*1098582aSPeter Zijlstra 	rcu_idle_enter();
239554c06baSDaniel Lezcano 	entered_state = target_state->enter(dev, drv, index);
240*1098582aSPeter Zijlstra 	rcu_idle_exit();
24163caae84SLucas Stach 	start_critical_timings();
242554c06baSDaniel Lezcano 
243f9fccdb9SPeter Zijlstra 	sched_clock_idle_wakeup_event();
244dbd1b8eaSShreyas B. Prabhu 	time_end = ns_to_ktime(local_clock());
245*1098582aSPeter Zijlstra 	trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
246554c06baSDaniel Lezcano 
247faad3849SRafael J. Wysocki 	/* The cpu is no longer idle or about to enter idle. */
248faad3849SRafael J. Wysocki 	sched_idle_set_state(NULL);
249faad3849SRafael J. Wysocki 
250df8d9eeaSRafael J. Wysocki 	if (broadcast) {
251df8d9eeaSRafael J. Wysocki 		if (WARN_ON_ONCE(!irqs_disabled()))
252df8d9eeaSRafael J. Wysocki 			local_irq_disable();
253df8d9eeaSRafael J. Wysocki 
254df8d9eeaSRafael J. Wysocki 		tick_broadcast_exit();
255df8d9eeaSRafael J. Wysocki 	}
256df8d9eeaSRafael J. Wysocki 
257e7387da5SDaniel Lezcano 	if (!cpuidle_state_is_coupled(drv, index))
258554c06baSDaniel Lezcano 		local_irq_enable();
259554c06baSDaniel Lezcano 
2607037b43eSFieah Lim 	if (entered_state >= 0) {
261c1d51f68SRafael J. Wysocki 		s64 diff, delay = drv->states[entered_state].exit_latency_ns;
26204dab58aSRafael J. Wysocki 		int i;
26304dab58aSRafael J. Wysocki 
2647037b43eSFieah Lim 		/*
2657037b43eSFieah Lim 		 * Update cpuidle counters
2667037b43eSFieah Lim 		 * This can be moved to within driver enter routine,
2677037b43eSFieah Lim 		 * but that results in multiple copies of same code.
2687037b43eSFieah Lim 		 */
269c1d51f68SRafael J. Wysocki 		diff = ktime_sub(time_end, time_start);
270554c06baSDaniel Lezcano 
271c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = diff;
272c1d51f68SRafael J. Wysocki 		dev->states_usage[entered_state].time_ns += diff;
27356cfbf74SColin Cross 		dev->states_usage[entered_state].usage++;
27404dab58aSRafael J. Wysocki 
275c1d51f68SRafael J. Wysocki 		if (diff < drv->states[entered_state].target_residency_ns) {
27604dab58aSRafael J. Wysocki 			for (i = entered_state - 1; i >= 0; i--) {
27799e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
27804dab58aSRafael J. Wysocki 					continue;
27904dab58aSRafael J. Wysocki 
28004dab58aSRafael J. Wysocki 				/* Shallower states are enabled, so update. */
28104dab58aSRafael J. Wysocki 				dev->states_usage[entered_state].above++;
28204dab58aSRafael J. Wysocki 				break;
28304dab58aSRafael J. Wysocki 			}
28404dab58aSRafael J. Wysocki 		} else if (diff > delay) {
28504dab58aSRafael J. Wysocki 			for (i = entered_state + 1; i < drv->state_count; i++) {
28699e98d3fSRafael J. Wysocki 				if (dev->states_usage[i].disable)
28704dab58aSRafael J. Wysocki 					continue;
28804dab58aSRafael J. Wysocki 
28904dab58aSRafael J. Wysocki 				/*
29004dab58aSRafael J. Wysocki 				 * Update if a deeper state would have been a
29104dab58aSRafael J. Wysocki 				 * better match for the observed idle duration.
29204dab58aSRafael J. Wysocki 				 */
293c1d51f68SRafael J. Wysocki 				if (diff - delay >= drv->states[i].target_residency_ns)
29404dab58aSRafael J. Wysocki 					dev->states_usage[entered_state].below++;
29504dab58aSRafael J. Wysocki 
29604dab58aSRafael J. Wysocki 				break;
29704dab58aSRafael J. Wysocki 			}
29804dab58aSRafael J. Wysocki 		}
29956cfbf74SColin Cross 	} else {
300c1d51f68SRafael J. Wysocki 		dev->last_residency_ns = 0;
30156cfbf74SColin Cross 	}
30256cfbf74SColin Cross 
30356cfbf74SColin Cross 	return entered_state;
30456cfbf74SColin Cross }
30556cfbf74SColin Cross 
30656cfbf74SColin Cross /**
307907e30f1SDaniel Lezcano  * cpuidle_select - ask the cpuidle framework to choose an idle state
3084f86d3a8SLen Brown  *
309907e30f1SDaniel Lezcano  * @drv: the cpuidle driver
310907e30f1SDaniel Lezcano  * @dev: the cpuidle device
31145f1ff59SRafael J. Wysocki  * @stop_tick: indication on whether or not to stop the tick
312907e30f1SDaniel Lezcano  *
31351164251SRafael J. Wysocki  * Returns the index of the idle state.  The return value must not be negative.
31445f1ff59SRafael J. Wysocki  *
31545f1ff59SRafael J. Wysocki  * The memory location pointed to by @stop_tick is expected to be written the
31645f1ff59SRafael J. Wysocki  * 'false' boolean value if the scheduler tick should not be stopped before
31745f1ff59SRafael J. Wysocki  * entering the returned state.
3184f86d3a8SLen Brown  */
31945f1ff59SRafael J. Wysocki int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
32045f1ff59SRafael J. Wysocki 		   bool *stop_tick)
3214f86d3a8SLen Brown {
32245f1ff59SRafael J. Wysocki 	return cpuidle_curr_governor->select(drv, dev, stop_tick);
323246eb7f0SKevin Hilman }
324246eb7f0SKevin Hilman 
325907e30f1SDaniel Lezcano /**
326907e30f1SDaniel Lezcano  * cpuidle_enter - enter into the specified idle state
327907e30f1SDaniel Lezcano  *
328907e30f1SDaniel Lezcano  * @drv:   the cpuidle driver tied with the cpu
329907e30f1SDaniel Lezcano  * @dev:   the cpuidle device
330907e30f1SDaniel Lezcano  * @index: the index in the idle state table
331907e30f1SDaniel Lezcano  *
332907e30f1SDaniel Lezcano  * Returns the index in the idle state, < 0 in case of error.
333907e30f1SDaniel Lezcano  * The error code depends on the backend driver
334907e30f1SDaniel Lezcano  */
335907e30f1SDaniel Lezcano int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
336907e30f1SDaniel Lezcano 		  int index)
337907e30f1SDaniel Lezcano {
3386f9b83acSUlf Hansson 	int ret = 0;
3396f9b83acSUlf Hansson 
3406f9b83acSUlf Hansson 	/*
3416f9b83acSUlf Hansson 	 * Store the next hrtimer, which becomes either next tick or the next
3426f9b83acSUlf Hansson 	 * timer event, whatever expires first. Additionally, to make this data
3436f9b83acSUlf Hansson 	 * useful for consumers outside cpuidle, we rely on that the governor's
3446f9b83acSUlf Hansson 	 * ->select() callback have decided, whether to stop the tick or not.
3456f9b83acSUlf Hansson 	 */
3466f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
3476f9b83acSUlf Hansson 
3484c1ed5a6SXunlei Pang 	if (cpuidle_state_is_coupled(drv, index))
3496f9b83acSUlf Hansson 		ret = cpuidle_enter_state_coupled(dev, drv, index);
3506f9b83acSUlf Hansson 	else
3516f9b83acSUlf Hansson 		ret = cpuidle_enter_state(dev, drv, index);
3526f9b83acSUlf Hansson 
3536f9b83acSUlf Hansson 	WRITE_ONCE(dev->next_hrtimer, 0);
3546f9b83acSUlf Hansson 	return ret;
355907e30f1SDaniel Lezcano }
356fb11c9c6SViresh Kumar 
357907e30f1SDaniel Lezcano /**
358907e30f1SDaniel Lezcano  * cpuidle_reflect - tell the underlying governor what was the state
359907e30f1SDaniel Lezcano  * we were in
360907e30f1SDaniel Lezcano  *
361907e30f1SDaniel Lezcano  * @dev  : the cpuidle device
362907e30f1SDaniel Lezcano  * @index: the index in the idle state table
363907e30f1SDaniel Lezcano  *
364907e30f1SDaniel Lezcano  */
365907e30f1SDaniel Lezcano void cpuidle_reflect(struct cpuidle_device *dev, int index)
366907e30f1SDaniel Lezcano {
367a802ea96SRafael J. Wysocki 	if (cpuidle_curr_governor->reflect && index >= 0)
368907e30f1SDaniel Lezcano 		cpuidle_curr_governor->reflect(dev, index);
3694f86d3a8SLen Brown }
3704f86d3a8SLen Brown 
3714f86d3a8SLen Brown /**
372259231a0SMarcelo Tosatti  * cpuidle_poll_time - return amount of time to poll for,
373259231a0SMarcelo Tosatti  * governors can override dev->poll_limit_ns if necessary
374259231a0SMarcelo Tosatti  *
375259231a0SMarcelo Tosatti  * @drv:   the cpuidle driver tied with the cpu
376259231a0SMarcelo Tosatti  * @dev:   the cpuidle device
377259231a0SMarcelo Tosatti  *
378259231a0SMarcelo Tosatti  */
379259231a0SMarcelo Tosatti u64 cpuidle_poll_time(struct cpuidle_driver *drv,
380259231a0SMarcelo Tosatti 		      struct cpuidle_device *dev)
381259231a0SMarcelo Tosatti {
382259231a0SMarcelo Tosatti 	int i;
383259231a0SMarcelo Tosatti 	u64 limit_ns;
384259231a0SMarcelo Tosatti 
385259231a0SMarcelo Tosatti 	if (dev->poll_limit_ns)
386259231a0SMarcelo Tosatti 		return dev->poll_limit_ns;
387259231a0SMarcelo Tosatti 
388259231a0SMarcelo Tosatti 	limit_ns = TICK_NSEC;
389259231a0SMarcelo Tosatti 	for (i = 1; i < drv->state_count; i++) {
39099e98d3fSRafael J. Wysocki 		if (dev->states_usage[i].disable)
391259231a0SMarcelo Tosatti 			continue;
392259231a0SMarcelo Tosatti 
393d4d81401SRafael J. Wysocki 		limit_ns = drv->states[i].target_residency_ns;
39436fcb429SMarcelo Tosatti 		break;
395259231a0SMarcelo Tosatti 	}
396259231a0SMarcelo Tosatti 
397259231a0SMarcelo Tosatti 	dev->poll_limit_ns = limit_ns;
398259231a0SMarcelo Tosatti 
399259231a0SMarcelo Tosatti 	return dev->poll_limit_ns;
400259231a0SMarcelo Tosatti }
401259231a0SMarcelo Tosatti 
402259231a0SMarcelo Tosatti /**
4034f86d3a8SLen Brown  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
4044f86d3a8SLen Brown  */
4054f86d3a8SLen Brown void cpuidle_install_idle_handler(void)
4064f86d3a8SLen Brown {
407a0bfa137SLen Brown 	if (enabled_devices) {
4084f86d3a8SLen Brown 		/* Make sure all changes finished before we switch to new idle */
4094f86d3a8SLen Brown 		smp_wmb();
410a0bfa137SLen Brown 		initialized = 1;
4114f86d3a8SLen Brown 	}
4124f86d3a8SLen Brown }
4134f86d3a8SLen Brown 
4144f86d3a8SLen Brown /**
4154f86d3a8SLen Brown  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
4164f86d3a8SLen Brown  */
4174f86d3a8SLen Brown void cpuidle_uninstall_idle_handler(void)
4184f86d3a8SLen Brown {
419a0bfa137SLen Brown 	if (enabled_devices) {
420a0bfa137SLen Brown 		initialized = 0;
4212ed903c5SChuansheng Liu 		wake_up_all_idle_cpus();
4224f86d3a8SLen Brown 	}
423442bf3aaSDaniel Lezcano 
424442bf3aaSDaniel Lezcano 	/*
425442bf3aaSDaniel Lezcano 	 * Make sure external observers (such as the scheduler)
426442bf3aaSDaniel Lezcano 	 * are done looking at pointed idle states.
427442bf3aaSDaniel Lezcano 	 */
428442bf3aaSDaniel Lezcano 	synchronize_rcu();
4294f86d3a8SLen Brown }
4304f86d3a8SLen Brown 
4314f86d3a8SLen Brown /**
4324f86d3a8SLen Brown  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
4334f86d3a8SLen Brown  */
4344f86d3a8SLen Brown void cpuidle_pause_and_lock(void)
4354f86d3a8SLen Brown {
4364f86d3a8SLen Brown 	mutex_lock(&cpuidle_lock);
4374f86d3a8SLen Brown 	cpuidle_uninstall_idle_handler();
4384f86d3a8SLen Brown }
4394f86d3a8SLen Brown 
4404f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
4414f86d3a8SLen Brown 
4424f86d3a8SLen Brown /**
4434f86d3a8SLen Brown  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
4444f86d3a8SLen Brown  */
4454f86d3a8SLen Brown void cpuidle_resume_and_unlock(void)
4464f86d3a8SLen Brown {
4474f86d3a8SLen Brown 	cpuidle_install_idle_handler();
4484f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
4494f86d3a8SLen Brown }
4504f86d3a8SLen Brown 
4514f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
4524f86d3a8SLen Brown 
4538651f97bSPreeti U Murthy /* Currently used in suspend/resume path to suspend cpuidle */
4548651f97bSPreeti U Murthy void cpuidle_pause(void)
4558651f97bSPreeti U Murthy {
4568651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4578651f97bSPreeti U Murthy 	cpuidle_uninstall_idle_handler();
4588651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4598651f97bSPreeti U Murthy }
4608651f97bSPreeti U Murthy 
4618651f97bSPreeti U Murthy /* Currently used in suspend/resume path to resume cpuidle */
4628651f97bSPreeti U Murthy void cpuidle_resume(void)
4638651f97bSPreeti U Murthy {
4648651f97bSPreeti U Murthy 	mutex_lock(&cpuidle_lock);
4658651f97bSPreeti U Murthy 	cpuidle_install_idle_handler();
4668651f97bSPreeti U Murthy 	mutex_unlock(&cpuidle_lock);
4678651f97bSPreeti U Murthy }
4688651f97bSPreeti U Murthy 
4694f86d3a8SLen Brown /**
4704f86d3a8SLen Brown  * cpuidle_enable_device - enables idle PM for a CPU
4714f86d3a8SLen Brown  * @dev: the CPU
4724f86d3a8SLen Brown  *
4734f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
4744f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
4754f86d3a8SLen Brown  */
4764f86d3a8SLen Brown int cpuidle_enable_device(struct cpuidle_device *dev)
4774f86d3a8SLen Brown {
4785df0aa73SDaniel Lezcano 	int ret;
479bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv;
4804f86d3a8SLen Brown 
4811b0a0e9aSSrivatsa S. Bhat 	if (!dev)
4821b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
4831b0a0e9aSSrivatsa S. Bhat 
4844f86d3a8SLen Brown 	if (dev->enabled)
4854f86d3a8SLen Brown 		return 0;
486bf4d1b5dSDaniel Lezcano 
487e7b06a09SGaurav Jindal 	if (!cpuidle_curr_governor)
488e7b06a09SGaurav Jindal 		return -EIO;
489e7b06a09SGaurav Jindal 
490bf4d1b5dSDaniel Lezcano 	drv = cpuidle_get_cpu_driver(dev);
491bf4d1b5dSDaniel Lezcano 
492e7b06a09SGaurav Jindal 	if (!drv)
4934f86d3a8SLen Brown 		return -EIO;
494bf4d1b5dSDaniel Lezcano 
49510b9d3f8SDaniel Lezcano 	if (!dev->registered)
49610b9d3f8SDaniel Lezcano 		return -EINVAL;
49710b9d3f8SDaniel Lezcano 
498bf4d1b5dSDaniel Lezcano 	ret = cpuidle_add_device_sysfs(dev);
499bf4d1b5dSDaniel Lezcano 	if (ret)
5004f86d3a8SLen Brown 		return ret;
5014f86d3a8SLen Brown 
5023fc74bd8SGaurav Jindal 	if (cpuidle_curr_governor->enable) {
5033fc74bd8SGaurav Jindal 		ret = cpuidle_curr_governor->enable(drv, dev);
5043fc74bd8SGaurav Jindal 		if (ret)
5054f86d3a8SLen Brown 			goto fail_sysfs;
5063fc74bd8SGaurav Jindal 	}
5074f86d3a8SLen Brown 
5084f86d3a8SLen Brown 	smp_wmb();
5094f86d3a8SLen Brown 
5104f86d3a8SLen Brown 	dev->enabled = 1;
5114f86d3a8SLen Brown 
5124f86d3a8SLen Brown 	enabled_devices++;
5134f86d3a8SLen Brown 	return 0;
5144f86d3a8SLen Brown 
5154f86d3a8SLen Brown fail_sysfs:
516bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5174f86d3a8SLen Brown 
5184f86d3a8SLen Brown 	return ret;
5194f86d3a8SLen Brown }
5204f86d3a8SLen Brown 
5214f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_enable_device);
5224f86d3a8SLen Brown 
5234f86d3a8SLen Brown /**
5244f86d3a8SLen Brown  * cpuidle_disable_device - disables idle PM for a CPU
5254f86d3a8SLen Brown  * @dev: the CPU
5264f86d3a8SLen Brown  *
5274f86d3a8SLen Brown  * This function must be called between cpuidle_pause_and_lock and
5284f86d3a8SLen Brown  * cpuidle_resume_and_unlock when used externally.
5294f86d3a8SLen Brown  */
5304f86d3a8SLen Brown void cpuidle_disable_device(struct cpuidle_device *dev)
5314f86d3a8SLen Brown {
532bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
533bf4d1b5dSDaniel Lezcano 
534cf31cd1aSSrivatsa S. Bhat 	if (!dev || !dev->enabled)
5354f86d3a8SLen Brown 		return;
536bf4d1b5dSDaniel Lezcano 
537bf4d1b5dSDaniel Lezcano 	if (!drv || !cpuidle_curr_governor)
5384f86d3a8SLen Brown 		return;
5394f86d3a8SLen Brown 
5404f86d3a8SLen Brown 	dev->enabled = 0;
5414f86d3a8SLen Brown 
5424f86d3a8SLen Brown 	if (cpuidle_curr_governor->disable)
543bf4d1b5dSDaniel Lezcano 		cpuidle_curr_governor->disable(drv, dev);
5444f86d3a8SLen Brown 
545bf4d1b5dSDaniel Lezcano 	cpuidle_remove_device_sysfs(dev);
5464f86d3a8SLen Brown 	enabled_devices--;
5474f86d3a8SLen Brown }
5484f86d3a8SLen Brown 
5494f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_disable_device);
5504f86d3a8SLen Brown 
551f6bb51a5SDaniel Lezcano static void __cpuidle_unregister_device(struct cpuidle_device *dev)
552f6bb51a5SDaniel Lezcano {
553f6bb51a5SDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
554f6bb51a5SDaniel Lezcano 
555f6bb51a5SDaniel Lezcano 	list_del(&dev->device_list);
556f6bb51a5SDaniel Lezcano 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
557f6bb51a5SDaniel Lezcano 	module_put(drv->owner);
558c998c078SDave Gerlach 
559c998c078SDave Gerlach 	dev->registered = 0;
560f6bb51a5SDaniel Lezcano }
561f6bb51a5SDaniel Lezcano 
562267d4bf8SViresh Kumar static void __cpuidle_device_init(struct cpuidle_device *dev)
5635df0aa73SDaniel Lezcano {
5645df0aa73SDaniel Lezcano 	memset(dev->states_usage, 0, sizeof(dev->states_usage));
565c1d51f68SRafael J. Wysocki 	dev->last_residency_ns = 0;
5666f9b83acSUlf Hansson 	dev->next_hrtimer = 0;
5675df0aa73SDaniel Lezcano }
5685df0aa73SDaniel Lezcano 
5694f86d3a8SLen Brown /**
570dcb84f33SVenkatesh Pallipadi  * __cpuidle_register_device - internal register function called before register
571dcb84f33SVenkatesh Pallipadi  * and enable routines
5724f86d3a8SLen Brown  * @dev: the cpu
573dcb84f33SVenkatesh Pallipadi  *
574dcb84f33SVenkatesh Pallipadi  * cpuidle_lock mutex must be held before this is called
5754f86d3a8SLen Brown  */
576dcb84f33SVenkatesh Pallipadi static int __cpuidle_register_device(struct cpuidle_device *dev)
5774f86d3a8SLen Brown {
578bf4d1b5dSDaniel Lezcano 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
57999e98d3fSRafael J. Wysocki 	int i, ret;
5804f86d3a8SLen Brown 
581bf4d1b5dSDaniel Lezcano 	if (!try_module_get(drv->owner))
5824f86d3a8SLen Brown 		return -EINVAL;
5834f86d3a8SLen Brown 
58475a80267SRafael J. Wysocki 	for (i = 0; i < drv->state_count; i++) {
585ba1e78a1SRafael J. Wysocki 		if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
58699e98d3fSRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
58799e98d3fSRafael J. Wysocki 
58875a80267SRafael J. Wysocki 		if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
58975a80267SRafael J. Wysocki 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
59075a80267SRafael J. Wysocki 	}
59175a80267SRafael J. Wysocki 
5924f86d3a8SLen Brown 	per_cpu(cpuidle_devices, dev->cpu) = dev;
5934f86d3a8SLen Brown 	list_add(&dev->device_list, &cpuidle_detected_devices);
5944f86d3a8SLen Brown 
5954126c019SColin Cross 	ret = cpuidle_coupled_register_device(dev);
59647182668SViresh Kumar 	if (ret)
597f6bb51a5SDaniel Lezcano 		__cpuidle_unregister_device(dev);
59847182668SViresh Kumar 	else
599dcb84f33SVenkatesh Pallipadi 		dev->registered = 1;
60047182668SViresh Kumar 
60147182668SViresh Kumar 	return ret;
602dcb84f33SVenkatesh Pallipadi }
603dcb84f33SVenkatesh Pallipadi 
604dcb84f33SVenkatesh Pallipadi /**
605dcb84f33SVenkatesh Pallipadi  * cpuidle_register_device - registers a CPU's idle PM feature
606dcb84f33SVenkatesh Pallipadi  * @dev: the cpu
607dcb84f33SVenkatesh Pallipadi  */
608dcb84f33SVenkatesh Pallipadi int cpuidle_register_device(struct cpuidle_device *dev)
609dcb84f33SVenkatesh Pallipadi {
610c878a52dSDaniel Lezcano 	int ret = -EBUSY;
611dcb84f33SVenkatesh Pallipadi 
6121b0a0e9aSSrivatsa S. Bhat 	if (!dev)
6131b0a0e9aSSrivatsa S. Bhat 		return -EINVAL;
6141b0a0e9aSSrivatsa S. Bhat 
615dcb84f33SVenkatesh Pallipadi 	mutex_lock(&cpuidle_lock);
616dcb84f33SVenkatesh Pallipadi 
617c878a52dSDaniel Lezcano 	if (dev->registered)
618c878a52dSDaniel Lezcano 		goto out_unlock;
619c878a52dSDaniel Lezcano 
620267d4bf8SViresh Kumar 	__cpuidle_device_init(dev);
6215df0aa73SDaniel Lezcano 
622f6bb51a5SDaniel Lezcano 	ret = __cpuidle_register_device(dev);
623f6bb51a5SDaniel Lezcano 	if (ret)
624f6bb51a5SDaniel Lezcano 		goto out_unlock;
625f6bb51a5SDaniel Lezcano 
626f6bb51a5SDaniel Lezcano 	ret = cpuidle_add_sysfs(dev);
627f6bb51a5SDaniel Lezcano 	if (ret)
628f6bb51a5SDaniel Lezcano 		goto out_unregister;
629dcb84f33SVenkatesh Pallipadi 
63010b9d3f8SDaniel Lezcano 	ret = cpuidle_enable_device(dev);
631f6bb51a5SDaniel Lezcano 	if (ret)
632f6bb51a5SDaniel Lezcano 		goto out_sysfs;
63310b9d3f8SDaniel Lezcano 
6344f86d3a8SLen Brown 	cpuidle_install_idle_handler();
6354f86d3a8SLen Brown 
636f6bb51a5SDaniel Lezcano out_unlock:
6374f86d3a8SLen Brown 	mutex_unlock(&cpuidle_lock);
6384f86d3a8SLen Brown 
639f6bb51a5SDaniel Lezcano 	return ret;
640f6bb51a5SDaniel Lezcano 
641f6bb51a5SDaniel Lezcano out_sysfs:
642f6bb51a5SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
643f6bb51a5SDaniel Lezcano out_unregister:
644f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
645f6bb51a5SDaniel Lezcano 	goto out_unlock;
6464f86d3a8SLen Brown }
6474f86d3a8SLen Brown 
6484f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_register_device);
6494f86d3a8SLen Brown 
6504f86d3a8SLen Brown /**
6514f86d3a8SLen Brown  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
6524f86d3a8SLen Brown  * @dev: the cpu
6534f86d3a8SLen Brown  */
6544f86d3a8SLen Brown void cpuidle_unregister_device(struct cpuidle_device *dev)
6554f86d3a8SLen Brown {
656813e8e3dSKonrad Rzeszutek Wilk 	if (!dev || dev->registered == 0)
657dcb84f33SVenkatesh Pallipadi 		return;
658dcb84f33SVenkatesh Pallipadi 
6594f86d3a8SLen Brown 	cpuidle_pause_and_lock();
6604f86d3a8SLen Brown 
6614f86d3a8SLen Brown 	cpuidle_disable_device(dev);
6624f86d3a8SLen Brown 
6631aef40e2SDaniel Lezcano 	cpuidle_remove_sysfs(dev);
664f6bb51a5SDaniel Lezcano 
665f6bb51a5SDaniel Lezcano 	__cpuidle_unregister_device(dev);
6664f86d3a8SLen Brown 
6674126c019SColin Cross 	cpuidle_coupled_unregister_device(dev);
6684126c019SColin Cross 
6694f86d3a8SLen Brown 	cpuidle_resume_and_unlock();
6704f86d3a8SLen Brown }
6714f86d3a8SLen Brown 
6724f86d3a8SLen Brown EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
6734f86d3a8SLen Brown 
6741c192d04SDaniel Lezcano /**
6754c637b21SDaniel Lezcano  * cpuidle_unregister: unregister a driver and the devices. This function
6764c637b21SDaniel Lezcano  * can be used only if the driver has been previously registered through
6774c637b21SDaniel Lezcano  * the cpuidle_register function.
6784c637b21SDaniel Lezcano  *
6794c637b21SDaniel Lezcano  * @drv: a valid pointer to a struct cpuidle_driver
6804c637b21SDaniel Lezcano  */
6814c637b21SDaniel Lezcano void cpuidle_unregister(struct cpuidle_driver *drv)
6824c637b21SDaniel Lezcano {
6834c637b21SDaniel Lezcano 	int cpu;
6844c637b21SDaniel Lezcano 	struct cpuidle_device *device;
6854c637b21SDaniel Lezcano 
68682467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
6874c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
6884c637b21SDaniel Lezcano 		cpuidle_unregister_device(device);
6894c637b21SDaniel Lezcano 	}
6904c637b21SDaniel Lezcano 
6914c637b21SDaniel Lezcano 	cpuidle_unregister_driver(drv);
6924c637b21SDaniel Lezcano }
6934c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_unregister);
6944c637b21SDaniel Lezcano 
6954c637b21SDaniel Lezcano /**
6964c637b21SDaniel Lezcano  * cpuidle_register: registers the driver and the cpu devices with the
6974c637b21SDaniel Lezcano  * coupled_cpus passed as parameter. This function is used for all common
6984c637b21SDaniel Lezcano  * initialization pattern there are in the arch specific drivers. The
6994c637b21SDaniel Lezcano  * devices is globally defined in this file.
7004c637b21SDaniel Lezcano  *
7014c637b21SDaniel Lezcano  * @drv         : a valid pointer to a struct cpuidle_driver
7024c637b21SDaniel Lezcano  * @coupled_cpus: a cpumask for the coupled states
7034c637b21SDaniel Lezcano  *
7044c637b21SDaniel Lezcano  * Returns 0 on success, < 0 otherwise
7054c637b21SDaniel Lezcano  */
7064c637b21SDaniel Lezcano int cpuidle_register(struct cpuidle_driver *drv,
7074c637b21SDaniel Lezcano 		     const struct cpumask *const coupled_cpus)
7084c637b21SDaniel Lezcano {
7094c637b21SDaniel Lezcano 	int ret, cpu;
7104c637b21SDaniel Lezcano 	struct cpuidle_device *device;
7114c637b21SDaniel Lezcano 
7124c637b21SDaniel Lezcano 	ret = cpuidle_register_driver(drv);
7134c637b21SDaniel Lezcano 	if (ret) {
7144c637b21SDaniel Lezcano 		pr_err("failed to register cpuidle driver\n");
7154c637b21SDaniel Lezcano 		return ret;
7164c637b21SDaniel Lezcano 	}
7174c637b21SDaniel Lezcano 
71882467a5aSDaniel Lezcano 	for_each_cpu(cpu, drv->cpumask) {
7194c637b21SDaniel Lezcano 		device = &per_cpu(cpuidle_dev, cpu);
7204c637b21SDaniel Lezcano 		device->cpu = cpu;
7214c637b21SDaniel Lezcano 
7224c637b21SDaniel Lezcano #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
7234c637b21SDaniel Lezcano 		/*
724caf4a36eSViresh Kumar 		 * On multiplatform for ARM, the coupled idle states could be
7254c637b21SDaniel Lezcano 		 * enabled in the kernel even if the cpuidle driver does not
7264c637b21SDaniel Lezcano 		 * use it. Note, coupled_cpus is a struct copy.
7274c637b21SDaniel Lezcano 		 */
7284c637b21SDaniel Lezcano 		if (coupled_cpus)
7294c637b21SDaniel Lezcano 			device->coupled_cpus = *coupled_cpus;
7304c637b21SDaniel Lezcano #endif
7314c637b21SDaniel Lezcano 		ret = cpuidle_register_device(device);
7324c637b21SDaniel Lezcano 		if (!ret)
7334c637b21SDaniel Lezcano 			continue;
7344c637b21SDaniel Lezcano 
7354c637b21SDaniel Lezcano 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
7364c637b21SDaniel Lezcano 
7374c637b21SDaniel Lezcano 		cpuidle_unregister(drv);
7384c637b21SDaniel Lezcano 		break;
7394c637b21SDaniel Lezcano 	}
7404c637b21SDaniel Lezcano 
7414c637b21SDaniel Lezcano 	return ret;
7424c637b21SDaniel Lezcano }
7434c637b21SDaniel Lezcano EXPORT_SYMBOL_GPL(cpuidle_register);
7444c637b21SDaniel Lezcano 
7454f86d3a8SLen Brown /**
7464f86d3a8SLen Brown  * cpuidle_init - core initializer
7474f86d3a8SLen Brown  */
7484f86d3a8SLen Brown static int __init cpuidle_init(void)
7494f86d3a8SLen Brown {
75062027aeaSLen Brown 	if (cpuidle_disabled())
75162027aeaSLen Brown 		return -ENODEV;
75262027aeaSLen Brown 
7533a4a0042SRafael J. Wysocki 	return cpuidle_add_interface(cpu_subsys.dev_root);
7544f86d3a8SLen Brown }
7554f86d3a8SLen Brown 
75662027aeaSLen Brown module_param(off, int, 0444);
75761cb5758SRafael J. Wysocki module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
7584f86d3a8SLen Brown core_initcall(cpuidle_init);
759