Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * driver.c - driver support
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/mutex.h>
0012 #include <linux/module.h>
0013 #include <linux/sched.h>
0014 #include <linux/sched/idle.h>
0015 #include <linux/cpuidle.h>
0016 #include <linux/cpumask.h>
0017 #include <linux/tick.h>
0018 #include <linux/cpu.h>
0019 
0020 #include "cpuidle.h"
0021 
0022 DEFINE_SPINLOCK(cpuidle_driver_lock);
0023 
0024 #ifdef CONFIG_CPU_IDLE_MULTIPLE_DRIVERS
0025 
0026 static DEFINE_PER_CPU(struct cpuidle_driver *, cpuidle_drivers);
0027 
0028 /**
0029  * __cpuidle_get_cpu_driver - return the cpuidle driver tied to a CPU.
0030  * @cpu: the CPU handled by the driver
0031  *
0032  * Returns a pointer to struct cpuidle_driver or NULL if no driver has been
0033  * registered for @cpu.
0034  */
0035 static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
0036 {
0037     return per_cpu(cpuidle_drivers, cpu);
0038 }
0039 
0040 /**
0041  * __cpuidle_unset_driver - unset per CPU driver variables.
0042  * @drv: a valid pointer to a struct cpuidle_driver
0043  *
0044  * For each CPU in the driver's CPU mask, unset the registered driver per CPU
0045  * variable. If @drv is different from the registered driver, the corresponding
0046  * variable is not cleared.
0047  */
0048 static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
0049 {
0050     int cpu;
0051 
0052     for_each_cpu(cpu, drv->cpumask) {
0053 
0054         if (drv != __cpuidle_get_cpu_driver(cpu))
0055             continue;
0056 
0057         per_cpu(cpuidle_drivers, cpu) = NULL;
0058     }
0059 }
0060 
0061 /**
0062  * __cpuidle_set_driver - set per CPU driver variables for the given driver.
0063  * @drv: a valid pointer to a struct cpuidle_driver
0064  *
0065  * Returns 0 on success, -EBUSY if any CPU in the cpumask have a driver
0066  * different from drv already.
0067  */
0068 static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
0069 {
0070     int cpu;
0071 
0072     for_each_cpu(cpu, drv->cpumask) {
0073         struct cpuidle_driver *old_drv;
0074 
0075         old_drv = __cpuidle_get_cpu_driver(cpu);
0076         if (old_drv && old_drv != drv)
0077             return -EBUSY;
0078     }
0079 
0080     for_each_cpu(cpu, drv->cpumask)
0081         per_cpu(cpuidle_drivers, cpu) = drv;
0082 
0083     return 0;
0084 }
0085 
0086 #else
0087 
0088 static struct cpuidle_driver *cpuidle_curr_driver;
0089 
0090 /**
0091  * __cpuidle_get_cpu_driver - return the global cpuidle driver pointer.
0092  * @cpu: ignored without the multiple driver support
0093  *
0094  * Return a pointer to a struct cpuidle_driver object or NULL if no driver was
0095  * previously registered.
0096  */
0097 static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
0098 {
0099     return cpuidle_curr_driver;
0100 }
0101 
0102 /**
0103  * __cpuidle_set_driver - assign the global cpuidle driver variable.
0104  * @drv: pointer to a struct cpuidle_driver object
0105  *
0106  * Returns 0 on success, -EBUSY if the driver is already registered.
0107  */
0108 static inline int __cpuidle_set_driver(struct cpuidle_driver *drv)
0109 {
0110     if (cpuidle_curr_driver)
0111         return -EBUSY;
0112 
0113     cpuidle_curr_driver = drv;
0114 
0115     return 0;
0116 }
0117 
0118 /**
0119  * __cpuidle_unset_driver - unset the global cpuidle driver variable.
0120  * @drv: a pointer to a struct cpuidle_driver
0121  *
0122  * Reset the global cpuidle variable to NULL.  If @drv does not match the
0123  * registered driver, do nothing.
0124  */
0125 static inline void __cpuidle_unset_driver(struct cpuidle_driver *drv)
0126 {
0127     if (drv == cpuidle_curr_driver)
0128         cpuidle_curr_driver = NULL;
0129 }
0130 
0131 #endif
0132 
0133 /**
0134  * cpuidle_setup_broadcast_timer - enable/disable the broadcast timer on a cpu
0135  * @arg: a void pointer used to match the SMP cross call API
0136  *
0137  * If @arg is NULL broadcast is disabled otherwise enabled
0138  *
0139  * This function is executed per CPU by an SMP cross call.  It's not
0140  * supposed to be called directly.
0141  */
0142 static void cpuidle_setup_broadcast_timer(void *arg)
0143 {
0144     if (arg)
0145         tick_broadcast_enable();
0146     else
0147         tick_broadcast_disable();
0148 }
0149 
0150 /**
0151  * __cpuidle_driver_init - initialize the driver's internal data
0152  * @drv: a valid pointer to a struct cpuidle_driver
0153  */
0154 static void __cpuidle_driver_init(struct cpuidle_driver *drv)
0155 {
0156     int i;
0157 
0158     /*
0159      * Use all possible CPUs as the default, because if the kernel boots
0160      * with some CPUs offline and then we online one of them, the CPU
0161      * notifier has to know which driver to assign.
0162      */
0163     if (!drv->cpumask)
0164         drv->cpumask = (struct cpumask *)cpu_possible_mask;
0165 
0166     for (i = 0; i < drv->state_count; i++) {
0167         struct cpuidle_state *s = &drv->states[i];
0168 
0169         /*
0170          * Look for the timer stop flag in the different states and if
0171          * it is found, indicate that the broadcast timer has to be set
0172          * up.
0173          */
0174         if (s->flags & CPUIDLE_FLAG_TIMER_STOP)
0175             drv->bctimer = 1;
0176 
0177         /*
0178          * The core will use the target residency and exit latency
0179          * values in nanoseconds, but allow drivers to provide them in
0180          * microseconds too.
0181          */
0182         if (s->target_residency > 0)
0183             s->target_residency_ns = s->target_residency * NSEC_PER_USEC;
0184         else if (s->target_residency_ns < 0)
0185             s->target_residency_ns = 0;
0186 
0187         if (s->exit_latency > 0)
0188             s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC;
0189         else if (s->exit_latency_ns < 0)
0190             s->exit_latency_ns =  0;
0191     }
0192 }
0193 
0194 /**
0195  * __cpuidle_register_driver: register the driver
0196  * @drv: a valid pointer to a struct cpuidle_driver
0197  *
0198  * Do some sanity checks, initialize the driver, assign the driver to the
0199  * global cpuidle driver variable(s) and set up the broadcast timer if the
0200  * cpuidle driver has some states that shut down the local timer.
0201  *
0202  * Returns 0 on success, a negative error code otherwise:
0203  *  * -EINVAL if the driver pointer is NULL or no idle states are available
0204  *  * -ENODEV if the cpuidle framework is disabled
0205  *  * -EBUSY if the driver is already assigned to the global variable(s)
0206  */
0207 static int __cpuidle_register_driver(struct cpuidle_driver *drv)
0208 {
0209     int ret;
0210 
0211     if (!drv || !drv->state_count)
0212         return -EINVAL;
0213 
0214     ret = cpuidle_coupled_state_verify(drv);
0215     if (ret)
0216         return ret;
0217 
0218     if (cpuidle_disabled())
0219         return -ENODEV;
0220 
0221     __cpuidle_driver_init(drv);
0222 
0223     ret = __cpuidle_set_driver(drv);
0224     if (ret)
0225         return ret;
0226 
0227     if (drv->bctimer)
0228         on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
0229                  (void *)1, 1);
0230 
0231     return 0;
0232 }
0233 
0234 /**
0235  * __cpuidle_unregister_driver - unregister the driver
0236  * @drv: a valid pointer to a struct cpuidle_driver
0237  *
0238  * Check if the driver is no longer in use, reset the global cpuidle driver
0239  * variable(s) and disable the timer broadcast notification mechanism if it was
0240  * in use.
0241  *
0242  */
0243 static void __cpuidle_unregister_driver(struct cpuidle_driver *drv)
0244 {
0245     if (drv->bctimer) {
0246         drv->bctimer = 0;
0247         on_each_cpu_mask(drv->cpumask, cpuidle_setup_broadcast_timer,
0248                  NULL, 1);
0249     }
0250 
0251     __cpuidle_unset_driver(drv);
0252 }
0253 
0254 /**
0255  * cpuidle_register_driver - registers a driver
0256  * @drv: a pointer to a valid struct cpuidle_driver
0257  *
0258  * Register the driver under a lock to prevent concurrent attempts to
0259  * [un]register the driver from occuring at the same time.
0260  *
0261  * Returns 0 on success, a negative error code (returned by
0262  * __cpuidle_register_driver()) otherwise.
0263  */
0264 int cpuidle_register_driver(struct cpuidle_driver *drv)
0265 {
0266     struct cpuidle_governor *gov;
0267     int ret;
0268 
0269     spin_lock(&cpuidle_driver_lock);
0270     ret = __cpuidle_register_driver(drv);
0271     spin_unlock(&cpuidle_driver_lock);
0272 
0273     if (!ret && !strlen(param_governor) && drv->governor &&
0274         (cpuidle_get_driver() == drv)) {
0275         mutex_lock(&cpuidle_lock);
0276         gov = cpuidle_find_governor(drv->governor);
0277         if (gov) {
0278             cpuidle_prev_governor = cpuidle_curr_governor;
0279             if (cpuidle_switch_governor(gov) < 0)
0280                 cpuidle_prev_governor = NULL;
0281         }
0282         mutex_unlock(&cpuidle_lock);
0283     }
0284 
0285     return ret;
0286 }
0287 EXPORT_SYMBOL_GPL(cpuidle_register_driver);
0288 
0289 /**
0290  * cpuidle_unregister_driver - unregisters a driver
0291  * @drv: a pointer to a valid struct cpuidle_driver
0292  *
0293  * Unregisters the cpuidle driver under a lock to prevent concurrent attempts
0294  * to [un]register the driver from occuring at the same time.  @drv has to
0295  * match the currently registered driver.
0296  */
0297 void cpuidle_unregister_driver(struct cpuidle_driver *drv)
0298 {
0299     bool enabled = (cpuidle_get_driver() == drv);
0300 
0301     spin_lock(&cpuidle_driver_lock);
0302     __cpuidle_unregister_driver(drv);
0303     spin_unlock(&cpuidle_driver_lock);
0304 
0305     if (!enabled)
0306         return;
0307 
0308     mutex_lock(&cpuidle_lock);
0309     if (cpuidle_prev_governor) {
0310         if (!cpuidle_switch_governor(cpuidle_prev_governor))
0311             cpuidle_prev_governor = NULL;
0312     }
0313     mutex_unlock(&cpuidle_lock);
0314 }
0315 EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
0316 
0317 /**
0318  * cpuidle_get_driver - return the driver tied to the current CPU.
0319  *
0320  * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered.
0321  */
0322 struct cpuidle_driver *cpuidle_get_driver(void)
0323 {
0324     struct cpuidle_driver *drv;
0325     int cpu;
0326 
0327     cpu = get_cpu();
0328     drv = __cpuidle_get_cpu_driver(cpu);
0329     put_cpu();
0330 
0331     return drv;
0332 }
0333 EXPORT_SYMBOL_GPL(cpuidle_get_driver);
0334 
0335 /**
0336  * cpuidle_get_cpu_driver - return the driver registered for a CPU.
0337  * @dev: a valid pointer to a struct cpuidle_device
0338  *
0339  * Returns a struct cpuidle_driver pointer, or NULL if no driver is registered
0340  * for the CPU associated with @dev.
0341  */
0342 struct cpuidle_driver *cpuidle_get_cpu_driver(struct cpuidle_device *dev)
0343 {
0344     if (!dev)
0345         return NULL;
0346 
0347     return __cpuidle_get_cpu_driver(dev->cpu);
0348 }
0349 EXPORT_SYMBOL_GPL(cpuidle_get_cpu_driver);
0350 
0351 /**
0352  * cpuidle_driver_state_disabled - Disable or enable an idle state
0353  * @drv: cpuidle driver owning the state
0354  * @idx: State index
0355  * @disable: Whether or not to disable the state
0356  */
0357 void cpuidle_driver_state_disabled(struct cpuidle_driver *drv, int idx,
0358                  bool disable)
0359 {
0360     unsigned int cpu;
0361 
0362     mutex_lock(&cpuidle_lock);
0363 
0364     spin_lock(&cpuidle_driver_lock);
0365 
0366     if (!drv->cpumask) {
0367         drv->states[idx].flags |= CPUIDLE_FLAG_UNUSABLE;
0368         goto unlock;
0369     }
0370 
0371     for_each_cpu(cpu, drv->cpumask) {
0372         struct cpuidle_device *dev = per_cpu(cpuidle_devices, cpu);
0373 
0374         if (!dev)
0375             continue;
0376 
0377         if (disable)
0378             dev->states_usage[idx].disable |= CPUIDLE_STATE_DISABLED_BY_DRIVER;
0379         else
0380             dev->states_usage[idx].disable &= ~CPUIDLE_STATE_DISABLED_BY_DRIVER;
0381     }
0382 
0383 unlock:
0384     spin_unlock(&cpuidle_driver_lock);
0385 
0386     mutex_unlock(&cpuidle_lock);
0387 }