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