Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * cpuidle.c - core cpuidle infrastructure
0003  *
0004  * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
0005  *               Shaohua Li <shaohua.li@intel.com>
0006  *               Adam Belay <abelay@novell.com>
0007  *
0008  * This code is licenced under the GPL.
0009  */
0010 
0011 #include "linux/percpu-defs.h"
0012 #include <linux/clockchips.h>
0013 #include <linux/kernel.h>
0014 #include <linux/mutex.h>
0015 #include <linux/sched.h>
0016 #include <linux/sched/clock.h>
0017 #include <linux/notifier.h>
0018 #include <linux/pm_qos.h>
0019 #include <linux/cpu.h>
0020 #include <linux/cpuidle.h>
0021 #include <linux/ktime.h>
0022 #include <linux/hrtimer.h>
0023 #include <linux/module.h>
0024 #include <linux/suspend.h>
0025 #include <linux/tick.h>
0026 #include <linux/mmu_context.h>
0027 #include <linux/context_tracking.h>
0028 #include <trace/events/power.h>
0029 
0030 #include "cpuidle.h"
0031 
0032 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
0033 DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
0034 
0035 DEFINE_MUTEX(cpuidle_lock);
0036 LIST_HEAD(cpuidle_detected_devices);
0037 
0038 static int enabled_devices;
0039 static int off __read_mostly;
0040 static int initialized __read_mostly;
0041 
0042 int cpuidle_disabled(void)
0043 {
0044     return off;
0045 }
0046 void disable_cpuidle(void)
0047 {
0048     off = 1;
0049 }
0050 
0051 bool cpuidle_not_available(struct cpuidle_driver *drv,
0052                struct cpuidle_device *dev)
0053 {
0054     return off || !initialized || !drv || !dev || !dev->enabled;
0055 }
0056 
0057 /**
0058  * cpuidle_play_dead - cpu off-lining
0059  *
0060  * Returns in case of an error or no driver
0061  */
0062 int cpuidle_play_dead(void)
0063 {
0064     struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
0065     struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0066     int i;
0067 
0068     if (!drv)
0069         return -ENODEV;
0070 
0071     /* Find lowest-power state that supports long-term idle */
0072     for (i = drv->state_count - 1; i >= 0; i--)
0073         if (drv->states[i].enter_dead)
0074             return drv->states[i].enter_dead(dev, i);
0075 
0076     return -ENODEV;
0077 }
0078 
0079 static int find_deepest_state(struct cpuidle_driver *drv,
0080                   struct cpuidle_device *dev,
0081                   u64 max_latency_ns,
0082                   unsigned int forbidden_flags,
0083                   bool s2idle)
0084 {
0085     u64 latency_req = 0;
0086     int i, ret = 0;
0087 
0088     for (i = 1; i < drv->state_count; i++) {
0089         struct cpuidle_state *s = &drv->states[i];
0090 
0091         if (dev->states_usage[i].disable ||
0092             s->exit_latency_ns <= latency_req ||
0093             s->exit_latency_ns > max_latency_ns ||
0094             (s->flags & forbidden_flags) ||
0095             (s2idle && !s->enter_s2idle))
0096             continue;
0097 
0098         latency_req = s->exit_latency_ns;
0099         ret = i;
0100     }
0101     return ret;
0102 }
0103 
0104 /**
0105  * cpuidle_use_deepest_state - Set/unset governor override mode.
0106  * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
0107  *
0108  * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
0109  * state with exit latency within @latency_limit_ns (override governors going
0110  * forward), or do not override governors if it is zero.
0111  */
0112 void cpuidle_use_deepest_state(u64 latency_limit_ns)
0113 {
0114     struct cpuidle_device *dev;
0115 
0116     preempt_disable();
0117     dev = cpuidle_get_device();
0118     if (dev)
0119         dev->forced_idle_latency_limit_ns = latency_limit_ns;
0120     preempt_enable();
0121 }
0122 
0123 /**
0124  * cpuidle_find_deepest_state - Find the deepest available idle state.
0125  * @drv: cpuidle driver for the given CPU.
0126  * @dev: cpuidle device for the given CPU.
0127  * @latency_limit_ns: Idle state exit latency limit
0128  *
0129  * Return: the index of the deepest available idle state.
0130  */
0131 int cpuidle_find_deepest_state(struct cpuidle_driver *drv,
0132                    struct cpuidle_device *dev,
0133                    u64 latency_limit_ns)
0134 {
0135     return find_deepest_state(drv, dev, latency_limit_ns, 0, false);
0136 }
0137 
0138 #ifdef CONFIG_SUSPEND
0139 static void enter_s2idle_proper(struct cpuidle_driver *drv,
0140                 struct cpuidle_device *dev, int index)
0141 {
0142     ktime_t time_start, time_end;
0143     struct cpuidle_state *target_state = &drv->states[index];
0144 
0145     time_start = ns_to_ktime(local_clock());
0146 
0147     tick_freeze();
0148     /*
0149      * The state used here cannot be a "coupled" one, because the "coupled"
0150      * cpuidle mechanism enables interrupts and doing that with timekeeping
0151      * suspended is generally unsafe.
0152      */
0153     stop_critical_timings();
0154     if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0155         ct_idle_enter();
0156     target_state->enter_s2idle(dev, drv, index);
0157     if (WARN_ON_ONCE(!irqs_disabled()))
0158         local_irq_disable();
0159     if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0160         ct_idle_exit();
0161     tick_unfreeze();
0162     start_critical_timings();
0163 
0164     time_end = ns_to_ktime(local_clock());
0165 
0166     dev->states_usage[index].s2idle_time += ktime_us_delta(time_end, time_start);
0167     dev->states_usage[index].s2idle_usage++;
0168 }
0169 
0170 /**
0171  * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
0172  * @drv: cpuidle driver for the given CPU.
0173  * @dev: cpuidle device for the given CPU.
0174  *
0175  * If there are states with the ->enter_s2idle callback, find the deepest of
0176  * them and enter it with frozen tick.
0177  */
0178 int cpuidle_enter_s2idle(struct cpuidle_driver *drv, struct cpuidle_device *dev)
0179 {
0180     int index;
0181 
0182     /*
0183      * Find the deepest state with ->enter_s2idle present, which guarantees
0184      * that interrupts won't be enabled when it exits and allows the tick to
0185      * be frozen safely.
0186      */
0187     index = find_deepest_state(drv, dev, U64_MAX, 0, true);
0188     if (index > 0) {
0189         enter_s2idle_proper(drv, dev, index);
0190         local_irq_enable();
0191     }
0192     return index;
0193 }
0194 #endif /* CONFIG_SUSPEND */
0195 
0196 /**
0197  * cpuidle_enter_state - enter the state and update stats
0198  * @dev: cpuidle device for this cpu
0199  * @drv: cpuidle driver for this cpu
0200  * @index: index into the states table in @drv of the state to enter
0201  */
0202 int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
0203             int index)
0204 {
0205     int entered_state;
0206 
0207     struct cpuidle_state *target_state = &drv->states[index];
0208     bool broadcast = !!(target_state->flags & CPUIDLE_FLAG_TIMER_STOP);
0209     ktime_t time_start, time_end;
0210 
0211     /*
0212      * Tell the time framework to switch to a broadcast timer because our
0213      * local timer will be shut down.  If a local timer is used from another
0214      * CPU as a broadcast timer, this call may fail if it is not available.
0215      */
0216     if (broadcast && tick_broadcast_enter()) {
0217         index = find_deepest_state(drv, dev, target_state->exit_latency_ns,
0218                        CPUIDLE_FLAG_TIMER_STOP, false);
0219         if (index < 0) {
0220             default_idle_call();
0221             return -EBUSY;
0222         }
0223         target_state = &drv->states[index];
0224         broadcast = false;
0225     }
0226 
0227     if (target_state->flags & CPUIDLE_FLAG_TLB_FLUSHED)
0228         leave_mm(dev->cpu);
0229 
0230     /* Take note of the planned idle state. */
0231     sched_idle_set_state(target_state);
0232 
0233     trace_cpu_idle(index, dev->cpu);
0234     time_start = ns_to_ktime(local_clock());
0235 
0236     stop_critical_timings();
0237     if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0238         ct_idle_enter();
0239     entered_state = target_state->enter(dev, drv, index);
0240     if (!(target_state->flags & CPUIDLE_FLAG_RCU_IDLE))
0241         ct_idle_exit();
0242     start_critical_timings();
0243 
0244     sched_clock_idle_wakeup_event();
0245     time_end = ns_to_ktime(local_clock());
0246     trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
0247 
0248     /* The cpu is no longer idle or about to enter idle. */
0249     sched_idle_set_state(NULL);
0250 
0251     if (broadcast) {
0252         if (WARN_ON_ONCE(!irqs_disabled()))
0253             local_irq_disable();
0254 
0255         tick_broadcast_exit();
0256     }
0257 
0258     if (!cpuidle_state_is_coupled(drv, index))
0259         local_irq_enable();
0260 
0261     if (entered_state >= 0) {
0262         s64 diff, delay = drv->states[entered_state].exit_latency_ns;
0263         int i;
0264 
0265         /*
0266          * Update cpuidle counters
0267          * This can be moved to within driver enter routine,
0268          * but that results in multiple copies of same code.
0269          */
0270         diff = ktime_sub(time_end, time_start);
0271 
0272         dev->last_residency_ns = diff;
0273         dev->states_usage[entered_state].time_ns += diff;
0274         dev->states_usage[entered_state].usage++;
0275 
0276         if (diff < drv->states[entered_state].target_residency_ns) {
0277             for (i = entered_state - 1; i >= 0; i--) {
0278                 if (dev->states_usage[i].disable)
0279                     continue;
0280 
0281                 /* Shallower states are enabled, so update. */
0282                 dev->states_usage[entered_state].above++;
0283                 trace_cpu_idle_miss(dev->cpu, entered_state, false);
0284                 break;
0285             }
0286         } else if (diff > delay) {
0287             for (i = entered_state + 1; i < drv->state_count; i++) {
0288                 if (dev->states_usage[i].disable)
0289                     continue;
0290 
0291                 /*
0292                  * Update if a deeper state would have been a
0293                  * better match for the observed idle duration.
0294                  */
0295                 if (diff - delay >= drv->states[i].target_residency_ns) {
0296                     dev->states_usage[entered_state].below++;
0297                     trace_cpu_idle_miss(dev->cpu, entered_state, true);
0298                 }
0299 
0300                 break;
0301             }
0302         }
0303     } else {
0304         dev->last_residency_ns = 0;
0305         dev->states_usage[index].rejected++;
0306     }
0307 
0308     return entered_state;
0309 }
0310 
0311 /**
0312  * cpuidle_select - ask the cpuidle framework to choose an idle state
0313  *
0314  * @drv: the cpuidle driver
0315  * @dev: the cpuidle device
0316  * @stop_tick: indication on whether or not to stop the tick
0317  *
0318  * Returns the index of the idle state.  The return value must not be negative.
0319  *
0320  * The memory location pointed to by @stop_tick is expected to be written the
0321  * 'false' boolean value if the scheduler tick should not be stopped before
0322  * entering the returned state.
0323  */
0324 int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev,
0325            bool *stop_tick)
0326 {
0327     return cpuidle_curr_governor->select(drv, dev, stop_tick);
0328 }
0329 
0330 /**
0331  * cpuidle_enter - enter into the specified idle state
0332  *
0333  * @drv:   the cpuidle driver tied with the cpu
0334  * @dev:   the cpuidle device
0335  * @index: the index in the idle state table
0336  *
0337  * Returns the index in the idle state, < 0 in case of error.
0338  * The error code depends on the backend driver
0339  */
0340 int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev,
0341           int index)
0342 {
0343     int ret = 0;
0344 
0345     /*
0346      * Store the next hrtimer, which becomes either next tick or the next
0347      * timer event, whatever expires first. Additionally, to make this data
0348      * useful for consumers outside cpuidle, we rely on that the governor's
0349      * ->select() callback have decided, whether to stop the tick or not.
0350      */
0351     WRITE_ONCE(dev->next_hrtimer, tick_nohz_get_next_hrtimer());
0352 
0353     if (cpuidle_state_is_coupled(drv, index))
0354         ret = cpuidle_enter_state_coupled(dev, drv, index);
0355     else
0356         ret = cpuidle_enter_state(dev, drv, index);
0357 
0358     WRITE_ONCE(dev->next_hrtimer, 0);
0359     return ret;
0360 }
0361 
0362 /**
0363  * cpuidle_reflect - tell the underlying governor what was the state
0364  * we were in
0365  *
0366  * @dev  : the cpuidle device
0367  * @index: the index in the idle state table
0368  *
0369  */
0370 void cpuidle_reflect(struct cpuidle_device *dev, int index)
0371 {
0372     if (cpuidle_curr_governor->reflect && index >= 0)
0373         cpuidle_curr_governor->reflect(dev, index);
0374 }
0375 
0376 /*
0377  * Min polling interval of 10usec is a guess. It is assuming that
0378  * for most users, the time for a single ping-pong workload like
0379  * perf bench pipe would generally complete within 10usec but
0380  * this is hardware dependant. Actual time can be estimated with
0381  *
0382  * perf bench sched pipe -l 10000
0383  *
0384  * Run multiple times to avoid cpufreq effects.
0385  */
0386 #define CPUIDLE_POLL_MIN 10000
0387 #define CPUIDLE_POLL_MAX (TICK_NSEC / 16)
0388 
0389 /**
0390  * cpuidle_poll_time - return amount of time to poll for,
0391  * governors can override dev->poll_limit_ns if necessary
0392  *
0393  * @drv:   the cpuidle driver tied with the cpu
0394  * @dev:   the cpuidle device
0395  *
0396  */
0397 u64 cpuidle_poll_time(struct cpuidle_driver *drv,
0398               struct cpuidle_device *dev)
0399 {
0400     int i;
0401     u64 limit_ns;
0402 
0403     BUILD_BUG_ON(CPUIDLE_POLL_MIN > CPUIDLE_POLL_MAX);
0404 
0405     if (dev->poll_limit_ns)
0406         return dev->poll_limit_ns;
0407 
0408     limit_ns = CPUIDLE_POLL_MAX;
0409     for (i = 1; i < drv->state_count; i++) {
0410         u64 state_limit;
0411 
0412         if (dev->states_usage[i].disable)
0413             continue;
0414 
0415         state_limit = drv->states[i].target_residency_ns;
0416         if (state_limit < CPUIDLE_POLL_MIN)
0417             continue;
0418 
0419         limit_ns = min_t(u64, state_limit, CPUIDLE_POLL_MAX);
0420         break;
0421     }
0422 
0423     dev->poll_limit_ns = limit_ns;
0424 
0425     return dev->poll_limit_ns;
0426 }
0427 
0428 /**
0429  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
0430  */
0431 void cpuidle_install_idle_handler(void)
0432 {
0433     if (enabled_devices) {
0434         /* Make sure all changes finished before we switch to new idle */
0435         smp_wmb();
0436         initialized = 1;
0437     }
0438 }
0439 
0440 /**
0441  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
0442  */
0443 void cpuidle_uninstall_idle_handler(void)
0444 {
0445     if (enabled_devices) {
0446         initialized = 0;
0447         wake_up_all_idle_cpus();
0448     }
0449 
0450     /*
0451      * Make sure external observers (such as the scheduler)
0452      * are done looking at pointed idle states.
0453      */
0454     synchronize_rcu();
0455 }
0456 
0457 /**
0458  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
0459  */
0460 void cpuidle_pause_and_lock(void)
0461 {
0462     mutex_lock(&cpuidle_lock);
0463     cpuidle_uninstall_idle_handler();
0464 }
0465 
0466 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
0467 
0468 /**
0469  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
0470  */
0471 void cpuidle_resume_and_unlock(void)
0472 {
0473     cpuidle_install_idle_handler();
0474     mutex_unlock(&cpuidle_lock);
0475 }
0476 
0477 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
0478 
0479 /* Currently used in suspend/resume path to suspend cpuidle */
0480 void cpuidle_pause(void)
0481 {
0482     mutex_lock(&cpuidle_lock);
0483     cpuidle_uninstall_idle_handler();
0484     mutex_unlock(&cpuidle_lock);
0485 }
0486 
0487 /* Currently used in suspend/resume path to resume cpuidle */
0488 void cpuidle_resume(void)
0489 {
0490     mutex_lock(&cpuidle_lock);
0491     cpuidle_install_idle_handler();
0492     mutex_unlock(&cpuidle_lock);
0493 }
0494 
0495 /**
0496  * cpuidle_enable_device - enables idle PM for a CPU
0497  * @dev: the CPU
0498  *
0499  * This function must be called between cpuidle_pause_and_lock and
0500  * cpuidle_resume_and_unlock when used externally.
0501  */
0502 int cpuidle_enable_device(struct cpuidle_device *dev)
0503 {
0504     int ret;
0505     struct cpuidle_driver *drv;
0506 
0507     if (!dev)
0508         return -EINVAL;
0509 
0510     if (dev->enabled)
0511         return 0;
0512 
0513     if (!cpuidle_curr_governor)
0514         return -EIO;
0515 
0516     drv = cpuidle_get_cpu_driver(dev);
0517 
0518     if (!drv)
0519         return -EIO;
0520 
0521     if (!dev->registered)
0522         return -EINVAL;
0523 
0524     ret = cpuidle_add_device_sysfs(dev);
0525     if (ret)
0526         return ret;
0527 
0528     if (cpuidle_curr_governor->enable) {
0529         ret = cpuidle_curr_governor->enable(drv, dev);
0530         if (ret)
0531             goto fail_sysfs;
0532     }
0533 
0534     smp_wmb();
0535 
0536     dev->enabled = 1;
0537 
0538     enabled_devices++;
0539     return 0;
0540 
0541 fail_sysfs:
0542     cpuidle_remove_device_sysfs(dev);
0543 
0544     return ret;
0545 }
0546 
0547 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
0548 
0549 /**
0550  * cpuidle_disable_device - disables idle PM for a CPU
0551  * @dev: the CPU
0552  *
0553  * This function must be called between cpuidle_pause_and_lock and
0554  * cpuidle_resume_and_unlock when used externally.
0555  */
0556 void cpuidle_disable_device(struct cpuidle_device *dev)
0557 {
0558     struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0559 
0560     if (!dev || !dev->enabled)
0561         return;
0562 
0563     if (!drv || !cpuidle_curr_governor)
0564         return;
0565 
0566     dev->enabled = 0;
0567 
0568     if (cpuidle_curr_governor->disable)
0569         cpuidle_curr_governor->disable(drv, dev);
0570 
0571     cpuidle_remove_device_sysfs(dev);
0572     enabled_devices--;
0573 }
0574 
0575 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
0576 
0577 static void __cpuidle_unregister_device(struct cpuidle_device *dev)
0578 {
0579     struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0580 
0581     list_del(&dev->device_list);
0582     per_cpu(cpuidle_devices, dev->cpu) = NULL;
0583     module_put(drv->owner);
0584 
0585     dev->registered = 0;
0586 }
0587 
0588 static void __cpuidle_device_init(struct cpuidle_device *dev)
0589 {
0590     memset(dev->states_usage, 0, sizeof(dev->states_usage));
0591     dev->last_residency_ns = 0;
0592     dev->next_hrtimer = 0;
0593 }
0594 
0595 /**
0596  * __cpuidle_register_device - internal register function called before register
0597  * and enable routines
0598  * @dev: the cpu
0599  *
0600  * cpuidle_lock mutex must be held before this is called
0601  */
0602 static int __cpuidle_register_device(struct cpuidle_device *dev)
0603 {
0604     struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
0605     int i, ret;
0606 
0607     if (!try_module_get(drv->owner))
0608         return -EINVAL;
0609 
0610     for (i = 0; i < drv->state_count; i++) {
0611         if (drv->states[i].flags & CPUIDLE_FLAG_UNUSABLE)
0612             dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
0613 
0614         if (drv->states[i].flags & CPUIDLE_FLAG_OFF)
0615             dev->states_usage[i].disable |= CPUIDLE_STATE_DISABLED_BY_USER;
0616     }
0617 
0618     per_cpu(cpuidle_devices, dev->cpu) = dev;
0619     list_add(&dev->device_list, &cpuidle_detected_devices);
0620 
0621     ret = cpuidle_coupled_register_device(dev);
0622     if (ret)
0623         __cpuidle_unregister_device(dev);
0624     else
0625         dev->registered = 1;
0626 
0627     return ret;
0628 }
0629 
0630 /**
0631  * cpuidle_register_device - registers a CPU's idle PM feature
0632  * @dev: the cpu
0633  */
0634 int cpuidle_register_device(struct cpuidle_device *dev)
0635 {
0636     int ret = -EBUSY;
0637 
0638     if (!dev)
0639         return -EINVAL;
0640 
0641     mutex_lock(&cpuidle_lock);
0642 
0643     if (dev->registered)
0644         goto out_unlock;
0645 
0646     __cpuidle_device_init(dev);
0647 
0648     ret = __cpuidle_register_device(dev);
0649     if (ret)
0650         goto out_unlock;
0651 
0652     ret = cpuidle_add_sysfs(dev);
0653     if (ret)
0654         goto out_unregister;
0655 
0656     ret = cpuidle_enable_device(dev);
0657     if (ret)
0658         goto out_sysfs;
0659 
0660     cpuidle_install_idle_handler();
0661 
0662 out_unlock:
0663     mutex_unlock(&cpuidle_lock);
0664 
0665     return ret;
0666 
0667 out_sysfs:
0668     cpuidle_remove_sysfs(dev);
0669 out_unregister:
0670     __cpuidle_unregister_device(dev);
0671     goto out_unlock;
0672 }
0673 
0674 EXPORT_SYMBOL_GPL(cpuidle_register_device);
0675 
0676 /**
0677  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
0678  * @dev: the cpu
0679  */
0680 void cpuidle_unregister_device(struct cpuidle_device *dev)
0681 {
0682     if (!dev || dev->registered == 0)
0683         return;
0684 
0685     cpuidle_pause_and_lock();
0686 
0687     cpuidle_disable_device(dev);
0688 
0689     cpuidle_remove_sysfs(dev);
0690 
0691     __cpuidle_unregister_device(dev);
0692 
0693     cpuidle_coupled_unregister_device(dev);
0694 
0695     cpuidle_resume_and_unlock();
0696 }
0697 
0698 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
0699 
0700 /**
0701  * cpuidle_unregister: unregister a driver and the devices. This function
0702  * can be used only if the driver has been previously registered through
0703  * the cpuidle_register function.
0704  *
0705  * @drv: a valid pointer to a struct cpuidle_driver
0706  */
0707 void cpuidle_unregister(struct cpuidle_driver *drv)
0708 {
0709     int cpu;
0710     struct cpuidle_device *device;
0711 
0712     for_each_cpu(cpu, drv->cpumask) {
0713         device = &per_cpu(cpuidle_dev, cpu);
0714         cpuidle_unregister_device(device);
0715     }
0716 
0717     cpuidle_unregister_driver(drv);
0718 }
0719 EXPORT_SYMBOL_GPL(cpuidle_unregister);
0720 
0721 /**
0722  * cpuidle_register: registers the driver and the cpu devices with the
0723  * coupled_cpus passed as parameter. This function is used for all common
0724  * initialization pattern there are in the arch specific drivers. The
0725  * devices is globally defined in this file.
0726  *
0727  * @drv         : a valid pointer to a struct cpuidle_driver
0728  * @coupled_cpus: a cpumask for the coupled states
0729  *
0730  * Returns 0 on success, < 0 otherwise
0731  */
0732 int cpuidle_register(struct cpuidle_driver *drv,
0733              const struct cpumask *const coupled_cpus)
0734 {
0735     int ret, cpu;
0736     struct cpuidle_device *device;
0737 
0738     ret = cpuidle_register_driver(drv);
0739     if (ret) {
0740         pr_err("failed to register cpuidle driver\n");
0741         return ret;
0742     }
0743 
0744     for_each_cpu(cpu, drv->cpumask) {
0745         device = &per_cpu(cpuidle_dev, cpu);
0746         device->cpu = cpu;
0747 
0748 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
0749         /*
0750          * On multiplatform for ARM, the coupled idle states could be
0751          * enabled in the kernel even if the cpuidle driver does not
0752          * use it. Note, coupled_cpus is a struct copy.
0753          */
0754         if (coupled_cpus)
0755             device->coupled_cpus = *coupled_cpus;
0756 #endif
0757         ret = cpuidle_register_device(device);
0758         if (!ret)
0759             continue;
0760 
0761         pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
0762 
0763         cpuidle_unregister(drv);
0764         break;
0765     }
0766 
0767     return ret;
0768 }
0769 EXPORT_SYMBOL_GPL(cpuidle_register);
0770 
0771 /**
0772  * cpuidle_init - core initializer
0773  */
0774 static int __init cpuidle_init(void)
0775 {
0776     if (cpuidle_disabled())
0777         return -ENODEV;
0778 
0779     return cpuidle_add_interface(cpu_subsys.dev_root);
0780 }
0781 
0782 module_param(off, int, 0444);
0783 module_param_string(governor, param_governor, CPUIDLE_NAME_LEN, 0444);
0784 core_initcall(cpuidle_init);