0001
0002
0003
0004
0005
0006
0007
0008
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
0030
0031
0032
0033
0034
0035 static struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
0036 {
0037 return per_cpu(cpuidle_drivers, cpu);
0038 }
0039
0040
0041
0042
0043
0044
0045
0046
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
0063
0064
0065
0066
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
0092
0093
0094
0095
0096
0097 static inline struct cpuidle_driver *__cpuidle_get_cpu_driver(int cpu)
0098 {
0099 return cpuidle_curr_driver;
0100 }
0101
0102
0103
0104
0105
0106
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
0120
0121
0122
0123
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
0135
0136
0137
0138
0139
0140
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
0152
0153
0154 static void __cpuidle_driver_init(struct cpuidle_driver *drv)
0155 {
0156 int i;
0157
0158
0159
0160
0161
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
0171
0172
0173
0174 if (s->flags & CPUIDLE_FLAG_TIMER_STOP)
0175 drv->bctimer = 1;
0176
0177
0178
0179
0180
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
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
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
0236
0237
0238
0239
0240
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
0256
0257
0258
0259
0260
0261
0262
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
0291
0292
0293
0294
0295
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
0319
0320
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
0337
0338
0339
0340
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
0353
0354
0355
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 }