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