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