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