Branch data Line data Source code
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/notifier.h>
16 : : #include <linux/pm_qos.h>
17 : : #include <linux/cpu.h>
18 : : #include <linux/cpuidle.h>
19 : : #include <linux/ktime.h>
20 : : #include <linux/hrtimer.h>
21 : : #include <linux/module.h>
22 : : #include <trace/events/power.h>
23 : :
24 : : #include "cpuidle.h"
25 : :
26 : : DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
27 : : DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev);
28 : :
29 : : DEFINE_MUTEX(cpuidle_lock);
30 : : LIST_HEAD(cpuidle_detected_devices);
31 : :
32 : : static int enabled_devices;
33 : : static int off __read_mostly;
34 : : static int initialized __read_mostly;
35 : :
36 : 0 : int cpuidle_disabled(void)
37 : : {
38 : 0 : return off;
39 : : }
40 : 0 : void disable_cpuidle(void)
41 : : {
42 : 0 : off = 1;
43 : 0 : }
44 : :
45 : : /**
46 : : * cpuidle_play_dead - cpu off-lining
47 : : *
48 : : * Returns in case of an error or no driver
49 : : */
50 : 0 : int cpuidle_play_dead(void)
51 : : {
52 : 0 : struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
53 : 0 : struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
54 : : int i;
55 : :
56 [ # # ]: 0 : if (!drv)
57 : : return -ENODEV;
58 : :
59 : : /* Find lowest-power state that supports long-term idle */
60 [ # # ]: 0 : for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
61 [ # # ]: 0 : if (drv->states[i].enter_dead)
62 : 0 : return drv->states[i].enter_dead(dev, i);
63 : :
64 : : return -ENODEV;
65 : : }
66 : :
67 : : /**
68 : : * cpuidle_enter_state - enter the state and update stats
69 : : * @dev: cpuidle device for this cpu
70 : : * @drv: cpuidle driver for this cpu
71 : : * @next_state: index into drv->states of the state to enter
72 : : */
73 : 0 : int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
74 : : int index)
75 : : {
76 : : int entered_state;
77 : :
78 : 5593177 : struct cpuidle_state *target_state = &drv->states[index];
79 : : ktime_t time_start, time_end;
80 : : s64 diff;
81 : :
82 : 5593177 : time_start = ktime_get();
83 : :
84 : 5593699 : entered_state = target_state->enter(dev, drv, index);
85 : :
86 : 5592877 : time_end = ktime_get();
87 : :
88 : : local_irq_enable();
89 : :
90 : 5593824 : diff = ktime_to_us(ktime_sub(time_end, time_start));
91 [ - + ]: 11187010 : if (diff > INT_MAX)
92 : : diff = INT_MAX;
93 : :
94 : 5593833 : dev->last_residency = (int) diff;
95 : :
96 [ + - ]: 5593833 : if (entered_state >= 0) {
97 : : /* Update cpuidle counters */
98 : : /* This can be moved to within driver enter routine
99 : : * but that results in multiple copies of same code.
100 : : */
101 : 5593833 : dev->states_usage[entered_state].time += dev->last_residency;
102 : 5593833 : dev->states_usage[entered_state].usage++;
103 : : } else {
104 : 0 : dev->last_residency = 0;
105 : : }
106 : :
107 : 5593833 : return entered_state;
108 : : }
109 : :
110 : : /**
111 : : * cpuidle_idle_call - the main idle loop
112 : : *
113 : : * NOTE: no locks or semaphores should be used here
114 : : * return non-zero on failure
115 : : */
116 : 0 : int cpuidle_idle_call(void)
117 : : {
118 : 11275160 : struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
119 : : struct cpuidle_driver *drv;
120 : : int next_state, entered_state;
121 : : bool broadcast;
122 : :
123 [ + ][ + + ]: 5637580 : if (off || !initialized)
124 : : return -ENODEV;
125 : :
126 : : /* check if the device is ready */
127 [ + ][ + + ]: 5637646 : if (!dev || !dev->enabled)
128 : : return -EBUSY;
129 : :
130 : 5637643 : drv = cpuidle_get_cpu_driver(dev);
131 : :
132 : : /* ask the governor for the next state */
133 : 5637539 : next_state = cpuidle_curr_governor->select(drv, dev);
134 [ + + ]: 5636847 : if (need_resched()) {
135 : 43884 : dev->last_residency = 0;
136 : : /* give the governor an opportunity to reflect on the outcome */
137 [ + - ]: 43884 : if (cpuidle_curr_governor->reflect)
138 : 43884 : cpuidle_curr_governor->reflect(dev, next_state);
139 : : local_irq_enable();
140 : 43884 : return 0;
141 : : }
142 : :
143 [ + ]: 5592963 : trace_cpu_idle_rcuidle(next_state, dev->cpu);
144 : :
145 : 5593135 : broadcast = !!(drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP);
146 : :
147 [ + + ]: 5593135 : if (broadcast)
148 : 1195288 : clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &dev->cpu);
149 : :
150 : : if (cpuidle_state_is_coupled(dev, drv, next_state))
151 : : entered_state = cpuidle_enter_state_coupled(dev, drv,
152 : : next_state);
153 : : else
154 : 5593149 : entered_state = cpuidle_enter_state(dev, drv, next_state);
155 : :
156 [ + + ]: 5593840 : if (broadcast)
157 : 1195300 : clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &dev->cpu);
158 : :
159 : 5593839 : trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
160 : :
161 : : /* give the governor an opportunity to reflect on the outcome */
162 [ + - ]: 5593839 : if (cpuidle_curr_governor->reflect)
163 : 5593839 : cpuidle_curr_governor->reflect(dev, entered_state);
164 : :
165 : : return 0;
166 : : }
167 : :
168 : : /**
169 : : * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
170 : : */
171 : 0 : void cpuidle_install_idle_handler(void)
172 : : {
173 [ # # # # ]: 0 : if (enabled_devices) {
[ # # ][ # # ]
174 : : /* Make sure all changes finished before we switch to new idle */
175 : 0 : smp_wmb();
176 : 0 : initialized = 1;
177 : : }
178 : 0 : }
179 : :
180 : : /**
181 : : * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
182 : : */
183 : 0 : void cpuidle_uninstall_idle_handler(void)
184 : : {
185 [ # # # # ]: 0 : if (enabled_devices) {
[ # # ]
186 : 0 : initialized = 0;
187 : 0 : kick_all_cpus_sync();
188 : : }
189 : 0 : }
190 : :
191 : : /**
192 : : * cpuidle_pause_and_lock - temporarily disables CPUIDLE
193 : : */
194 : 0 : void cpuidle_pause_and_lock(void)
195 : : {
196 : 0 : mutex_lock(&cpuidle_lock);
197 : : cpuidle_uninstall_idle_handler();
198 : 0 : }
199 : :
200 : : EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
201 : :
202 : : /**
203 : : * cpuidle_resume_and_unlock - resumes CPUIDLE operation
204 : : */
205 : 0 : void cpuidle_resume_and_unlock(void)
206 : : {
207 : : cpuidle_install_idle_handler();
208 : 0 : mutex_unlock(&cpuidle_lock);
209 : 0 : }
210 : :
211 : : EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
212 : :
213 : : /* Currently used in suspend/resume path to suspend cpuidle */
214 : 0 : void cpuidle_pause(void)
215 : : {
216 : 0 : mutex_lock(&cpuidle_lock);
217 : : cpuidle_uninstall_idle_handler();
218 : 0 : mutex_unlock(&cpuidle_lock);
219 : 0 : }
220 : :
221 : : /* Currently used in suspend/resume path to resume cpuidle */
222 : 0 : void cpuidle_resume(void)
223 : : {
224 : 0 : mutex_lock(&cpuidle_lock);
225 : : cpuidle_install_idle_handler();
226 : 0 : mutex_unlock(&cpuidle_lock);
227 : 0 : }
228 : :
229 : : /**
230 : : * cpuidle_enable_device - enables idle PM for a CPU
231 : : * @dev: the CPU
232 : : *
233 : : * This function must be called between cpuidle_pause_and_lock and
234 : : * cpuidle_resume_and_unlock when used externally.
235 : : */
236 : 0 : int cpuidle_enable_device(struct cpuidle_device *dev)
237 : : {
238 : : int ret;
239 : : struct cpuidle_driver *drv;
240 : :
241 [ # # ]: 0 : if (!dev)
242 : : return -EINVAL;
243 : :
244 [ # # ]: 0 : if (dev->enabled)
245 : : return 0;
246 : :
247 : 0 : drv = cpuidle_get_cpu_driver(dev);
248 : :
249 [ # # ][ # # ]: 0 : if (!drv || !cpuidle_curr_governor)
250 : : return -EIO;
251 : :
252 [ # # ]: 0 : if (!dev->registered)
253 : : return -EINVAL;
254 : :
255 [ # # ]: 0 : if (!dev->state_count)
256 : 0 : dev->state_count = drv->state_count;
257 : :
258 : 0 : ret = cpuidle_add_device_sysfs(dev);
259 [ # # ]: 0 : if (ret)
260 : : return ret;
261 : :
262 [ # # ][ # # ]: 0 : if (cpuidle_curr_governor->enable &&
263 : : (ret = cpuidle_curr_governor->enable(drv, dev)))
264 : : goto fail_sysfs;
265 : :
266 : 0 : smp_wmb();
267 : :
268 : 0 : dev->enabled = 1;
269 : :
270 : 0 : enabled_devices++;
271 : 0 : return 0;
272 : :
273 : : fail_sysfs:
274 : 0 : cpuidle_remove_device_sysfs(dev);
275 : :
276 : 0 : return ret;
277 : : }
278 : :
279 : : EXPORT_SYMBOL_GPL(cpuidle_enable_device);
280 : :
281 : : /**
282 : : * cpuidle_disable_device - disables idle PM for a CPU
283 : : * @dev: the CPU
284 : : *
285 : : * This function must be called between cpuidle_pause_and_lock and
286 : : * cpuidle_resume_and_unlock when used externally.
287 : : */
288 : 0 : void cpuidle_disable_device(struct cpuidle_device *dev)
289 : : {
290 : 0 : struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
291 : :
292 [ # # ][ # # ]: 0 : if (!dev || !dev->enabled)
293 : : return;
294 : :
295 [ # # ][ # # ]: 0 : if (!drv || !cpuidle_curr_governor)
296 : : return;
297 : :
298 : 0 : dev->enabled = 0;
299 : :
300 [ # # ]: 0 : if (cpuidle_curr_governor->disable)
301 : 0 : cpuidle_curr_governor->disable(drv, dev);
302 : :
303 : 0 : cpuidle_remove_device_sysfs(dev);
304 : 0 : enabled_devices--;
305 : : }
306 : :
307 : : EXPORT_SYMBOL_GPL(cpuidle_disable_device);
308 : :
309 : 0 : static void __cpuidle_unregister_device(struct cpuidle_device *dev)
310 : : {
311 : 0 : struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
312 : :
313 : : list_del(&dev->device_list);
314 : 0 : per_cpu(cpuidle_devices, dev->cpu) = NULL;
315 : 0 : module_put(drv->owner);
316 : 0 : }
317 : :
318 : : static void __cpuidle_device_init(struct cpuidle_device *dev)
319 : : {
320 : 0 : memset(dev->states_usage, 0, sizeof(dev->states_usage));
321 : 0 : dev->last_residency = 0;
322 : : }
323 : :
324 : : /**
325 : : * __cpuidle_register_device - internal register function called before register
326 : : * and enable routines
327 : : * @dev: the cpu
328 : : *
329 : : * cpuidle_lock mutex must be held before this is called
330 : : */
331 : 0 : static int __cpuidle_register_device(struct cpuidle_device *dev)
332 : : {
333 : : int ret;
334 : 0 : struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
335 : :
336 [ # # ]: 0 : if (!try_module_get(drv->owner))
337 : : return -EINVAL;
338 : :
339 : 0 : per_cpu(cpuidle_devices, dev->cpu) = dev;
340 : 0 : list_add(&dev->device_list, &cpuidle_detected_devices);
341 : :
342 : : ret = cpuidle_coupled_register_device(dev);
343 : : if (ret)
344 : : __cpuidle_unregister_device(dev);
345 : : else
346 : 0 : dev->registered = 1;
347 : :
348 : 0 : return ret;
349 : : }
350 : :
351 : : /**
352 : : * cpuidle_register_device - registers a CPU's idle PM feature
353 : : * @dev: the cpu
354 : : */
355 : 0 : int cpuidle_register_device(struct cpuidle_device *dev)
356 : : {
357 : : int ret = -EBUSY;
358 : :
359 [ # # ]: 0 : if (!dev)
360 : : return -EINVAL;
361 : :
362 : 0 : mutex_lock(&cpuidle_lock);
363 : :
364 [ # # ]: 0 : if (dev->registered)
365 : : goto out_unlock;
366 : :
367 : : __cpuidle_device_init(dev);
368 : :
369 : 0 : ret = __cpuidle_register_device(dev);
370 [ # # ]: 0 : if (ret)
371 : : goto out_unlock;
372 : :
373 : 0 : ret = cpuidle_add_sysfs(dev);
374 [ # # ]: 0 : if (ret)
375 : : goto out_unregister;
376 : :
377 : 0 : ret = cpuidle_enable_device(dev);
378 [ # # ]: 0 : if (ret)
379 : : goto out_sysfs;
380 : :
381 : : cpuidle_install_idle_handler();
382 : :
383 : : out_unlock:
384 : 0 : mutex_unlock(&cpuidle_lock);
385 : :
386 : 0 : return ret;
387 : :
388 : : out_sysfs:
389 : 0 : cpuidle_remove_sysfs(dev);
390 : : out_unregister:
391 : 0 : __cpuidle_unregister_device(dev);
392 : 0 : goto out_unlock;
393 : : }
394 : :
395 : : EXPORT_SYMBOL_GPL(cpuidle_register_device);
396 : :
397 : : /**
398 : : * cpuidle_unregister_device - unregisters a CPU's idle PM feature
399 : : * @dev: the cpu
400 : : */
401 : 0 : void cpuidle_unregister_device(struct cpuidle_device *dev)
402 : : {
403 [ # # ][ # # ]: 0 : if (!dev || dev->registered == 0)
404 : 0 : return;
405 : :
406 : 0 : cpuidle_pause_and_lock();
407 : :
408 : 0 : cpuidle_disable_device(dev);
409 : :
410 : 0 : cpuidle_remove_sysfs(dev);
411 : :
412 : 0 : __cpuidle_unregister_device(dev);
413 : :
414 : : cpuidle_coupled_unregister_device(dev);
415 : :
416 : 0 : cpuidle_resume_and_unlock();
417 : : }
418 : :
419 : : EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
420 : :
421 : : /**
422 : : * cpuidle_unregister: unregister a driver and the devices. This function
423 : : * can be used only if the driver has been previously registered through
424 : : * the cpuidle_register function.
425 : : *
426 : : * @drv: a valid pointer to a struct cpuidle_driver
427 : : */
428 : 0 : void cpuidle_unregister(struct cpuidle_driver *drv)
429 : : {
430 : : int cpu;
431 : : struct cpuidle_device *device;
432 : :
433 [ # # ]: 0 : for_each_cpu(cpu, drv->cpumask) {
434 : 0 : device = &per_cpu(cpuidle_dev, cpu);
435 : 0 : cpuidle_unregister_device(device);
436 : : }
437 : :
438 : 0 : cpuidle_unregister_driver(drv);
439 : 0 : }
440 : : EXPORT_SYMBOL_GPL(cpuidle_unregister);
441 : :
442 : : /**
443 : : * cpuidle_register: registers the driver and the cpu devices with the
444 : : * coupled_cpus passed as parameter. This function is used for all common
445 : : * initialization pattern there are in the arch specific drivers. The
446 : : * devices is globally defined in this file.
447 : : *
448 : : * @drv : a valid pointer to a struct cpuidle_driver
449 : : * @coupled_cpus: a cpumask for the coupled states
450 : : *
451 : : * Returns 0 on success, < 0 otherwise
452 : : */
453 : 0 : int cpuidle_register(struct cpuidle_driver *drv,
454 : : const struct cpumask *const coupled_cpus)
455 : : {
456 : : int ret, cpu;
457 : : struct cpuidle_device *device;
458 : :
459 : 0 : ret = cpuidle_register_driver(drv);
460 [ # # ]: 0 : if (ret) {
461 : 0 : pr_err("failed to register cpuidle driver\n");
462 : 0 : return ret;
463 : : }
464 : :
465 [ # # ]: 0 : for_each_cpu(cpu, drv->cpumask) {
466 : 0 : device = &per_cpu(cpuidle_dev, cpu);
467 : 0 : device->cpu = cpu;
468 : :
469 : : #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
470 : : /*
471 : : * On multiplatform for ARM, the coupled idle states could be
472 : : * enabled in the kernel even if the cpuidle driver does not
473 : : * use it. Note, coupled_cpus is a struct copy.
474 : : */
475 : : if (coupled_cpus)
476 : : device->coupled_cpus = *coupled_cpus;
477 : : #endif
478 : 0 : ret = cpuidle_register_device(device);
479 [ # # ]: 0 : if (!ret)
480 : 0 : continue;
481 : :
482 : 0 : pr_err("Failed to register cpuidle device for cpu%d\n", cpu);
483 : :
484 : 0 : cpuidle_unregister(drv);
485 : 0 : break;
486 : : }
487 : :
488 : 0 : return ret;
489 : : }
490 : : EXPORT_SYMBOL_GPL(cpuidle_register);
491 : :
492 : : #ifdef CONFIG_SMP
493 : :
494 : 0 : static void smp_callback(void *v)
495 : : {
496 : : /* we already woke the CPU up, nothing more to do */
497 : 0 : }
498 : :
499 : : /*
500 : : * This function gets called when a part of the kernel has a new latency
501 : : * requirement. This means we need to get all processors out of their C-state,
502 : : * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
503 : : * wakes them all right up.
504 : : */
505 : 0 : static int cpuidle_latency_notify(struct notifier_block *b,
506 : : unsigned long l, void *v)
507 : : {
508 : 0 : smp_call_function(smp_callback, NULL, 1);
509 : 0 : return NOTIFY_OK;
510 : : }
511 : :
512 : : static struct notifier_block cpuidle_latency_notifier = {
513 : : .notifier_call = cpuidle_latency_notify,
514 : : };
515 : :
516 : : static inline void latency_notifier_init(struct notifier_block *n)
517 : : {
518 : 0 : pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
519 : : }
520 : :
521 : : #else /* CONFIG_SMP */
522 : :
523 : : #define latency_notifier_init(x) do { } while (0)
524 : :
525 : : #endif /* CONFIG_SMP */
526 : :
527 : : /**
528 : : * cpuidle_init - core initializer
529 : : */
530 : 0 : static int __init cpuidle_init(void)
531 : : {
532 : : int ret;
533 : :
534 [ # # ]: 0 : if (cpuidle_disabled())
535 : : return -ENODEV;
536 : :
537 : 0 : ret = cpuidle_add_interface(cpu_subsys.dev_root);
538 [ # # ]: 0 : if (ret)
539 : : return ret;
540 : :
541 : : latency_notifier_init(&cpuidle_latency_notifier);
542 : :
543 : 0 : return 0;
544 : : }
545 : :
546 : : module_param(off, int, 0444);
547 : : core_initcall(cpuidle_init);
|