xref: /openbmc/linux/drivers/cpuidle/cpuidle.c (revision 17cc2b5525856d7374f09b28ba1faf1fa61b2352)
1 /*
2  * cpuidle.c - core cpuidle infrastructure
3  *
4  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5  *               Shaohua Li <shaohua.li@intel.com>
6  *               Adam Belay <abelay@novell.com>
7  *
8  * This code is licenced under the GPL.
9  */
10 
11 #include "linux/percpu-defs.h"
12 #include <linux/clockchips.h>
13 #include <linux/kernel.h>
14 #include <linux/mutex.h>
15 #include <linux/sched.h>
16 #include <linux/sched/clock.h>
17 #include <linux/sched/idle.h>
18 #include <linux/notifier.h>
19 #include <linux/pm_qos.h>
20 #include <linux/cpu.h>
21 #include <linux/cpuidle.h>
22 #include <linux/ktime.h>
23 #include <linux/hrtimer.h>
24 #include <linux/module.h>
25 #include <linux/suspend.h>
26 #include <linux/tick.h>
27 #include <linux/mmu_context.h>
28 #include <linux/context_tracking.h>
29 #include <trace/events/power.h>
30 
31 #include "cpuidle.h"
32 
33 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
34 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
35 
36 DEFINE_MUTEX(cpuidle_lock);
37 LIST_HEAD(cpuidle_detected_devices);
38 
39 static int enabled_devices;
40 static int off __read_mostly;
41 static int initialized __read_mostly;
42 
43 int cpuidle_disabled(void)
44 {
45 	return off;
46 }
47 void disable_cpuidle(void)
48 {
49 	off = 1;
50 }
51 
52 bool cpuidle_not_available(struct cpuidle_driver *drv,
53 			   struct cpuidle_device *dev)
54 {
55 	return off || !initialized || !drv || !dev || !dev->enabled;
56 }
57 
58 /**
59  * cpuidle_play_dead - cpu off-lining
60  *
61  * Returns in case of an error or no driver
62  */
63 int cpuidle_play_dead(void)
64 {
65 	struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
66 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
67 	int i;
68 
69 	if (!drv)
70 		return -ENODEV;
71 
72 	/* Find lowest-power state that supports long-term idle */
73 	for (i = drv->state_count - 1; i >= 0; i--)
74 		if (drv->states[i].enter_dead)
75 			return drv->states[i].enter_dead(dev, i);
76 
77 	return -ENODEV;
78 }
79 
80 static int find_deepest_state(struct cpuidle_driver *drv,
81 			      struct cpuidle_device *dev,
82 			      u64 max_latency_ns,
83 			      unsigned int forbidden_flags,
84 			      bool s2idle)
85 {
86 	u64 latency_req = 0;
87 	int i, ret = 0;
88 
89 	for (i = 1; i < drv->state_count; i++) {
90 		struct cpuidle_state *s = &drv->states[i];
91 
92 		if (dev->states_usage[i].disable ||
93 		    s->exit_latency_ns <= latency_req ||
94 		    s->exit_latency_ns > max_latency_ns ||
95 		    (s->flags & forbidden_flags) ||
96 		    (s2idle && !s->enter_s2idle))
97 			continue;
98 
99 		latency_req = s->exit_latency_ns;
100 		ret = i;
101 	}
102 	return ret;
103 }
104 
105 /**
106  * cpuidle_use_deepest_state - Set/unset governor override mode.
107  * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
108  *
109  * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
110  * state with exit latency within @latency_limit_ns (override governors going
111  * forward), or do not override governors if it is zero.
112  */
113 void cpuidle_use_deepest_state(u64 latency_limit_ns)
114 {
115 	struct cpuidle_device *dev;
116 
117 	preempt_disable();
118 	dev = cpuidle_get_device();
119 	if (dev)
120 		dev->forced_idle_latency_limit_ns = latency_limit_ns;
121 	preempt_enable();
122 }
123 
124 /**
125  * cpuidle_find_deepest_state - Find the deepest available idle state.
126  * @drv: cpuidle driver for the given CPU.
127  * @dev: cpuidle device for the given CPU.
128  * @latency_limit_ns: Idle state exit latency limit
129  *
130  * Return: the index of the deepest available idle state.
131  */
132 int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
133 			       struct cpuidle_device *dev,
134 			       u64 latency_limit_ns)
135 {
136 	return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
137 }
138 
139 #ifdef CONFIG_SUSPEND
140 static noinstr void enter_s2idle_proper(struct cpuidle_driver *drv,
141 					 struct cpuidle_device *dev, int index)
142 {
143 	struct cpuidle_state *target_state = &drv->states[index];
144 	ktime_t time_start, time_end;
145 
146 	instrumentation_begin();
147 
148 	time_start = ns_to_ktime(local_clock());
149 
150 	tick_freeze();
151 	/*
152 	 * The state used here cannot be a "coupled" one, because the "coupled"
153 	 * cpuidle mechanism enables interrupts and doing that with timekeeping
154 	 * suspended is generally unsafe.
155 	 */
156 	stop_critical_timings();
157 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
158 		ct_cpuidle_enter();
159 		/* Annotate away the indirect call */
160 		instrumentation_begin();
161 	}
162 	target_state->enter_s2idle(dev, drv, index);
163 	if (WARN_ON_ONCE(!irqs_disabled()))
164 		raw_local_irq_disable();
165 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
166 		instrumentation_end();
167 		ct_cpuidle_exit();
168 	}
169 	tick_unfreeze();
170 	start_critical_timings();
171 
172 	time_end = ns_to_ktime(local_clock());
173 
174 	dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
175 	dev->states_usage[index].s2idle_usage++;
176 	instrumentation_end();
177 }
178 
179 /**
180  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
181  * @drv: cpuidle driver for the given CPU.
182  * @dev: cpuidle device for the given CPU.
183  *
184  * If there are states with the ->enter_s2idle callback, find the deepest of
185  * them and enter it with frozen tick.
186  */
187 int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
188 {
189 	int index;
190 
191 	/*
192 	 * Find the deepest state with ->enter_s2idle present, which guarantees
193 	 * that interrupts won't be enabled when it exits and allows the tick to
194 	 * be frozen safely.
195 	 */
196 	index = find_deepest_state(drv, dev, U64_MAX, 0, true);
197 	if (index > 0) {
198 		enter_s2idle_proper(drv, dev, index);
199 		local_irq_enable();
200 	}
201 	return index;
202 }
203 #endif /* CONFIG_SUSPEND */
204 
205 /**
206  * cpuidle_enter_state - enter the state and update stats
207  * @dev: cpuidle device for this cpu
208  * @drv: cpuidle driver for this cpu
209  * @index: index into the states table in @drv of the state to enter
210  */
211 noinstr int cpuidle_enter_state(struct cpuidle_device *dev,
212 				 struct cpuidle_driver *drv,
213 				 int index)
214 {
215 	int entered_state;
216 
217 	struct cpuidle_state *target_state = &drv->states[index];
218 	bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
219 	ktime_t time_start, time_end;
220 
221 	instrumentation_begin();
222 
223 	/*
224 	 * Tell the time framework to switch to a broadcast timer because our
225 	 * local timer will be shut down.  If a local timer is used from another
226 	 * CPU as a broadcast timer, this call may fail if it is not available.
227 	 */
228 	if (broadcast && tick_broadcast_enter()) {
229 		index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
230 					   CPUIDLE_FLAG_TIMER_STOP, false);
231 		if (index < 0) {
232 			default_idle_call();
233 			return -EBUSY;
234 		}
235 		target_state = &drv->states[index];
236 		broadcast = false;
237 	}
238 
239 	if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
240 		leave_mm(dev->cpu);
241 
242 	/* Take note of the planned idle state. */
243 	sched_idle_set_state(target_state);
244 
245 	trace_cpu_idle(index, dev->cpu);
246 	time_start = ns_to_ktime(local_clock());
247 
248 	stop_critical_timings();
249 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
250 		ct_cpuidle_enter();
251 		/* Annotate away the indirect call */
252 		instrumentation_begin();
253 	}
254 
255 	entered_state = target_state->enter(dev, drv, index);
256 
257 	if (WARN_ONCE(!irqs_disabled(), "%ps leaked IRQ state", target_state->enter))
258 		raw_local_irq_disable();
259 
260 	if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE)) {
261 		instrumentation_end();
262 		ct_cpuidle_exit();
263 	}
264 	start_critical_timings();
265 
266 	sched_clock_idle_wakeup_event();
267 	time_end = ns_to_ktime(local_clock());
268 	trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
269 
270 	/* The cpu is no longer idle or about to enter idle. */
271 	sched_idle_set_state(NULL);
272 
273 	if (broadcast)
274 		tick_broadcast_exit();
275 
276 	if (!cpuidle_state_is_coupled(drv, index))
277 		local_irq_enable();
278 
279 	if (entered_state >= 0) {
280 		s64 diff, delay = drv->states[entered_state].exit_latency_ns;
281 		int i;
282 
283 		/*
284 		 * Update cpuidle counters
285 		 * This can be moved to within driver enter routine,
286 		 * but that results in multiple copies of same code.
287 		 */
288 		diff = ktime_sub(time_end, time_start);
289 
290 		dev->last_residency_ns = diff;
291 		dev->states_usage[entered_state].time_ns += diff;
292 		dev->states_usage[entered_state].usage++;
293 
294 		if (diff < drv->states[entered_state].target_residency_ns) {
295 			for (i = entered_state - 1; i >= 0; i--) {
296 				if (dev->states_usage[i].disable)
297 					continue;
298 
299 				/* Shallower states are enabled, so update. */
300 				dev->states_usage[entered_state].above++;
301 				trace_cpu_idle_miss(dev->cpu, entered_state, false);
302 				break;
303 			}
304 		} else if (diff > delay) {
305 			for (i = entered_state + 1; i < drv->state_count; i++) {
306 				if (dev->states_usage[i].disable)
307 					continue;
308 
309 				/*
310 				 * Update if a deeper state would have been a
311 				 * better match for the observed idle duration.
312 				 */
313 				if (diff - delay >= drv->states[i].target_residency_ns) {
314 					dev->states_usage[entered_state].below++;
315 					trace_cpu_idle_miss(dev->cpu, entered_state, true);
316 				}
317 
318 				break;
319 			}
320 		}
321 	} else {
322 		dev->last_residency_ns = 0;
323 		dev->states_usage[index].rejected++;
324 	}
325 
326 	instrumentation_end();
327 
328 	return entered_state;
329 }
330 
331 /**
332  * cpuidle_select - ask the cpuidle framework to choose an idle state
333  *
334  * @drv: the cpuidle driver
335  * @dev: the cpuidle device
336  * @stop_tick: indication on whether or not to stop the tick
337  *
338  * Returns the index of the idle state.  The return value must not be negative.
339  *
340  * The memory location pointed to by @stop_tick is expected to be written the
341  * 'false' boolean value if the scheduler tick should not be stopped before
342  * entering the returned state.
343  */
344 int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
345 		   bool *stop_tick)
346 {
347 	return cpuidle_curr_governor->select(drv, dev, stop_tick);
348 }
349 
350 /**
351  * cpuidle_enter - enter into the specified idle state
352  *
353  * @drv:   the cpuidle driver tied with the cpu
354  * @dev:   the cpuidle device
355  * @index: the index in the idle state table
356  *
357  * Returns the index in the idle state, < 0 in case of error.
358  * The error code depends on the backend driver
359  */
360 int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
361 		  int index)
362 {
363 	int ret = 0;
364 
365 	/*
366 	 * Store the next hrtimer, which becomes either next tick or the next
367 	 * timer event, whatever expires first. Additionally, to make this data
368 	 * useful for consumers outside cpuidle, we rely on that the governor's
369 	 * ->select() callback have decided, whether to stop the tick or not.
370 	 */
371 	WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
372 
373 	if (cpuidle_state_is_coupled(drv, index))
374 		ret = cpuidle_enter_state_coupled(dev, drv, index);
375 	else
376 		ret = cpuidle_enter_state(dev, drv, index);
377 
378 	WRITE_ONCE(dev->next_hrtimer, 0);
379 	return ret;
380 }
381 
382 /**
383  * cpuidle_reflect - tell the underlying governor what was the state
384  * we were in
385  *
386  * @dev  : the cpuidle device
387  * @index: the index in the idle state table
388  *
389  */
390 void cpuidle_reflect(struct cpuidle_device *dev, int index)
391 {
392 	if (cpuidle_curr_governor->reflect && index >= 0)
393 		cpuidle_curr_governor->reflect(dev, index);
394 }
395 
396 /*
397  * Min polling interval of 10usec is a guess. It is assuming that
398  * for most users, the time for a single ping-pong workload like
399  * perf bench pipe would generally complete within 10usec but
400  * this is hardware dependant. Actual time can be estimated with
401  *
402  * perf bench sched pipe -l 10000
403  *
404  * Run multiple times to avoid cpufreq effects.
405  */
406 #define CPUIDLE_POLL_MIN 10000
407 #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
408 
409 /**
410  * cpuidle_poll_time - return amount of time to poll for,
411  * governors can override dev->poll_limit_ns if necessary
412  *
413  * @drv:   the cpuidle driver tied with the cpu
414  * @dev:   the cpuidle device
415  *
416  */
417 u64 cpuidle_poll_time(struct cpuidle_driver *drv,
418 		      struct cpuidle_device *dev)
419 {
420 	int i;
421 	u64 limit_ns;
422 
423 	BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
424 
425 	if (dev->poll_limit_ns)
426 		return dev->poll_limit_ns;
427 
428 	limit_ns = CPUIDLE_POLL_MAX;
429 	for (i = 1; i < drv->state_count; i++) {
430 		u64 state_limit;
431 
432 		if (dev->states_usage[i].disable)
433 			continue;
434 
435 		state_limit = drv->states[i].target_residency_ns;
436 		if (state_limit < CPUIDLE_POLL_MIN)
437 			continue;
438 
439 		limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
440 		break;
441 	}
442 
443 	dev->poll_limit_ns = limit_ns;
444 
445 	return dev->poll_limit_ns;
446 }
447 
448 /**
449  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
450  */
451 void cpuidle_install_idle_handler(void)
452 {
453 	if (enabled_devices) {
454 		/* Make sure all changes finished before we switch to new idle */
455 		smp_wmb();
456 		initialized = 1;
457 	}
458 }
459 
460 /**
461  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
462  */
463 void cpuidle_uninstall_idle_handler(void)
464 {
465 	if (enabled_devices) {
466 		initialized = 0;
467 		wake_up_all_idle_cpus();
468 	}
469 
470 	/*
471 	 * Make sure external observers (such as the scheduler)
472 	 * are done looking at pointed idle states.
473 	 */
474 	synchronize_rcu();
475 }
476 
477 /**
478  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
479  */
480 void cpuidle_pause_and_lock(void)
481 {
482 	mutex_lock(&cpuidle_lock);
483 	cpuidle_uninstall_idle_handler();
484 }
485 
486 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
487 
488 /**
489  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
490  */
491 void cpuidle_resume_and_unlock(void)
492 {
493 	cpuidle_install_idle_handler();
494 	mutex_unlock(&cpuidle_lock);
495 }
496 
497 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
498 
499 /* Currently used in suspend/resume path to suspend cpuidle */
500 void cpuidle_pause(void)
501 {
502 	mutex_lock(&cpuidle_lock);
503 	cpuidle_uninstall_idle_handler();
504 	mutex_unlock(&cpuidle_lock);
505 }
506 
507 /* Currently used in suspend/resume path to resume cpuidle */
508 void cpuidle_resume(void)
509 {
510 	mutex_lock(&cpuidle_lock);
511 	cpuidle_install_idle_handler();
512 	mutex_unlock(&cpuidle_lock);
513 }
514 
515 /**
516  * cpuidle_enable_device - enables idle PM for a CPU
517  * @dev: the CPU
518  *
519  * This function must be called between cpuidle_pause_and_lock and
520  * cpuidle_resume_and_unlock when used externally.
521  */
522 int cpuidle_enable_device(struct cpuidle_device *dev)
523 {
524 	int ret;
525 	struct cpuidle_driver *drv;
526 
527 	if (!dev)
528 		return -EINVAL;
529 
530 	if (dev->enabled)
531 		return 0;
532 
533 	if (!cpuidle_curr_governor)
534 		return -EIO;
535 
536 	drv = cpuidle_get_cpu_driver(dev);
537 
538 	if (!drv)
539 		return -EIO;
540 
541 	if (!dev->registered)
542 		return -EINVAL;
543 
544 	ret = cpuidle_add_device_sysfs(dev);
545 	if (ret)
546 		return ret;
547 
548 	if (cpuidle_curr_governor->enable) {
549 		ret = cpuidle_curr_governor->enable(drv, dev);
550 		if (ret)
551 			goto fail_sysfs;
552 	}
553 
554 	smp_wmb();
555 
556 	dev->enabled = 1;
557 
558 	enabled_devices++;
559 	return 0;
560 
561 fail_sysfs:
562 	cpuidle_remove_device_sysfs(dev);
563 
564 	return ret;
565 }
566 
567 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
568 
569 /**
570  * cpuidle_disable_device - disables idle PM for a CPU
571  * @dev: the CPU
572  *
573  * This function must be called between cpuidle_pause_and_lock and
574  * cpuidle_resume_and_unlock when used externally.
575  */
576 void cpuidle_disable_device(struct cpuidle_device *dev)
577 {
578 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
579 
580 	if (!dev || !dev->enabled)
581 		return;
582 
583 	if (!drv || !cpuidle_curr_governor)
584 		return;
585 
586 	dev->enabled = 0;
587 
588 	if (cpuidle_curr_governor->disable)
589 		cpuidle_curr_governor->disable(drv, dev);
590 
591 	cpuidle_remove_device_sysfs(dev);
592 	enabled_devices--;
593 }
594 
595 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
596 
597 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
598 {
599 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
600 
601 	list_del(&dev->device_list);
602 	per_cpu(cpuidle_devices, dev->cpu) = NULL;
603 	module_put(drv->owner);
604 
605 	dev->registered = 0;
606 }
607 
608 static void __cpuidle_device_init(struct cpuidle_device *dev)
609 {
610 	memset(dev->states_usage, 0, sizeof(dev->states_usage));
611 	dev->last_residency_ns = 0;
612 	dev->next_hrtimer = 0;
613 }
614 
615 /**
616  * __cpuidle_register_device - internal register function called before register
617  * and enable routines
618  * @dev: the cpu
619  *
620  * cpuidle_lock mutex must be held before this is called
621  */
622 static int __cpuidle_register_device(struct cpuidle_device *dev)
623 {
624 	struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
625 	int i, ret;
626 
627 	if (!try_module_get(drv->owner))
628 		return -EINVAL;
629 
630 	for (i = 0; i < drv->state_count; i++) {
631 		if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
632 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
633 
634 		if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
635 			dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
636 	}
637 
638 	per_cpu(cpuidle_devices, dev->cpu) = dev;
639 	list_add(&dev->device_list, &cpuidle_detected_devices);
640 
641 	ret = cpuidle_coupled_register_device(dev);
642 	if (ret)
643 		__cpuidle_unregister_device(dev);
644 	else
645 		dev->registered = 1;
646 
647 	return ret;
648 }
649 
650 /**
651  * cpuidle_register_device - registers a CPU's idle PM feature
652  * @dev: the cpu
653  */
654 int cpuidle_register_device(struct cpuidle_device *dev)
655 {
656 	int ret = -EBUSY;
657 
658 	if (!dev)
659 		return -EINVAL;
660 
661 	mutex_lock(&cpuidle_lock);
662 
663 	if (dev->registered)
664 		goto out_unlock;
665 
666 	__cpuidle_device_init(dev);
667 
668 	ret = __cpuidle_register_device(dev);
669 	if (ret)
670 		goto out_unlock;
671 
672 	ret = cpuidle_add_sysfs(dev);
673 	if (ret)
674 		goto out_unregister;
675 
676 	ret = cpuidle_enable_device(dev);
677 	if (ret)
678 		goto out_sysfs;
679 
680 	cpuidle_install_idle_handler();
681 
682 out_unlock:
683 	mutex_unlock(&cpuidle_lock);
684 
685 	return ret;
686 
687 out_sysfs:
688 	cpuidle_remove_sysfs(dev);
689 out_unregister:
690 	__cpuidle_unregister_device(dev);
691 	goto out_unlock;
692 }
693 
694 EXPORT_SYMBOL_GPL(cpuidle_register_device);
695 
696 /**
697  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
698  * @dev: the cpu
699  */
700 void cpuidle_unregister_device(struct cpuidle_device *dev)
701 {
702 	if (!dev || dev->registered == 0)
703 		return;
704 
705 	cpuidle_pause_and_lock();
706 
707 	cpuidle_disable_device(dev);
708 
709 	cpuidle_remove_sysfs(dev);
710 
711 	__cpuidle_unregister_device(dev);
712 
713 	cpuidle_coupled_unregister_device(dev);
714 
715 	cpuidle_resume_and_unlock();
716 }
717 
718 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
719 
720 /**
721  * cpuidle_unregister: unregister a driver and the devices. This function
722  * can be used only if the driver has been previously registered through
723  * the cpuidle_register function.
724  *
725  * @drv: a valid pointer to a struct cpuidle_driver
726  */
727 void cpuidle_unregister(struct cpuidle_driver *drv)
728 {
729 	int cpu;
730 	struct cpuidle_device *device;
731 
732 	for_each_cpu(cpu, drv->cpumask) {
733 		device = &per_cpu(cpuidle_dev, cpu);
734 		cpuidle_unregister_device(device);
735 	}
736 
737 	cpuidle_unregister_driver(drv);
738 }
739 EXPORT_SYMBOL_GPL(cpuidle_unregister);
740 
741 /**
742  * cpuidle_register: registers the driver and the cpu devices with the
743  * coupled_cpus passed as parameter. This function is used for all common
744  * initialization pattern there are in the arch specific drivers. The
745  * devices is globally defined in this file.
746  *
747  * @drv         : a valid pointer to a struct cpuidle_driver
748  * @coupled_cpus: a cpumask for the coupled states
749  *
750  * Returns 0 on success, < 0 otherwise
751  */
752 int cpuidle_register(struct cpuidle_driver *drv,
753 		     const struct cpumask *const coupled_cpus)
754 {
755 	int ret, cpu;
756 	struct cpuidle_device *device;
757 
758 	ret = cpuidle_register_driver(drv);
759 	if (ret) {
760 		pr_err("failed to register cpuidle driver\n");
761 		return ret;
762 	}
763 
764 	for_each_cpu(cpu, drv->cpumask) {
765 		device = &per_cpu(cpuidle_dev, cpu);
766 		device->cpu = cpu;
767 
768 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
769 		/*
770 		 * On multiplatform for ARM, the coupled idle states could be
771 		 * enabled in the kernel even if the cpuidle driver does not
772 		 * use it. Note, coupled_cpus is a struct copy.
773 		 */
774 		if (coupled_cpus)
775 			device->coupled_cpus = *coupled_cpus;
776 #endif
777 		ret = cpuidle_register_device(device);
778 		if (!ret)
779 			continue;
780 
781 		pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
782 
783 		cpuidle_unregister(drv);
784 		break;
785 	}
786 
787 	return ret;
788 }
789 EXPORT_SYMBOL_GPL(cpuidle_register);
790 
791 /**
792  * cpuidle_init - core initializer
793  */
794 static int __init cpuidle_init(void)
795 {
796 	if (cpuidle_disabled())
797 		return -ENODEV;
798 
799 	return cpuidle_add_interface(cpu_subsys.dev_root);
800 }
801 
802 module_param(off, int, 0444);
803 module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
804 core_initcall(cpuidle_init);
805