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/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/tick.h>
33 #include <linux/sched.h>
35 #include <trace/events/power.h>
37 static LIST_HEAD(cpufreq_policy_list);
39 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
41 return cpumask_empty(policy->cpus);
44 static bool suitable_policy(struct cpufreq_policy *policy, bool active)
46 return active == !policy_is_inactive(policy);
49 /* Finds Next Acive/Inactive policy */
50 static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy,
54 policy = list_next_entry(policy, policy_list);
56 /* No more policies in the list */
57 if (&policy->policy_list == &cpufreq_policy_list)
59 } while (!suitable_policy(policy, active));
64 static struct cpufreq_policy *first_policy(bool active)
66 struct cpufreq_policy *policy;
68 /* No policies in the list */
69 if (list_empty(&cpufreq_policy_list))
72 policy = list_first_entry(&cpufreq_policy_list, typeof(*policy),
75 if (!suitable_policy(policy, active))
76 policy = next_policy(policy, active);
81 /* Macros to iterate over CPU policies */
82 #define for_each_suitable_policy(__policy, __active) \
83 for (__policy = first_policy(__active); \
85 __policy = next_policy(__policy, __active))
87 #define for_each_active_policy(__policy) \
88 for_each_suitable_policy(__policy, true)
89 #define for_each_inactive_policy(__policy) \
90 for_each_suitable_policy(__policy, false)
92 #define for_each_policy(__policy) \
93 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
95 /* Iterate over governors */
96 static LIST_HEAD(cpufreq_governor_list);
97 #define for_each_governor(__governor) \
98 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
101 * The "cpufreq driver" - the arch- or hardware-dependent low
102 * level driver of CPUFreq support, and its spinlock. This lock
103 * also protects the cpufreq_cpu_data array.
105 static struct cpufreq_driver *cpufreq_driver;
106 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
107 static DEFINE_RWLOCK(cpufreq_driver_lock);
108 DEFINE_MUTEX(cpufreq_governor_lock);
110 /* Flag to suspend/resume CPUFreq governors */
111 static bool cpufreq_suspended;
113 static inline bool has_target(void)
115 return cpufreq_driver->target_index || cpufreq_driver->target;
118 /* internal prototypes */
119 static int __cpufreq_governor(struct cpufreq_policy *policy,
121 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
122 static void handle_update(struct work_struct *work);
125 * Two notifier lists: the "policy" list is involved in the
126 * validation process for a new CPU frequency policy; the
127 * "transition" list for kernel code that needs to handle
128 * changes to devices when the CPU clock speed changes.
129 * The mutex locks both lists.
131 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
132 static struct srcu_notifier_head cpufreq_transition_notifier_list;
134 static bool init_cpufreq_transition_notifier_list_called;
135 static int __init init_cpufreq_transition_notifier_list(void)
137 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
138 init_cpufreq_transition_notifier_list_called = true;
141 pure_initcall(init_cpufreq_transition_notifier_list);
143 static int off __read_mostly;
144 static int cpufreq_disabled(void)
148 void disable_cpufreq(void)
152 static DEFINE_MUTEX(cpufreq_governor_mutex);
154 bool have_governor_per_policy(void)
156 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
158 EXPORT_SYMBOL_GPL(have_governor_per_policy);
160 bool cpufreq_driver_is_slow(void)
162 return !(cpufreq_driver->flags & CPUFREQ_DRIVER_FAST);
164 EXPORT_SYMBOL_GPL(cpufreq_driver_is_slow);
166 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
168 if (have_governor_per_policy())
169 return &policy->kobj;
171 return cpufreq_global_kobject;
173 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
175 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
177 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
179 return policy && !policy_is_inactive(policy) ?
180 policy->freq_table : NULL;
182 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
184 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
190 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
192 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
193 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
194 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
195 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
196 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
197 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
199 idle_time = cur_wall_time - busy_time;
201 *wall = cputime_to_usecs(cur_wall_time);
203 return cputime_to_usecs(idle_time);
206 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
208 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
210 if (idle_time == -1ULL)
211 return get_cpu_idle_time_jiffy(cpu, wall);
213 idle_time += get_cpu_iowait_time_us(cpu, wall);
217 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
220 * This is a generic cpufreq init() routine which can be used by cpufreq
221 * drivers of SMP systems. It will do following:
222 * - validate & show freq table passed
223 * - set policies transition latency
224 * - policy->cpus with all possible CPUs
226 int cpufreq_generic_init(struct cpufreq_policy *policy,
227 struct cpufreq_frequency_table *table,
228 unsigned int transition_latency)
232 ret = cpufreq_table_validate_and_show(policy, table);
234 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
238 policy->cpuinfo.transition_latency = transition_latency;
241 * The driver only supports the SMP configuration where all processors
242 * share the clock and voltage and clock.
244 cpumask_setall(policy->cpus);
248 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
250 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
252 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
254 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
256 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
258 unsigned int cpufreq_generic_get(unsigned int cpu)
260 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
262 if (!policy || IS_ERR(policy->clk)) {
263 pr_err("%s: No %s associated to cpu: %d\n",
264 __func__, policy ? "clk" : "policy", cpu);
268 return clk_get_rate(policy->clk) / 1000;
270 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
273 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
275 * @cpu: cpu to find policy for.
277 * This returns policy for 'cpu', returns NULL if it doesn't exist.
278 * It also increments the kobject reference count to mark it busy and so would
279 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
280 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
281 * freed as that depends on the kobj count.
283 * Return: A valid policy on success, otherwise NULL on failure.
285 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
287 struct cpufreq_policy *policy = NULL;
290 if (WARN_ON(cpu >= nr_cpu_ids))
293 /* get the cpufreq driver */
294 read_lock_irqsave(&cpufreq_driver_lock, flags);
296 if (cpufreq_driver) {
298 policy = cpufreq_cpu_get_raw(cpu);
300 kobject_get(&policy->kobj);
303 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
307 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
310 * cpufreq_cpu_put: Decrements the usage count of a policy
312 * @policy: policy earlier returned by cpufreq_cpu_get().
314 * This decrements the kobject reference count incremented earlier by calling
317 void cpufreq_cpu_put(struct cpufreq_policy *policy)
319 kobject_put(&policy->kobj);
321 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
323 /*********************************************************************
324 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
325 *********************************************************************/
328 * adjust_jiffies - adjust the system "loops_per_jiffy"
330 * This function alters the system "loops_per_jiffy" for the clock
331 * speed change. Note that loops_per_jiffy cannot be updated on SMP
332 * systems as each CPU might be scaled differently. So, use the arch
333 * per-CPU loops_per_jiffy value wherever possible.
335 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
338 static unsigned long l_p_j_ref;
339 static unsigned int l_p_j_ref_freq;
341 if (ci->flags & CPUFREQ_CONST_LOOPS)
344 if (!l_p_j_ref_freq) {
345 l_p_j_ref = loops_per_jiffy;
346 l_p_j_ref_freq = ci->old;
347 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
348 l_p_j_ref, l_p_j_ref_freq);
350 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
351 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
353 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
354 loops_per_jiffy, ci->new);
359 /*********************************************************************
360 * FREQUENCY INVARIANT CPU CAPACITY *
361 *********************************************************************/
363 static DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
364 static DEFINE_PER_CPU(unsigned long, max_freq_scale) = SCHED_CAPACITY_SCALE;
367 scale_freq_capacity(struct cpufreq_policy *policy, struct cpufreq_freqs *freqs)
369 unsigned long cur = freqs ? freqs->new : policy->cur;
370 unsigned long scale = (cur << SCHED_CAPACITY_SHIFT) / policy->max;
371 struct cpufreq_cpuinfo *cpuinfo = &policy->cpuinfo;
374 pr_debug("cpus %*pbl cur/cur max freq %lu/%u kHz freq scale %lu\n",
375 cpumask_pr_args(policy->cpus), cur, policy->max, scale);
377 for_each_cpu(cpu, policy->cpus)
378 per_cpu(freq_scale, cpu) = scale;
383 scale = (policy->max << SCHED_CAPACITY_SHIFT) / cpuinfo->max_freq;
385 pr_debug("cpus %*pbl cur max/max freq %u/%u kHz max freq scale %lu\n",
386 cpumask_pr_args(policy->cpus), policy->max, cpuinfo->max_freq,
389 for_each_cpu(cpu, policy->cpus)
390 per_cpu(max_freq_scale, cpu) = scale;
393 unsigned long cpufreq_scale_freq_capacity(struct sched_domain *sd, int cpu)
395 return per_cpu(freq_scale, cpu);
398 unsigned long cpufreq_scale_max_freq_capacity(int cpu)
400 return per_cpu(max_freq_scale, cpu);
403 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
404 struct cpufreq_freqs *freqs, unsigned int state)
406 BUG_ON(irqs_disabled());
408 if (cpufreq_disabled())
411 freqs->flags = cpufreq_driver->flags;
412 pr_debug("notification %u of frequency transition to %u kHz\n",
417 case CPUFREQ_PRECHANGE:
418 /* detect if the driver reported a value as "old frequency"
419 * which is not equal to what the cpufreq core thinks is
422 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
423 if ((policy) && (policy->cpu == freqs->cpu) &&
424 (policy->cur) && (policy->cur != freqs->old)) {
425 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
426 freqs->old, policy->cur);
427 freqs->old = policy->cur;
430 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
431 CPUFREQ_PRECHANGE, freqs);
432 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
435 case CPUFREQ_POSTCHANGE:
436 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
437 pr_debug("FREQ: %lu - CPU: %lu\n",
438 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
439 trace_cpu_frequency(freqs->new, freqs->cpu);
440 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
441 CPUFREQ_POSTCHANGE, freqs);
442 if (likely(policy) && likely(policy->cpu == freqs->cpu))
443 policy->cur = freqs->new;
449 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
450 * on frequency transition.
452 * This function calls the transition notifiers and the "adjust_jiffies"
453 * function. It is called twice on all CPU frequency changes that have
456 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
457 struct cpufreq_freqs *freqs, unsigned int state)
459 for_each_cpu(freqs->cpu, policy->cpus)
460 __cpufreq_notify_transition(policy, freqs, state);
463 /* Do post notifications when there are chances that transition has failed */
464 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
465 struct cpufreq_freqs *freqs, int transition_failed)
467 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
468 if (!transition_failed)
471 swap(freqs->old, freqs->new);
472 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
473 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
476 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
477 struct cpufreq_freqs *freqs)
484 * Catch double invocations of _begin() which lead to self-deadlock.
485 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
486 * doesn't invoke _begin() on their behalf, and hence the chances of
487 * double invocations are very low. Moreover, there are scenarios
488 * where these checks can emit false-positive warnings in these
489 * drivers; so we avoid that by skipping them altogether.
491 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
492 && current == policy->transition_task);
495 wait_event(policy->transition_wait, !policy->transition_ongoing);
497 spin_lock(&policy->transition_lock);
499 if (unlikely(policy->transition_ongoing)) {
500 spin_unlock(&policy->transition_lock);
504 policy->transition_ongoing = true;
505 policy->transition_task = current;
507 spin_unlock(&policy->transition_lock);
509 scale_freq_capacity(policy, freqs);
511 for_each_cpu(cpu, policy->cpus)
512 trace_cpu_capacity(capacity_curr_of(cpu), cpu);
515 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
517 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
519 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
520 struct cpufreq_freqs *freqs, int transition_failed)
522 if (unlikely(WARN_ON(!policy->transition_ongoing)))
525 cpufreq_notify_post_transition(policy, freqs, transition_failed);
527 policy->transition_ongoing = false;
528 policy->transition_task = NULL;
530 wake_up(&policy->transition_wait);
532 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
535 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
537 * @target_freq: target frequency to resolve.
539 * The target to driver frequency mapping is cached in the policy.
541 * Return: Lowest driver-supported frequency greater than or equal to the
542 * given target_freq, subject to policy (min/max) and driver limitations.
544 unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
545 unsigned int target_freq)
547 target_freq = clamp_val(target_freq, policy->min, policy->max);
548 policy->cached_target_freq = target_freq;
550 if (cpufreq_driver->target_index) {
553 rv = cpufreq_frequency_table_target(policy, policy->freq_table,
559 policy->cached_resolved_idx = idx;
560 return policy->freq_table[idx].frequency;
565 EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
567 /*********************************************************************
569 *********************************************************************/
570 static ssize_t show_boost(struct kobject *kobj,
571 struct attribute *attr, char *buf)
573 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
576 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
577 const char *buf, size_t count)
581 ret = sscanf(buf, "%d", &enable);
582 if (ret != 1 || enable < 0 || enable > 1)
585 if (cpufreq_boost_trigger_state(enable)) {
586 pr_err("%s: Cannot %s BOOST!\n",
587 __func__, enable ? "enable" : "disable");
591 pr_debug("%s: cpufreq BOOST %s\n",
592 __func__, enable ? "enabled" : "disabled");
596 define_one_global_rw(boost);
598 static struct cpufreq_governor *find_governor(const char *str_governor)
600 struct cpufreq_governor *t;
603 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
610 * cpufreq_parse_governor - parse a governor string
612 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
613 struct cpufreq_governor **governor)
617 if (cpufreq_driver->setpolicy) {
618 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
619 *policy = CPUFREQ_POLICY_PERFORMANCE;
621 } else if (!strncasecmp(str_governor, "powersave",
623 *policy = CPUFREQ_POLICY_POWERSAVE;
627 struct cpufreq_governor *t;
629 mutex_lock(&cpufreq_governor_mutex);
631 t = find_governor(str_governor);
636 mutex_unlock(&cpufreq_governor_mutex);
637 ret = request_module("cpufreq_%s", str_governor);
638 mutex_lock(&cpufreq_governor_mutex);
641 t = find_governor(str_governor);
649 mutex_unlock(&cpufreq_governor_mutex);
655 * cpufreq_per_cpu_attr_read() / show_##file_name() -
656 * print out cpufreq information
658 * Write out information from cpufreq_driver->policy[cpu]; object must be
662 #define show_one(file_name, object) \
663 static ssize_t show_##file_name \
664 (struct cpufreq_policy *policy, char *buf) \
666 return sprintf(buf, "%u\n", policy->object); \
669 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
670 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
671 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
672 show_one(scaling_min_freq, min);
673 show_one(scaling_max_freq, max);
675 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
679 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
680 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
682 ret = sprintf(buf, "%u\n", policy->cur);
686 static int cpufreq_set_policy(struct cpufreq_policy *policy,
687 struct cpufreq_policy *new_policy);
690 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
692 #define store_one(file_name, object) \
693 static ssize_t store_##file_name \
694 (struct cpufreq_policy *policy, const char *buf, size_t count) \
697 struct cpufreq_policy new_policy; \
699 memcpy(&new_policy, policy, sizeof(*policy)); \
701 ret = sscanf(buf, "%u", &new_policy.object); \
705 temp = new_policy.object; \
706 ret = cpufreq_set_policy(policy, &new_policy); \
708 policy->user_policy.object = temp; \
710 return ret ? ret : count; \
713 store_one(scaling_min_freq, min);
714 store_one(scaling_max_freq, max);
717 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
719 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
722 unsigned int cur_freq = __cpufreq_get(policy);
725 return sprintf(buf, "%u\n", cur_freq);
727 return sprintf(buf, "<unknown>\n");
731 * show_scaling_governor - show the current policy for the specified CPU
733 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
735 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
736 return sprintf(buf, "powersave\n");
737 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
738 return sprintf(buf, "performance\n");
739 else if (policy->governor)
740 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
741 policy->governor->name);
746 * store_scaling_governor - store policy for the specified CPU
748 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
749 const char *buf, size_t count)
752 char str_governor[16];
753 struct cpufreq_policy new_policy;
755 memcpy(&new_policy, policy, sizeof(*policy));
757 ret = sscanf(buf, "%15s", str_governor);
761 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
762 &new_policy.governor))
765 ret = cpufreq_set_policy(policy, &new_policy);
766 return ret ? ret : count;
770 * show_scaling_driver - show the cpufreq driver currently loaded
772 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
774 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
778 * show_scaling_available_governors - show the available CPUfreq governors
780 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
784 struct cpufreq_governor *t;
787 i += sprintf(buf, "performance powersave");
791 for_each_governor(t) {
792 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
793 - (CPUFREQ_NAME_LEN + 2)))
795 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
798 i += sprintf(&buf[i], "\n");
802 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
807 for_each_cpu(cpu, mask) {
809 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
810 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
811 if (i >= (PAGE_SIZE - 5))
814 i += sprintf(&buf[i], "\n");
817 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
820 * show_related_cpus - show the CPUs affected by each transition even if
821 * hw coordination is in use
823 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
825 return cpufreq_show_cpus(policy->related_cpus, buf);
829 * show_affected_cpus - show the CPUs affected by each transition
831 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
833 return cpufreq_show_cpus(policy->cpus, buf);
836 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
837 const char *buf, size_t count)
839 unsigned int freq = 0;
842 if (!policy->governor || !policy->governor->store_setspeed)
845 ret = sscanf(buf, "%u", &freq);
849 policy->governor->store_setspeed(policy, freq);
854 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
856 if (!policy->governor || !policy->governor->show_setspeed)
857 return sprintf(buf, "<unsupported>\n");
859 return policy->governor->show_setspeed(policy, buf);
863 * show_bios_limit - show the current cpufreq HW/BIOS limitation
865 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
869 if (cpufreq_driver->bios_limit) {
870 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
872 return sprintf(buf, "%u\n", limit);
874 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
877 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
878 cpufreq_freq_attr_ro(cpuinfo_min_freq);
879 cpufreq_freq_attr_ro(cpuinfo_max_freq);
880 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
881 cpufreq_freq_attr_ro(scaling_available_governors);
882 cpufreq_freq_attr_ro(scaling_driver);
883 cpufreq_freq_attr_ro(scaling_cur_freq);
884 cpufreq_freq_attr_ro(bios_limit);
885 cpufreq_freq_attr_ro(related_cpus);
886 cpufreq_freq_attr_ro(affected_cpus);
887 cpufreq_freq_attr_rw(scaling_min_freq);
888 cpufreq_freq_attr_rw(scaling_max_freq);
889 cpufreq_freq_attr_rw(scaling_governor);
890 cpufreq_freq_attr_rw(scaling_setspeed);
892 static struct attribute *default_attrs[] = {
893 &cpuinfo_min_freq.attr,
894 &cpuinfo_max_freq.attr,
895 &cpuinfo_transition_latency.attr,
896 &scaling_min_freq.attr,
897 &scaling_max_freq.attr,
900 &scaling_governor.attr,
901 &scaling_driver.attr,
902 &scaling_available_governors.attr,
903 &scaling_setspeed.attr,
907 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
908 #define to_attr(a) container_of(a, struct freq_attr, attr)
910 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
912 struct cpufreq_policy *policy = to_policy(kobj);
913 struct freq_attr *fattr = to_attr(attr);
916 down_read(&policy->rwsem);
919 ret = fattr->show(policy, buf);
923 up_read(&policy->rwsem);
928 static ssize_t store(struct kobject *kobj, struct attribute *attr,
929 const char *buf, size_t count)
931 struct cpufreq_policy *policy = to_policy(kobj);
932 struct freq_attr *fattr = to_attr(attr);
933 ssize_t ret = -EINVAL;
937 if (!cpu_online(policy->cpu))
940 down_write(&policy->rwsem);
943 ret = fattr->store(policy, buf, count);
947 up_write(&policy->rwsem);
954 static void cpufreq_sysfs_release(struct kobject *kobj)
956 struct cpufreq_policy *policy = to_policy(kobj);
957 pr_debug("last reference is dropped\n");
958 complete(&policy->kobj_unregister);
961 static const struct sysfs_ops sysfs_ops = {
966 static struct kobj_type ktype_cpufreq = {
967 .sysfs_ops = &sysfs_ops,
968 .default_attrs = default_attrs,
969 .release = cpufreq_sysfs_release,
972 static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
974 struct device *cpu_dev;
976 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
981 cpu_dev = get_cpu_device(cpu);
982 if (WARN_ON(!cpu_dev))
985 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
988 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
990 struct device *cpu_dev;
992 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
994 cpu_dev = get_cpu_device(cpu);
995 if (WARN_ON(!cpu_dev))
998 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1001 /* Add/remove symlinks for all related CPUs */
1002 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
1007 /* Some related CPUs might not be present (physically hotplugged) */
1008 for_each_cpu(j, policy->real_cpus) {
1009 ret = add_cpu_dev_symlink(policy, j);
1017 static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
1021 /* Some related CPUs might not be present (physically hotplugged) */
1022 for_each_cpu(j, policy->real_cpus)
1023 remove_cpu_dev_symlink(policy, j);
1026 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
1028 struct freq_attr **drv_attr;
1031 /* set up files for this cpu device */
1032 drv_attr = cpufreq_driver->attr;
1033 while (drv_attr && *drv_attr) {
1034 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1039 if (cpufreq_driver->get) {
1040 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1045 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1049 if (cpufreq_driver->bios_limit) {
1050 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1055 return cpufreq_add_dev_symlink(policy);
1058 static int cpufreq_init_policy(struct cpufreq_policy *policy)
1060 struct cpufreq_governor *gov = NULL;
1061 struct cpufreq_policy new_policy;
1063 memcpy(&new_policy, policy, sizeof(*policy));
1065 /* Update governor of new_policy to the governor used before hotplug */
1066 gov = find_governor(policy->last_governor);
1068 pr_debug("Restoring governor %s for cpu %d\n",
1069 policy->governor->name, policy->cpu);
1071 gov = CPUFREQ_DEFAULT_GOVERNOR;
1073 new_policy.governor = gov;
1075 /* Use the default policy if there is no last_policy. */
1076 if (cpufreq_driver->setpolicy) {
1077 if (policy->last_policy)
1078 new_policy.policy = policy->last_policy;
1080 cpufreq_parse_governor(gov->name, &new_policy.policy,
1083 /* set default policy */
1084 return cpufreq_set_policy(policy, &new_policy);
1087 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1091 /* Has this CPU been taken care of already? */
1092 if (cpumask_test_cpu(cpu, policy->cpus))
1096 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1098 pr_err("%s: Failed to stop governor\n", __func__);
1103 down_write(&policy->rwsem);
1104 cpumask_set_cpu(cpu, policy->cpus);
1105 up_write(&policy->rwsem);
1108 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1110 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1113 pr_err("%s: Failed to start governor\n", __func__);
1121 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1123 struct device *dev = get_cpu_device(cpu);
1124 struct cpufreq_policy *policy;
1129 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1133 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1134 goto err_free_policy;
1136 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1137 goto err_free_cpumask;
1139 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1140 goto err_free_rcpumask;
1142 kobject_init(&policy->kobj, &ktype_cpufreq);
1143 INIT_LIST_HEAD(&policy->policy_list);
1144 init_rwsem(&policy->rwsem);
1145 spin_lock_init(&policy->transition_lock);
1146 init_waitqueue_head(&policy->transition_wait);
1147 init_completion(&policy->kobj_unregister);
1148 INIT_WORK(&policy->update, handle_update);
1154 free_cpumask_var(policy->related_cpus);
1156 free_cpumask_var(policy->cpus);
1163 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
1165 struct kobject *kobj;
1166 struct completion *cmp;
1169 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1170 CPUFREQ_REMOVE_POLICY, policy);
1172 down_write(&policy->rwsem);
1173 cpufreq_remove_dev_symlink(policy);
1174 kobj = &policy->kobj;
1175 cmp = &policy->kobj_unregister;
1176 up_write(&policy->rwsem);
1180 * We need to make sure that the underlying kobj is
1181 * actually not referenced anymore by anybody before we
1182 * proceed with unloading.
1184 pr_debug("waiting for dropping of refcount\n");
1185 wait_for_completion(cmp);
1186 pr_debug("wait complete\n");
1189 static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
1191 unsigned long flags;
1194 /* Remove policy from list */
1195 write_lock_irqsave(&cpufreq_driver_lock, flags);
1196 list_del(&policy->policy_list);
1198 for_each_cpu(cpu, policy->related_cpus)
1199 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1200 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1202 cpufreq_policy_put_kobj(policy, notify);
1203 free_cpumask_var(policy->real_cpus);
1204 free_cpumask_var(policy->related_cpus);
1205 free_cpumask_var(policy->cpus);
1209 static int cpufreq_online(unsigned int cpu)
1211 struct cpufreq_policy *policy;
1213 unsigned long flags;
1217 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1219 /* Check if this CPU already has a policy to manage it */
1220 policy = per_cpu(cpufreq_cpu_data, cpu);
1222 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1223 if (!policy_is_inactive(policy))
1224 return cpufreq_add_policy_cpu(policy, cpu);
1226 /* This is the only online CPU for the policy. Start over. */
1228 down_write(&policy->rwsem);
1230 policy->governor = NULL;
1231 up_write(&policy->rwsem);
1234 policy = cpufreq_policy_alloc(cpu);
1239 cpumask_copy(policy->cpus, cpumask_of(cpu));
1241 /* call driver. From then on the cpufreq must be able
1242 * to accept all calls to ->verify and ->setpolicy for this CPU
1244 ret = cpufreq_driver->init(policy);
1246 pr_debug("initialization failed\n");
1247 goto out_free_policy;
1250 down_write(&policy->rwsem);
1253 /* related_cpus should at least include policy->cpus. */
1254 cpumask_copy(policy->related_cpus, policy->cpus);
1255 /* Remember CPUs present at the policy creation time. */
1256 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
1258 /* Name and add the kobject */
1259 ret = kobject_add(&policy->kobj, cpufreq_global_kobject,
1261 cpumask_first(policy->related_cpus));
1263 pr_err("%s: failed to add policy->kobj: %d\n", __func__,
1265 goto out_exit_policy;
1270 * affected cpus must always be the one, which are online. We aren't
1271 * managing offline cpus here.
1273 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1276 policy->user_policy.min = policy->min;
1277 policy->user_policy.max = policy->max;
1279 write_lock_irqsave(&cpufreq_driver_lock, flags);
1280 for_each_cpu(j, policy->related_cpus)
1281 per_cpu(cpufreq_cpu_data, j) = policy;
1282 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1284 policy->min = policy->user_policy.min;
1285 policy->max = policy->user_policy.max;
1288 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1289 policy->cur = cpufreq_driver->get(policy->cpu);
1291 pr_err("%s: ->get() failed\n", __func__);
1292 goto out_exit_policy;
1297 * Sometimes boot loaders set CPU frequency to a value outside of
1298 * frequency table present with cpufreq core. In such cases CPU might be
1299 * unstable if it has to run on that frequency for long duration of time
1300 * and so its better to set it to a frequency which is specified in
1301 * freq-table. This also makes cpufreq stats inconsistent as
1302 * cpufreq-stats would fail to register because current frequency of CPU
1303 * isn't found in freq-table.
1305 * Because we don't want this change to effect boot process badly, we go
1306 * for the next freq which is >= policy->cur ('cur' must be set by now,
1307 * otherwise we will end up setting freq to lowest of the table as 'cur'
1308 * is initialized to zero).
1310 * We are passing target-freq as "policy->cur - 1" otherwise
1311 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1312 * equal to target-freq.
1314 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1316 /* Are we running at unknown frequency ? */
1317 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1318 if (ret == -EINVAL) {
1319 /* Warn user and fix it */
1320 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1321 __func__, policy->cpu, policy->cur);
1322 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1323 CPUFREQ_RELATION_L);
1326 * Reaching here after boot in a few seconds may not
1327 * mean that system will remain stable at "unknown"
1328 * frequency for longer duration. Hence, a BUG_ON().
1331 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1332 __func__, policy->cpu, policy->cur);
1336 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1337 CPUFREQ_START, policy);
1340 ret = cpufreq_add_dev_interface(policy);
1342 goto out_exit_policy;
1343 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1344 CPUFREQ_CREATE_POLICY, policy);
1346 write_lock_irqsave(&cpufreq_driver_lock, flags);
1347 list_add(&policy->policy_list, &cpufreq_policy_list);
1348 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1351 ret = cpufreq_init_policy(policy);
1353 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1354 __func__, cpu, ret);
1355 /* cpufreq_policy_free() will notify based on this */
1357 goto out_exit_policy;
1360 up_write(&policy->rwsem);
1362 kobject_uevent(&policy->kobj, KOBJ_ADD);
1364 /* Callback for handling stuff after policy is ready */
1365 if (cpufreq_driver->ready)
1366 cpufreq_driver->ready(policy);
1368 pr_debug("initialization complete\n");
1373 up_write(&policy->rwsem);
1375 if (cpufreq_driver->exit)
1376 cpufreq_driver->exit(policy);
1378 cpufreq_policy_free(policy, !new_policy);
1383 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1385 * @sif: Subsystem interface structure pointer (not used)
1387 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1389 unsigned cpu = dev->id;
1392 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1394 if (cpu_online(cpu)) {
1395 ret = cpufreq_online(cpu);
1398 * A hotplug notifier will follow and we will handle it as CPU
1399 * online then. For now, just create the sysfs link, unless
1400 * there is no policy or the link is already present.
1402 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1404 ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus)
1405 ? add_cpu_dev_symlink(policy, cpu) : 0;
1411 static void cpufreq_offline_prepare(unsigned int cpu)
1413 struct cpufreq_policy *policy;
1415 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1417 policy = cpufreq_cpu_get_raw(cpu);
1419 pr_debug("%s: No cpu_data found\n", __func__);
1424 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1426 pr_err("%s: Failed to stop governor\n", __func__);
1429 down_write(&policy->rwsem);
1430 cpumask_clear_cpu(cpu, policy->cpus);
1432 if (policy_is_inactive(policy)) {
1434 strncpy(policy->last_governor, policy->governor->name,
1437 policy->last_policy = policy->policy;
1438 } else if (cpu == policy->cpu) {
1439 /* Nominate new CPU */
1440 policy->cpu = cpumask_any(policy->cpus);
1442 up_write(&policy->rwsem);
1444 /* Start governor again for active policy */
1445 if (!policy_is_inactive(policy)) {
1447 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1449 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1452 pr_err("%s: Failed to start governor\n", __func__);
1454 } else if (cpufreq_driver->stop_cpu) {
1455 cpufreq_driver->stop_cpu(policy);
1459 static void cpufreq_offline_finish(unsigned int cpu)
1461 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1464 pr_debug("%s: No cpu_data found\n", __func__);
1468 /* Only proceed for inactive policies */
1469 if (!policy_is_inactive(policy))
1472 /* If cpu is last user of policy, free policy */
1474 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
1476 pr_err("%s: Failed to exit governor\n", __func__);
1480 * Perform the ->exit() even during light-weight tear-down,
1481 * since this is a core component, and is essential for the
1482 * subsequent light-weight ->init() to succeed.
1484 if (cpufreq_driver->exit) {
1485 cpufreq_driver->exit(policy);
1486 policy->freq_table = NULL;
1491 * cpufreq_remove_dev - remove a CPU device
1493 * Removes the cpufreq interface for a CPU device.
1495 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1497 unsigned int cpu = dev->id;
1498 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1503 if (cpu_online(cpu)) {
1504 cpufreq_offline_prepare(cpu);
1505 cpufreq_offline_finish(cpu);
1508 cpumask_clear_cpu(cpu, policy->real_cpus);
1509 remove_cpu_dev_symlink(policy, cpu);
1511 if (cpumask_empty(policy->real_cpus))
1512 cpufreq_policy_free(policy, true);
1515 static void handle_update(struct work_struct *work)
1517 struct cpufreq_policy *policy =
1518 container_of(work, struct cpufreq_policy, update);
1519 unsigned int cpu = policy->cpu;
1520 pr_debug("handle_update for cpu %u called\n", cpu);
1521 cpufreq_update_policy(cpu);
1525 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1527 * @policy: policy managing CPUs
1528 * @new_freq: CPU frequency the CPU actually runs at
1530 * We adjust to current frequency first, and need to clean up later.
1531 * So either call to cpufreq_update_policy() or schedule handle_update()).
1533 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1534 unsigned int new_freq)
1536 struct cpufreq_freqs freqs;
1538 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1539 policy->cur, new_freq);
1541 freqs.old = policy->cur;
1542 freqs.new = new_freq;
1544 cpufreq_freq_transition_begin(policy, &freqs);
1545 cpufreq_freq_transition_end(policy, &freqs, 0);
1549 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1552 * This is the last known freq, without actually getting it from the driver.
1553 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1555 unsigned int cpufreq_quick_get(unsigned int cpu)
1557 struct cpufreq_policy *policy;
1558 unsigned int ret_freq = 0;
1560 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1561 return cpufreq_driver->get(cpu);
1563 policy = cpufreq_cpu_get(cpu);
1565 ret_freq = policy->cur;
1566 cpufreq_cpu_put(policy);
1571 EXPORT_SYMBOL(cpufreq_quick_get);
1574 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1577 * Just return the max possible frequency for a given CPU.
1579 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1581 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1582 unsigned int ret_freq = 0;
1585 ret_freq = policy->max;
1586 cpufreq_cpu_put(policy);
1591 EXPORT_SYMBOL(cpufreq_quick_get_max);
1593 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1595 unsigned int ret_freq = 0;
1597 if (!cpufreq_driver->get)
1600 ret_freq = cpufreq_driver->get(policy->cpu);
1602 /* Updating inactive policies is invalid, so avoid doing that. */
1603 if (unlikely(policy_is_inactive(policy)))
1606 if (ret_freq && policy->cur &&
1607 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1608 /* verify no discrepancy between actual and
1609 saved value exists */
1610 if (unlikely(ret_freq != policy->cur)) {
1611 cpufreq_out_of_sync(policy, ret_freq);
1612 schedule_work(&policy->update);
1620 * cpufreq_get - get the current CPU frequency (in kHz)
1623 * Get the CPU current (static) CPU frequency
1625 unsigned int cpufreq_get(unsigned int cpu)
1627 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1628 unsigned int ret_freq = 0;
1631 down_read(&policy->rwsem);
1632 ret_freq = __cpufreq_get(policy);
1633 up_read(&policy->rwsem);
1635 cpufreq_cpu_put(policy);
1640 EXPORT_SYMBOL(cpufreq_get);
1642 static struct subsys_interface cpufreq_interface = {
1644 .subsys = &cpu_subsys,
1645 .add_dev = cpufreq_add_dev,
1646 .remove_dev = cpufreq_remove_dev,
1650 * In case platform wants some specific frequency to be configured
1653 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1657 if (!policy->suspend_freq) {
1658 pr_debug("%s: suspend_freq not defined\n", __func__);
1662 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1663 policy->suspend_freq);
1665 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1666 CPUFREQ_RELATION_H);
1668 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1669 __func__, policy->suspend_freq, ret);
1673 EXPORT_SYMBOL(cpufreq_generic_suspend);
1676 * cpufreq_suspend() - Suspend CPUFreq governors
1678 * Called during system wide Suspend/Hibernate cycles for suspending governors
1679 * as some platforms can't change frequency after this point in suspend cycle.
1680 * Because some of the devices (like: i2c, regulators, etc) they use for
1681 * changing frequency are suspended quickly after this point.
1683 void cpufreq_suspend(void)
1685 struct cpufreq_policy *policy;
1687 if (!cpufreq_driver)
1693 pr_debug("%s: Suspending Governors\n", __func__);
1695 for_each_active_policy(policy) {
1696 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1697 pr_err("%s: Failed to stop governor for policy: %p\n",
1699 else if (cpufreq_driver->suspend
1700 && cpufreq_driver->suspend(policy))
1701 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1706 cpufreq_suspended = true;
1710 * cpufreq_resume() - Resume CPUFreq governors
1712 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1713 * are suspended with cpufreq_suspend().
1715 void cpufreq_resume(void)
1717 struct cpufreq_policy *policy;
1719 if (!cpufreq_driver)
1722 cpufreq_suspended = false;
1727 pr_debug("%s: Resuming Governors\n", __func__);
1729 for_each_active_policy(policy) {
1730 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1731 pr_err("%s: Failed to resume driver: %p\n", __func__,
1733 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
1734 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1735 pr_err("%s: Failed to start governor for policy: %p\n",
1740 * schedule call cpufreq_update_policy() for first-online CPU, as that
1741 * wouldn't be hotplugged-out on suspend. It will verify that the
1742 * current freq is in sync with what we believe it to be.
1744 policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1745 if (WARN_ON(!policy))
1748 schedule_work(&policy->update);
1752 * cpufreq_get_current_driver - return current driver's name
1754 * Return the name string of the currently loaded cpufreq driver
1757 const char *cpufreq_get_current_driver(void)
1760 return cpufreq_driver->name;
1764 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1767 * cpufreq_get_driver_data - return current driver data
1769 * Return the private data of the currently loaded cpufreq
1770 * driver, or NULL if no cpufreq driver is loaded.
1772 void *cpufreq_get_driver_data(void)
1775 return cpufreq_driver->driver_data;
1779 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1781 /*********************************************************************
1782 * NOTIFIER LISTS INTERFACE *
1783 *********************************************************************/
1786 * cpufreq_register_notifier - register a driver with cpufreq
1787 * @nb: notifier function to register
1788 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1790 * Add a driver to one of two lists: either a list of drivers that
1791 * are notified about clock rate changes (once before and once after
1792 * the transition), or a list of drivers that are notified about
1793 * changes in cpufreq policy.
1795 * This function may sleep, and has the same return conditions as
1796 * blocking_notifier_chain_register.
1798 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1802 if (cpufreq_disabled())
1805 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1808 case CPUFREQ_TRANSITION_NOTIFIER:
1809 ret = srcu_notifier_chain_register(
1810 &cpufreq_transition_notifier_list, nb);
1812 case CPUFREQ_POLICY_NOTIFIER:
1813 ret = blocking_notifier_chain_register(
1814 &cpufreq_policy_notifier_list, nb);
1822 EXPORT_SYMBOL(cpufreq_register_notifier);
1825 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1826 * @nb: notifier block to be unregistered
1827 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1829 * Remove a driver from the CPU frequency notifier list.
1831 * This function may sleep, and has the same return conditions as
1832 * blocking_notifier_chain_unregister.
1834 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1838 if (cpufreq_disabled())
1842 case CPUFREQ_TRANSITION_NOTIFIER:
1843 ret = srcu_notifier_chain_unregister(
1844 &cpufreq_transition_notifier_list, nb);
1846 case CPUFREQ_POLICY_NOTIFIER:
1847 ret = blocking_notifier_chain_unregister(
1848 &cpufreq_policy_notifier_list, nb);
1856 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1859 /*********************************************************************
1861 *********************************************************************/
1863 /* Must set freqs->new to intermediate frequency */
1864 static int __target_intermediate(struct cpufreq_policy *policy,
1865 struct cpufreq_freqs *freqs, int index)
1869 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1871 /* We don't need to switch to intermediate freq */
1875 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1876 __func__, policy->cpu, freqs->old, freqs->new);
1878 cpufreq_freq_transition_begin(policy, freqs);
1879 ret = cpufreq_driver->target_intermediate(policy, index);
1880 cpufreq_freq_transition_end(policy, freqs, ret);
1883 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1889 static int __target_index(struct cpufreq_policy *policy,
1890 struct cpufreq_frequency_table *freq_table, int index)
1892 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1893 unsigned int intermediate_freq = 0;
1894 int retval = -EINVAL;
1897 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1899 /* Handle switching to intermediate frequency */
1900 if (cpufreq_driver->get_intermediate) {
1901 retval = __target_intermediate(policy, &freqs, index);
1905 intermediate_freq = freqs.new;
1906 /* Set old freq to intermediate */
1907 if (intermediate_freq)
1908 freqs.old = freqs.new;
1911 freqs.new = freq_table[index].frequency;
1912 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1913 __func__, policy->cpu, freqs.old, freqs.new);
1915 cpufreq_freq_transition_begin(policy, &freqs);
1918 retval = cpufreq_driver->target_index(policy, index);
1920 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1924 cpufreq_freq_transition_end(policy, &freqs, retval);
1927 * Failed after setting to intermediate freq? Driver should have
1928 * reverted back to initial frequency and so should we. Check
1929 * here for intermediate_freq instead of get_intermediate, in
1930 * case we haven't switched to intermediate freq at all.
1932 if (unlikely(retval && intermediate_freq)) {
1933 freqs.old = intermediate_freq;
1934 freqs.new = policy->restore_freq;
1935 cpufreq_freq_transition_begin(policy, &freqs);
1936 cpufreq_freq_transition_end(policy, &freqs, 0);
1943 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1944 unsigned int target_freq,
1945 unsigned int relation)
1947 unsigned int old_target_freq = target_freq;
1948 int retval = -EINVAL;
1950 if (cpufreq_disabled())
1953 /* Make sure that target_freq is within supported range */
1954 if (target_freq > policy->max)
1955 target_freq = policy->max;
1956 if (target_freq < policy->min)
1957 target_freq = policy->min;
1959 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1960 policy->cpu, target_freq, relation, old_target_freq);
1963 * This might look like a redundant call as we are checking it again
1964 * after finding index. But it is left intentionally for cases where
1965 * exactly same freq is called again and so we can save on few function
1968 if (target_freq == policy->cur)
1971 /* Save last value to restore later on errors */
1972 policy->restore_freq = policy->cur;
1974 if (cpufreq_driver->target)
1975 retval = cpufreq_driver->target(policy, target_freq, relation);
1976 else if (cpufreq_driver->target_index) {
1977 struct cpufreq_frequency_table *freq_table;
1980 freq_table = cpufreq_frequency_get_table(policy->cpu);
1981 if (unlikely(!freq_table)) {
1982 pr_err("%s: Unable to find freq_table\n", __func__);
1986 retval = cpufreq_frequency_table_target(policy, freq_table,
1987 target_freq, relation, &index);
1988 if (unlikely(retval)) {
1989 pr_err("%s: Unable to find matching freq\n", __func__);
1993 if (freq_table[index].frequency == policy->cur) {
1998 retval = __target_index(policy, freq_table, index);
2004 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2006 int cpufreq_driver_target(struct cpufreq_policy *policy,
2007 unsigned int target_freq,
2008 unsigned int relation)
2012 down_write(&policy->rwsem);
2014 ret = __cpufreq_driver_target(policy, target_freq, relation);
2016 up_write(&policy->rwsem);
2020 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2022 static int __cpufreq_governor(struct cpufreq_policy *policy,
2027 /* Only must be defined when default governor is known to have latency
2028 restrictions, like e.g. conservative or ondemand.
2029 That this is the case is already ensured in Kconfig
2031 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
2032 struct cpufreq_governor *gov = &cpufreq_gov_performance;
2034 struct cpufreq_governor *gov = NULL;
2037 /* Don't start any governor operations if we are entering suspend */
2038 if (cpufreq_suspended)
2041 * Governor might not be initiated here if ACPI _PPC changed
2042 * notification happened, so check it.
2044 if (!policy->governor)
2047 if (policy->governor->max_transition_latency &&
2048 policy->cpuinfo.transition_latency >
2049 policy->governor->max_transition_latency) {
2053 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2054 policy->governor->name, gov->name);
2055 policy->governor = gov;
2059 if (event == CPUFREQ_GOV_POLICY_INIT)
2060 if (!try_module_get(policy->governor->owner))
2063 pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
2065 mutex_lock(&cpufreq_governor_lock);
2066 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
2067 || (!policy->governor_enabled
2068 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
2069 mutex_unlock(&cpufreq_governor_lock);
2073 if (event == CPUFREQ_GOV_STOP)
2074 policy->governor_enabled = false;
2075 else if (event == CPUFREQ_GOV_START)
2076 policy->governor_enabled = true;
2078 mutex_unlock(&cpufreq_governor_lock);
2080 ret = policy->governor->governor(policy, event);
2083 if (event == CPUFREQ_GOV_POLICY_INIT)
2084 policy->governor->initialized++;
2085 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2086 policy->governor->initialized--;
2088 /* Restore original values */
2089 mutex_lock(&cpufreq_governor_lock);
2090 if (event == CPUFREQ_GOV_STOP)
2091 policy->governor_enabled = true;
2092 else if (event == CPUFREQ_GOV_START)
2093 policy->governor_enabled = false;
2094 mutex_unlock(&cpufreq_governor_lock);
2097 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2098 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
2099 module_put(policy->governor->owner);
2104 int cpufreq_register_governor(struct cpufreq_governor *governor)
2111 if (cpufreq_disabled())
2114 mutex_lock(&cpufreq_governor_mutex);
2116 governor->initialized = 0;
2118 if (!find_governor(governor->name)) {
2120 list_add(&governor->governor_list, &cpufreq_governor_list);
2123 mutex_unlock(&cpufreq_governor_mutex);
2126 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2128 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2130 struct cpufreq_policy *policy;
2131 unsigned long flags;
2136 if (cpufreq_disabled())
2139 /* clear last_governor for all inactive policies */
2140 read_lock_irqsave(&cpufreq_driver_lock, flags);
2141 for_each_inactive_policy(policy) {
2142 if (!strcmp(policy->last_governor, governor->name)) {
2143 policy->governor = NULL;
2144 strcpy(policy->last_governor, "\0");
2147 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2149 mutex_lock(&cpufreq_governor_mutex);
2150 list_del(&governor->governor_list);
2151 mutex_unlock(&cpufreq_governor_mutex);
2154 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2157 /*********************************************************************
2158 * POLICY INTERFACE *
2159 *********************************************************************/
2162 * cpufreq_get_policy - get the current cpufreq_policy
2163 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2166 * Reads the current cpufreq policy.
2168 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2170 struct cpufreq_policy *cpu_policy;
2174 cpu_policy = cpufreq_cpu_get(cpu);
2178 memcpy(policy, cpu_policy, sizeof(*policy));
2180 cpufreq_cpu_put(cpu_policy);
2183 EXPORT_SYMBOL(cpufreq_get_policy);
2186 * policy : current policy.
2187 * new_policy: policy to be set.
2189 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2190 struct cpufreq_policy *new_policy)
2192 struct cpufreq_governor *old_gov;
2195 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2196 new_policy->cpu, new_policy->min, new_policy->max);
2198 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2201 * This check works well when we store new min/max freq attributes,
2202 * because new_policy is a copy of policy with one field updated.
2204 if (new_policy->min > new_policy->max)
2207 /* verify the cpu speed can be set within this limit */
2208 ret = cpufreq_driver->verify(new_policy);
2212 /* adjust if necessary - all reasons */
2213 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2214 CPUFREQ_ADJUST, new_policy);
2217 * verify the cpu speed can be set within this limit, which might be
2218 * different to the first one
2220 ret = cpufreq_driver->verify(new_policy);
2224 /* notification of the new policy */
2225 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2226 CPUFREQ_NOTIFY, new_policy);
2228 scale_freq_capacity(new_policy, NULL);
2230 policy->min = new_policy->min;
2231 policy->max = new_policy->max;
2232 trace_cpu_frequency_limits(policy->max, policy->min, policy->cpu);
2234 pr_debug("new min and max freqs are %u - %u kHz\n",
2235 policy->min, policy->max);
2237 if (cpufreq_driver->setpolicy) {
2238 policy->policy = new_policy->policy;
2239 pr_debug("setting range\n");
2240 return cpufreq_driver->setpolicy(new_policy);
2243 if (new_policy->governor == policy->governor)
2246 pr_debug("governor switch\n");
2248 /* save old, working values */
2249 old_gov = policy->governor;
2250 /* end old governor */
2252 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2254 /* This can happen due to race with other operations */
2255 pr_debug("%s: Failed to Stop Governor: %s (%d)\n",
2256 __func__, old_gov->name, ret);
2260 up_write(&policy->rwsem);
2261 ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2262 down_write(&policy->rwsem);
2265 pr_err("%s: Failed to Exit Governor: %s (%d)\n",
2266 __func__, old_gov->name, ret);
2271 /* start new governor */
2272 policy->governor = new_policy->governor;
2273 ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2275 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
2279 up_write(&policy->rwsem);
2280 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2281 down_write(&policy->rwsem);
2284 /* new governor failed, so re-start old one */
2285 pr_debug("starting governor %s failed\n", policy->governor->name);
2287 policy->governor = old_gov;
2288 if (__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
2289 policy->governor = NULL;
2291 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2297 pr_debug("governor: change or update limits\n");
2298 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2302 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2303 * @cpu: CPU which shall be re-evaluated
2305 * Useful for policy notifiers which have different necessities
2306 * at different times.
2308 int cpufreq_update_policy(unsigned int cpu)
2310 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2311 struct cpufreq_policy new_policy;
2317 down_write(&policy->rwsem);
2319 pr_debug("updating policy for CPU %u\n", cpu);
2320 memcpy(&new_policy, policy, sizeof(*policy));
2321 new_policy.min = policy->user_policy.min;
2322 new_policy.max = policy->user_policy.max;
2325 * BIOS might change freq behind our back
2326 * -> ask driver for current freq and notify governors about a change
2328 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2329 new_policy.cur = cpufreq_driver->get(cpu);
2330 if (WARN_ON(!new_policy.cur)) {
2336 pr_debug("Driver did not initialize current freq\n");
2337 policy->cur = new_policy.cur;
2339 if (policy->cur != new_policy.cur && has_target())
2340 cpufreq_out_of_sync(policy, new_policy.cur);
2344 ret = cpufreq_set_policy(policy, &new_policy);
2347 up_write(&policy->rwsem);
2349 cpufreq_cpu_put(policy);
2352 EXPORT_SYMBOL(cpufreq_update_policy);
2354 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2355 unsigned long action, void *hcpu)
2357 unsigned int cpu = (unsigned long)hcpu;
2359 switch (action & ~CPU_TASKS_FROZEN) {
2361 cpufreq_online(cpu);
2364 case CPU_DOWN_PREPARE:
2365 cpufreq_offline_prepare(cpu);
2369 cpufreq_offline_finish(cpu);
2372 case CPU_DOWN_FAILED:
2373 cpufreq_online(cpu);
2379 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2380 .notifier_call = cpufreq_cpu_callback,
2383 /*********************************************************************
2385 *********************************************************************/
2386 static int cpufreq_boost_set_sw(int state)
2388 struct cpufreq_frequency_table *freq_table;
2389 struct cpufreq_policy *policy;
2392 for_each_active_policy(policy) {
2393 freq_table = cpufreq_frequency_get_table(policy->cpu);
2395 ret = cpufreq_frequency_table_cpuinfo(policy,
2398 pr_err("%s: Policy frequency update failed\n",
2402 policy->user_policy.max = policy->max;
2403 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2410 int cpufreq_boost_trigger_state(int state)
2412 unsigned long flags;
2415 if (cpufreq_driver->boost_enabled == state)
2418 write_lock_irqsave(&cpufreq_driver_lock, flags);
2419 cpufreq_driver->boost_enabled = state;
2420 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2422 ret = cpufreq_driver->set_boost(state);
2424 write_lock_irqsave(&cpufreq_driver_lock, flags);
2425 cpufreq_driver->boost_enabled = !state;
2426 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2428 pr_err("%s: Cannot %s BOOST\n",
2429 __func__, state ? "enable" : "disable");
2435 int cpufreq_boost_supported(void)
2437 if (likely(cpufreq_driver))
2438 return cpufreq_driver->boost_supported;
2442 EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2444 static int create_boost_sysfs_file(void)
2448 if (!cpufreq_boost_supported())
2452 * Check if driver provides function to enable boost -
2453 * if not, use cpufreq_boost_set_sw as default
2455 if (!cpufreq_driver->set_boost)
2456 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2458 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2460 pr_err("%s: cannot register global BOOST sysfs file\n",
2466 static void remove_boost_sysfs_file(void)
2468 if (cpufreq_boost_supported())
2469 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2472 int cpufreq_enable_boost_support(void)
2474 if (!cpufreq_driver)
2477 if (cpufreq_boost_supported())
2480 cpufreq_driver->boost_supported = true;
2482 /* This will get removed on driver unregister */
2483 return create_boost_sysfs_file();
2485 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2487 int cpufreq_boost_enabled(void)
2489 return cpufreq_driver->boost_enabled;
2491 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2493 /*********************************************************************
2494 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2495 *********************************************************************/
2498 * cpufreq_register_driver - register a CPU Frequency driver
2499 * @driver_data: A struct cpufreq_driver containing the values#
2500 * submitted by the CPU Frequency driver.
2502 * Registers a CPU Frequency driver to this core code. This code
2503 * returns zero on success, -EBUSY when another driver got here first
2504 * (and isn't unregistered in the meantime).
2507 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2509 unsigned long flags;
2512 if (cpufreq_disabled())
2515 if (!driver_data || !driver_data->verify || !driver_data->init ||
2516 !(driver_data->setpolicy || driver_data->target_index ||
2517 driver_data->target) ||
2518 (driver_data->setpolicy && (driver_data->target_index ||
2519 driver_data->target)) ||
2520 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
2523 pr_debug("trying to register driver %s\n", driver_data->name);
2525 /* Protect against concurrent CPU online/offline. */
2528 write_lock_irqsave(&cpufreq_driver_lock, flags);
2529 if (cpufreq_driver) {
2530 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2534 cpufreq_driver = driver_data;
2535 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2537 if (driver_data->setpolicy)
2538 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2540 ret = create_boost_sysfs_file();
2542 goto err_null_driver;
2544 ret = subsys_interface_register(&cpufreq_interface);
2546 goto err_boost_unreg;
2548 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2549 list_empty(&cpufreq_policy_list)) {
2550 /* if all ->init() calls failed, unregister */
2552 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2557 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2558 pr_debug("driver %s up and running\n", driver_data->name);
2565 subsys_interface_unregister(&cpufreq_interface);
2567 remove_boost_sysfs_file();
2569 write_lock_irqsave(&cpufreq_driver_lock, flags);
2570 cpufreq_driver = NULL;
2571 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2574 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2577 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2579 * Unregister the current CPUFreq driver. Only call this if you have
2580 * the right to do so, i.e. if you have succeeded in initialising before!
2581 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2582 * currently not initialised.
2584 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2586 unsigned long flags;
2588 if (!cpufreq_driver || (driver != cpufreq_driver))
2591 pr_debug("unregistering driver %s\n", driver->name);
2593 /* Protect against concurrent cpu hotplug */
2595 subsys_interface_unregister(&cpufreq_interface);
2596 remove_boost_sysfs_file();
2597 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2599 write_lock_irqsave(&cpufreq_driver_lock, flags);
2601 cpufreq_driver = NULL;
2603 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2608 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2611 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2612 * or mutexes when secondary CPUs are halted.
2614 static struct syscore_ops cpufreq_syscore_ops = {
2615 .shutdown = cpufreq_suspend,
2618 struct kobject *cpufreq_global_kobject;
2619 EXPORT_SYMBOL(cpufreq_global_kobject);
2621 static int __init cpufreq_core_init(void)
2623 if (cpufreq_disabled())
2626 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2627 BUG_ON(!cpufreq_global_kobject);
2629 register_syscore_ops(&cpufreq_syscore_ops);
2633 core_initcall(cpufreq_core_init);