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