cpuidle: Remove CPUIDLE_FLAG_IGNORE and dev->prepare()
[platform/adaptation/renesas_rcar/renesas_kernel.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/kernel.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h>
14 #include <linux/notifier.h>
15 #include <linux/pm_qos_params.h>
16 #include <linux/cpu.h>
17 #include <linux/cpuidle.h>
18 #include <linux/ktime.h>
19 #include <linux/hrtimer.h>
20 #include <trace/events/power.h>
21
22 #include "cpuidle.h"
23
24 DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
25
26 DEFINE_MUTEX(cpuidle_lock);
27 LIST_HEAD(cpuidle_detected_devices);
28
29 static int enabled_devices;
30 static int off __read_mostly;
31 static int initialized __read_mostly;
32
33 int cpuidle_disabled(void)
34 {
35         return off;
36 }
37 void disable_cpuidle(void)
38 {
39         off = 1;
40 }
41
42 #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
43 static void cpuidle_kick_cpus(void)
44 {
45         cpu_idle_wait();
46 }
47 #elif defined(CONFIG_SMP)
48 # error "Arch needs cpu_idle_wait() equivalent here"
49 #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
50 static void cpuidle_kick_cpus(void) {}
51 #endif
52
53 static int __cpuidle_register_device(struct cpuidle_device *dev);
54
55 /**
56  * cpuidle_idle_call - the main idle loop
57  *
58  * NOTE: no locks or semaphores should be used here
59  * return non-zero on failure
60  */
61 int cpuidle_idle_call(void)
62 {
63         struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
64         struct cpuidle_state *target_state;
65         int next_state, entered_state;
66
67         if (off)
68                 return -ENODEV;
69
70         if (!initialized)
71                 return -ENODEV;
72
73         /* check if the device is ready */
74         if (!dev || !dev->enabled)
75                 return -EBUSY;
76
77 #if 0
78         /* shows regressions, re-enable for 2.6.29 */
79         /*
80          * run any timers that can be run now, at this point
81          * before calculating the idle duration etc.
82          */
83         hrtimer_peek_ahead_timers();
84 #endif
85
86         /* ask the governor for the next state */
87         next_state = cpuidle_curr_governor->select(dev);
88         if (need_resched()) {
89                 local_irq_enable();
90                 return 0;
91         }
92
93         target_state = &dev->states[next_state];
94
95         trace_power_start(POWER_CSTATE, next_state, dev->cpu);
96         trace_cpu_idle(next_state, dev->cpu);
97
98         entered_state = target_state->enter(dev, next_state);
99
100         trace_power_end(dev->cpu);
101         trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu);
102
103         if (entered_state >= 0) {
104                 /* Update cpuidle counters */
105                 /* This can be moved to within driver enter routine
106                  * but that results in multiple copies of same code.
107                  */
108                 dev->states[entered_state].time +=
109                                 (unsigned long long)dev->last_residency;
110                 dev->states[entered_state].usage++;
111         }
112
113         /* give the governor an opportunity to reflect on the outcome */
114         if (cpuidle_curr_governor->reflect)
115                 cpuidle_curr_governor->reflect(dev, entered_state);
116
117         return 0;
118 }
119
120 /**
121  * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
122  */
123 void cpuidle_install_idle_handler(void)
124 {
125         if (enabled_devices) {
126                 /* Make sure all changes finished before we switch to new idle */
127                 smp_wmb();
128                 initialized = 1;
129         }
130 }
131
132 /**
133  * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
134  */
135 void cpuidle_uninstall_idle_handler(void)
136 {
137         if (enabled_devices) {
138                 initialized = 0;
139                 cpuidle_kick_cpus();
140         }
141 }
142
143 /**
144  * cpuidle_pause_and_lock - temporarily disables CPUIDLE
145  */
146 void cpuidle_pause_and_lock(void)
147 {
148         mutex_lock(&cpuidle_lock);
149         cpuidle_uninstall_idle_handler();
150 }
151
152 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
153
154 /**
155  * cpuidle_resume_and_unlock - resumes CPUIDLE operation
156  */
157 void cpuidle_resume_and_unlock(void)
158 {
159         cpuidle_install_idle_handler();
160         mutex_unlock(&cpuidle_lock);
161 }
162
163 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
164
165 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
166 static int poll_idle(struct cpuidle_device *dev, int index)
167 {
168         ktime_t t1, t2;
169         s64 diff;
170
171         t1 = ktime_get();
172         local_irq_enable();
173         while (!need_resched())
174                 cpu_relax();
175
176         t2 = ktime_get();
177         diff = ktime_to_us(ktime_sub(t2, t1));
178         if (diff > INT_MAX)
179                 diff = INT_MAX;
180
181         dev->last_residency = (int) diff;
182
183         return index;
184 }
185
186 static void poll_idle_init(struct cpuidle_device *dev)
187 {
188         struct cpuidle_state *state = &dev->states[0];
189
190         cpuidle_set_statedata(state, NULL);
191
192         snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
193         snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
194         state->exit_latency = 0;
195         state->target_residency = 0;
196         state->power_usage = -1;
197         state->flags = 0;
198         state->enter = poll_idle;
199 }
200 #else
201 static void poll_idle_init(struct cpuidle_device *dev) {}
202 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
203
204 /**
205  * cpuidle_enable_device - enables idle PM for a CPU
206  * @dev: the CPU
207  *
208  * This function must be called between cpuidle_pause_and_lock and
209  * cpuidle_resume_and_unlock when used externally.
210  */
211 int cpuidle_enable_device(struct cpuidle_device *dev)
212 {
213         int ret, i;
214
215         if (dev->enabled)
216                 return 0;
217         if (!cpuidle_get_driver() || !cpuidle_curr_governor)
218                 return -EIO;
219         if (!dev->state_count)
220                 return -EINVAL;
221
222         if (dev->registered == 0) {
223                 ret = __cpuidle_register_device(dev);
224                 if (ret)
225                         return ret;
226         }
227
228         poll_idle_init(dev);
229
230         if ((ret = cpuidle_add_state_sysfs(dev)))
231                 return ret;
232
233         if (cpuidle_curr_governor->enable &&
234             (ret = cpuidle_curr_governor->enable(dev)))
235                 goto fail_sysfs;
236
237         for (i = 0; i < dev->state_count; i++) {
238                 dev->states[i].usage = 0;
239                 dev->states[i].time = 0;
240         }
241         dev->last_residency = 0;
242
243         smp_wmb();
244
245         dev->enabled = 1;
246
247         enabled_devices++;
248         return 0;
249
250 fail_sysfs:
251         cpuidle_remove_state_sysfs(dev);
252
253         return ret;
254 }
255
256 EXPORT_SYMBOL_GPL(cpuidle_enable_device);
257
258 /**
259  * cpuidle_disable_device - disables idle PM for a CPU
260  * @dev: the CPU
261  *
262  * This function must be called between cpuidle_pause_and_lock and
263  * cpuidle_resume_and_unlock when used externally.
264  */
265 void cpuidle_disable_device(struct cpuidle_device *dev)
266 {
267         if (!dev->enabled)
268                 return;
269         if (!cpuidle_get_driver() || !cpuidle_curr_governor)
270                 return;
271
272         dev->enabled = 0;
273
274         if (cpuidle_curr_governor->disable)
275                 cpuidle_curr_governor->disable(dev);
276
277         cpuidle_remove_state_sysfs(dev);
278         enabled_devices--;
279 }
280
281 EXPORT_SYMBOL_GPL(cpuidle_disable_device);
282
283 /**
284  * __cpuidle_register_device - internal register function called before register
285  * and enable routines
286  * @dev: the cpu
287  *
288  * cpuidle_lock mutex must be held before this is called
289  */
290 static int __cpuidle_register_device(struct cpuidle_device *dev)
291 {
292         int ret;
293         struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
294         struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
295
296         if (!sys_dev)
297                 return -EINVAL;
298         if (!try_module_get(cpuidle_driver->owner))
299                 return -EINVAL;
300
301         init_completion(&dev->kobj_unregister);
302
303         /*
304          * cpuidle driver should set the dev->power_specified bit
305          * before registering the device if the driver provides
306          * power_usage numbers.
307          *
308          * For those devices whose ->power_specified is not set,
309          * we fill in power_usage with decreasing values as the
310          * cpuidle code has an implicit assumption that state Cn
311          * uses less power than C(n-1).
312          *
313          * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
314          * an power value of -1.  So we use -2, -3, etc, for other
315          * c-states.
316          */
317         if (!dev->power_specified) {
318                 int i;
319                 for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
320                         dev->states[i].power_usage = -1 - i;
321         }
322
323         per_cpu(cpuidle_devices, dev->cpu) = dev;
324         list_add(&dev->device_list, &cpuidle_detected_devices);
325         if ((ret = cpuidle_add_sysfs(sys_dev))) {
326                 module_put(cpuidle_driver->owner);
327                 return ret;
328         }
329
330         dev->registered = 1;
331         return 0;
332 }
333
334 /**
335  * cpuidle_register_device - registers a CPU's idle PM feature
336  * @dev: the cpu
337  */
338 int cpuidle_register_device(struct cpuidle_device *dev)
339 {
340         int ret;
341
342         mutex_lock(&cpuidle_lock);
343
344         if ((ret = __cpuidle_register_device(dev))) {
345                 mutex_unlock(&cpuidle_lock);
346                 return ret;
347         }
348
349         cpuidle_enable_device(dev);
350         cpuidle_install_idle_handler();
351
352         mutex_unlock(&cpuidle_lock);
353
354         return 0;
355
356 }
357
358 EXPORT_SYMBOL_GPL(cpuidle_register_device);
359
360 /**
361  * cpuidle_unregister_device - unregisters a CPU's idle PM feature
362  * @dev: the cpu
363  */
364 void cpuidle_unregister_device(struct cpuidle_device *dev)
365 {
366         struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
367         struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
368
369         if (dev->registered == 0)
370                 return;
371
372         cpuidle_pause_and_lock();
373
374         cpuidle_disable_device(dev);
375
376         cpuidle_remove_sysfs(sys_dev);
377         list_del(&dev->device_list);
378         wait_for_completion(&dev->kobj_unregister);
379         per_cpu(cpuidle_devices, dev->cpu) = NULL;
380
381         cpuidle_resume_and_unlock();
382
383         module_put(cpuidle_driver->owner);
384 }
385
386 EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
387
388 #ifdef CONFIG_SMP
389
390 static void smp_callback(void *v)
391 {
392         /* we already woke the CPU up, nothing more to do */
393 }
394
395 /*
396  * This function gets called when a part of the kernel has a new latency
397  * requirement.  This means we need to get all processors out of their C-state,
398  * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
399  * wakes them all right up.
400  */
401 static int cpuidle_latency_notify(struct notifier_block *b,
402                 unsigned long l, void *v)
403 {
404         smp_call_function(smp_callback, NULL, 1);
405         return NOTIFY_OK;
406 }
407
408 static struct notifier_block cpuidle_latency_notifier = {
409         .notifier_call = cpuidle_latency_notify,
410 };
411
412 static inline void latency_notifier_init(struct notifier_block *n)
413 {
414         pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
415 }
416
417 #else /* CONFIG_SMP */
418
419 #define latency_notifier_init(x) do { } while (0)
420
421 #endif /* CONFIG_SMP */
422
423 /**
424  * cpuidle_init - core initializer
425  */
426 static int __init cpuidle_init(void)
427 {
428         int ret;
429
430         if (cpuidle_disabled())
431                 return -ENODEV;
432
433         ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
434         if (ret)
435                 return ret;
436
437         latency_notifier_init(&cpuidle_latency_notifier);
438
439         return 0;
440 }
441
442 module_param(off, int, 0444);
443 core_initcall(cpuidle_init);