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 * Min polling interval of 10usec is a guess. It is assuming that
386 * for most users, the time for a single ping-pong workload like
387 * perf bench pipe would generally complete within 10usec but
388 * this is hardware dependant. Actual time can be estimated with
389 *
390 * perf bench sched pipe -l 10000
391 *
392 * Run multiple times to avoid cpufreq effects.
393 */
394 #define CPUIDLE_POLL_MIN 10000
395 #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
396
397 /**
398 * cpuidle_poll_time - return amount of time to poll for,
399 * governors can override dev->poll_limit_ns if necessary
400 *
401 * @drv: the cpuidle driver tied with the cpu
402 * @dev: the cpuidle device
403 *
404 */
cpuidle_poll_time(struct cpuidle_driver * drv,struct cpuidle_device * dev)405 u64 cpuidle_poll_time(struct cpuidle_driver *drv,
406 struct cpuidle_device *dev)
407 {
408 int i;
409 u64 limit_ns;
410
411 BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
412
413 if (dev->poll_limit_ns)
414 return dev->poll_limit_ns;
415
416 limit_ns = CPUIDLE_POLL_MAX;
417 for (i = 1; i < drv->state_count; i++) {
418 u64 state_limit;
419
420 if (dev->states_usage[i].disable)
421 continue;
422
423 state_limit = drv->states[i].target_residency_ns;
424 if (state_limit < CPUIDLE_POLL_MIN)
425 continue;
426
427 limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
428 break;
429 }
430
431 dev->poll_limit_ns = limit_ns;
432
433 return dev->poll_limit_ns;
434 }
435
436 /**
437 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
438 */
cpuidle_install_idle_handler(void)439 void cpuidle_install_idle_handler(void)
440 {
441 if (enabled_devices) {
442 /* Make sure all changes finished before we switch to new idle */
443 smp_wmb();
444 initialized = 1;
445 }
446 }
447
448 /**
449 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
450 */
cpuidle_uninstall_idle_handler(void)451 void cpuidle_uninstall_idle_handler(void)
452 {
453 if (enabled_devices) {
454 initialized = 0;
455 wake_up_all_idle_cpus();
456 }
457
458 /*
459 * Make sure external observers (such as the scheduler)
460 * are done looking at pointed idle states.
461 */
462 synchronize_rcu();
463 }
464
465 /**
466 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
467 */
cpuidle_pause_and_lock(void)468 void cpuidle_pause_and_lock(void)
469 {
470 mutex_lock(&cpuidle_lock);
471 cpuidle_uninstall_idle_handler();
472 }
473
474 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
475
476 /**
477 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
478 */
cpuidle_resume_and_unlock(void)479 void cpuidle_resume_and_unlock(void)
480 {
481 cpuidle_install_idle_handler();
482 mutex_unlock(&cpuidle_lock);
483 }
484
485 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
486
487 /* Currently used in suspend/resume path to suspend cpuidle */
cpuidle_pause(void)488 void cpuidle_pause(void)
489 {
490 mutex_lock(&cpuidle_lock);
491 cpuidle_uninstall_idle_handler();
492 mutex_unlock(&cpuidle_lock);
493 }
494
495 /* Currently used in suspend/resume path to resume cpuidle */
cpuidle_resume(void)496 void cpuidle_resume(void)
497 {
498 mutex_lock(&cpuidle_lock);
499 cpuidle_install_idle_handler();
500 mutex_unlock(&cpuidle_lock);
501 }
502
503 /**
504 * cpuidle_enable_device - enables idle PM for a CPU
505 * @dev: the CPU
506 *
507 * This function must be called between cpuidle_pause_and_lock and
508 * cpuidle_resume_and_unlock when used externally.
509 */
cpuidle_enable_device(struct cpuidle_device * dev)510 int cpuidle_enable_device(struct cpuidle_device *dev)
511 {
512 int ret;
513 struct cpuidle_driver *drv;
514
515 if (!dev)
516 return -EINVAL;
517
518 if (dev->enabled)
519 return 0;
520
521 if (!cpuidle_curr_governor)
522 return -EIO;
523
524 drv = cpuidle_get_cpu_driver(dev);
525
526 if (!drv)
527 return -EIO;
528
529 if (!dev->registered)
530 return -EINVAL;
531
532 ret = cpuidle_add_device_sysfs(dev);
533 if (ret)
534 return ret;
535
536 if (cpuidle_curr_governor->enable) {
537 ret = cpuidle_curr_governor->enable(drv, dev);
538 if (ret)
539 goto fail_sysfs;
540 }
541
542 smp_wmb();
543
544 dev->enabled = 1;
545
546 enabled_devices++;
547 return 0;
548
549 fail_sysfs:
550 cpuidle_remove_device_sysfs(dev);
551
552 return ret;
553 }
554
555 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
556
557 /**
558 * cpuidle_disable_device - disables idle PM for a CPU
559 * @dev: the CPU
560 *
561 * This function must be called between cpuidle_pause_and_lock and
562 * cpuidle_resume_and_unlock when used externally.
563 */
cpuidle_disable_device(struct cpuidle_device * dev)564 void cpuidle_disable_device(struct cpuidle_device *dev)
565 {
566 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
567
568 if (!dev || !dev->enabled)
569 return;
570
571 if (!drv || !cpuidle_curr_governor)
572 return;
573
574 dev->enabled = 0;
575
576 if (cpuidle_curr_governor->disable)
577 cpuidle_curr_governor->disable(drv, dev);
578
579 cpuidle_remove_device_sysfs(dev);
580 enabled_devices--;
581 }
582
583 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
584
__cpuidle_unregister_device(struct cpuidle_device * dev)585 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
586 {
587 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
588
589 list_del(&dev->device_list);
590 per_cpu(cpuidle_devices, dev->cpu) = NULL;
591 module_put(drv->owner);
592
593 dev->registered = 0;
594 }
595
__cpuidle_device_init(struct cpuidle_device * dev)596 static void __cpuidle_device_init(struct cpuidle_device *dev)
597 {
598 memset(dev->states_usage, 0, sizeof(dev->states_usage));
599 dev->last_residency_ns = 0;
600 dev->next_hrtimer = 0;
601 }
602
603 /**
604 * __cpuidle_register_device - internal register function called before register
605 * and enable routines
606 * @dev: the cpu
607 *
608 * cpuidle_lock mutex must be held before this is called
609 */
__cpuidle_register_device(struct cpuidle_device * dev)610 static int __cpuidle_register_device(struct cpuidle_device *dev)
611 {
612 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
613 int i, ret;
614
615 if (!try_module_get(drv->owner))
616 return -EINVAL;
617
618 for (i = 0; i < drv->state_count; i++) {
619 if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
620 dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
621
622 if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
623 dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
624 }
625
626 per_cpu(cpuidle_devices, dev->cpu) = dev;
627 list_add(&dev->device_list, &cpuidle_detected_devices);
628
629 ret = cpuidle_coupled_register_device(dev);
630 if (ret)
631 __cpuidle_unregister_device(dev);
632 else
633 dev->registered = 1;
634
635 return ret;
636 }
637
638 /**
639 * cpuidle_register_device - registers a CPU's idle PM feature
640 * @dev: the cpu
641 */
cpuidle_register_device(struct cpuidle_device * dev)642 int cpuidle_register_device(struct cpuidle_device *dev)
643 {
644 int ret = -EBUSY;
645
646 if (!dev)
647 return -EINVAL;
648
649 mutex_lock(&cpuidle_lock);
650
651 if (dev->registered)
652 goto out_unlock;
653
654 __cpuidle_device_init(dev);
655
656 ret = __cpuidle_register_device(dev);
657 if (ret)
658 goto out_unlock;
659
660 ret = cpuidle_add_sysfs(dev);
661 if (ret)
662 goto out_unregister;
663
664 ret = cpuidle_enable_device(dev);
665 if (ret)
666 goto out_sysfs;
667
668 cpuidle_install_idle_handler();
669
670 out_unlock:
671 mutex_unlock(&cpuidle_lock);
672
673 return ret;
674
675 out_sysfs:
676 cpuidle_remove_sysfs(dev);
677 out_unregister:
678 __cpuidle_unregister_device(dev);
679 goto out_unlock;
680 }
681
682 EXPORT_SYMBOL_GPL(cpuidle_register_device);
683
684 /**
685 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
686 * @dev: the cpu
687 */
cpuidle_unregister_device(struct cpuidle_device * dev)688 void cpuidle_unregister_device(struct cpuidle_device *dev)
689 {
690 if (!dev || dev->registered == 0)
691 return;
692
693 cpuidle_pause_and_lock();
694
695 cpuidle_disable_device(dev);
696
697 cpuidle_remove_sysfs(dev);
698
699 __cpuidle_unregister_device(dev);
700
701 cpuidle_coupled_unregister_device(dev);
702
703 cpuidle_resume_and_unlock();
704 }
705
706 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
707
708 /**
709 * cpuidle_unregister: unregister a driver and the devices. This function
710 * can be used only if the driver has been previously registered through
711 * the cpuidle_register function.
712 *
713 * @drv: a valid pointer to a struct cpuidle_driver
714 */
cpuidle_unregister(struct cpuidle_driver * drv)715 void cpuidle_unregister(struct cpuidle_driver *drv)
716 {
717 int cpu;
718 struct cpuidle_device *device;
719
720 for_each_cpu(cpu, drv->cpumask) {
721 device = &per_cpu(cpuidle_dev, cpu);
722 cpuidle_unregister_device(device);
723 }
724
725 cpuidle_unregister_driver(drv);
726 }
727 EXPORT_SYMBOL_GPL(cpuidle_unregister);
728
729 /**
730 * cpuidle_register: registers the driver and the cpu devices with the
731 * coupled_cpus passed as parameter. This function is used for all common
732 * initialization pattern there are in the arch specific drivers. The
733 * devices is globally defined in this file.
734 *
735 * @drv : a valid pointer to a struct cpuidle_driver
736 * @coupled_cpus: a cpumask for the coupled states
737 *
738 * Returns 0 on success, < 0 otherwise
739 */
cpuidle_register(struct cpuidle_driver * drv,const struct cpumask * const coupled_cpus)740 int cpuidle_register(struct cpuidle_driver *drv,
741 const struct cpumask *const coupled_cpus)
742 {
743 int ret, cpu;
744 struct cpuidle_device *device;
745
746 ret = cpuidle_register_driver(drv);
747 if (ret) {
748 pr_err("failed to register cpuidle driver\n");
749 return ret;
750 }
751
752 for_each_cpu(cpu, drv->cpumask) {
753 device = &per_cpu(cpuidle_dev, cpu);
754 device->cpu = cpu;
755
756 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
757 /*
758 * On multiplatform for ARM, the coupled idle states could be
759 * enabled in the kernel even if the cpuidle driver does not
760 * use it. Note, coupled_cpus is a struct copy.
761 */
762 if (coupled_cpus)
763 device->coupled_cpus = *coupled_cpus;
764 #endif
765 ret = cpuidle_register_device(device);
766 if (!ret)
767 continue;
768
769 pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
770
771 cpuidle_unregister(drv);
772 break;
773 }
774
775 return ret;
776 }
777 EXPORT_SYMBOL_GPL(cpuidle_register);
778
779 /**
780 * cpuidle_init - core initializer
781 */
cpuidle_init(void)782 static int __init cpuidle_init(void)
783 {
784 if (cpuidle_disabled())
785 return -ENODEV;
786
787 return cpuidle_add_interface(cpu_subsys.dev_root);
788 }
789
790 module_param(off, int, 0444);
791 module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
792 core_initcall(cpuidle_init);
793