mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / cpufreq / cpufreq.c
blob8356b481e339d924c2bf3d340a16873b9bf41a08
1 /*
2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9 * Added handling for CPU hotplug
10 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/cpu.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/tick.h>
31 #include <trace/events/power.h>
33 /**
34 * The "cpufreq driver" - the arch- or hardware-dependent low
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
38 static struct cpufreq_driver *cpufreq_driver;
39 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
40 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41 static DEFINE_RWLOCK(cpufreq_driver_lock);
42 static DEFINE_MUTEX(cpufreq_governor_lock);
43 static LIST_HEAD(cpufreq_policy_list);
45 #ifdef CONFIG_HOTPLUG_CPU
46 /* This one keeps track of the previously set governor of a removed CPU */
47 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
48 #endif
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
61 * Additional rules:
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
64 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
67 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
69 #define lock_policy_rwsem(mode, cpu) \
70 static int lock_policy_rwsem_##mode(int cpu) \
71 { \
72 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
73 BUG_ON(!policy); \
74 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
76 return 0; \
79 lock_policy_rwsem(read, cpu);
80 lock_policy_rwsem(write, cpu);
82 #define unlock_policy_rwsem(mode, cpu) \
83 static void unlock_policy_rwsem_##mode(int cpu) \
84 { \
85 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
86 BUG_ON(!policy); \
87 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
90 unlock_policy_rwsem(read, cpu);
91 unlock_policy_rwsem(write, cpu);
94 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
95 * sections
97 static DECLARE_RWSEM(cpufreq_rwsem);
99 /* internal prototypes */
100 static int __cpufreq_governor(struct cpufreq_policy *policy,
101 unsigned int event);
102 static unsigned int __cpufreq_get(unsigned int cpu);
103 static void handle_update(struct work_struct *work);
106 * Two notifier lists: the "policy" list is involved in the
107 * validation process for a new CPU frequency policy; the
108 * "transition" list for kernel code that needs to handle
109 * changes to devices when the CPU clock speed changes.
110 * The mutex locks both lists.
112 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
113 static struct srcu_notifier_head cpufreq_transition_notifier_list;
115 static bool init_cpufreq_transition_notifier_list_called;
116 static int __init init_cpufreq_transition_notifier_list(void)
118 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
119 init_cpufreq_transition_notifier_list_called = true;
120 return 0;
122 pure_initcall(init_cpufreq_transition_notifier_list);
124 static int off __read_mostly;
125 static int cpufreq_disabled(void)
127 return off;
129 void disable_cpufreq(void)
131 off = 1;
133 static LIST_HEAD(cpufreq_governor_list);
134 static DEFINE_MUTEX(cpufreq_governor_mutex);
136 bool have_governor_per_policy(void)
138 return cpufreq_driver->have_governor_per_policy;
140 EXPORT_SYMBOL_GPL(have_governor_per_policy);
142 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
144 if (have_governor_per_policy())
145 return &policy->kobj;
146 else
147 return cpufreq_global_kobject;
149 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
151 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
153 u64 idle_time;
154 u64 cur_wall_time;
155 u64 busy_time;
157 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
159 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
166 idle_time = cur_wall_time - busy_time;
167 if (wall)
168 *wall = cputime_to_usecs(cur_wall_time);
170 return cputime_to_usecs(idle_time);
173 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
175 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
177 if (idle_time == -1ULL)
178 return get_cpu_idle_time_jiffy(cpu, wall);
179 else if (!io_busy)
180 idle_time += get_cpu_iowait_time_us(cpu, wall);
182 return idle_time;
184 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
186 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
188 struct cpufreq_policy *policy = NULL;
189 unsigned long flags;
191 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
192 return NULL;
194 if (!down_read_trylock(&cpufreq_rwsem))
195 return NULL;
197 /* get the cpufreq driver */
198 read_lock_irqsave(&cpufreq_driver_lock, flags);
200 if (cpufreq_driver) {
201 /* get the CPU */
202 policy = per_cpu(cpufreq_cpu_data, cpu);
203 if (policy)
204 kobject_get(&policy->kobj);
207 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
209 if (!policy)
210 up_read(&cpufreq_rwsem);
212 return policy;
214 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
216 void cpufreq_cpu_put(struct cpufreq_policy *policy)
218 if (cpufreq_disabled())
219 return;
221 kobject_put(&policy->kobj);
222 up_read(&cpufreq_rwsem);
224 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
226 /*********************************************************************
227 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
228 *********************************************************************/
231 * adjust_jiffies - adjust the system "loops_per_jiffy"
233 * This function alters the system "loops_per_jiffy" for the clock
234 * speed change. Note that loops_per_jiffy cannot be updated on SMP
235 * systems as each CPU might be scaled differently. So, use the arch
236 * per-CPU loops_per_jiffy value wherever possible.
238 #ifndef CONFIG_SMP
239 static unsigned long l_p_j_ref;
240 static unsigned int l_p_j_ref_freq;
242 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
244 if (ci->flags & CPUFREQ_CONST_LOOPS)
245 return;
247 if (!l_p_j_ref_freq) {
248 l_p_j_ref = loops_per_jiffy;
249 l_p_j_ref_freq = ci->old;
250 pr_debug("saving %lu as reference value for loops_per_jiffy; "
251 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
253 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
254 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
255 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
256 ci->new);
257 pr_debug("scaling loops_per_jiffy to %lu "
258 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
261 #else
262 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
264 return;
266 #endif
268 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
269 struct cpufreq_freqs *freqs, unsigned int state)
271 BUG_ON(irqs_disabled());
273 if (cpufreq_disabled())
274 return;
276 freqs->flags = cpufreq_driver->flags;
277 pr_debug("notification %u of frequency transition to %u kHz\n",
278 state, freqs->new);
280 switch (state) {
282 case CPUFREQ_PRECHANGE:
283 /* detect if the driver reported a value as "old frequency"
284 * which is not equal to what the cpufreq core thinks is
285 * "old frequency".
287 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
288 if ((policy) && (policy->cpu == freqs->cpu) &&
289 (policy->cur) && (policy->cur != freqs->old)) {
290 pr_debug("Warning: CPU frequency is"
291 " %u, cpufreq assumed %u kHz.\n",
292 freqs->old, policy->cur);
293 freqs->old = policy->cur;
296 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
297 CPUFREQ_PRECHANGE, freqs);
298 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
299 break;
301 case CPUFREQ_POSTCHANGE:
302 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
303 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
304 (unsigned long)freqs->cpu);
305 trace_cpu_frequency(freqs->new, freqs->cpu);
306 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
307 CPUFREQ_POSTCHANGE, freqs);
308 if (likely(policy) && likely(policy->cpu == freqs->cpu))
309 policy->cur = freqs->new;
310 break;
315 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
316 * on frequency transition.
318 * This function calls the transition notifiers and the "adjust_jiffies"
319 * function. It is called twice on all CPU frequency changes that have
320 * external effects.
322 void cpufreq_notify_transition(struct cpufreq_policy *policy,
323 struct cpufreq_freqs *freqs, unsigned int state)
325 for_each_cpu(freqs->cpu, policy->cpus)
326 __cpufreq_notify_transition(policy, freqs, state);
328 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
331 /*********************************************************************
332 * SYSFS INTERFACE *
333 *********************************************************************/
335 static struct cpufreq_governor *__find_governor(const char *str_governor)
337 struct cpufreq_governor *t;
339 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
340 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
341 return t;
343 return NULL;
347 * cpufreq_parse_governor - parse a governor string
349 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
350 struct cpufreq_governor **governor)
352 int err = -EINVAL;
354 if (!cpufreq_driver)
355 goto out;
357 if (cpufreq_driver->setpolicy) {
358 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
359 *policy = CPUFREQ_POLICY_PERFORMANCE;
360 err = 0;
361 } else if (!strnicmp(str_governor, "powersave",
362 CPUFREQ_NAME_LEN)) {
363 *policy = CPUFREQ_POLICY_POWERSAVE;
364 err = 0;
366 } else if (cpufreq_driver->target) {
367 struct cpufreq_governor *t;
369 mutex_lock(&cpufreq_governor_mutex);
371 t = __find_governor(str_governor);
373 if (t == NULL) {
374 int ret;
376 mutex_unlock(&cpufreq_governor_mutex);
377 ret = request_module("cpufreq_%s", str_governor);
378 mutex_lock(&cpufreq_governor_mutex);
380 if (ret == 0)
381 t = __find_governor(str_governor);
384 if (t != NULL) {
385 *governor = t;
386 err = 0;
389 mutex_unlock(&cpufreq_governor_mutex);
391 out:
392 return err;
396 * cpufreq_per_cpu_attr_read() / show_##file_name() -
397 * print out cpufreq information
399 * Write out information from cpufreq_driver->policy[cpu]; object must be
400 * "unsigned int".
403 #define show_one(file_name, object) \
404 static ssize_t show_##file_name \
405 (struct cpufreq_policy *policy, char *buf) \
407 return sprintf(buf, "%u\n", policy->object); \
410 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
411 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
412 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
413 show_one(scaling_min_freq, min);
414 show_one(scaling_max_freq, max);
416 static ssize_t show_scaling_cur_freq(
417 struct cpufreq_policy *policy, char *buf)
419 ssize_t ret;
421 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
422 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
423 else
424 ret = sprintf(buf, "%u\n", policy->cur);
425 return ret;
428 static int __cpufreq_set_policy(struct cpufreq_policy *policy,
429 struct cpufreq_policy *new_policy);
432 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
434 #define store_one(file_name, object) \
435 static ssize_t store_##file_name \
436 (struct cpufreq_policy *policy, const char *buf, size_t count) \
438 int ret; \
439 struct cpufreq_policy new_policy; \
441 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
442 if (ret) \
443 return -EINVAL; \
445 ret = sscanf(buf, "%u", &new_policy.object); \
446 if (ret != 1) \
447 return -EINVAL; \
449 ret = __cpufreq_set_policy(policy, &new_policy); \
450 policy->user_policy.object = policy->object; \
452 return ret ? ret : count; \
455 store_one(scaling_min_freq, min);
456 store_one(scaling_max_freq, max);
459 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
461 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
462 char *buf)
464 unsigned int cur_freq = __cpufreq_get(policy->cpu);
465 if (!cur_freq)
466 return sprintf(buf, "<unknown>");
467 return sprintf(buf, "%u\n", cur_freq);
471 * show_scaling_governor - show the current policy for the specified CPU
473 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
475 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
476 return sprintf(buf, "powersave\n");
477 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
478 return sprintf(buf, "performance\n");
479 else if (policy->governor)
480 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
481 policy->governor->name);
482 return -EINVAL;
486 * store_scaling_governor - store policy for the specified CPU
488 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
489 const char *buf, size_t count)
491 int ret;
492 char str_governor[16];
493 struct cpufreq_policy new_policy;
495 ret = cpufreq_get_policy(&new_policy, policy->cpu);
496 if (ret)
497 return ret;
499 ret = sscanf(buf, "%15s", str_governor);
500 if (ret != 1)
501 return -EINVAL;
503 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
504 &new_policy.governor))
505 return -EINVAL;
508 * Do not use cpufreq_set_policy here or the user_policy.max
509 * will be wrongly overridden
511 ret = __cpufreq_set_policy(policy, &new_policy);
513 policy->user_policy.policy = policy->policy;
514 policy->user_policy.governor = policy->governor;
516 if (ret)
517 return ret;
518 else
519 return count;
523 * show_scaling_driver - show the cpufreq driver currently loaded
525 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
527 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
531 * show_scaling_available_governors - show the available CPUfreq governors
533 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
534 char *buf)
536 ssize_t i = 0;
537 struct cpufreq_governor *t;
539 if (!cpufreq_driver->target) {
540 i += sprintf(buf, "performance powersave");
541 goto out;
544 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
545 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
546 - (CPUFREQ_NAME_LEN + 2)))
547 goto out;
548 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
550 out:
551 i += sprintf(&buf[i], "\n");
552 return i;
555 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
557 ssize_t i = 0;
558 unsigned int cpu;
560 for_each_cpu(cpu, mask) {
561 if (i)
562 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
563 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
564 if (i >= (PAGE_SIZE - 5))
565 break;
567 i += sprintf(&buf[i], "\n");
568 return i;
570 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
573 * show_related_cpus - show the CPUs affected by each transition even if
574 * hw coordination is in use
576 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
578 return cpufreq_show_cpus(policy->related_cpus, buf);
582 * show_affected_cpus - show the CPUs affected by each transition
584 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
586 return cpufreq_show_cpus(policy->cpus, buf);
589 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
590 const char *buf, size_t count)
592 unsigned int freq = 0;
593 unsigned int ret;
595 if (!policy->governor || !policy->governor->store_setspeed)
596 return -EINVAL;
598 ret = sscanf(buf, "%u", &freq);
599 if (ret != 1)
600 return -EINVAL;
602 policy->governor->store_setspeed(policy, freq);
604 return count;
607 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
609 if (!policy->governor || !policy->governor->show_setspeed)
610 return sprintf(buf, "<unsupported>\n");
612 return policy->governor->show_setspeed(policy, buf);
616 * show_bios_limit - show the current cpufreq HW/BIOS limitation
618 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
620 unsigned int limit;
621 int ret;
622 if (cpufreq_driver->bios_limit) {
623 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
624 if (!ret)
625 return sprintf(buf, "%u\n", limit);
627 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
630 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
631 cpufreq_freq_attr_ro(cpuinfo_min_freq);
632 cpufreq_freq_attr_ro(cpuinfo_max_freq);
633 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
634 cpufreq_freq_attr_ro(scaling_available_governors);
635 cpufreq_freq_attr_ro(scaling_driver);
636 cpufreq_freq_attr_ro(scaling_cur_freq);
637 cpufreq_freq_attr_ro(bios_limit);
638 cpufreq_freq_attr_ro(related_cpus);
639 cpufreq_freq_attr_ro(affected_cpus);
640 cpufreq_freq_attr_rw(scaling_min_freq);
641 cpufreq_freq_attr_rw(scaling_max_freq);
642 cpufreq_freq_attr_rw(scaling_governor);
643 cpufreq_freq_attr_rw(scaling_setspeed);
645 static struct attribute *default_attrs[] = {
646 &cpuinfo_min_freq.attr,
647 &cpuinfo_max_freq.attr,
648 &cpuinfo_transition_latency.attr,
649 &scaling_min_freq.attr,
650 &scaling_max_freq.attr,
651 &affected_cpus.attr,
652 &related_cpus.attr,
653 &scaling_governor.attr,
654 &scaling_driver.attr,
655 &scaling_available_governors.attr,
656 &scaling_setspeed.attr,
657 NULL
660 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
661 #define to_attr(a) container_of(a, struct freq_attr, attr)
663 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
665 struct cpufreq_policy *policy = to_policy(kobj);
666 struct freq_attr *fattr = to_attr(attr);
667 ssize_t ret = -EINVAL;
669 if (!down_read_trylock(&cpufreq_rwsem))
670 goto exit;
672 if (lock_policy_rwsem_read(policy->cpu) < 0)
673 goto up_read;
675 if (fattr->show)
676 ret = fattr->show(policy, buf);
677 else
678 ret = -EIO;
680 unlock_policy_rwsem_read(policy->cpu);
682 up_read:
683 up_read(&cpufreq_rwsem);
684 exit:
685 return ret;
688 static ssize_t store(struct kobject *kobj, struct attribute *attr,
689 const char *buf, size_t count)
691 struct cpufreq_policy *policy = to_policy(kobj);
692 struct freq_attr *fattr = to_attr(attr);
693 ssize_t ret = -EINVAL;
695 get_online_cpus();
697 if (!cpu_online(policy->cpu))
698 goto unlock;
700 if (!down_read_trylock(&cpufreq_rwsem))
701 goto unlock;
703 if (lock_policy_rwsem_write(policy->cpu) < 0)
704 goto up_read;
706 if (fattr->store)
707 ret = fattr->store(policy, buf, count);
708 else
709 ret = -EIO;
711 unlock_policy_rwsem_write(policy->cpu);
713 up_read:
714 up_read(&cpufreq_rwsem);
715 unlock:
716 put_online_cpus();
718 return ret;
721 static void cpufreq_sysfs_release(struct kobject *kobj)
723 struct cpufreq_policy *policy = to_policy(kobj);
724 pr_debug("last reference is dropped\n");
725 complete(&policy->kobj_unregister);
728 static const struct sysfs_ops sysfs_ops = {
729 .show = show,
730 .store = store,
733 static struct kobj_type ktype_cpufreq = {
734 .sysfs_ops = &sysfs_ops,
735 .default_attrs = default_attrs,
736 .release = cpufreq_sysfs_release,
739 struct kobject *cpufreq_global_kobject;
740 EXPORT_SYMBOL(cpufreq_global_kobject);
742 static int cpufreq_global_kobject_usage;
744 int cpufreq_get_global_kobject(void)
746 if (!cpufreq_global_kobject_usage++)
747 return kobject_add(cpufreq_global_kobject,
748 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
750 return 0;
752 EXPORT_SYMBOL(cpufreq_get_global_kobject);
754 void cpufreq_put_global_kobject(void)
756 if (!--cpufreq_global_kobject_usage)
757 kobject_del(cpufreq_global_kobject);
759 EXPORT_SYMBOL(cpufreq_put_global_kobject);
761 int cpufreq_sysfs_create_file(const struct attribute *attr)
763 int ret = cpufreq_get_global_kobject();
765 if (!ret) {
766 ret = sysfs_create_file(cpufreq_global_kobject, attr);
767 if (ret)
768 cpufreq_put_global_kobject();
771 return ret;
773 EXPORT_SYMBOL(cpufreq_sysfs_create_file);
775 void cpufreq_sysfs_remove_file(const struct attribute *attr)
777 sysfs_remove_file(cpufreq_global_kobject, attr);
778 cpufreq_put_global_kobject();
780 EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
782 /* symlink affected CPUs */
783 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
785 unsigned int j;
786 int ret = 0;
788 for_each_cpu(j, policy->cpus) {
789 struct device *cpu_dev;
791 if (j == policy->cpu)
792 continue;
794 pr_debug("Adding link for CPU: %u\n", j);
795 cpu_dev = get_cpu_device(j);
796 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
797 "cpufreq");
798 if (ret)
799 break;
801 return ret;
804 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
805 struct device *dev)
807 struct freq_attr **drv_attr;
808 int ret = 0;
810 /* prepare interface data */
811 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
812 &dev->kobj, "cpufreq");
813 if (ret)
814 return ret;
816 /* set up files for this cpu device */
817 drv_attr = cpufreq_driver->attr;
818 while ((drv_attr) && (*drv_attr)) {
819 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
820 if (ret)
821 goto err_out_kobj_put;
822 drv_attr++;
824 if (cpufreq_driver->get) {
825 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
826 if (ret)
827 goto err_out_kobj_put;
830 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
831 if (ret)
832 goto err_out_kobj_put;
834 if (cpufreq_driver->bios_limit) {
835 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
836 if (ret)
837 goto err_out_kobj_put;
840 ret = cpufreq_add_dev_symlink(policy);
841 if (ret)
842 goto err_out_kobj_put;
844 return ret;
846 err_out_kobj_put:
847 kobject_put(&policy->kobj);
848 wait_for_completion(&policy->kobj_unregister);
849 return ret;
852 static void cpufreq_init_policy(struct cpufreq_policy *policy)
854 struct cpufreq_policy new_policy;
855 int ret = 0;
857 memcpy(&new_policy, policy, sizeof(*policy));
858 /* assure that the starting sequence is run in __cpufreq_set_policy */
859 policy->governor = NULL;
861 /* set default policy */
862 ret = __cpufreq_set_policy(policy, &new_policy);
863 policy->user_policy.policy = policy->policy;
864 policy->user_policy.governor = policy->governor;
866 if (ret) {
867 pr_debug("setting policy failed\n");
868 if (cpufreq_driver->exit)
869 cpufreq_driver->exit(policy);
873 #ifdef CONFIG_HOTPLUG_CPU
874 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
875 unsigned int cpu, struct device *dev,
876 bool frozen)
878 int ret = 0, has_target = !!cpufreq_driver->target;
879 unsigned long flags;
881 if (has_target) {
882 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
883 if (ret) {
884 pr_err("%s: Failed to stop governor\n", __func__);
885 return ret;
889 lock_policy_rwsem_write(policy->cpu);
891 write_lock_irqsave(&cpufreq_driver_lock, flags);
893 cpumask_set_cpu(cpu, policy->cpus);
894 per_cpu(cpufreq_cpu_data, cpu) = policy;
895 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
897 unlock_policy_rwsem_write(policy->cpu);
899 if (has_target) {
900 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
901 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
902 pr_err("%s: Failed to start governor\n", __func__);
903 return ret;
907 /* Don't touch sysfs links during light-weight init */
908 if (!frozen)
909 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
911 return ret;
913 #endif
915 static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
917 struct cpufreq_policy *policy;
918 unsigned long flags;
920 read_lock_irqsave(&cpufreq_driver_lock, flags);
922 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
924 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
926 return policy;
929 static struct cpufreq_policy *cpufreq_policy_alloc(void)
931 struct cpufreq_policy *policy;
933 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
934 if (!policy)
935 return NULL;
937 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
938 goto err_free_policy;
940 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
941 goto err_free_cpumask;
943 INIT_LIST_HEAD(&policy->policy_list);
944 return policy;
946 err_free_cpumask:
947 free_cpumask_var(policy->cpus);
948 err_free_policy:
949 kfree(policy);
951 return NULL;
954 static void cpufreq_policy_free(struct cpufreq_policy *policy)
956 free_cpumask_var(policy->related_cpus);
957 free_cpumask_var(policy->cpus);
958 kfree(policy);
961 static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
963 if (cpu == policy->cpu)
964 return;
967 * Take direct locks as lock_policy_rwsem_write wouldn't work here.
968 * Also lock for last cpu is enough here as contention will happen only
969 * after policy->cpu is changed and after it is changed, other threads
970 * will try to acquire lock for new cpu. And policy is already updated
971 * by then.
973 down_write(&per_cpu(cpu_policy_rwsem, policy->cpu));
975 policy->last_cpu = policy->cpu;
976 policy->cpu = cpu;
978 up_write(&per_cpu(cpu_policy_rwsem, policy->last_cpu));
980 #ifdef CONFIG_CPU_FREQ_TABLE
981 cpufreq_frequency_table_update_policy_cpu(policy);
982 #endif
983 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
984 CPUFREQ_UPDATE_POLICY_CPU, policy);
987 static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
988 bool frozen)
990 unsigned int j, cpu = dev->id;
991 int ret = -ENOMEM;
992 struct cpufreq_policy *policy;
993 unsigned long flags;
994 #ifdef CONFIG_HOTPLUG_CPU
995 struct cpufreq_policy *tpolicy;
996 struct cpufreq_governor *gov;
997 #endif
999 if (cpu_is_offline(cpu))
1000 return 0;
1002 pr_debug("adding CPU %u\n", cpu);
1004 #ifdef CONFIG_SMP
1005 /* check whether a different CPU already registered this
1006 * CPU because it is in the same boat. */
1007 policy = cpufreq_cpu_get(cpu);
1008 if (unlikely(policy)) {
1009 cpufreq_cpu_put(policy);
1010 return 0;
1012 #endif
1014 if (!down_read_trylock(&cpufreq_rwsem))
1015 return 0;
1017 #ifdef CONFIG_HOTPLUG_CPU
1018 /* Check if this cpu was hot-unplugged earlier and has siblings */
1019 read_lock_irqsave(&cpufreq_driver_lock, flags);
1020 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1021 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
1022 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1023 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
1024 up_read(&cpufreq_rwsem);
1025 return ret;
1028 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1029 #endif
1031 if (frozen)
1032 /* Restore the saved policy when doing light-weight init */
1033 policy = cpufreq_policy_restore(cpu);
1034 else
1035 policy = cpufreq_policy_alloc();
1037 if (!policy)
1038 goto nomem_out;
1042 * In the resume path, since we restore a saved policy, the assignment
1043 * to policy->cpu is like an update of the existing policy, rather than
1044 * the creation of a brand new one. So we need to perform this update
1045 * by invoking update_policy_cpu().
1047 if (frozen && cpu != policy->cpu)
1048 update_policy_cpu(policy, cpu);
1049 else
1050 policy->cpu = cpu;
1052 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1053 cpumask_copy(policy->cpus, cpumask_of(cpu));
1055 init_completion(&policy->kobj_unregister);
1056 INIT_WORK(&policy->update, handle_update);
1058 /* call driver. From then on the cpufreq must be able
1059 * to accept all calls to ->verify and ->setpolicy for this CPU
1061 ret = cpufreq_driver->init(policy);
1062 if (ret) {
1063 pr_debug("initialization failed\n");
1064 goto err_set_policy_cpu;
1067 /* related cpus should atleast have policy->cpus */
1068 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1071 * affected cpus must always be the one, which are online. We aren't
1072 * managing offline cpus here.
1074 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1076 policy->user_policy.min = policy->min;
1077 policy->user_policy.max = policy->max;
1079 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1080 CPUFREQ_START, policy);
1082 #ifdef CONFIG_HOTPLUG_CPU
1083 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1084 if (gov) {
1085 policy->governor = gov;
1086 pr_debug("Restoring governor %s for cpu %d\n",
1087 policy->governor->name, cpu);
1089 #endif
1091 write_lock_irqsave(&cpufreq_driver_lock, flags);
1092 for_each_cpu(j, policy->cpus)
1093 per_cpu(cpufreq_cpu_data, j) = policy;
1094 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1096 if (!frozen) {
1097 ret = cpufreq_add_dev_interface(policy, dev);
1098 if (ret)
1099 goto err_out_unregister;
1102 write_lock_irqsave(&cpufreq_driver_lock, flags);
1103 list_add(&policy->policy_list, &cpufreq_policy_list);
1104 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1106 cpufreq_init_policy(policy);
1108 kobject_uevent(&policy->kobj, KOBJ_ADD);
1109 up_read(&cpufreq_rwsem);
1111 pr_debug("initialization complete\n");
1113 return 0;
1115 err_out_unregister:
1116 write_lock_irqsave(&cpufreq_driver_lock, flags);
1117 for_each_cpu(j, policy->cpus)
1118 per_cpu(cpufreq_cpu_data, j) = NULL;
1119 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1121 err_set_policy_cpu:
1122 cpufreq_policy_free(policy);
1123 nomem_out:
1124 up_read(&cpufreq_rwsem);
1126 return ret;
1130 * cpufreq_add_dev - add a CPU device
1132 * Adds the cpufreq interface for a CPU device.
1134 * The Oracle says: try running cpufreq registration/unregistration concurrently
1135 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1136 * mess up, but more thorough testing is needed. - Mathieu
1138 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1140 return __cpufreq_add_dev(dev, sif, false);
1143 static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1144 unsigned int old_cpu, bool frozen)
1146 struct device *cpu_dev;
1147 int ret;
1149 /* first sibling now owns the new sysfs dir */
1150 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
1152 /* Don't touch sysfs files during light-weight tear-down */
1153 if (frozen)
1154 return cpu_dev->id;
1156 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1157 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1158 if (ret) {
1159 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1161 WARN_ON(lock_policy_rwsem_write(old_cpu));
1162 cpumask_set_cpu(old_cpu, policy->cpus);
1163 unlock_policy_rwsem_write(old_cpu);
1165 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1166 "cpufreq");
1168 return -EINVAL;
1171 return cpu_dev->id;
1174 static int __cpufreq_remove_dev_prepare(struct device *dev,
1175 struct subsys_interface *sif,
1176 bool frozen)
1178 unsigned int cpu = dev->id, cpus;
1179 int new_cpu, ret;
1180 unsigned long flags;
1181 struct cpufreq_policy *policy;
1183 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1185 write_lock_irqsave(&cpufreq_driver_lock, flags);
1187 policy = per_cpu(cpufreq_cpu_data, cpu);
1189 /* Save the policy somewhere when doing a light-weight tear-down */
1190 if (frozen)
1191 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
1193 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1195 if (!policy) {
1196 pr_debug("%s: No cpu_data found\n", __func__);
1197 return -EINVAL;
1200 if (cpufreq_driver->target) {
1201 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1202 if (ret) {
1203 pr_err("%s: Failed to stop governor\n", __func__);
1204 return ret;
1208 #ifdef CONFIG_HOTPLUG_CPU
1209 if (!cpufreq_driver->setpolicy)
1210 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1211 policy->governor->name, CPUFREQ_NAME_LEN);
1212 #endif
1214 lock_policy_rwsem_read(cpu);
1215 cpus = cpumask_weight(policy->cpus);
1216 unlock_policy_rwsem_read(cpu);
1218 if (cpu != policy->cpu) {
1219 if (!frozen)
1220 sysfs_remove_link(&dev->kobj, "cpufreq");
1221 } else if (cpus > 1) {
1223 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
1224 if (new_cpu >= 0) {
1225 update_policy_cpu(policy, new_cpu);
1227 if (!frozen) {
1228 pr_debug("%s: policy Kobject moved to cpu: %d "
1229 "from: %d\n",__func__, new_cpu, cpu);
1234 return 0;
1237 static int __cpufreq_remove_dev_finish(struct device *dev,
1238 struct subsys_interface *sif,
1239 bool frozen)
1241 unsigned int cpu = dev->id, cpus;
1242 int ret;
1243 unsigned long flags;
1244 struct cpufreq_policy *policy;
1245 struct kobject *kobj;
1246 struct completion *cmp;
1248 read_lock_irqsave(&cpufreq_driver_lock, flags);
1249 policy = per_cpu(cpufreq_cpu_data, cpu);
1250 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1252 if (!policy) {
1253 pr_debug("%s: No cpu_data found\n", __func__);
1254 return -EINVAL;
1257 WARN_ON(lock_policy_rwsem_write(cpu));
1258 cpus = cpumask_weight(policy->cpus);
1260 if (cpus > 1)
1261 cpumask_clear_cpu(cpu, policy->cpus);
1262 unlock_policy_rwsem_write(cpu);
1264 /* If cpu is last user of policy, free policy */
1265 if (cpus == 1) {
1266 if (cpufreq_driver->target) {
1267 ret = __cpufreq_governor(policy,
1268 CPUFREQ_GOV_POLICY_EXIT);
1269 if (ret) {
1270 pr_err("%s: Failed to exit governor\n",
1271 __func__);
1272 return ret;
1276 if (!frozen) {
1277 lock_policy_rwsem_read(cpu);
1278 kobj = &policy->kobj;
1279 cmp = &policy->kobj_unregister;
1280 unlock_policy_rwsem_read(cpu);
1281 kobject_put(kobj);
1284 * We need to make sure that the underlying kobj is
1285 * actually not referenced anymore by anybody before we
1286 * proceed with unloading.
1288 pr_debug("waiting for dropping of refcount\n");
1289 wait_for_completion(cmp);
1290 pr_debug("wait complete\n");
1294 * Perform the ->exit() even during light-weight tear-down,
1295 * since this is a core component, and is essential for the
1296 * subsequent light-weight ->init() to succeed.
1298 if (cpufreq_driver->exit)
1299 cpufreq_driver->exit(policy);
1301 /* Remove policy from list of active policies */
1302 write_lock_irqsave(&cpufreq_driver_lock, flags);
1303 list_del(&policy->policy_list);
1304 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1306 if (!frozen)
1307 cpufreq_policy_free(policy);
1308 } else {
1309 if (cpufreq_driver->target) {
1310 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1311 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1312 pr_err("%s: Failed to start governor\n",
1313 __func__);
1314 return ret;
1319 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1320 return 0;
1324 * __cpufreq_remove_dev - remove a CPU device
1326 * Removes the cpufreq interface for a CPU device.
1327 * Caller should already have policy_rwsem in write mode for this CPU.
1328 * This routine frees the rwsem before returning.
1330 static inline int __cpufreq_remove_dev(struct device *dev,
1331 struct subsys_interface *sif,
1332 bool frozen)
1334 int ret;
1336 ret = __cpufreq_remove_dev_prepare(dev, sif, frozen);
1338 if (!ret)
1339 ret = __cpufreq_remove_dev_finish(dev, sif, frozen);
1341 return ret;
1344 static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1346 unsigned int cpu = dev->id;
1347 int retval;
1349 if (cpu_is_offline(cpu))
1350 return 0;
1352 retval = __cpufreq_remove_dev(dev, sif, false);
1353 return retval;
1356 static void handle_update(struct work_struct *work)
1358 struct cpufreq_policy *policy =
1359 container_of(work, struct cpufreq_policy, update);
1360 unsigned int cpu = policy->cpu;
1361 pr_debug("handle_update for cpu %u called\n", cpu);
1362 cpufreq_update_policy(cpu);
1366 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1367 * in deep trouble.
1368 * @cpu: cpu number
1369 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1370 * @new_freq: CPU frequency the CPU actually runs at
1372 * We adjust to current frequency first, and need to clean up later.
1373 * So either call to cpufreq_update_policy() or schedule handle_update()).
1375 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1376 unsigned int new_freq)
1378 struct cpufreq_policy *policy;
1379 struct cpufreq_freqs freqs;
1380 unsigned long flags;
1382 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1383 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1385 freqs.old = old_freq;
1386 freqs.new = new_freq;
1388 read_lock_irqsave(&cpufreq_driver_lock, flags);
1389 policy = per_cpu(cpufreq_cpu_data, cpu);
1390 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1392 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1393 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1397 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1398 * @cpu: CPU number
1400 * This is the last known freq, without actually getting it from the driver.
1401 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1403 unsigned int cpufreq_quick_get(unsigned int cpu)
1405 struct cpufreq_policy *policy;
1406 unsigned int ret_freq = 0;
1408 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1409 return cpufreq_driver->get(cpu);
1411 policy = cpufreq_cpu_get(cpu);
1412 if (policy) {
1413 ret_freq = policy->cur;
1414 cpufreq_cpu_put(policy);
1417 return ret_freq;
1419 EXPORT_SYMBOL(cpufreq_quick_get);
1422 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1423 * @cpu: CPU number
1425 * Just return the max possible frequency for a given CPU.
1427 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1429 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1430 unsigned int ret_freq = 0;
1432 if (policy) {
1433 ret_freq = policy->max;
1434 cpufreq_cpu_put(policy);
1437 return ret_freq;
1439 EXPORT_SYMBOL(cpufreq_quick_get_max);
1441 static unsigned int __cpufreq_get(unsigned int cpu)
1443 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1444 unsigned int ret_freq = 0;
1446 if (!cpufreq_driver->get)
1447 return ret_freq;
1449 ret_freq = cpufreq_driver->get(cpu);
1451 if (ret_freq && policy->cur &&
1452 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1453 /* verify no discrepancy between actual and
1454 saved value exists */
1455 if (unlikely(ret_freq != policy->cur)) {
1456 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1457 schedule_work(&policy->update);
1461 return ret_freq;
1465 * cpufreq_get - get the current CPU frequency (in kHz)
1466 * @cpu: CPU number
1468 * Get the CPU current (static) CPU frequency
1470 unsigned int cpufreq_get(unsigned int cpu)
1472 unsigned int ret_freq = 0;
1474 if (cpufreq_disabled() || !cpufreq_driver)
1475 return -ENOENT;
1477 if (!down_read_trylock(&cpufreq_rwsem))
1478 return 0;
1480 if (unlikely(lock_policy_rwsem_read(cpu)))
1481 goto out_policy;
1483 ret_freq = __cpufreq_get(cpu);
1485 unlock_policy_rwsem_read(cpu);
1487 out_policy:
1488 up_read(&cpufreq_rwsem);
1490 return ret_freq;
1492 EXPORT_SYMBOL(cpufreq_get);
1494 static struct subsys_interface cpufreq_interface = {
1495 .name = "cpufreq",
1496 .subsys = &cpu_subsys,
1497 .add_dev = cpufreq_add_dev,
1498 .remove_dev = cpufreq_remove_dev,
1502 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1504 * This function is only executed for the boot processor. The other CPUs
1505 * have been put offline by means of CPU hotplug.
1507 static int cpufreq_bp_suspend(void)
1509 int ret = 0;
1511 int cpu = smp_processor_id();
1512 struct cpufreq_policy *policy;
1514 pr_debug("suspending cpu %u\n", cpu);
1516 /* If there's no policy for the boot CPU, we have nothing to do. */
1517 policy = cpufreq_cpu_get(cpu);
1518 if (!policy)
1519 return 0;
1521 if (cpufreq_driver->suspend) {
1522 ret = cpufreq_driver->suspend(policy);
1523 if (ret)
1524 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1525 "step on CPU %u\n", policy->cpu);
1528 cpufreq_cpu_put(policy);
1529 return ret;
1533 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1535 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1536 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1537 * restored. It will verify that the current freq is in sync with
1538 * what we believe it to be. This is a bit later than when it
1539 * should be, but nonethteless it's better than calling
1540 * cpufreq_driver->get() here which might re-enable interrupts...
1542 * This function is only executed for the boot CPU. The other CPUs have not
1543 * been turned on yet.
1545 static void cpufreq_bp_resume(void)
1547 int ret = 0;
1549 int cpu = smp_processor_id();
1550 struct cpufreq_policy *policy;
1552 pr_debug("resuming cpu %u\n", cpu);
1554 /* If there's no policy for the boot CPU, we have nothing to do. */
1555 policy = cpufreq_cpu_get(cpu);
1556 if (!policy)
1557 return;
1559 if (cpufreq_driver->resume) {
1560 ret = cpufreq_driver->resume(policy);
1561 if (ret) {
1562 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1563 "step on CPU %u\n", policy->cpu);
1564 goto fail;
1568 schedule_work(&policy->update);
1570 fail:
1571 cpufreq_cpu_put(policy);
1574 static struct syscore_ops cpufreq_syscore_ops = {
1575 .suspend = cpufreq_bp_suspend,
1576 .resume = cpufreq_bp_resume,
1580 * cpufreq_get_current_driver - return current driver's name
1582 * Return the name string of the currently loaded cpufreq driver
1583 * or NULL, if none.
1585 const char *cpufreq_get_current_driver(void)
1587 if (cpufreq_driver)
1588 return cpufreq_driver->name;
1590 return NULL;
1592 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1594 /*********************************************************************
1595 * NOTIFIER LISTS INTERFACE *
1596 *********************************************************************/
1599 * cpufreq_register_notifier - register a driver with cpufreq
1600 * @nb: notifier function to register
1601 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1603 * Add a driver to one of two lists: either a list of drivers that
1604 * are notified about clock rate changes (once before and once after
1605 * the transition), or a list of drivers that are notified about
1606 * changes in cpufreq policy.
1608 * This function may sleep, and has the same return conditions as
1609 * blocking_notifier_chain_register.
1611 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1613 int ret;
1615 if (cpufreq_disabled())
1616 return -EINVAL;
1618 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1620 switch (list) {
1621 case CPUFREQ_TRANSITION_NOTIFIER:
1622 ret = srcu_notifier_chain_register(
1623 &cpufreq_transition_notifier_list, nb);
1624 break;
1625 case CPUFREQ_POLICY_NOTIFIER:
1626 ret = blocking_notifier_chain_register(
1627 &cpufreq_policy_notifier_list, nb);
1628 break;
1629 default:
1630 ret = -EINVAL;
1633 return ret;
1635 EXPORT_SYMBOL(cpufreq_register_notifier);
1638 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1639 * @nb: notifier block to be unregistered
1640 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1642 * Remove a driver from the CPU frequency notifier list.
1644 * This function may sleep, and has the same return conditions as
1645 * blocking_notifier_chain_unregister.
1647 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1649 int ret;
1651 if (cpufreq_disabled())
1652 return -EINVAL;
1654 switch (list) {
1655 case CPUFREQ_TRANSITION_NOTIFIER:
1656 ret = srcu_notifier_chain_unregister(
1657 &cpufreq_transition_notifier_list, nb);
1658 break;
1659 case CPUFREQ_POLICY_NOTIFIER:
1660 ret = blocking_notifier_chain_unregister(
1661 &cpufreq_policy_notifier_list, nb);
1662 break;
1663 default:
1664 ret = -EINVAL;
1667 return ret;
1669 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1672 /*********************************************************************
1673 * GOVERNORS *
1674 *********************************************************************/
1676 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1677 unsigned int target_freq,
1678 unsigned int relation)
1680 int retval = -EINVAL;
1681 unsigned int old_target_freq = target_freq;
1683 if (cpufreq_disabled())
1684 return -ENODEV;
1686 /* Make sure that target_freq is within supported range */
1687 if (target_freq > policy->max)
1688 target_freq = policy->max;
1689 if (target_freq < policy->min)
1690 target_freq = policy->min;
1692 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1693 policy->cpu, target_freq, relation, old_target_freq);
1695 if (target_freq == policy->cur)
1696 return 0;
1698 if (cpufreq_driver->target)
1699 retval = cpufreq_driver->target(policy, target_freq, relation);
1701 return retval;
1703 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1705 int cpufreq_driver_target(struct cpufreq_policy *policy,
1706 unsigned int target_freq,
1707 unsigned int relation)
1709 int ret = -EINVAL;
1711 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1712 goto fail;
1714 ret = __cpufreq_driver_target(policy, target_freq, relation);
1716 unlock_policy_rwsem_write(policy->cpu);
1718 fail:
1719 return ret;
1721 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1724 * when "event" is CPUFREQ_GOV_LIMITS
1727 static int __cpufreq_governor(struct cpufreq_policy *policy,
1728 unsigned int event)
1730 int ret;
1732 /* Only must be defined when default governor is known to have latency
1733 restrictions, like e.g. conservative or ondemand.
1734 That this is the case is already ensured in Kconfig
1736 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1737 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1738 #else
1739 struct cpufreq_governor *gov = NULL;
1740 #endif
1743 * Governor might not be initiated here if ACPI _PPC changed
1744 * notification happened, so check it.
1746 if (!policy->governor)
1747 return -EINVAL;
1749 if (policy->governor->max_transition_latency &&
1750 policy->cpuinfo.transition_latency >
1751 policy->governor->max_transition_latency) {
1752 if (!gov)
1753 return -EINVAL;
1754 else {
1755 printk(KERN_WARNING "%s governor failed, too long"
1756 " transition latency of HW, fallback"
1757 " to %s governor\n",
1758 policy->governor->name,
1759 gov->name);
1760 policy->governor = gov;
1764 if (event == CPUFREQ_GOV_POLICY_INIT)
1765 if (!try_module_get(policy->governor->owner))
1766 return -EINVAL;
1768 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1769 policy->cpu, event);
1771 mutex_lock(&cpufreq_governor_lock);
1772 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
1773 || (!policy->governor_enabled
1774 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
1775 mutex_unlock(&cpufreq_governor_lock);
1776 return -EBUSY;
1779 if (event == CPUFREQ_GOV_STOP)
1780 policy->governor_enabled = false;
1781 else if (event == CPUFREQ_GOV_START)
1782 policy->governor_enabled = true;
1784 mutex_unlock(&cpufreq_governor_lock);
1786 ret = policy->governor->governor(policy, event);
1788 if (!ret) {
1789 if (event == CPUFREQ_GOV_POLICY_INIT)
1790 policy->governor->initialized++;
1791 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1792 policy->governor->initialized--;
1793 } else {
1794 /* Restore original values */
1795 mutex_lock(&cpufreq_governor_lock);
1796 if (event == CPUFREQ_GOV_STOP)
1797 policy->governor_enabled = true;
1798 else if (event == CPUFREQ_GOV_START)
1799 policy->governor_enabled = false;
1800 mutex_unlock(&cpufreq_governor_lock);
1803 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1804 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
1805 module_put(policy->governor->owner);
1807 return ret;
1810 int cpufreq_register_governor(struct cpufreq_governor *governor)
1812 int err;
1814 if (!governor)
1815 return -EINVAL;
1817 if (cpufreq_disabled())
1818 return -ENODEV;
1820 mutex_lock(&cpufreq_governor_mutex);
1822 governor->initialized = 0;
1823 err = -EBUSY;
1824 if (__find_governor(governor->name) == NULL) {
1825 err = 0;
1826 list_add(&governor->governor_list, &cpufreq_governor_list);
1829 mutex_unlock(&cpufreq_governor_mutex);
1830 return err;
1832 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1834 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1836 #ifdef CONFIG_HOTPLUG_CPU
1837 int cpu;
1838 #endif
1840 if (!governor)
1841 return;
1843 if (cpufreq_disabled())
1844 return;
1846 #ifdef CONFIG_HOTPLUG_CPU
1847 for_each_present_cpu(cpu) {
1848 if (cpu_online(cpu))
1849 continue;
1850 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1851 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1853 #endif
1855 mutex_lock(&cpufreq_governor_mutex);
1856 list_del(&governor->governor_list);
1857 mutex_unlock(&cpufreq_governor_mutex);
1858 return;
1860 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1863 /*********************************************************************
1864 * POLICY INTERFACE *
1865 *********************************************************************/
1868 * cpufreq_get_policy - get the current cpufreq_policy
1869 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1870 * is written
1872 * Reads the current cpufreq policy.
1874 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1876 struct cpufreq_policy *cpu_policy;
1877 if (!policy)
1878 return -EINVAL;
1880 cpu_policy = cpufreq_cpu_get(cpu);
1881 if (!cpu_policy)
1882 return -EINVAL;
1884 memcpy(policy, cpu_policy, sizeof(*policy));
1886 cpufreq_cpu_put(cpu_policy);
1887 return 0;
1889 EXPORT_SYMBOL(cpufreq_get_policy);
1892 * data : current policy.
1893 * policy : policy to be set.
1895 static int __cpufreq_set_policy(struct cpufreq_policy *policy,
1896 struct cpufreq_policy *new_policy)
1898 int ret = 0, failed = 1;
1900 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1901 new_policy->min, new_policy->max);
1903 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1905 if (new_policy->min > policy->max || new_policy->max < policy->min) {
1906 ret = -EINVAL;
1907 goto error_out;
1910 /* verify the cpu speed can be set within this limit */
1911 ret = cpufreq_driver->verify(new_policy);
1912 if (ret)
1913 goto error_out;
1915 /* adjust if necessary - all reasons */
1916 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1917 CPUFREQ_ADJUST, new_policy);
1919 /* adjust if necessary - hardware incompatibility*/
1920 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1921 CPUFREQ_INCOMPATIBLE, new_policy);
1924 * verify the cpu speed can be set within this limit, which might be
1925 * different to the first one
1927 ret = cpufreq_driver->verify(new_policy);
1928 if (ret)
1929 goto error_out;
1931 /* notification of the new policy */
1932 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1933 CPUFREQ_NOTIFY, new_policy);
1935 policy->min = new_policy->min;
1936 policy->max = new_policy->max;
1938 pr_debug("new min and max freqs are %u - %u kHz\n",
1939 policy->min, policy->max);
1941 if (cpufreq_driver->setpolicy) {
1942 policy->policy = new_policy->policy;
1943 pr_debug("setting range\n");
1944 ret = cpufreq_driver->setpolicy(new_policy);
1945 } else {
1946 if (new_policy->governor != policy->governor) {
1947 /* save old, working values */
1948 struct cpufreq_governor *old_gov = policy->governor;
1950 pr_debug("governor switch\n");
1952 /* end old governor */
1953 if (policy->governor) {
1954 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1955 unlock_policy_rwsem_write(new_policy->cpu);
1956 __cpufreq_governor(policy,
1957 CPUFREQ_GOV_POLICY_EXIT);
1958 lock_policy_rwsem_write(new_policy->cpu);
1961 /* start new governor */
1962 policy->governor = new_policy->governor;
1963 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1964 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
1965 failed = 0;
1966 } else {
1967 unlock_policy_rwsem_write(new_policy->cpu);
1968 __cpufreq_governor(policy,
1969 CPUFREQ_GOV_POLICY_EXIT);
1970 lock_policy_rwsem_write(new_policy->cpu);
1974 if (failed) {
1975 /* new governor failed, so re-start old one */
1976 pr_debug("starting governor %s failed\n",
1977 policy->governor->name);
1978 if (old_gov) {
1979 policy->governor = old_gov;
1980 __cpufreq_governor(policy,
1981 CPUFREQ_GOV_POLICY_INIT);
1982 __cpufreq_governor(policy,
1983 CPUFREQ_GOV_START);
1985 ret = -EINVAL;
1986 goto error_out;
1988 /* might be a policy change, too, so fall through */
1990 pr_debug("governor: change or update limits\n");
1991 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1994 error_out:
1995 return ret;
1999 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2000 * @cpu: CPU which shall be re-evaluated
2002 * Useful for policy notifiers which have different necessities
2003 * at different times.
2005 int cpufreq_update_policy(unsigned int cpu)
2007 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2008 struct cpufreq_policy new_policy;
2009 int ret;
2011 if (!policy) {
2012 ret = -ENODEV;
2013 goto no_policy;
2016 if (unlikely(lock_policy_rwsem_write(cpu))) {
2017 ret = -EINVAL;
2018 goto fail;
2021 pr_debug("updating policy for CPU %u\n", cpu);
2022 memcpy(&new_policy, policy, sizeof(*policy));
2023 new_policy.min = policy->user_policy.min;
2024 new_policy.max = policy->user_policy.max;
2025 new_policy.policy = policy->user_policy.policy;
2026 new_policy.governor = policy->user_policy.governor;
2029 * BIOS might change freq behind our back
2030 * -> ask driver for current freq and notify governors about a change
2032 if (cpufreq_driver->get) {
2033 new_policy.cur = cpufreq_driver->get(cpu);
2034 if (!policy->cur) {
2035 pr_debug("Driver did not initialize current freq");
2036 policy->cur = new_policy.cur;
2037 } else {
2038 if (policy->cur != new_policy.cur && cpufreq_driver->target)
2039 cpufreq_out_of_sync(cpu, policy->cur,
2040 new_policy.cur);
2044 ret = __cpufreq_set_policy(policy, &new_policy);
2046 unlock_policy_rwsem_write(cpu);
2048 fail:
2049 cpufreq_cpu_put(policy);
2050 no_policy:
2051 return ret;
2053 EXPORT_SYMBOL(cpufreq_update_policy);
2055 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2056 unsigned long action, void *hcpu)
2058 unsigned int cpu = (unsigned long)hcpu;
2059 struct device *dev;
2060 bool frozen = false;
2062 dev = get_cpu_device(cpu);
2063 if (dev) {
2065 if (action & CPU_TASKS_FROZEN)
2066 frozen = true;
2068 switch (action & ~CPU_TASKS_FROZEN) {
2069 case CPU_ONLINE:
2070 __cpufreq_add_dev(dev, NULL, frozen);
2071 cpufreq_update_policy(cpu);
2072 break;
2074 case CPU_DOWN_PREPARE:
2075 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
2076 break;
2078 case CPU_POST_DEAD:
2079 __cpufreq_remove_dev_finish(dev, NULL, frozen);
2080 break;
2082 case CPU_DOWN_FAILED:
2083 __cpufreq_add_dev(dev, NULL, frozen);
2084 break;
2087 return NOTIFY_OK;
2090 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2091 .notifier_call = cpufreq_cpu_callback,
2094 /*********************************************************************
2095 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2096 *********************************************************************/
2099 * cpufreq_register_driver - register a CPU Frequency driver
2100 * @driver_data: A struct cpufreq_driver containing the values#
2101 * submitted by the CPU Frequency driver.
2103 * Registers a CPU Frequency driver to this core code. This code
2104 * returns zero on success, -EBUSY when another driver got here first
2105 * (and isn't unregistered in the meantime).
2108 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2110 unsigned long flags;
2111 int ret;
2113 if (cpufreq_disabled())
2114 return -ENODEV;
2116 if (!driver_data || !driver_data->verify || !driver_data->init ||
2117 ((!driver_data->setpolicy) && (!driver_data->target)))
2118 return -EINVAL;
2120 pr_debug("trying to register driver %s\n", driver_data->name);
2122 if (driver_data->setpolicy)
2123 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2125 write_lock_irqsave(&cpufreq_driver_lock, flags);
2126 if (cpufreq_driver) {
2127 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2128 return -EEXIST;
2130 cpufreq_driver = driver_data;
2131 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2133 ret = subsys_interface_register(&cpufreq_interface);
2134 if (ret)
2135 goto err_null_driver;
2137 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
2138 int i;
2139 ret = -ENODEV;
2141 /* check for at least one working CPU */
2142 for (i = 0; i < nr_cpu_ids; i++)
2143 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
2144 ret = 0;
2145 break;
2148 /* if all ->init() calls failed, unregister */
2149 if (ret) {
2150 pr_debug("no CPU initialized for driver %s\n",
2151 driver_data->name);
2152 goto err_if_unreg;
2156 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2157 pr_debug("driver %s up and running\n", driver_data->name);
2159 return 0;
2160 err_if_unreg:
2161 subsys_interface_unregister(&cpufreq_interface);
2162 err_null_driver:
2163 write_lock_irqsave(&cpufreq_driver_lock, flags);
2164 cpufreq_driver = NULL;
2165 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2166 return ret;
2168 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2171 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2173 * Unregister the current CPUFreq driver. Only call this if you have
2174 * the right to do so, i.e. if you have succeeded in initialising before!
2175 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2176 * currently not initialised.
2178 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2180 unsigned long flags;
2182 if (!cpufreq_driver || (driver != cpufreq_driver))
2183 return -EINVAL;
2185 pr_debug("unregistering driver %s\n", driver->name);
2187 subsys_interface_unregister(&cpufreq_interface);
2188 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2190 down_write(&cpufreq_rwsem);
2191 write_lock_irqsave(&cpufreq_driver_lock, flags);
2193 cpufreq_driver = NULL;
2195 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2196 up_write(&cpufreq_rwsem);
2198 return 0;
2200 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2202 static int __init cpufreq_core_init(void)
2204 int cpu;
2206 if (cpufreq_disabled())
2207 return -ENODEV;
2209 for_each_possible_cpu(cpu)
2210 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2212 cpufreq_global_kobject = kobject_create();
2213 BUG_ON(!cpufreq_global_kobject);
2214 register_syscore_ops(&cpufreq_syscore_ops);
2216 return 0;
2218 core_initcall(cpufreq_core_init);