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