Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / cpufreq / cpufreq.c
1 /*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <asm/cputime.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/notifier.h>
26 #include <linux/cpufreq.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/spinlock.h>
30 #include <linux/tick.h>
31 #include <linux/device.h>
32 #include <linux/slab.h>
33 #include <linux/cpu.h>
34 #include <linux/completion.h>
35 #include <linux/mutex.h>
36 #include <linux/syscore_ops.h>
37
38 #include <trace/events/power.h>
39
40 /**
41 * The "cpufreq driver" - the arch- or hardware-dependent low
42 * level driver of CPUFreq support, and its spinlock. This lock
43 * also protects the cpufreq_cpu_data array.
44 */
45 static struct cpufreq_driver *cpufreq_driver;
46 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
47 #ifdef CONFIG_HOTPLUG_CPU
48 /* This one keeps track of the previously set governor of a removed CPU */
49 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
50 #endif
51 static DEFINE_RWLOCK(cpufreq_driver_lock);
52 static DEFINE_MUTEX(cpufreq_governor_lock);
53
54 /*
55 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
56 * all cpufreq/hotplug/workqueue/etc related lock issues.
57 *
58 * The rules for this semaphore:
59 * - Any routine that wants to read from the policy structure will
60 * do a down_read on this semaphore.
61 * - Any routine that will write to the policy structure and/or may take away
62 * the policy altogether (eg. CPU hotplug), will hold this lock in write
63 * mode before doing so.
64 *
65 * Additional rules:
66 * - Governor routines that can be called in cpufreq hotplug path should not
67 * take this sem as top level hotplug notifier handler takes this.
68 * - Lock should not be held across
69 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
70 */
71 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
72 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
73
74 #define lock_policy_rwsem(mode, cpu) \
75 static int lock_policy_rwsem_##mode(int cpu) \
76 { \
77 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
78 BUG_ON(policy_cpu == -1); \
79 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
80 \
81 return 0; \
82 }
83
84 lock_policy_rwsem(read, cpu);
85 lock_policy_rwsem(write, cpu);
86
87 #define unlock_policy_rwsem(mode, cpu) \
88 static void unlock_policy_rwsem_##mode(int cpu) \
89 { \
90 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
91 BUG_ON(policy_cpu == -1); \
92 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
93 }
94
95 unlock_policy_rwsem(read, cpu);
96 unlock_policy_rwsem(write, cpu);
97
98 /* internal prototypes */
99 static int __cpufreq_governor(struct cpufreq_policy *policy,
100 unsigned int event);
101 static unsigned int __cpufreq_get(unsigned int cpu);
102 static void handle_update(struct work_struct *work);
103
104 /**
105 * Two notifier lists: the "policy" list is involved in the
106 * validation process for a new CPU frequency policy; the
107 * "transition" list for kernel code that needs to handle
108 * changes to devices when the CPU clock speed changes.
109 * The mutex locks both lists.
110 */
111 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
112 static struct srcu_notifier_head cpufreq_transition_notifier_list;
113
114 static bool init_cpufreq_transition_notifier_list_called;
115 static int __init init_cpufreq_transition_notifier_list(void)
116 {
117 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
118 init_cpufreq_transition_notifier_list_called = true;
119 return 0;
120 }
121 pure_initcall(init_cpufreq_transition_notifier_list);
122
123 static int off __read_mostly;
124 static int cpufreq_disabled(void)
125 {
126 return off;
127 }
128 void disable_cpufreq(void)
129 {
130 off = 1;
131 }
132 static LIST_HEAD(cpufreq_governor_list);
133 static DEFINE_MUTEX(cpufreq_governor_mutex);
134
135 bool have_governor_per_policy(void)
136 {
137 return cpufreq_driver->have_governor_per_policy;
138 }
139 EXPORT_SYMBOL_GPL(have_governor_per_policy);
140
141 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
142 {
143 if (have_governor_per_policy())
144 return &policy->kobj;
145 else
146 return cpufreq_global_kobject;
147 }
148 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
149
150 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
151 {
152 u64 idle_time;
153 u64 cur_wall_time;
154 u64 busy_time;
155
156 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
157
158 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
159 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
164
165 idle_time = cur_wall_time - busy_time;
166 if (wall)
167 *wall = cputime_to_usecs(cur_wall_time);
168
169 return cputime_to_usecs(idle_time);
170 }
171
172 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
173 {
174 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
175
176 if (idle_time == -1ULL)
177 return get_cpu_idle_time_jiffy(cpu, wall);
178 else if (!io_busy)
179 idle_time += get_cpu_iowait_time_us(cpu, wall);
180
181 return idle_time;
182 }
183 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
184
185 static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
186 {
187 struct cpufreq_policy *data;
188 unsigned long flags;
189
190 if (cpu >= nr_cpu_ids)
191 goto err_out;
192
193 /* get the cpufreq driver */
194 read_lock_irqsave(&cpufreq_driver_lock, flags);
195
196 if (!cpufreq_driver)
197 goto err_out_unlock;
198
199 if (!try_module_get(cpufreq_driver->owner))
200 goto err_out_unlock;
201
202
203 /* get the CPU */
204 data = per_cpu(cpufreq_cpu_data, cpu);
205
206 if (!data)
207 goto err_out_put_module;
208
209 if (!sysfs && !kobject_get(&data->kobj))
210 goto err_out_put_module;
211
212 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
213 return data;
214
215 err_out_put_module:
216 module_put(cpufreq_driver->owner);
217 err_out_unlock:
218 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
219 err_out:
220 return NULL;
221 }
222
223 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
224 {
225 if (cpufreq_disabled())
226 return NULL;
227
228 return __cpufreq_cpu_get(cpu, false);
229 }
230 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
231
232 static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
233 {
234 return __cpufreq_cpu_get(cpu, true);
235 }
236
237 static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
238 {
239 if (!sysfs)
240 kobject_put(&data->kobj);
241 module_put(cpufreq_driver->owner);
242 }
243
244 void cpufreq_cpu_put(struct cpufreq_policy *data)
245 {
246 if (cpufreq_disabled())
247 return;
248
249 __cpufreq_cpu_put(data, false);
250 }
251 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
252
253 static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
254 {
255 __cpufreq_cpu_put(data, true);
256 }
257
258 /*********************************************************************
259 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
260 *********************************************************************/
261
262 /**
263 * adjust_jiffies - adjust the system "loops_per_jiffy"
264 *
265 * This function alters the system "loops_per_jiffy" for the clock
266 * speed change. Note that loops_per_jiffy cannot be updated on SMP
267 * systems as each CPU might be scaled differently. So, use the arch
268 * per-CPU loops_per_jiffy value wherever possible.
269 */
270 #ifndef CONFIG_SMP
271 static unsigned long l_p_j_ref;
272 static unsigned int l_p_j_ref_freq;
273
274 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
275 {
276 if (ci->flags & CPUFREQ_CONST_LOOPS)
277 return;
278
279 if (!l_p_j_ref_freq) {
280 l_p_j_ref = loops_per_jiffy;
281 l_p_j_ref_freq = ci->old;
282 pr_debug("saving %lu as reference value for loops_per_jiffy; "
283 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
284 }
285 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
286 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
287 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
288 ci->new);
289 pr_debug("scaling loops_per_jiffy to %lu "
290 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
291 }
292 }
293 #else
294 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
295 {
296 return;
297 }
298 #endif
299
300
301 void __cpufreq_notify_transition(struct cpufreq_policy *policy,
302 struct cpufreq_freqs *freqs, unsigned int state)
303 {
304 BUG_ON(irqs_disabled());
305
306 if (cpufreq_disabled())
307 return;
308
309 freqs->flags = cpufreq_driver->flags;
310 pr_debug("notification %u of frequency transition to %u kHz\n",
311 state, freqs->new);
312
313 switch (state) {
314
315 case CPUFREQ_PRECHANGE:
316 /* detect if the driver reported a value as "old frequency"
317 * which is not equal to what the cpufreq core thinks is
318 * "old frequency".
319 */
320 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
321 if ((policy) && (policy->cpu == freqs->cpu) &&
322 (policy->cur) && (policy->cur != freqs->old)) {
323 pr_debug("Warning: CPU frequency is"
324 " %u, cpufreq assumed %u kHz.\n",
325 freqs->old, policy->cur);
326 freqs->old = policy->cur;
327 }
328 }
329 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
330 CPUFREQ_PRECHANGE, freqs);
331 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
332 break;
333
334 case CPUFREQ_POSTCHANGE:
335 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
336 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
337 (unsigned long)freqs->cpu);
338 trace_cpu_frequency(freqs->new, freqs->cpu);
339 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
340 CPUFREQ_POSTCHANGE, freqs);
341 if (likely(policy) && likely(policy->cpu == freqs->cpu))
342 policy->cur = freqs->new;
343 break;
344 }
345 }
346 /**
347 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
348 * on frequency transition.
349 *
350 * This function calls the transition notifiers and the "adjust_jiffies"
351 * function. It is called twice on all CPU frequency changes that have
352 * external effects.
353 */
354 void cpufreq_notify_transition(struct cpufreq_policy *policy,
355 struct cpufreq_freqs *freqs, unsigned int state)
356 {
357 for_each_cpu(freqs->cpu, policy->cpus)
358 __cpufreq_notify_transition(policy, freqs, state);
359 }
360 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
361
362
363
364 /*********************************************************************
365 * SYSFS INTERFACE *
366 *********************************************************************/
367
368 static struct cpufreq_governor *__find_governor(const char *str_governor)
369 {
370 struct cpufreq_governor *t;
371
372 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
373 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
374 return t;
375
376 return NULL;
377 }
378
379 /**
380 * cpufreq_parse_governor - parse a governor string
381 */
382 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
383 struct cpufreq_governor **governor)
384 {
385 int err = -EINVAL;
386
387 if (!cpufreq_driver)
388 goto out;
389
390 if (cpufreq_driver->setpolicy) {
391 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
392 *policy = CPUFREQ_POLICY_PERFORMANCE;
393 err = 0;
394 } else if (!strnicmp(str_governor, "powersave",
395 CPUFREQ_NAME_LEN)) {
396 *policy = CPUFREQ_POLICY_POWERSAVE;
397 err = 0;
398 }
399 } else if (cpufreq_driver->target) {
400 struct cpufreq_governor *t;
401
402 mutex_lock(&cpufreq_governor_mutex);
403
404 t = __find_governor(str_governor);
405
406 if (t == NULL) {
407 int ret;
408
409 mutex_unlock(&cpufreq_governor_mutex);
410 ret = request_module("cpufreq_%s", str_governor);
411 mutex_lock(&cpufreq_governor_mutex);
412
413 if (ret == 0)
414 t = __find_governor(str_governor);
415 }
416
417 if (t != NULL) {
418 *governor = t;
419 err = 0;
420 }
421
422 mutex_unlock(&cpufreq_governor_mutex);
423 }
424 out:
425 return err;
426 }
427
428
429 /**
430 * cpufreq_per_cpu_attr_read() / show_##file_name() -
431 * print out cpufreq information
432 *
433 * Write out information from cpufreq_driver->policy[cpu]; object must be
434 * "unsigned int".
435 */
436
437 #define show_one(file_name, object) \
438 static ssize_t show_##file_name \
439 (struct cpufreq_policy *policy, char *buf) \
440 { \
441 return sprintf(buf, "%u\n", policy->object); \
442 }
443
444 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
445 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
446 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
447 show_one(scaling_min_freq, min);
448 show_one(scaling_max_freq, max);
449 show_one(scaling_cur_freq, cur);
450
451 static int __cpufreq_set_policy(struct cpufreq_policy *data,
452 struct cpufreq_policy *policy);
453
454 /**
455 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
456 */
457 #define store_one(file_name, object) \
458 static ssize_t store_##file_name \
459 (struct cpufreq_policy *policy, const char *buf, size_t count) \
460 { \
461 unsigned int ret; \
462 struct cpufreq_policy new_policy; \
463 \
464 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
465 if (ret) \
466 return -EINVAL; \
467 \
468 ret = sscanf(buf, "%u", &new_policy.object); \
469 if (ret != 1) \
470 return -EINVAL; \
471 \
472 ret = __cpufreq_set_policy(policy, &new_policy); \
473 policy->user_policy.object = policy->object; \
474 \
475 return ret ? ret : count; \
476 }
477
478 store_one(scaling_min_freq, min);
479 store_one(scaling_max_freq, max);
480
481 /**
482 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
483 */
484 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
485 char *buf)
486 {
487 unsigned int cur_freq = __cpufreq_get(policy->cpu);
488
489 if (cur_freq)
490 return sprintf(buf, "%u\n", cur_freq);
491
492 return sprintf(buf, "<unknown>\n");
493 }
494
495
496 /**
497 * show_scaling_governor - show the current policy for the specified CPU
498 */
499 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
500 {
501 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
502 return sprintf(buf, "powersave\n");
503 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
504 return sprintf(buf, "performance\n");
505 else if (policy->governor)
506 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
507 policy->governor->name);
508 return -EINVAL;
509 }
510
511
512 /**
513 * store_scaling_governor - store policy for the specified CPU
514 */
515 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
516 const char *buf, size_t count)
517 {
518 unsigned int ret;
519 char str_governor[16];
520 struct cpufreq_policy new_policy;
521
522 ret = cpufreq_get_policy(&new_policy, policy->cpu);
523 if (ret)
524 return ret;
525
526 ret = sscanf(buf, "%15s", str_governor);
527 if (ret != 1)
528 return -EINVAL;
529
530 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
531 &new_policy.governor))
532 return -EINVAL;
533
534 /* Do not use cpufreq_set_policy here or the user_policy.max
535 will be wrongly overridden */
536 ret = __cpufreq_set_policy(policy, &new_policy);
537
538 policy->user_policy.policy = policy->policy;
539 policy->user_policy.governor = policy->governor;
540
541 if (ret)
542 return ret;
543 else
544 return count;
545 }
546
547 /**
548 * show_scaling_driver - show the cpufreq driver currently loaded
549 */
550 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
551 {
552 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
553 }
554
555 /**
556 * show_scaling_available_governors - show the available CPUfreq governors
557 */
558 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
559 char *buf)
560 {
561 ssize_t i = 0;
562 struct cpufreq_governor *t;
563
564 if (!cpufreq_driver->target) {
565 i += sprintf(buf, "performance powersave");
566 goto out;
567 }
568
569 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
570 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
571 - (CPUFREQ_NAME_LEN + 2)))
572 goto out;
573 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
574 }
575 out:
576 i += sprintf(&buf[i], "\n");
577 return i;
578 }
579
580 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
581 {
582 ssize_t i = 0;
583 unsigned int cpu;
584
585 for_each_cpu(cpu, mask) {
586 if (i)
587 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
588 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
589 if (i >= (PAGE_SIZE - 5))
590 break;
591 }
592 i += sprintf(&buf[i], "\n");
593 return i;
594 }
595
596 /**
597 * show_related_cpus - show the CPUs affected by each transition even if
598 * hw coordination is in use
599 */
600 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
601 {
602 return show_cpus(policy->related_cpus, buf);
603 }
604
605 /**
606 * show_affected_cpus - show the CPUs affected by each transition
607 */
608 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
609 {
610 return show_cpus(policy->cpus, buf);
611 }
612
613 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
614 const char *buf, size_t count)
615 {
616 unsigned int freq = 0;
617 unsigned int ret;
618
619 if (!policy->governor || !policy->governor->store_setspeed)
620 return -EINVAL;
621
622 ret = sscanf(buf, "%u", &freq);
623 if (ret != 1)
624 return -EINVAL;
625
626 policy->governor->store_setspeed(policy, freq);
627
628 return count;
629 }
630
631 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
632 {
633 if (!policy->governor || !policy->governor->show_setspeed)
634 return sprintf(buf, "<unsupported>\n");
635
636 return policy->governor->show_setspeed(policy, buf);
637 }
638
639 /**
640 * show_bios_limit - show the current cpufreq HW/BIOS limitation
641 */
642 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
643 {
644 unsigned int limit;
645 int ret;
646 if (cpufreq_driver->bios_limit) {
647 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
648 if (!ret)
649 return sprintf(buf, "%u\n", limit);
650 }
651 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
652 }
653
654 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
655 cpufreq_freq_attr_ro(cpuinfo_min_freq);
656 cpufreq_freq_attr_ro(cpuinfo_max_freq);
657 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
658 cpufreq_freq_attr_ro(scaling_available_governors);
659 cpufreq_freq_attr_ro(scaling_driver);
660 cpufreq_freq_attr_ro(scaling_cur_freq);
661 cpufreq_freq_attr_ro(bios_limit);
662 cpufreq_freq_attr_ro(related_cpus);
663 cpufreq_freq_attr_ro(affected_cpus);
664 cpufreq_freq_attr_rw(scaling_min_freq);
665 cpufreq_freq_attr_rw(scaling_max_freq);
666 cpufreq_freq_attr_rw(scaling_governor);
667 cpufreq_freq_attr_rw(scaling_setspeed);
668
669 static struct attribute *default_attrs[] = {
670 &cpuinfo_min_freq.attr,
671 &cpuinfo_max_freq.attr,
672 &cpuinfo_transition_latency.attr,
673 &scaling_min_freq.attr,
674 &scaling_max_freq.attr,
675 &affected_cpus.attr,
676 &related_cpus.attr,
677 &scaling_governor.attr,
678 &scaling_driver.attr,
679 &scaling_available_governors.attr,
680 &scaling_setspeed.attr,
681 NULL
682 };
683
684 struct kobject *cpufreq_global_kobject;
685 EXPORT_SYMBOL(cpufreq_global_kobject);
686
687 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
688 #define to_attr(a) container_of(a, struct freq_attr, attr)
689
690 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
691 {
692 struct cpufreq_policy *policy = to_policy(kobj);
693 struct freq_attr *fattr = to_attr(attr);
694 ssize_t ret = -EINVAL;
695 policy = cpufreq_cpu_get_sysfs(policy->cpu);
696 if (!policy)
697 goto no_policy;
698
699 if (lock_policy_rwsem_read(policy->cpu) < 0)
700 goto fail;
701
702 if (fattr->show)
703 ret = fattr->show(policy, buf);
704 else
705 ret = -EIO;
706
707 unlock_policy_rwsem_read(policy->cpu);
708 fail:
709 cpufreq_cpu_put_sysfs(policy);
710 no_policy:
711 return ret;
712 }
713
714 static ssize_t store(struct kobject *kobj, struct attribute *attr,
715 const char *buf, size_t count)
716 {
717 struct cpufreq_policy *policy = to_policy(kobj);
718 struct freq_attr *fattr = to_attr(attr);
719 ssize_t ret = -EINVAL;
720 policy = cpufreq_cpu_get_sysfs(policy->cpu);
721 if (!policy)
722 goto no_policy;
723
724 if (lock_policy_rwsem_write(policy->cpu) < 0)
725 goto fail;
726
727 if (fattr->store)
728 ret = fattr->store(policy, buf, count);
729 else
730 ret = -EIO;
731
732 unlock_policy_rwsem_write(policy->cpu);
733 fail:
734 cpufreq_cpu_put_sysfs(policy);
735 no_policy:
736 return ret;
737 }
738
739 static void cpufreq_sysfs_release(struct kobject *kobj)
740 {
741 struct cpufreq_policy *policy = to_policy(kobj);
742 pr_debug("last reference is dropped\n");
743 complete(&policy->kobj_unregister);
744 }
745
746 static const struct sysfs_ops sysfs_ops = {
747 .show = show,
748 .store = store,
749 };
750
751 static struct kobj_type ktype_cpufreq = {
752 .sysfs_ops = &sysfs_ops,
753 .default_attrs = default_attrs,
754 .release = cpufreq_sysfs_release,
755 };
756
757 /* symlink affected CPUs */
758 static int cpufreq_add_dev_symlink(unsigned int cpu,
759 struct cpufreq_policy *policy)
760 {
761 unsigned int j;
762 int ret = 0;
763
764 for_each_cpu(j, policy->cpus) {
765 struct cpufreq_policy *managed_policy;
766 struct device *cpu_dev;
767
768 if (j == cpu)
769 continue;
770
771 pr_debug("CPU %u already managed, adding link\n", j);
772 managed_policy = cpufreq_cpu_get(cpu);
773 cpu_dev = get_cpu_device(j);
774 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
775 "cpufreq");
776 if (ret) {
777 cpufreq_cpu_put(managed_policy);
778 return ret;
779 }
780 }
781 return ret;
782 }
783
784 static int cpufreq_add_dev_interface(unsigned int cpu,
785 struct cpufreq_policy *policy,
786 struct device *dev)
787 {
788 struct cpufreq_policy new_policy;
789 struct freq_attr **drv_attr;
790 unsigned long flags;
791 int ret = 0;
792 unsigned int j;
793
794 /* prepare interface data */
795 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
796 &dev->kobj, "cpufreq");
797 if (ret)
798 return ret;
799
800 /* set up files for this cpu device */
801 drv_attr = cpufreq_driver->attr;
802 while ((drv_attr) && (*drv_attr)) {
803 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
804 if (ret)
805 goto err_out_kobj_put;
806 drv_attr++;
807 }
808 if (cpufreq_driver->get) {
809 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
810 if (ret)
811 goto err_out_kobj_put;
812 }
813 if (cpufreq_driver->target) {
814 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
815 if (ret)
816 goto err_out_kobj_put;
817 }
818 if (cpufreq_driver->bios_limit) {
819 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
820 if (ret)
821 goto err_out_kobj_put;
822 }
823
824 write_lock_irqsave(&cpufreq_driver_lock, flags);
825 for_each_cpu(j, policy->cpus) {
826 per_cpu(cpufreq_cpu_data, j) = policy;
827 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
828 }
829 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
830
831 ret = cpufreq_add_dev_symlink(cpu, policy);
832 if (ret)
833 goto err_out_kobj_put;
834
835 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
836 /* assure that the starting sequence is run in __cpufreq_set_policy */
837 policy->governor = NULL;
838
839 /* set default policy */
840 ret = __cpufreq_set_policy(policy, &new_policy);
841 policy->user_policy.policy = policy->policy;
842 policy->user_policy.governor = policy->governor;
843
844 if (ret) {
845 pr_debug("setting policy failed\n");
846 if (cpufreq_driver->exit)
847 cpufreq_driver->exit(policy);
848 }
849 return ret;
850
851 err_out_kobj_put:
852 kobject_put(&policy->kobj);
853 wait_for_completion(&policy->kobj_unregister);
854 return ret;
855 }
856
857 #ifdef CONFIG_HOTPLUG_CPU
858 static int cpufreq_add_policy_cpu(unsigned int cpu, unsigned int sibling,
859 struct device *dev)
860 {
861 struct cpufreq_policy *policy;
862 int ret = 0, has_target = !!cpufreq_driver->target;
863 unsigned long flags;
864
865 policy = cpufreq_cpu_get(sibling);
866 WARN_ON(!policy);
867
868 if (has_target)
869 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
870
871 lock_policy_rwsem_write(sibling);
872
873 write_lock_irqsave(&cpufreq_driver_lock, flags);
874
875 cpumask_set_cpu(cpu, policy->cpus);
876 per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
877 per_cpu(cpufreq_cpu_data, cpu) = policy;
878 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
879
880 unlock_policy_rwsem_write(sibling);
881
882 if (has_target) {
883 __cpufreq_governor(policy, CPUFREQ_GOV_START);
884 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
885 }
886
887 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
888 if (ret) {
889 cpufreq_cpu_put(policy);
890 return ret;
891 }
892
893 return 0;
894 }
895 #endif
896
897 /**
898 * cpufreq_add_dev - add a CPU device
899 *
900 * Adds the cpufreq interface for a CPU device.
901 *
902 * The Oracle says: try running cpufreq registration/unregistration concurrently
903 * with with cpu hotplugging and all hell will break loose. Tried to clean this
904 * mess up, but more thorough testing is needed. - Mathieu
905 */
906 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
907 {
908 unsigned int j, cpu = dev->id;
909 int ret = -ENOMEM;
910 struct cpufreq_policy *policy;
911 unsigned long flags;
912 #ifdef CONFIG_HOTPLUG_CPU
913 struct cpufreq_governor *gov;
914 int sibling;
915 #endif
916
917 if (cpu_is_offline(cpu))
918 return 0;
919
920 pr_debug("adding CPU %u\n", cpu);
921
922 #ifdef CONFIG_SMP
923 /* check whether a different CPU already registered this
924 * CPU because it is in the same boat. */
925 policy = cpufreq_cpu_get(cpu);
926 if (unlikely(policy)) {
927 cpufreq_cpu_put(policy);
928 return 0;
929 }
930
931 #ifdef CONFIG_HOTPLUG_CPU
932 /* Check if this cpu was hot-unplugged earlier and has siblings */
933 read_lock_irqsave(&cpufreq_driver_lock, flags);
934 for_each_online_cpu(sibling) {
935 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
936 if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
937 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
938 return cpufreq_add_policy_cpu(cpu, sibling, dev);
939 }
940 }
941 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
942 #endif
943 #endif
944
945 if (!try_module_get(cpufreq_driver->owner)) {
946 ret = -EINVAL;
947 goto module_out;
948 }
949
950 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
951 if (!policy)
952 goto nomem_out;
953
954 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
955 goto err_free_policy;
956
957 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
958 goto err_free_cpumask;
959
960 policy->cpu = cpu;
961 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
962 cpumask_copy(policy->cpus, cpumask_of(cpu));
963
964 /* Initially set CPU itself as the policy_cpu */
965 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
966
967 init_completion(&policy->kobj_unregister);
968 INIT_WORK(&policy->update, handle_update);
969
970 /* call driver. From then on the cpufreq must be able
971 * to accept all calls to ->verify and ->setpolicy for this CPU
972 */
973 ret = cpufreq_driver->init(policy);
974 if (ret) {
975 pr_debug("initialization failed\n");
976 goto err_set_policy_cpu;
977 }
978
979 /* related cpus should atleast have policy->cpus */
980 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
981
982 /*
983 * affected cpus must always be the one, which are online. We aren't
984 * managing offline cpus here.
985 */
986 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
987
988 policy->user_policy.min = policy->min;
989 policy->user_policy.max = policy->max;
990
991 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
992 CPUFREQ_START, policy);
993
994 #ifdef CONFIG_HOTPLUG_CPU
995 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
996 if (gov) {
997 policy->governor = gov;
998 pr_debug("Restoring governor %s for cpu %d\n",
999 policy->governor->name, cpu);
1000 }
1001 #endif
1002
1003 ret = cpufreq_add_dev_interface(cpu, policy, dev);
1004 if (ret)
1005 goto err_out_unregister;
1006
1007 kobject_uevent(&policy->kobj, KOBJ_ADD);
1008 module_put(cpufreq_driver->owner);
1009 pr_debug("initialization complete\n");
1010
1011 return 0;
1012
1013 err_out_unregister:
1014 write_lock_irqsave(&cpufreq_driver_lock, flags);
1015 for_each_cpu(j, policy->cpus)
1016 per_cpu(cpufreq_cpu_data, j) = NULL;
1017 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1018
1019 kobject_put(&policy->kobj);
1020 wait_for_completion(&policy->kobj_unregister);
1021
1022 err_set_policy_cpu:
1023 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1024 free_cpumask_var(policy->related_cpus);
1025 err_free_cpumask:
1026 free_cpumask_var(policy->cpus);
1027 err_free_policy:
1028 kfree(policy);
1029 nomem_out:
1030 module_put(cpufreq_driver->owner);
1031 module_out:
1032 return ret;
1033 }
1034
1035 static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1036 {
1037 int j;
1038
1039 policy->last_cpu = policy->cpu;
1040 policy->cpu = cpu;
1041
1042 for_each_cpu(j, policy->cpus)
1043 per_cpu(cpufreq_policy_cpu, j) = cpu;
1044
1045 #ifdef CONFIG_CPU_FREQ_TABLE
1046 cpufreq_frequency_table_update_policy_cpu(policy);
1047 #endif
1048 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1049 CPUFREQ_UPDATE_POLICY_CPU, policy);
1050 }
1051
1052 /**
1053 * __cpufreq_remove_dev - remove a CPU device
1054 *
1055 * Removes the cpufreq interface for a CPU device.
1056 * Caller should already have policy_rwsem in write mode for this CPU.
1057 * This routine frees the rwsem before returning.
1058 */
1059 static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1060 {
1061 unsigned int cpu = dev->id, ret, cpus;
1062 unsigned long flags;
1063 struct cpufreq_policy *data;
1064 struct kobject *kobj;
1065 struct completion *cmp;
1066 struct device *cpu_dev;
1067
1068 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1069
1070 write_lock_irqsave(&cpufreq_driver_lock, flags);
1071
1072 data = per_cpu(cpufreq_cpu_data, cpu);
1073 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1074
1075 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1076
1077 if (!data) {
1078 pr_debug("%s: No cpu_data found\n", __func__);
1079 return -EINVAL;
1080 }
1081
1082 if (cpufreq_driver->target)
1083 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1084
1085 #ifdef CONFIG_HOTPLUG_CPU
1086 if (!cpufreq_driver->setpolicy)
1087 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1088 data->governor->name, CPUFREQ_NAME_LEN);
1089 #endif
1090
1091 WARN_ON(lock_policy_rwsem_write(cpu));
1092 cpus = cpumask_weight(data->cpus);
1093
1094 if (cpus > 1)
1095 cpumask_clear_cpu(cpu, data->cpus);
1096 unlock_policy_rwsem_write(cpu);
1097
1098 if (cpu != data->cpu) {
1099 sysfs_remove_link(&dev->kobj, "cpufreq");
1100 } else if (cpus > 1) {
1101 /* first sibling now owns the new sysfs dir */
1102 cpu_dev = get_cpu_device(cpumask_first(data->cpus));
1103 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1104 ret = kobject_move(&data->kobj, &cpu_dev->kobj);
1105 if (ret) {
1106 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1107
1108 WARN_ON(lock_policy_rwsem_write(cpu));
1109 cpumask_set_cpu(cpu, data->cpus);
1110
1111 write_lock_irqsave(&cpufreq_driver_lock, flags);
1112 per_cpu(cpufreq_cpu_data, cpu) = data;
1113 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1114
1115 unlock_policy_rwsem_write(cpu);
1116
1117 ret = sysfs_create_link(&cpu_dev->kobj, &data->kobj,
1118 "cpufreq");
1119 return -EINVAL;
1120 }
1121
1122 WARN_ON(lock_policy_rwsem_write(cpu));
1123 update_policy_cpu(data, cpu_dev->id);
1124 unlock_policy_rwsem_write(cpu);
1125 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1126 __func__, cpu_dev->id, cpu);
1127 }
1128
1129 /* If cpu is last user of policy, free policy */
1130 if (cpus == 1) {
1131 if (cpufreq_driver->target)
1132 __cpufreq_governor(data, CPUFREQ_GOV_POLICY_EXIT);
1133
1134 lock_policy_rwsem_read(cpu);
1135 kobj = &data->kobj;
1136 cmp = &data->kobj_unregister;
1137 unlock_policy_rwsem_read(cpu);
1138 kobject_put(kobj);
1139
1140 /* we need to make sure that the underlying kobj is actually
1141 * not referenced anymore by anybody before we proceed with
1142 * unloading.
1143 */
1144 pr_debug("waiting for dropping of refcount\n");
1145 wait_for_completion(cmp);
1146 pr_debug("wait complete\n");
1147
1148 if (cpufreq_driver->exit)
1149 cpufreq_driver->exit(data);
1150
1151 free_cpumask_var(data->related_cpus);
1152 free_cpumask_var(data->cpus);
1153 kfree(data);
1154 } else {
1155 pr_debug("%s: removing link, cpu: %d\n", __func__, cpu);
1156 cpufreq_cpu_put(data);
1157 if (cpufreq_driver->target) {
1158 __cpufreq_governor(data, CPUFREQ_GOV_START);
1159 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1160 }
1161 }
1162
1163 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1164 return 0;
1165 }
1166
1167
1168 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1169 {
1170 unsigned int cpu = dev->id;
1171 int retval;
1172
1173 if (cpu_is_offline(cpu))
1174 return 0;
1175
1176 retval = __cpufreq_remove_dev(dev, sif);
1177 return retval;
1178 }
1179
1180
1181 static void handle_update(struct work_struct *work)
1182 {
1183 struct cpufreq_policy *policy =
1184 container_of(work, struct cpufreq_policy, update);
1185 unsigned int cpu = policy->cpu;
1186 pr_debug("handle_update for cpu %u called\n", cpu);
1187 cpufreq_update_policy(cpu);
1188 }
1189
1190 /**
1191 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1192 * @cpu: cpu number
1193 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1194 * @new_freq: CPU frequency the CPU actually runs at
1195 *
1196 * We adjust to current frequency first, and need to clean up later.
1197 * So either call to cpufreq_update_policy() or schedule handle_update()).
1198 */
1199 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1200 unsigned int new_freq)
1201 {
1202 struct cpufreq_policy *policy;
1203 struct cpufreq_freqs freqs;
1204 unsigned long flags;
1205
1206
1207 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1208 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1209
1210 freqs.old = old_freq;
1211 freqs.new = new_freq;
1212
1213 read_lock_irqsave(&cpufreq_driver_lock, flags);
1214 policy = per_cpu(cpufreq_cpu_data, cpu);
1215 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1216
1217 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1218 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1219 }
1220
1221
1222 /**
1223 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1224 * @cpu: CPU number
1225 *
1226 * This is the last known freq, without actually getting it from the driver.
1227 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1228 */
1229 unsigned int cpufreq_quick_get(unsigned int cpu)
1230 {
1231 struct cpufreq_policy *policy;
1232 unsigned int ret_freq = 0;
1233
1234 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1235 return cpufreq_driver->get(cpu);
1236
1237 policy = cpufreq_cpu_get(cpu);
1238 if (policy) {
1239 ret_freq = policy->cur;
1240 cpufreq_cpu_put(policy);
1241 }
1242
1243 return ret_freq;
1244 }
1245 EXPORT_SYMBOL(cpufreq_quick_get);
1246
1247 /**
1248 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1249 * @cpu: CPU number
1250 *
1251 * Just return the max possible frequency for a given CPU.
1252 */
1253 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1254 {
1255 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1256 unsigned int ret_freq = 0;
1257
1258 if (policy) {
1259 ret_freq = policy->max;
1260 cpufreq_cpu_put(policy);
1261 }
1262
1263 return ret_freq;
1264 }
1265 EXPORT_SYMBOL(cpufreq_quick_get_max);
1266
1267
1268 static unsigned int __cpufreq_get(unsigned int cpu)
1269 {
1270 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1271 unsigned int ret_freq = 0;
1272
1273 if (!cpufreq_driver->get)
1274 return ret_freq;
1275
1276 ret_freq = cpufreq_driver->get(cpu);
1277
1278 if (ret_freq && policy->cur &&
1279 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1280 /* verify no discrepancy between actual and
1281 saved value exists */
1282 if (unlikely(ret_freq != policy->cur)) {
1283 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1284 schedule_work(&policy->update);
1285 }
1286 }
1287
1288 return ret_freq;
1289 }
1290
1291 /**
1292 * cpufreq_get - get the current CPU frequency (in kHz)
1293 * @cpu: CPU number
1294 *
1295 * Get the CPU current (static) CPU frequency
1296 */
1297 unsigned int cpufreq_get(unsigned int cpu)
1298 {
1299 unsigned int ret_freq = 0;
1300 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1301
1302 if (!policy)
1303 goto out;
1304
1305 if (unlikely(lock_policy_rwsem_read(cpu)))
1306 goto out_policy;
1307
1308 ret_freq = __cpufreq_get(cpu);
1309
1310 unlock_policy_rwsem_read(cpu);
1311
1312 out_policy:
1313 cpufreq_cpu_put(policy);
1314 out:
1315 return ret_freq;
1316 }
1317 EXPORT_SYMBOL(cpufreq_get);
1318
1319 static struct subsys_interface cpufreq_interface = {
1320 .name = "cpufreq",
1321 .subsys = &cpu_subsys,
1322 .add_dev = cpufreq_add_dev,
1323 .remove_dev = cpufreq_remove_dev,
1324 };
1325
1326
1327 /**
1328 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1329 *
1330 * This function is only executed for the boot processor. The other CPUs
1331 * have been put offline by means of CPU hotplug.
1332 */
1333 static int cpufreq_bp_suspend(void)
1334 {
1335 int ret = 0;
1336
1337 int cpu = smp_processor_id();
1338 struct cpufreq_policy *cpu_policy;
1339
1340 pr_debug("suspending cpu %u\n", cpu);
1341
1342 /* If there's no policy for the boot CPU, we have nothing to do. */
1343 cpu_policy = cpufreq_cpu_get(cpu);
1344 if (!cpu_policy)
1345 return 0;
1346
1347 if (cpufreq_driver->suspend) {
1348 ret = cpufreq_driver->suspend(cpu_policy);
1349 if (ret)
1350 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1351 "step on CPU %u\n", cpu_policy->cpu);
1352 }
1353
1354 cpufreq_cpu_put(cpu_policy);
1355 return ret;
1356 }
1357
1358 /**
1359 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1360 *
1361 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1362 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1363 * restored. It will verify that the current freq is in sync with
1364 * what we believe it to be. This is a bit later than when it
1365 * should be, but nonethteless it's better than calling
1366 * cpufreq_driver->get() here which might re-enable interrupts...
1367 *
1368 * This function is only executed for the boot CPU. The other CPUs have not
1369 * been turned on yet.
1370 */
1371 static void cpufreq_bp_resume(void)
1372 {
1373 int ret = 0;
1374
1375 int cpu = smp_processor_id();
1376 struct cpufreq_policy *cpu_policy;
1377
1378 pr_debug("resuming cpu %u\n", cpu);
1379
1380 /* If there's no policy for the boot CPU, we have nothing to do. */
1381 cpu_policy = cpufreq_cpu_get(cpu);
1382 if (!cpu_policy)
1383 return;
1384
1385 if (cpufreq_driver->resume) {
1386 ret = cpufreq_driver->resume(cpu_policy);
1387 if (ret) {
1388 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1389 "step on CPU %u\n", cpu_policy->cpu);
1390 goto fail;
1391 }
1392 }
1393
1394 schedule_work(&cpu_policy->update);
1395
1396 fail:
1397 cpufreq_cpu_put(cpu_policy);
1398 }
1399
1400 static struct syscore_ops cpufreq_syscore_ops = {
1401 .suspend = cpufreq_bp_suspend,
1402 .resume = cpufreq_bp_resume,
1403 };
1404
1405 /**
1406 * cpufreq_get_current_driver - return current driver's name
1407 *
1408 * Return the name string of the currently loaded cpufreq driver
1409 * or NULL, if none.
1410 */
1411 const char *cpufreq_get_current_driver(void)
1412 {
1413 if (cpufreq_driver)
1414 return cpufreq_driver->name;
1415
1416 return NULL;
1417 }
1418 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1419
1420 /*********************************************************************
1421 * NOTIFIER LISTS INTERFACE *
1422 *********************************************************************/
1423
1424 /**
1425 * cpufreq_register_notifier - register a driver with cpufreq
1426 * @nb: notifier function to register
1427 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1428 *
1429 * Add a driver to one of two lists: either a list of drivers that
1430 * are notified about clock rate changes (once before and once after
1431 * the transition), or a list of drivers that are notified about
1432 * changes in cpufreq policy.
1433 *
1434 * This function may sleep, and has the same return conditions as
1435 * blocking_notifier_chain_register.
1436 */
1437 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1438 {
1439 int ret;
1440
1441 if (cpufreq_disabled())
1442 return -EINVAL;
1443
1444 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1445
1446 switch (list) {
1447 case CPUFREQ_TRANSITION_NOTIFIER:
1448 ret = srcu_notifier_chain_register(
1449 &cpufreq_transition_notifier_list, nb);
1450 break;
1451 case CPUFREQ_POLICY_NOTIFIER:
1452 ret = blocking_notifier_chain_register(
1453 &cpufreq_policy_notifier_list, nb);
1454 break;
1455 default:
1456 ret = -EINVAL;
1457 }
1458
1459 return ret;
1460 }
1461 EXPORT_SYMBOL(cpufreq_register_notifier);
1462
1463
1464 /**
1465 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1466 * @nb: notifier block to be unregistered
1467 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1468 *
1469 * Remove a driver from the CPU frequency notifier list.
1470 *
1471 * This function may sleep, and has the same return conditions as
1472 * blocking_notifier_chain_unregister.
1473 */
1474 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1475 {
1476 int ret;
1477
1478 if (cpufreq_disabled())
1479 return -EINVAL;
1480
1481 switch (list) {
1482 case CPUFREQ_TRANSITION_NOTIFIER:
1483 ret = srcu_notifier_chain_unregister(
1484 &cpufreq_transition_notifier_list, nb);
1485 break;
1486 case CPUFREQ_POLICY_NOTIFIER:
1487 ret = blocking_notifier_chain_unregister(
1488 &cpufreq_policy_notifier_list, nb);
1489 break;
1490 default:
1491 ret = -EINVAL;
1492 }
1493
1494 return ret;
1495 }
1496 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1497
1498
1499 /*********************************************************************
1500 * GOVERNORS *
1501 *********************************************************************/
1502
1503
1504 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1505 unsigned int target_freq,
1506 unsigned int relation)
1507 {
1508 int retval = -EINVAL;
1509 unsigned int old_target_freq = target_freq;
1510
1511 if (cpufreq_disabled())
1512 return -ENODEV;
1513
1514 /* Make sure that target_freq is within supported range */
1515 if (target_freq > policy->max)
1516 target_freq = policy->max;
1517 if (target_freq < policy->min)
1518 target_freq = policy->min;
1519
1520 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1521 policy->cpu, target_freq, relation, old_target_freq);
1522
1523 if (target_freq == policy->cur)
1524 return 0;
1525
1526 if (cpufreq_driver->target)
1527 retval = cpufreq_driver->target(policy, target_freq, relation);
1528
1529 return retval;
1530 }
1531 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1532
1533 int cpufreq_driver_target(struct cpufreq_policy *policy,
1534 unsigned int target_freq,
1535 unsigned int relation)
1536 {
1537 int ret = -EINVAL;
1538
1539 policy = cpufreq_cpu_get(policy->cpu);
1540 if (!policy)
1541 goto no_policy;
1542
1543 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1544 goto fail;
1545
1546 ret = __cpufreq_driver_target(policy, target_freq, relation);
1547
1548 unlock_policy_rwsem_write(policy->cpu);
1549
1550 fail:
1551 cpufreq_cpu_put(policy);
1552 no_policy:
1553 return ret;
1554 }
1555 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1556
1557 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1558 {
1559 int ret = 0;
1560
1561 if (cpufreq_disabled())
1562 return ret;
1563
1564 if (!cpufreq_driver->getavg)
1565 return 0;
1566
1567 policy = cpufreq_cpu_get(policy->cpu);
1568 if (!policy)
1569 return -EINVAL;
1570
1571 ret = cpufreq_driver->getavg(policy, cpu);
1572
1573 cpufreq_cpu_put(policy);
1574 return ret;
1575 }
1576 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1577
1578 /*
1579 * when "event" is CPUFREQ_GOV_LIMITS
1580 */
1581
1582 static int __cpufreq_governor(struct cpufreq_policy *policy,
1583 unsigned int event)
1584 {
1585 int ret;
1586
1587 /* Only must be defined when default governor is known to have latency
1588 restrictions, like e.g. conservative or ondemand.
1589 That this is the case is already ensured in Kconfig
1590 */
1591 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1592 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1593 #else
1594 struct cpufreq_governor *gov = NULL;
1595 #endif
1596
1597 if (policy->governor->max_transition_latency &&
1598 policy->cpuinfo.transition_latency >
1599 policy->governor->max_transition_latency) {
1600 if (!gov)
1601 return -EINVAL;
1602 else {
1603 printk(KERN_WARNING "%s governor failed, too long"
1604 " transition latency of HW, fallback"
1605 " to %s governor\n",
1606 policy->governor->name,
1607 gov->name);
1608 policy->governor = gov;
1609 }
1610 }
1611
1612 if (!try_module_get(policy->governor->owner))
1613 return -EINVAL;
1614
1615 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1616 policy->cpu, event);
1617
1618 mutex_lock(&cpufreq_governor_lock);
1619 if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
1620 (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
1621 mutex_unlock(&cpufreq_governor_lock);
1622 return -EBUSY;
1623 }
1624
1625 if (event == CPUFREQ_GOV_STOP)
1626 policy->governor_enabled = false;
1627 else if (event == CPUFREQ_GOV_START)
1628 policy->governor_enabled = true;
1629
1630 mutex_unlock(&cpufreq_governor_lock);
1631
1632 ret = policy->governor->governor(policy, event);
1633
1634 if (!ret) {
1635 if (event == CPUFREQ_GOV_POLICY_INIT)
1636 policy->governor->initialized++;
1637 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1638 policy->governor->initialized--;
1639 } else {
1640 /* Restore original values */
1641 mutex_lock(&cpufreq_governor_lock);
1642 if (event == CPUFREQ_GOV_STOP)
1643 policy->governor_enabled = true;
1644 else if (event == CPUFREQ_GOV_START)
1645 policy->governor_enabled = false;
1646 mutex_unlock(&cpufreq_governor_lock);
1647 }
1648
1649 /* we keep one module reference alive for
1650 each CPU governed by this CPU */
1651 if ((event != CPUFREQ_GOV_START) || ret)
1652 module_put(policy->governor->owner);
1653 if ((event == CPUFREQ_GOV_STOP) && !ret)
1654 module_put(policy->governor->owner);
1655
1656 return ret;
1657 }
1658
1659
1660 int cpufreq_register_governor(struct cpufreq_governor *governor)
1661 {
1662 int err;
1663
1664 if (!governor)
1665 return -EINVAL;
1666
1667 if (cpufreq_disabled())
1668 return -ENODEV;
1669
1670 mutex_lock(&cpufreq_governor_mutex);
1671
1672 governor->initialized = 0;
1673 err = -EBUSY;
1674 if (__find_governor(governor->name) == NULL) {
1675 err = 0;
1676 list_add(&governor->governor_list, &cpufreq_governor_list);
1677 }
1678
1679 mutex_unlock(&cpufreq_governor_mutex);
1680 return err;
1681 }
1682 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1683
1684
1685 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1686 {
1687 #ifdef CONFIG_HOTPLUG_CPU
1688 int cpu;
1689 #endif
1690
1691 if (!governor)
1692 return;
1693
1694 if (cpufreq_disabled())
1695 return;
1696
1697 #ifdef CONFIG_HOTPLUG_CPU
1698 for_each_present_cpu(cpu) {
1699 if (cpu_online(cpu))
1700 continue;
1701 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1702 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1703 }
1704 #endif
1705
1706 mutex_lock(&cpufreq_governor_mutex);
1707 list_del(&governor->governor_list);
1708 mutex_unlock(&cpufreq_governor_mutex);
1709 return;
1710 }
1711 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1712
1713
1714
1715 /*********************************************************************
1716 * POLICY INTERFACE *
1717 *********************************************************************/
1718
1719 /**
1720 * cpufreq_get_policy - get the current cpufreq_policy
1721 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1722 * is written
1723 *
1724 * Reads the current cpufreq policy.
1725 */
1726 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1727 {
1728 struct cpufreq_policy *cpu_policy;
1729 if (!policy)
1730 return -EINVAL;
1731
1732 cpu_policy = cpufreq_cpu_get(cpu);
1733 if (!cpu_policy)
1734 return -EINVAL;
1735
1736 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1737
1738 cpufreq_cpu_put(cpu_policy);
1739 return 0;
1740 }
1741 EXPORT_SYMBOL(cpufreq_get_policy);
1742
1743
1744 /*
1745 * data : current policy.
1746 * policy : policy to be set.
1747 */
1748 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1749 struct cpufreq_policy *policy)
1750 {
1751 int ret = 0, failed = 1;
1752
1753 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1754 policy->min, policy->max);
1755
1756 memcpy(&policy->cpuinfo, &data->cpuinfo,
1757 sizeof(struct cpufreq_cpuinfo));
1758
1759 if (policy->min > data->max || policy->max < data->min) {
1760 ret = -EINVAL;
1761 goto error_out;
1762 }
1763
1764 /* verify the cpu speed can be set within this limit */
1765 ret = cpufreq_driver->verify(policy);
1766 if (ret)
1767 goto error_out;
1768
1769 /* adjust if necessary - all reasons */
1770 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1771 CPUFREQ_ADJUST, policy);
1772
1773 /* adjust if necessary - hardware incompatibility*/
1774 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1775 CPUFREQ_INCOMPATIBLE, policy);
1776
1777 /* verify the cpu speed can be set within this limit,
1778 which might be different to the first one */
1779 ret = cpufreq_driver->verify(policy);
1780 if (ret)
1781 goto error_out;
1782
1783 /* notification of the new policy */
1784 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1785 CPUFREQ_NOTIFY, policy);
1786
1787 data->min = policy->min;
1788 data->max = policy->max;
1789
1790 pr_debug("new min and max freqs are %u - %u kHz\n",
1791 data->min, data->max);
1792
1793 if (cpufreq_driver->setpolicy) {
1794 data->policy = policy->policy;
1795 pr_debug("setting range\n");
1796 ret = cpufreq_driver->setpolicy(policy);
1797 } else {
1798 if (policy->governor != data->governor) {
1799 /* save old, working values */
1800 struct cpufreq_governor *old_gov = data->governor;
1801
1802 pr_debug("governor switch\n");
1803
1804 /* end old governor */
1805 if (data->governor) {
1806 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1807 unlock_policy_rwsem_write(policy->cpu);
1808 __cpufreq_governor(data,
1809 CPUFREQ_GOV_POLICY_EXIT);
1810 lock_policy_rwsem_write(policy->cpu);
1811 }
1812
1813 /* start new governor */
1814 data->governor = policy->governor;
1815 if (!__cpufreq_governor(data, CPUFREQ_GOV_POLICY_INIT)) {
1816 if (!__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1817 failed = 0;
1818 } else {
1819 unlock_policy_rwsem_write(policy->cpu);
1820 __cpufreq_governor(data,
1821 CPUFREQ_GOV_POLICY_EXIT);
1822 lock_policy_rwsem_write(policy->cpu);
1823 }
1824 }
1825
1826 if (failed) {
1827 /* new governor failed, so re-start old one */
1828 pr_debug("starting governor %s failed\n",
1829 data->governor->name);
1830 if (old_gov) {
1831 data->governor = old_gov;
1832 __cpufreq_governor(data,
1833 CPUFREQ_GOV_POLICY_INIT);
1834 __cpufreq_governor(data,
1835 CPUFREQ_GOV_START);
1836 }
1837 ret = -EINVAL;
1838 goto error_out;
1839 }
1840 /* might be a policy change, too, so fall through */
1841 }
1842 pr_debug("governor: change or update limits\n");
1843 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1844 }
1845
1846 error_out:
1847 return ret;
1848 }
1849
1850 /**
1851 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1852 * @cpu: CPU which shall be re-evaluated
1853 *
1854 * Useful for policy notifiers which have different necessities
1855 * at different times.
1856 */
1857 int cpufreq_update_policy(unsigned int cpu)
1858 {
1859 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1860 struct cpufreq_policy policy;
1861 int ret;
1862
1863 if (!data) {
1864 ret = -ENODEV;
1865 goto no_policy;
1866 }
1867
1868 if (unlikely(lock_policy_rwsem_write(cpu))) {
1869 ret = -EINVAL;
1870 goto fail;
1871 }
1872
1873 pr_debug("updating policy for CPU %u\n", cpu);
1874 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1875 policy.min = data->user_policy.min;
1876 policy.max = data->user_policy.max;
1877 policy.policy = data->user_policy.policy;
1878 policy.governor = data->user_policy.governor;
1879
1880 /* BIOS might change freq behind our back
1881 -> ask driver for current freq and notify governors about a change */
1882 if (cpufreq_driver->get) {
1883 policy.cur = cpufreq_driver->get(cpu);
1884 if (!data->cur) {
1885 pr_debug("Driver did not initialize current freq");
1886 data->cur = policy.cur;
1887 } else {
1888 if (data->cur != policy.cur && cpufreq_driver->target)
1889 cpufreq_out_of_sync(cpu, data->cur,
1890 policy.cur);
1891 }
1892 }
1893
1894 ret = __cpufreq_set_policy(data, &policy);
1895
1896 unlock_policy_rwsem_write(cpu);
1897
1898 fail:
1899 cpufreq_cpu_put(data);
1900 no_policy:
1901 return ret;
1902 }
1903 EXPORT_SYMBOL(cpufreq_update_policy);
1904
1905 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1906 unsigned long action, void *hcpu)
1907 {
1908 unsigned int cpu = (unsigned long)hcpu;
1909 struct device *dev;
1910
1911 dev = get_cpu_device(cpu);
1912 if (dev) {
1913 switch (action) {
1914 case CPU_ONLINE:
1915 case CPU_ONLINE_FROZEN:
1916 cpufreq_add_dev(dev, NULL);
1917 break;
1918 case CPU_DOWN_PREPARE:
1919 case CPU_DOWN_PREPARE_FROZEN:
1920 __cpufreq_remove_dev(dev, NULL);
1921 break;
1922 case CPU_DOWN_FAILED:
1923 case CPU_DOWN_FAILED_FROZEN:
1924 cpufreq_add_dev(dev, NULL);
1925 break;
1926 }
1927 }
1928 return NOTIFY_OK;
1929 }
1930
1931 static struct notifier_block __refdata cpufreq_cpu_notifier = {
1932 .notifier_call = cpufreq_cpu_callback,
1933 };
1934
1935 /*********************************************************************
1936 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1937 *********************************************************************/
1938
1939 /**
1940 * cpufreq_register_driver - register a CPU Frequency driver
1941 * @driver_data: A struct cpufreq_driver containing the values#
1942 * submitted by the CPU Frequency driver.
1943 *
1944 * Registers a CPU Frequency driver to this core code. This code
1945 * returns zero on success, -EBUSY when another driver got here first
1946 * (and isn't unregistered in the meantime).
1947 *
1948 */
1949 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1950 {
1951 unsigned long flags;
1952 int ret;
1953
1954 if (cpufreq_disabled())
1955 return -ENODEV;
1956
1957 if (!driver_data || !driver_data->verify || !driver_data->init ||
1958 ((!driver_data->setpolicy) && (!driver_data->target)))
1959 return -EINVAL;
1960
1961 pr_debug("trying to register driver %s\n", driver_data->name);
1962
1963 if (driver_data->setpolicy)
1964 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1965
1966 write_lock_irqsave(&cpufreq_driver_lock, flags);
1967 if (cpufreq_driver) {
1968 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1969 return -EBUSY;
1970 }
1971 cpufreq_driver = driver_data;
1972 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1973
1974 ret = subsys_interface_register(&cpufreq_interface);
1975 if (ret)
1976 goto err_null_driver;
1977
1978 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1979 int i;
1980 ret = -ENODEV;
1981
1982 /* check for at least one working CPU */
1983 for (i = 0; i < nr_cpu_ids; i++)
1984 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1985 ret = 0;
1986 break;
1987 }
1988
1989 /* if all ->init() calls failed, unregister */
1990 if (ret) {
1991 pr_debug("no CPU initialized for driver %s\n",
1992 driver_data->name);
1993 goto err_if_unreg;
1994 }
1995 }
1996
1997 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1998 pr_debug("driver %s up and running\n", driver_data->name);
1999
2000 return 0;
2001 err_if_unreg:
2002 subsys_interface_unregister(&cpufreq_interface);
2003 err_null_driver:
2004 write_lock_irqsave(&cpufreq_driver_lock, flags);
2005 cpufreq_driver = NULL;
2006 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2007 return ret;
2008 }
2009 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2010
2011
2012 /**
2013 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2014 *
2015 * Unregister the current CPUFreq driver. Only call this if you have
2016 * the right to do so, i.e. if you have succeeded in initialising before!
2017 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2018 * currently not initialised.
2019 */
2020 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2021 {
2022 unsigned long flags;
2023
2024 if (!cpufreq_driver || (driver != cpufreq_driver))
2025 return -EINVAL;
2026
2027 pr_debug("unregistering driver %s\n", driver->name);
2028
2029 subsys_interface_unregister(&cpufreq_interface);
2030 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2031
2032 write_lock_irqsave(&cpufreq_driver_lock, flags);
2033 cpufreq_driver = NULL;
2034 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2035
2036 return 0;
2037 }
2038 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2039
2040 static int __init cpufreq_core_init(void)
2041 {
2042 int cpu;
2043
2044 if (cpufreq_disabled())
2045 return -ENODEV;
2046
2047 for_each_possible_cpu(cpu) {
2048 per_cpu(cpufreq_policy_cpu, cpu) = -1;
2049 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2050 }
2051
2052 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2053 BUG_ON(!cpufreq_global_kobject);
2054 register_syscore_ops(&cpufreq_syscore_ops);
2055
2056 return 0;
2057 }
2058 core_initcall(cpufreq_core_init);