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/tick.h>
31 #include <trace/events/power.h>
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 DEFINE_MUTEX(cpufreq_governor_lock
);
43 static LIST_HEAD(cpufreq_policy_list
);
45 /* This one keeps track of the previously set governor of a removed CPU */
46 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN
], cpufreq_cpu_governor
);
48 /* Flag to suspend/resume CPUFreq governors */
49 static bool cpufreq_suspended
;
51 static inline bool has_target(void)
53 return cpufreq_driver
->target_index
|| cpufreq_driver
->target
;
57 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
60 static DECLARE_RWSEM(cpufreq_rwsem
);
62 /* internal prototypes */
63 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
65 static unsigned int __cpufreq_get(unsigned int cpu
);
66 static void handle_update(struct work_struct
*work
);
69 * Two notifier lists: the "policy" list is involved in the
70 * validation process for a new CPU frequency policy; the
71 * "transition" list for kernel code that needs to handle
72 * changes to devices when the CPU clock speed changes.
73 * The mutex locks both lists.
75 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list
);
76 static struct srcu_notifier_head cpufreq_transition_notifier_list
;
78 static bool init_cpufreq_transition_notifier_list_called
;
79 static int __init
init_cpufreq_transition_notifier_list(void)
81 srcu_init_notifier_head(&cpufreq_transition_notifier_list
);
82 init_cpufreq_transition_notifier_list_called
= true;
85 pure_initcall(init_cpufreq_transition_notifier_list
);
87 static int off __read_mostly
;
88 static int cpufreq_disabled(void)
92 void disable_cpufreq(void)
96 static LIST_HEAD(cpufreq_governor_list
);
97 static DEFINE_MUTEX(cpufreq_governor_mutex
);
99 bool have_governor_per_policy(void)
101 return !!(cpufreq_driver
->flags
& CPUFREQ_HAVE_GOVERNOR_PER_POLICY
);
103 EXPORT_SYMBOL_GPL(have_governor_per_policy
);
105 struct kobject
*get_governor_parent_kobj(struct cpufreq_policy
*policy
)
107 if (have_governor_per_policy())
108 return &policy
->kobj
;
110 return cpufreq_global_kobject
;
112 EXPORT_SYMBOL_GPL(get_governor_parent_kobj
);
114 static inline u64
get_cpu_idle_time_jiffy(unsigned int cpu
, u64
*wall
)
120 cur_wall_time
= jiffies64_to_cputime64(get_jiffies_64());
122 busy_time
= kcpustat_cpu(cpu
).cpustat
[CPUTIME_USER
];
123 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_SYSTEM
];
124 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_IRQ
];
125 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_SOFTIRQ
];
126 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_STEAL
];
127 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_NICE
];
129 idle_time
= cur_wall_time
- busy_time
;
131 *wall
= cputime_to_usecs(cur_wall_time
);
133 return cputime_to_usecs(idle_time
);
136 u64
get_cpu_idle_time(unsigned int cpu
, u64
*wall
, int io_busy
)
138 u64 idle_time
= get_cpu_idle_time_us(cpu
, io_busy
? wall
: NULL
);
140 if (idle_time
== -1ULL)
141 return get_cpu_idle_time_jiffy(cpu
, wall
);
143 idle_time
+= get_cpu_iowait_time_us(cpu
, wall
);
147 EXPORT_SYMBOL_GPL(get_cpu_idle_time
);
150 * This is a generic cpufreq init() routine which can be used by cpufreq
151 * drivers of SMP systems. It will do following:
152 * - validate & show freq table passed
153 * - set policies transition latency
154 * - policy->cpus with all possible CPUs
156 int cpufreq_generic_init(struct cpufreq_policy
*policy
,
157 struct cpufreq_frequency_table
*table
,
158 unsigned int transition_latency
)
162 ret
= cpufreq_table_validate_and_show(policy
, table
);
164 pr_err("%s: invalid frequency table: %d\n", __func__
, ret
);
168 policy
->cpuinfo
.transition_latency
= transition_latency
;
171 * The driver only supports the SMP configuartion where all processors
172 * share the clock and voltage and clock.
174 cpumask_setall(policy
->cpus
);
178 EXPORT_SYMBOL_GPL(cpufreq_generic_init
);
180 unsigned int cpufreq_generic_get(unsigned int cpu
)
182 struct cpufreq_policy
*policy
= per_cpu(cpufreq_cpu_data
, cpu
);
184 if (!policy
|| IS_ERR(policy
->clk
)) {
185 pr_err("%s: No %s associated to cpu: %d\n", __func__
,
186 policy
? "clk" : "policy", cpu
);
190 return clk_get_rate(policy
->clk
) / 1000;
192 EXPORT_SYMBOL_GPL(cpufreq_generic_get
);
194 struct cpufreq_policy
*cpufreq_cpu_get(unsigned int cpu
)
196 struct cpufreq_policy
*policy
= NULL
;
199 if (cpufreq_disabled() || (cpu
>= nr_cpu_ids
))
202 if (!down_read_trylock(&cpufreq_rwsem
))
205 /* get the cpufreq driver */
206 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
208 if (cpufreq_driver
) {
210 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
212 kobject_get(&policy
->kobj
);
215 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
218 up_read(&cpufreq_rwsem
);
222 EXPORT_SYMBOL_GPL(cpufreq_cpu_get
);
224 void cpufreq_cpu_put(struct cpufreq_policy
*policy
)
226 if (cpufreq_disabled())
229 kobject_put(&policy
->kobj
);
230 up_read(&cpufreq_rwsem
);
232 EXPORT_SYMBOL_GPL(cpufreq_cpu_put
);
234 /*********************************************************************
235 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
236 *********************************************************************/
239 * adjust_jiffies - adjust the system "loops_per_jiffy"
241 * This function alters the system "loops_per_jiffy" for the clock
242 * speed change. Note that loops_per_jiffy cannot be updated on SMP
243 * systems as each CPU might be scaled differently. So, use the arch
244 * per-CPU loops_per_jiffy value wherever possible.
247 static unsigned long l_p_j_ref
;
248 static unsigned int l_p_j_ref_freq
;
250 static void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
252 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
255 if (!l_p_j_ref_freq
) {
256 l_p_j_ref
= loops_per_jiffy
;
257 l_p_j_ref_freq
= ci
->old
;
258 pr_debug("saving %lu as reference value for loops_per_jiffy; "
259 "freq is %u kHz\n", l_p_j_ref
, l_p_j_ref_freq
);
261 if ((val
== CPUFREQ_POSTCHANGE
&& ci
->old
!= ci
->new) ||
262 (val
== CPUFREQ_RESUMECHANGE
|| val
== CPUFREQ_SUSPENDCHANGE
)) {
263 loops_per_jiffy
= cpufreq_scale(l_p_j_ref
, l_p_j_ref_freq
,
265 pr_debug("scaling loops_per_jiffy to %lu "
266 "for frequency %u kHz\n", loops_per_jiffy
, ci
->new);
270 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
276 static void __cpufreq_notify_transition(struct cpufreq_policy
*policy
,
277 struct cpufreq_freqs
*freqs
, unsigned int state
)
279 BUG_ON(irqs_disabled());
281 if (cpufreq_disabled())
284 freqs
->flags
= cpufreq_driver
->flags
;
285 pr_debug("notification %u of frequency transition to %u kHz\n",
290 case CPUFREQ_PRECHANGE
:
291 /* detect if the driver reported a value as "old frequency"
292 * which is not equal to what the cpufreq core thinks is
295 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
296 if ((policy
) && (policy
->cpu
== freqs
->cpu
) &&
297 (policy
->cur
) && (policy
->cur
!= freqs
->old
)) {
298 pr_debug("Warning: CPU frequency is"
299 " %u, cpufreq assumed %u kHz.\n",
300 freqs
->old
, policy
->cur
);
301 freqs
->old
= policy
->cur
;
304 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
305 CPUFREQ_PRECHANGE
, freqs
);
306 adjust_jiffies(CPUFREQ_PRECHANGE
, freqs
);
309 case CPUFREQ_POSTCHANGE
:
310 adjust_jiffies(CPUFREQ_POSTCHANGE
, freqs
);
311 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs
->new,
312 (unsigned long)freqs
->cpu
);
313 trace_cpu_frequency(freqs
->new, freqs
->cpu
);
314 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
315 CPUFREQ_POSTCHANGE
, freqs
);
316 if (likely(policy
) && likely(policy
->cpu
== freqs
->cpu
))
317 policy
->cur
= freqs
->new;
323 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
324 * on frequency transition.
326 * This function calls the transition notifiers and the "adjust_jiffies"
327 * function. It is called twice on all CPU frequency changes that have
330 void cpufreq_notify_transition(struct cpufreq_policy
*policy
,
331 struct cpufreq_freqs
*freqs
, unsigned int state
)
333 for_each_cpu(freqs
->cpu
, policy
->cpus
)
334 __cpufreq_notify_transition(policy
, freqs
, state
);
336 EXPORT_SYMBOL_GPL(cpufreq_notify_transition
);
338 /* Do post notifications when there are chances that transition has failed */
339 void cpufreq_notify_post_transition(struct cpufreq_policy
*policy
,
340 struct cpufreq_freqs
*freqs
, int transition_failed
)
342 cpufreq_notify_transition(policy
, freqs
, CPUFREQ_POSTCHANGE
);
343 if (!transition_failed
)
346 swap(freqs
->old
, freqs
->new);
347 cpufreq_notify_transition(policy
, freqs
, CPUFREQ_PRECHANGE
);
348 cpufreq_notify_transition(policy
, freqs
, CPUFREQ_POSTCHANGE
);
350 EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition
);
353 /*********************************************************************
355 *********************************************************************/
356 static ssize_t
show_boost(struct kobject
*kobj
,
357 struct attribute
*attr
, char *buf
)
359 return sprintf(buf
, "%d\n", cpufreq_driver
->boost_enabled
);
362 static ssize_t
store_boost(struct kobject
*kobj
, struct attribute
*attr
,
363 const char *buf
, size_t count
)
367 ret
= sscanf(buf
, "%d", &enable
);
368 if (ret
!= 1 || enable
< 0 || enable
> 1)
371 if (cpufreq_boost_trigger_state(enable
)) {
372 pr_err("%s: Cannot %s BOOST!\n", __func__
,
373 enable
? "enable" : "disable");
377 pr_debug("%s: cpufreq BOOST %s\n", __func__
,
378 enable
? "enabled" : "disabled");
382 define_one_global_rw(boost
);
384 static struct cpufreq_governor
*__find_governor(const char *str_governor
)
386 struct cpufreq_governor
*t
;
388 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
)
389 if (!strnicmp(str_governor
, t
->name
, CPUFREQ_NAME_LEN
))
396 * cpufreq_parse_governor - parse a governor string
398 static int cpufreq_parse_governor(char *str_governor
, unsigned int *policy
,
399 struct cpufreq_governor
**governor
)
406 if (cpufreq_driver
->setpolicy
) {
407 if (!strnicmp(str_governor
, "performance", CPUFREQ_NAME_LEN
)) {
408 *policy
= CPUFREQ_POLICY_PERFORMANCE
;
410 } else if (!strnicmp(str_governor
, "powersave",
412 *policy
= CPUFREQ_POLICY_POWERSAVE
;
415 } else if (has_target()) {
416 struct cpufreq_governor
*t
;
418 mutex_lock(&cpufreq_governor_mutex
);
420 t
= __find_governor(str_governor
);
425 mutex_unlock(&cpufreq_governor_mutex
);
426 ret
= request_module("cpufreq_%s", str_governor
);
427 mutex_lock(&cpufreq_governor_mutex
);
430 t
= __find_governor(str_governor
);
438 mutex_unlock(&cpufreq_governor_mutex
);
445 * cpufreq_per_cpu_attr_read() / show_##file_name() -
446 * print out cpufreq information
448 * Write out information from cpufreq_driver->policy[cpu]; object must be
452 #define show_one(file_name, object) \
453 static ssize_t show_##file_name \
454 (struct cpufreq_policy *policy, char *buf) \
456 return sprintf(buf, "%u\n", policy->object); \
459 show_one(cpuinfo_min_freq
, cpuinfo
.min_freq
);
460 show_one(cpuinfo_max_freq
, cpuinfo
.max_freq
);
461 show_one(cpuinfo_transition_latency
, cpuinfo
.transition_latency
);
462 show_one(scaling_min_freq
, min
);
463 show_one(scaling_max_freq
, max
);
464 show_one(scaling_cur_freq
, cur
);
466 static int cpufreq_set_policy(struct cpufreq_policy
*policy
,
467 struct cpufreq_policy
*new_policy
);
470 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
472 #define store_one(file_name, object) \
473 static ssize_t store_##file_name \
474 (struct cpufreq_policy *policy, const char *buf, size_t count) \
477 struct cpufreq_policy new_policy; \
479 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
483 ret = sscanf(buf, "%u", &new_policy.object); \
487 ret = cpufreq_set_policy(policy, &new_policy); \
488 policy->user_policy.object = policy->object; \
490 return ret ? ret : count; \
493 store_one(scaling_min_freq
, min
);
494 store_one(scaling_max_freq
, max
);
497 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
499 static ssize_t
show_cpuinfo_cur_freq(struct cpufreq_policy
*policy
,
502 unsigned int cur_freq
= __cpufreq_get(policy
->cpu
);
504 return sprintf(buf
, "<unknown>");
505 return sprintf(buf
, "%u\n", cur_freq
);
509 * show_scaling_governor - show the current policy for the specified CPU
511 static ssize_t
show_scaling_governor(struct cpufreq_policy
*policy
, char *buf
)
513 if (policy
->policy
== CPUFREQ_POLICY_POWERSAVE
)
514 return sprintf(buf
, "powersave\n");
515 else if (policy
->policy
== CPUFREQ_POLICY_PERFORMANCE
)
516 return sprintf(buf
, "performance\n");
517 else if (policy
->governor
)
518 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n",
519 policy
->governor
->name
);
524 * store_scaling_governor - store policy for the specified CPU
526 static ssize_t
store_scaling_governor(struct cpufreq_policy
*policy
,
527 const char *buf
, size_t count
)
530 char str_governor
[16];
531 struct cpufreq_policy new_policy
;
533 ret
= cpufreq_get_policy(&new_policy
, policy
->cpu
);
537 ret
= sscanf(buf
, "%15s", str_governor
);
541 if (cpufreq_parse_governor(str_governor
, &new_policy
.policy
,
542 &new_policy
.governor
))
545 ret
= cpufreq_set_policy(policy
, &new_policy
);
547 policy
->user_policy
.policy
= policy
->policy
;
548 policy
->user_policy
.governor
= policy
->governor
;
557 * show_scaling_driver - show the cpufreq driver currently loaded
559 static ssize_t
show_scaling_driver(struct cpufreq_policy
*policy
, char *buf
)
561 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n", cpufreq_driver
->name
);
565 * show_scaling_available_governors - show the available CPUfreq governors
567 static ssize_t
show_scaling_available_governors(struct cpufreq_policy
*policy
,
571 struct cpufreq_governor
*t
;
574 i
+= sprintf(buf
, "performance powersave");
578 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
579 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char))
580 - (CPUFREQ_NAME_LEN
+ 2)))
582 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_PLEN
, "%s ", t
->name
);
585 i
+= sprintf(&buf
[i
], "\n");
589 ssize_t
cpufreq_show_cpus(const struct cpumask
*mask
, char *buf
)
594 for_each_cpu(cpu
, mask
) {
596 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), " ");
597 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), "%u", cpu
);
598 if (i
>= (PAGE_SIZE
- 5))
601 i
+= sprintf(&buf
[i
], "\n");
604 EXPORT_SYMBOL_GPL(cpufreq_show_cpus
);
607 * show_related_cpus - show the CPUs affected by each transition even if
608 * hw coordination is in use
610 static ssize_t
show_related_cpus(struct cpufreq_policy
*policy
, char *buf
)
612 return cpufreq_show_cpus(policy
->related_cpus
, buf
);
616 * show_affected_cpus - show the CPUs affected by each transition
618 static ssize_t
show_affected_cpus(struct cpufreq_policy
*policy
, char *buf
)
620 return cpufreq_show_cpus(policy
->cpus
, buf
);
623 static ssize_t
store_scaling_setspeed(struct cpufreq_policy
*policy
,
624 const char *buf
, size_t count
)
626 unsigned int freq
= 0;
629 if (!policy
->governor
|| !policy
->governor
->store_setspeed
)
632 ret
= sscanf(buf
, "%u", &freq
);
636 policy
->governor
->store_setspeed(policy
, freq
);
641 static ssize_t
show_scaling_setspeed(struct cpufreq_policy
*policy
, char *buf
)
643 if (!policy
->governor
|| !policy
->governor
->show_setspeed
)
644 return sprintf(buf
, "<unsupported>\n");
646 return policy
->governor
->show_setspeed(policy
, buf
);
650 * show_bios_limit - show the current cpufreq HW/BIOS limitation
652 static ssize_t
show_bios_limit(struct cpufreq_policy
*policy
, char *buf
)
656 if (cpufreq_driver
->bios_limit
) {
657 ret
= cpufreq_driver
->bios_limit(policy
->cpu
, &limit
);
659 return sprintf(buf
, "%u\n", limit
);
661 return sprintf(buf
, "%u\n", policy
->cpuinfo
.max_freq
);
664 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq
, 0400);
665 cpufreq_freq_attr_ro(cpuinfo_min_freq
);
666 cpufreq_freq_attr_ro(cpuinfo_max_freq
);
667 cpufreq_freq_attr_ro(cpuinfo_transition_latency
);
668 cpufreq_freq_attr_ro(scaling_available_governors
);
669 cpufreq_freq_attr_ro(scaling_driver
);
670 cpufreq_freq_attr_ro(scaling_cur_freq
);
671 cpufreq_freq_attr_ro(bios_limit
);
672 cpufreq_freq_attr_ro(related_cpus
);
673 cpufreq_freq_attr_ro(affected_cpus
);
674 cpufreq_freq_attr_rw(scaling_min_freq
);
675 cpufreq_freq_attr_rw(scaling_max_freq
);
676 cpufreq_freq_attr_rw(scaling_governor
);
677 cpufreq_freq_attr_rw(scaling_setspeed
);
679 static struct attribute
*default_attrs
[] = {
680 &cpuinfo_min_freq
.attr
,
681 &cpuinfo_max_freq
.attr
,
682 &cpuinfo_transition_latency
.attr
,
683 &scaling_min_freq
.attr
,
684 &scaling_max_freq
.attr
,
687 &scaling_governor
.attr
,
688 &scaling_driver
.attr
,
689 &scaling_available_governors
.attr
,
690 &scaling_setspeed
.attr
,
694 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
695 #define to_attr(a) container_of(a, struct freq_attr, attr)
697 static ssize_t
show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
699 struct cpufreq_policy
*policy
= to_policy(kobj
);
700 struct freq_attr
*fattr
= to_attr(attr
);
703 if (!down_read_trylock(&cpufreq_rwsem
))
706 down_read(&policy
->rwsem
);
709 ret
= fattr
->show(policy
, buf
);
713 up_read(&policy
->rwsem
);
714 up_read(&cpufreq_rwsem
);
719 static ssize_t
store(struct kobject
*kobj
, struct attribute
*attr
,
720 const char *buf
, size_t count
)
722 struct cpufreq_policy
*policy
= to_policy(kobj
);
723 struct freq_attr
*fattr
= to_attr(attr
);
724 ssize_t ret
= -EINVAL
;
728 if (!cpu_online(policy
->cpu
))
731 if (!down_read_trylock(&cpufreq_rwsem
))
734 down_write(&policy
->rwsem
);
737 ret
= fattr
->store(policy
, buf
, count
);
741 up_write(&policy
->rwsem
);
743 up_read(&cpufreq_rwsem
);
750 static void cpufreq_sysfs_release(struct kobject
*kobj
)
752 struct cpufreq_policy
*policy
= to_policy(kobj
);
753 pr_debug("last reference is dropped\n");
754 complete(&policy
->kobj_unregister
);
757 static const struct sysfs_ops sysfs_ops
= {
762 static struct kobj_type ktype_cpufreq
= {
763 .sysfs_ops
= &sysfs_ops
,
764 .default_attrs
= default_attrs
,
765 .release
= cpufreq_sysfs_release
,
768 struct kobject
*cpufreq_global_kobject
;
769 EXPORT_SYMBOL(cpufreq_global_kobject
);
771 static int cpufreq_global_kobject_usage
;
773 int cpufreq_get_global_kobject(void)
775 if (!cpufreq_global_kobject_usage
++)
776 return kobject_add(cpufreq_global_kobject
,
777 &cpu_subsys
.dev_root
->kobj
, "%s", "cpufreq");
781 EXPORT_SYMBOL(cpufreq_get_global_kobject
);
783 void cpufreq_put_global_kobject(void)
785 if (!--cpufreq_global_kobject_usage
)
786 kobject_del(cpufreq_global_kobject
);
788 EXPORT_SYMBOL(cpufreq_put_global_kobject
);
790 int cpufreq_sysfs_create_file(const struct attribute
*attr
)
792 int ret
= cpufreq_get_global_kobject();
795 ret
= sysfs_create_file(cpufreq_global_kobject
, attr
);
797 cpufreq_put_global_kobject();
802 EXPORT_SYMBOL(cpufreq_sysfs_create_file
);
804 void cpufreq_sysfs_remove_file(const struct attribute
*attr
)
806 sysfs_remove_file(cpufreq_global_kobject
, attr
);
807 cpufreq_put_global_kobject();
809 EXPORT_SYMBOL(cpufreq_sysfs_remove_file
);
811 /* symlink affected CPUs */
812 static int cpufreq_add_dev_symlink(struct cpufreq_policy
*policy
)
817 for_each_cpu(j
, policy
->cpus
) {
818 struct device
*cpu_dev
;
820 if (j
== policy
->cpu
)
823 pr_debug("Adding link for CPU: %u\n", j
);
824 cpu_dev
= get_cpu_device(j
);
825 ret
= sysfs_create_link(&cpu_dev
->kobj
, &policy
->kobj
,
833 static int cpufreq_add_dev_interface(struct cpufreq_policy
*policy
,
836 struct freq_attr
**drv_attr
;
839 /* prepare interface data */
840 ret
= kobject_init_and_add(&policy
->kobj
, &ktype_cpufreq
,
841 &dev
->kobj
, "cpufreq");
845 /* set up files for this cpu device */
846 drv_attr
= cpufreq_driver
->attr
;
847 while ((drv_attr
) && (*drv_attr
)) {
848 ret
= sysfs_create_file(&policy
->kobj
, &((*drv_attr
)->attr
));
850 goto err_out_kobj_put
;
853 if (cpufreq_driver
->get
) {
854 ret
= sysfs_create_file(&policy
->kobj
, &cpuinfo_cur_freq
.attr
);
856 goto err_out_kobj_put
;
859 ret
= sysfs_create_file(&policy
->kobj
, &scaling_cur_freq
.attr
);
861 goto err_out_kobj_put
;
863 if (cpufreq_driver
->bios_limit
) {
864 ret
= sysfs_create_file(&policy
->kobj
, &bios_limit
.attr
);
866 goto err_out_kobj_put
;
869 ret
= cpufreq_add_dev_symlink(policy
);
871 goto err_out_kobj_put
;
876 kobject_put(&policy
->kobj
);
877 wait_for_completion(&policy
->kobj_unregister
);
881 static void cpufreq_init_policy(struct cpufreq_policy
*policy
)
883 struct cpufreq_governor
*gov
= NULL
;
884 struct cpufreq_policy new_policy
;
887 memcpy(&new_policy
, policy
, sizeof(*policy
));
889 /* Update governor of new_policy to the governor used before hotplug */
890 gov
= __find_governor(per_cpu(cpufreq_cpu_governor
, policy
->cpu
));
892 pr_debug("Restoring governor %s for cpu %d\n",
893 policy
->governor
->name
, policy
->cpu
);
895 gov
= CPUFREQ_DEFAULT_GOVERNOR
;
897 new_policy
.governor
= gov
;
899 /* Use the default policy if its valid. */
900 if (cpufreq_driver
->setpolicy
)
901 cpufreq_parse_governor(gov
->name
, &new_policy
.policy
, NULL
);
903 /* set default policy */
904 ret
= cpufreq_set_policy(policy
, &new_policy
);
906 pr_debug("setting policy failed\n");
907 if (cpufreq_driver
->exit
)
908 cpufreq_driver
->exit(policy
);
912 #ifdef CONFIG_HOTPLUG_CPU
913 static int cpufreq_add_policy_cpu(struct cpufreq_policy
*policy
,
914 unsigned int cpu
, struct device
*dev
)
920 ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
922 pr_err("%s: Failed to stop governor\n", __func__
);
927 down_write(&policy
->rwsem
);
929 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
931 cpumask_set_cpu(cpu
, policy
->cpus
);
932 per_cpu(cpufreq_cpu_data
, cpu
) = policy
;
933 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
935 up_write(&policy
->rwsem
);
938 if ((ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_START
)) ||
939 (ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
))) {
940 pr_err("%s: Failed to start governor\n", __func__
);
945 return sysfs_create_link(&dev
->kobj
, &policy
->kobj
, "cpufreq");
949 static struct cpufreq_policy
*cpufreq_policy_restore(unsigned int cpu
)
951 struct cpufreq_policy
*policy
;
954 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
956 policy
= per_cpu(cpufreq_cpu_data_fallback
, cpu
);
958 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
960 policy
->governor
= NULL
;
965 static struct cpufreq_policy
*cpufreq_policy_alloc(void)
967 struct cpufreq_policy
*policy
;
969 policy
= kzalloc(sizeof(*policy
), GFP_KERNEL
);
973 if (!alloc_cpumask_var(&policy
->cpus
, GFP_KERNEL
))
974 goto err_free_policy
;
976 if (!zalloc_cpumask_var(&policy
->related_cpus
, GFP_KERNEL
))
977 goto err_free_cpumask
;
979 INIT_LIST_HEAD(&policy
->policy_list
);
980 init_rwsem(&policy
->rwsem
);
985 free_cpumask_var(policy
->cpus
);
992 static void cpufreq_policy_put_kobj(struct cpufreq_policy
*policy
)
994 struct kobject
*kobj
;
995 struct completion
*cmp
;
997 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
998 CPUFREQ_REMOVE_POLICY
, policy
);
1000 down_read(&policy
->rwsem
);
1001 kobj
= &policy
->kobj
;
1002 cmp
= &policy
->kobj_unregister
;
1003 up_read(&policy
->rwsem
);
1007 * We need to make sure that the underlying kobj is
1008 * actually not referenced anymore by anybody before we
1009 * proceed with unloading.
1011 pr_debug("waiting for dropping of refcount\n");
1012 wait_for_completion(cmp
);
1013 pr_debug("wait complete\n");
1016 static void cpufreq_policy_free(struct cpufreq_policy
*policy
)
1018 free_cpumask_var(policy
->related_cpus
);
1019 free_cpumask_var(policy
->cpus
);
1023 static void update_policy_cpu(struct cpufreq_policy
*policy
, unsigned int cpu
)
1025 if (WARN_ON(cpu
== policy
->cpu
))
1028 down_write(&policy
->rwsem
);
1030 policy
->last_cpu
= policy
->cpu
;
1033 up_write(&policy
->rwsem
);
1035 cpufreq_frequency_table_update_policy_cpu(policy
);
1036 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1037 CPUFREQ_UPDATE_POLICY_CPU
, policy
);
1040 static int __cpufreq_add_dev(struct device
*dev
, struct subsys_interface
*sif
,
1043 unsigned int j
, cpu
= dev
->id
;
1045 struct cpufreq_policy
*policy
;
1046 unsigned long flags
;
1047 #ifdef CONFIG_HOTPLUG_CPU
1048 struct cpufreq_policy
*tpolicy
;
1051 if (cpu_is_offline(cpu
))
1054 pr_debug("adding CPU %u\n", cpu
);
1057 /* check whether a different CPU already registered this
1058 * CPU because it is in the same boat. */
1059 policy
= cpufreq_cpu_get(cpu
);
1060 if (unlikely(policy
)) {
1061 cpufreq_cpu_put(policy
);
1066 if (!down_read_trylock(&cpufreq_rwsem
))
1069 #ifdef CONFIG_HOTPLUG_CPU
1070 /* Check if this cpu was hot-unplugged earlier and has siblings */
1071 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1072 list_for_each_entry(tpolicy
, &cpufreq_policy_list
, policy_list
) {
1073 if (cpumask_test_cpu(cpu
, tpolicy
->related_cpus
)) {
1074 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1075 ret
= cpufreq_add_policy_cpu(tpolicy
, cpu
, dev
);
1076 up_read(&cpufreq_rwsem
);
1080 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1084 * Restore the saved policy when doing light-weight init and fall back
1085 * to the full init if that fails.
1087 policy
= frozen
? cpufreq_policy_restore(cpu
) : NULL
;
1090 policy
= cpufreq_policy_alloc();
1096 * In the resume path, since we restore a saved policy, the assignment
1097 * to policy->cpu is like an update of the existing policy, rather than
1098 * the creation of a brand new one. So we need to perform this update
1099 * by invoking update_policy_cpu().
1101 if (frozen
&& cpu
!= policy
->cpu
)
1102 update_policy_cpu(policy
, cpu
);
1106 cpumask_copy(policy
->cpus
, cpumask_of(cpu
));
1108 init_completion(&policy
->kobj_unregister
);
1109 INIT_WORK(&policy
->update
, handle_update
);
1111 /* call driver. From then on the cpufreq must be able
1112 * to accept all calls to ->verify and ->setpolicy for this CPU
1114 ret
= cpufreq_driver
->init(policy
);
1116 pr_debug("initialization failed\n");
1117 goto err_set_policy_cpu
;
1120 /* related cpus should atleast have policy->cpus */
1121 cpumask_or(policy
->related_cpus
, policy
->related_cpus
, policy
->cpus
);
1124 * affected cpus must always be the one, which are online. We aren't
1125 * managing offline cpus here.
1127 cpumask_and(policy
->cpus
, policy
->cpus
, cpu_online_mask
);
1130 policy
->user_policy
.min
= policy
->min
;
1131 policy
->user_policy
.max
= policy
->max
;
1134 down_write(&policy
->rwsem
);
1135 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1136 for_each_cpu(j
, policy
->cpus
)
1137 per_cpu(cpufreq_cpu_data
, j
) = policy
;
1138 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1140 if (cpufreq_driver
->get
) {
1141 policy
->cur
= cpufreq_driver
->get(policy
->cpu
);
1143 pr_err("%s: ->get() failed\n", __func__
);
1149 * Sometimes boot loaders set CPU frequency to a value outside of
1150 * frequency table present with cpufreq core. In such cases CPU might be
1151 * unstable if it has to run on that frequency for long duration of time
1152 * and so its better to set it to a frequency which is specified in
1153 * freq-table. This also makes cpufreq stats inconsistent as
1154 * cpufreq-stats would fail to register because current frequency of CPU
1155 * isn't found in freq-table.
1157 * Because we don't want this change to effect boot process badly, we go
1158 * for the next freq which is >= policy->cur ('cur' must be set by now,
1159 * otherwise we will end up setting freq to lowest of the table as 'cur'
1160 * is initialized to zero).
1162 * We are passing target-freq as "policy->cur - 1" otherwise
1163 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1164 * equal to target-freq.
1166 if ((cpufreq_driver
->flags
& CPUFREQ_NEED_INITIAL_FREQ_CHECK
)
1168 /* Are we running at unknown frequency ? */
1169 ret
= cpufreq_frequency_table_get_index(policy
, policy
->cur
);
1170 if (ret
== -EINVAL
) {
1171 /* Warn user and fix it */
1172 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1173 __func__
, policy
->cpu
, policy
->cur
);
1174 ret
= __cpufreq_driver_target(policy
, policy
->cur
- 1,
1175 CPUFREQ_RELATION_L
);
1178 * Reaching here after boot in a few seconds may not
1179 * mean that system will remain stable at "unknown"
1180 * frequency for longer duration. Hence, a BUG_ON().
1183 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1184 __func__
, policy
->cpu
, policy
->cur
);
1188 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1189 CPUFREQ_START
, policy
);
1192 ret
= cpufreq_add_dev_interface(policy
, dev
);
1194 goto err_out_unregister
;
1195 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1196 CPUFREQ_CREATE_POLICY
, policy
);
1199 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1200 list_add(&policy
->policy_list
, &cpufreq_policy_list
);
1201 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1203 cpufreq_init_policy(policy
);
1206 policy
->user_policy
.policy
= policy
->policy
;
1207 policy
->user_policy
.governor
= policy
->governor
;
1209 up_write(&policy
->rwsem
);
1211 kobject_uevent(&policy
->kobj
, KOBJ_ADD
);
1212 up_read(&cpufreq_rwsem
);
1214 pr_debug("initialization complete\n");
1220 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1221 for_each_cpu(j
, policy
->cpus
)
1222 per_cpu(cpufreq_cpu_data
, j
) = NULL
;
1223 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1225 if (cpufreq_driver
->exit
)
1226 cpufreq_driver
->exit(policy
);
1229 /* Do not leave stale fallback data behind. */
1230 per_cpu(cpufreq_cpu_data_fallback
, cpu
) = NULL
;
1231 cpufreq_policy_put_kobj(policy
);
1233 cpufreq_policy_free(policy
);
1236 up_read(&cpufreq_rwsem
);
1242 * cpufreq_add_dev - add a CPU device
1244 * Adds the cpufreq interface for a CPU device.
1246 * The Oracle says: try running cpufreq registration/unregistration concurrently
1247 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1248 * mess up, but more thorough testing is needed. - Mathieu
1250 static int cpufreq_add_dev(struct device
*dev
, struct subsys_interface
*sif
)
1252 return __cpufreq_add_dev(dev
, sif
, false);
1255 static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy
*policy
,
1256 unsigned int old_cpu
)
1258 struct device
*cpu_dev
;
1261 /* first sibling now owns the new sysfs dir */
1262 cpu_dev
= get_cpu_device(cpumask_any_but(policy
->cpus
, old_cpu
));
1264 sysfs_remove_link(&cpu_dev
->kobj
, "cpufreq");
1265 ret
= kobject_move(&policy
->kobj
, &cpu_dev
->kobj
);
1267 pr_err("%s: Failed to move kobj: %d", __func__
, ret
);
1269 down_write(&policy
->rwsem
);
1270 cpumask_set_cpu(old_cpu
, policy
->cpus
);
1271 up_write(&policy
->rwsem
);
1273 ret
= sysfs_create_link(&cpu_dev
->kobj
, &policy
->kobj
,
1282 static int __cpufreq_remove_dev_prepare(struct device
*dev
,
1283 struct subsys_interface
*sif
,
1286 unsigned int cpu
= dev
->id
, cpus
;
1288 unsigned long flags
;
1289 struct cpufreq_policy
*policy
;
1291 pr_debug("%s: unregistering CPU %u\n", __func__
, cpu
);
1293 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1295 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1297 /* Save the policy somewhere when doing a light-weight tear-down */
1299 per_cpu(cpufreq_cpu_data_fallback
, cpu
) = policy
;
1301 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1304 pr_debug("%s: No cpu_data found\n", __func__
);
1309 ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
1311 pr_err("%s: Failed to stop governor\n", __func__
);
1316 if (!cpufreq_driver
->setpolicy
)
1317 strncpy(per_cpu(cpufreq_cpu_governor
, cpu
),
1318 policy
->governor
->name
, CPUFREQ_NAME_LEN
);
1320 down_read(&policy
->rwsem
);
1321 cpus
= cpumask_weight(policy
->cpus
);
1322 up_read(&policy
->rwsem
);
1324 if (cpu
!= policy
->cpu
) {
1325 sysfs_remove_link(&dev
->kobj
, "cpufreq");
1326 } else if (cpus
> 1) {
1327 new_cpu
= cpufreq_nominate_new_policy_cpu(policy
, cpu
);
1329 update_policy_cpu(policy
, new_cpu
);
1332 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1333 __func__
, new_cpu
, cpu
);
1341 static int __cpufreq_remove_dev_finish(struct device
*dev
,
1342 struct subsys_interface
*sif
,
1345 unsigned int cpu
= dev
->id
, cpus
;
1347 unsigned long flags
;
1348 struct cpufreq_policy
*policy
;
1350 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1351 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1352 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1355 pr_debug("%s: No cpu_data found\n", __func__
);
1359 down_write(&policy
->rwsem
);
1360 cpus
= cpumask_weight(policy
->cpus
);
1363 cpumask_clear_cpu(cpu
, policy
->cpus
);
1364 up_write(&policy
->rwsem
);
1366 /* If cpu is last user of policy, free policy */
1369 ret
= __cpufreq_governor(policy
,
1370 CPUFREQ_GOV_POLICY_EXIT
);
1372 pr_err("%s: Failed to exit governor\n",
1379 cpufreq_policy_put_kobj(policy
);
1382 * Perform the ->exit() even during light-weight tear-down,
1383 * since this is a core component, and is essential for the
1384 * subsequent light-weight ->init() to succeed.
1386 if (cpufreq_driver
->exit
)
1387 cpufreq_driver
->exit(policy
);
1389 /* Remove policy from list of active policies */
1390 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1391 list_del(&policy
->policy_list
);
1392 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1395 cpufreq_policy_free(policy
);
1398 if ((ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_START
)) ||
1399 (ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
))) {
1400 pr_err("%s: Failed to start governor\n",
1407 per_cpu(cpufreq_cpu_data
, cpu
) = NULL
;
1412 * cpufreq_remove_dev - remove a CPU device
1414 * Removes the cpufreq interface for a CPU device.
1416 static int cpufreq_remove_dev(struct device
*dev
, struct subsys_interface
*sif
)
1418 unsigned int cpu
= dev
->id
;
1421 if (cpu_is_offline(cpu
))
1424 ret
= __cpufreq_remove_dev_prepare(dev
, sif
, false);
1427 ret
= __cpufreq_remove_dev_finish(dev
, sif
, false);
1432 static void handle_update(struct work_struct
*work
)
1434 struct cpufreq_policy
*policy
=
1435 container_of(work
, struct cpufreq_policy
, update
);
1436 unsigned int cpu
= policy
->cpu
;
1437 pr_debug("handle_update for cpu %u called\n", cpu
);
1438 cpufreq_update_policy(cpu
);
1442 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1445 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1446 * @new_freq: CPU frequency the CPU actually runs at
1448 * We adjust to current frequency first, and need to clean up later.
1449 * So either call to cpufreq_update_policy() or schedule handle_update()).
1451 static void cpufreq_out_of_sync(unsigned int cpu
, unsigned int old_freq
,
1452 unsigned int new_freq
)
1454 struct cpufreq_policy
*policy
;
1455 struct cpufreq_freqs freqs
;
1456 unsigned long flags
;
1458 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1459 "core thinks of %u, is %u kHz.\n", old_freq
, new_freq
);
1461 freqs
.old
= old_freq
;
1462 freqs
.new = new_freq
;
1464 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1465 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1466 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1468 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_PRECHANGE
);
1469 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_POSTCHANGE
);
1473 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1476 * This is the last known freq, without actually getting it from the driver.
1477 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1479 unsigned int cpufreq_quick_get(unsigned int cpu
)
1481 struct cpufreq_policy
*policy
;
1482 unsigned int ret_freq
= 0;
1484 if (cpufreq_driver
&& cpufreq_driver
->setpolicy
&& cpufreq_driver
->get
)
1485 return cpufreq_driver
->get(cpu
);
1487 policy
= cpufreq_cpu_get(cpu
);
1489 ret_freq
= policy
->cur
;
1490 cpufreq_cpu_put(policy
);
1495 EXPORT_SYMBOL(cpufreq_quick_get
);
1498 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1501 * Just return the max possible frequency for a given CPU.
1503 unsigned int cpufreq_quick_get_max(unsigned int cpu
)
1505 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1506 unsigned int ret_freq
= 0;
1509 ret_freq
= policy
->max
;
1510 cpufreq_cpu_put(policy
);
1515 EXPORT_SYMBOL(cpufreq_quick_get_max
);
1517 static unsigned int __cpufreq_get(unsigned int cpu
)
1519 struct cpufreq_policy
*policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1520 unsigned int ret_freq
= 0;
1522 if (!cpufreq_driver
->get
)
1525 ret_freq
= cpufreq_driver
->get(cpu
);
1527 if (ret_freq
&& policy
->cur
&&
1528 !(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
1529 /* verify no discrepancy between actual and
1530 saved value exists */
1531 if (unlikely(ret_freq
!= policy
->cur
)) {
1532 cpufreq_out_of_sync(cpu
, policy
->cur
, ret_freq
);
1533 schedule_work(&policy
->update
);
1541 * cpufreq_get - get the current CPU frequency (in kHz)
1544 * Get the CPU current (static) CPU frequency
1546 unsigned int cpufreq_get(unsigned int cpu
)
1548 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1549 unsigned int ret_freq
= 0;
1552 down_read(&policy
->rwsem
);
1553 ret_freq
= __cpufreq_get(cpu
);
1554 up_read(&policy
->rwsem
);
1556 cpufreq_cpu_put(policy
);
1561 EXPORT_SYMBOL(cpufreq_get
);
1563 static struct subsys_interface cpufreq_interface
= {
1565 .subsys
= &cpu_subsys
,
1566 .add_dev
= cpufreq_add_dev
,
1567 .remove_dev
= cpufreq_remove_dev
,
1571 * cpufreq_suspend() - Suspend CPUFreq governors
1573 * Called during system wide Suspend/Hibernate cycles for suspending governors
1574 * as some platforms can't change frequency after this point in suspend cycle.
1575 * Because some of the devices (like: i2c, regulators, etc) they use for
1576 * changing frequency are suspended quickly after this point.
1578 void cpufreq_suspend(void)
1580 struct cpufreq_policy
*policy
;
1582 if (!cpufreq_driver
)
1588 pr_debug("%s: Suspending Governors\n", __func__
);
1590 list_for_each_entry(policy
, &cpufreq_policy_list
, policy_list
) {
1591 if (__cpufreq_governor(policy
, CPUFREQ_GOV_STOP
))
1592 pr_err("%s: Failed to stop governor for policy: %p\n",
1594 else if (cpufreq_driver
->suspend
1595 && cpufreq_driver
->suspend(policy
))
1596 pr_err("%s: Failed to suspend driver: %p\n", __func__
,
1600 cpufreq_suspended
= true;
1604 * cpufreq_resume() - Resume CPUFreq governors
1606 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1607 * are suspended with cpufreq_suspend().
1609 void cpufreq_resume(void)
1611 struct cpufreq_policy
*policy
;
1613 if (!cpufreq_driver
)
1619 pr_debug("%s: Resuming Governors\n", __func__
);
1621 cpufreq_suspended
= false;
1623 list_for_each_entry(policy
, &cpufreq_policy_list
, policy_list
) {
1624 if (__cpufreq_governor(policy
, CPUFREQ_GOV_START
)
1625 || __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
))
1626 pr_err("%s: Failed to start governor for policy: %p\n",
1628 else if (cpufreq_driver
->resume
1629 && cpufreq_driver
->resume(policy
))
1630 pr_err("%s: Failed to resume driver: %p\n", __func__
,
1634 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
1635 * policy in list. It will verify that the current freq is in
1636 * sync with what we believe it to be.
1638 if (list_is_last(&policy
->policy_list
, &cpufreq_policy_list
))
1639 schedule_work(&policy
->update
);
1644 * cpufreq_get_current_driver - return current driver's name
1646 * Return the name string of the currently loaded cpufreq driver
1649 const char *cpufreq_get_current_driver(void)
1652 return cpufreq_driver
->name
;
1656 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver
);
1658 /*********************************************************************
1659 * NOTIFIER LISTS INTERFACE *
1660 *********************************************************************/
1663 * cpufreq_register_notifier - register a driver with cpufreq
1664 * @nb: notifier function to register
1665 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1667 * Add a driver to one of two lists: either a list of drivers that
1668 * are notified about clock rate changes (once before and once after
1669 * the transition), or a list of drivers that are notified about
1670 * changes in cpufreq policy.
1672 * This function may sleep, and has the same return conditions as
1673 * blocking_notifier_chain_register.
1675 int cpufreq_register_notifier(struct notifier_block
*nb
, unsigned int list
)
1679 if (cpufreq_disabled())
1682 WARN_ON(!init_cpufreq_transition_notifier_list_called
);
1685 case CPUFREQ_TRANSITION_NOTIFIER
:
1686 ret
= srcu_notifier_chain_register(
1687 &cpufreq_transition_notifier_list
, nb
);
1689 case CPUFREQ_POLICY_NOTIFIER
:
1690 ret
= blocking_notifier_chain_register(
1691 &cpufreq_policy_notifier_list
, nb
);
1699 EXPORT_SYMBOL(cpufreq_register_notifier
);
1702 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1703 * @nb: notifier block to be unregistered
1704 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1706 * Remove a driver from the CPU frequency notifier list.
1708 * This function may sleep, and has the same return conditions as
1709 * blocking_notifier_chain_unregister.
1711 int cpufreq_unregister_notifier(struct notifier_block
*nb
, unsigned int list
)
1715 if (cpufreq_disabled())
1719 case CPUFREQ_TRANSITION_NOTIFIER
:
1720 ret
= srcu_notifier_chain_unregister(
1721 &cpufreq_transition_notifier_list
, nb
);
1723 case CPUFREQ_POLICY_NOTIFIER
:
1724 ret
= blocking_notifier_chain_unregister(
1725 &cpufreq_policy_notifier_list
, nb
);
1733 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1736 /*********************************************************************
1738 *********************************************************************/
1740 int __cpufreq_driver_target(struct cpufreq_policy
*policy
,
1741 unsigned int target_freq
,
1742 unsigned int relation
)
1744 int retval
= -EINVAL
;
1745 unsigned int old_target_freq
= target_freq
;
1747 if (cpufreq_disabled())
1750 /* Make sure that target_freq is within supported range */
1751 if (target_freq
> policy
->max
)
1752 target_freq
= policy
->max
;
1753 if (target_freq
< policy
->min
)
1754 target_freq
= policy
->min
;
1756 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1757 policy
->cpu
, target_freq
, relation
, old_target_freq
);
1760 * This might look like a redundant call as we are checking it again
1761 * after finding index. But it is left intentionally for cases where
1762 * exactly same freq is called again and so we can save on few function
1765 if (target_freq
== policy
->cur
)
1768 if (cpufreq_driver
->target
)
1769 retval
= cpufreq_driver
->target(policy
, target_freq
, relation
);
1770 else if (cpufreq_driver
->target_index
) {
1771 struct cpufreq_frequency_table
*freq_table
;
1772 struct cpufreq_freqs freqs
;
1776 freq_table
= cpufreq_frequency_get_table(policy
->cpu
);
1777 if (unlikely(!freq_table
)) {
1778 pr_err("%s: Unable to find freq_table\n", __func__
);
1782 retval
= cpufreq_frequency_table_target(policy
, freq_table
,
1783 target_freq
, relation
, &index
);
1784 if (unlikely(retval
)) {
1785 pr_err("%s: Unable to find matching freq\n", __func__
);
1789 if (freq_table
[index
].frequency
== policy
->cur
) {
1794 notify
= !(cpufreq_driver
->flags
& CPUFREQ_ASYNC_NOTIFICATION
);
1797 freqs
.old
= policy
->cur
;
1798 freqs
.new = freq_table
[index
].frequency
;
1801 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1802 __func__
, policy
->cpu
, freqs
.old
,
1805 cpufreq_notify_transition(policy
, &freqs
,
1809 retval
= cpufreq_driver
->target_index(policy
, index
);
1811 pr_err("%s: Failed to change cpu frequency: %d\n",
1815 cpufreq_notify_post_transition(policy
, &freqs
, retval
);
1821 EXPORT_SYMBOL_GPL(__cpufreq_driver_target
);
1823 int cpufreq_driver_target(struct cpufreq_policy
*policy
,
1824 unsigned int target_freq
,
1825 unsigned int relation
)
1829 down_write(&policy
->rwsem
);
1831 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
1833 up_write(&policy
->rwsem
);
1837 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
1840 * when "event" is CPUFREQ_GOV_LIMITS
1843 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
1848 /* Only must be defined when default governor is known to have latency
1849 restrictions, like e.g. conservative or ondemand.
1850 That this is the case is already ensured in Kconfig
1852 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1853 struct cpufreq_governor
*gov
= &cpufreq_gov_performance
;
1855 struct cpufreq_governor
*gov
= NULL
;
1858 /* Don't start any governor operations if we are entering suspend */
1859 if (cpufreq_suspended
)
1862 if (policy
->governor
->max_transition_latency
&&
1863 policy
->cpuinfo
.transition_latency
>
1864 policy
->governor
->max_transition_latency
) {
1868 printk(KERN_WARNING
"%s governor failed, too long"
1869 " transition latency of HW, fallback"
1870 " to %s governor\n",
1871 policy
->governor
->name
,
1873 policy
->governor
= gov
;
1877 if (event
== CPUFREQ_GOV_POLICY_INIT
)
1878 if (!try_module_get(policy
->governor
->owner
))
1881 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1882 policy
->cpu
, event
);
1884 mutex_lock(&cpufreq_governor_lock
);
1885 if ((policy
->governor_enabled
&& event
== CPUFREQ_GOV_START
)
1886 || (!policy
->governor_enabled
1887 && (event
== CPUFREQ_GOV_LIMITS
|| event
== CPUFREQ_GOV_STOP
))) {
1888 mutex_unlock(&cpufreq_governor_lock
);
1892 if (event
== CPUFREQ_GOV_STOP
)
1893 policy
->governor_enabled
= false;
1894 else if (event
== CPUFREQ_GOV_START
)
1895 policy
->governor_enabled
= true;
1897 mutex_unlock(&cpufreq_governor_lock
);
1899 ret
= policy
->governor
->governor(policy
, event
);
1902 if (event
== CPUFREQ_GOV_POLICY_INIT
)
1903 policy
->governor
->initialized
++;
1904 else if (event
== CPUFREQ_GOV_POLICY_EXIT
)
1905 policy
->governor
->initialized
--;
1907 /* Restore original values */
1908 mutex_lock(&cpufreq_governor_lock
);
1909 if (event
== CPUFREQ_GOV_STOP
)
1910 policy
->governor_enabled
= true;
1911 else if (event
== CPUFREQ_GOV_START
)
1912 policy
->governor_enabled
= false;
1913 mutex_unlock(&cpufreq_governor_lock
);
1916 if (((event
== CPUFREQ_GOV_POLICY_INIT
) && ret
) ||
1917 ((event
== CPUFREQ_GOV_POLICY_EXIT
) && !ret
))
1918 module_put(policy
->governor
->owner
);
1923 int cpufreq_register_governor(struct cpufreq_governor
*governor
)
1930 if (cpufreq_disabled())
1933 mutex_lock(&cpufreq_governor_mutex
);
1935 governor
->initialized
= 0;
1937 if (__find_governor(governor
->name
) == NULL
) {
1939 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
1942 mutex_unlock(&cpufreq_governor_mutex
);
1945 EXPORT_SYMBOL_GPL(cpufreq_register_governor
);
1947 void cpufreq_unregister_governor(struct cpufreq_governor
*governor
)
1954 if (cpufreq_disabled())
1957 for_each_present_cpu(cpu
) {
1958 if (cpu_online(cpu
))
1960 if (!strcmp(per_cpu(cpufreq_cpu_governor
, cpu
), governor
->name
))
1961 strcpy(per_cpu(cpufreq_cpu_governor
, cpu
), "\0");
1964 mutex_lock(&cpufreq_governor_mutex
);
1965 list_del(&governor
->governor_list
);
1966 mutex_unlock(&cpufreq_governor_mutex
);
1969 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor
);
1972 /*********************************************************************
1973 * POLICY INTERFACE *
1974 *********************************************************************/
1977 * cpufreq_get_policy - get the current cpufreq_policy
1978 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1981 * Reads the current cpufreq policy.
1983 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
1985 struct cpufreq_policy
*cpu_policy
;
1989 cpu_policy
= cpufreq_cpu_get(cpu
);
1993 memcpy(policy
, cpu_policy
, sizeof(*policy
));
1995 cpufreq_cpu_put(cpu_policy
);
1998 EXPORT_SYMBOL(cpufreq_get_policy
);
2001 * policy : current policy.
2002 * new_policy: policy to be set.
2004 static int cpufreq_set_policy(struct cpufreq_policy
*policy
,
2005 struct cpufreq_policy
*new_policy
)
2007 struct cpufreq_governor
*old_gov
;
2010 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy
->cpu
,
2011 new_policy
->min
, new_policy
->max
);
2013 memcpy(&new_policy
->cpuinfo
, &policy
->cpuinfo
, sizeof(policy
->cpuinfo
));
2015 if (new_policy
->min
> policy
->max
|| new_policy
->max
< policy
->min
)
2018 /* verify the cpu speed can be set within this limit */
2019 ret
= cpufreq_driver
->verify(new_policy
);
2023 /* adjust if necessary - all reasons */
2024 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
2025 CPUFREQ_ADJUST
, new_policy
);
2027 /* adjust if necessary - hardware incompatibility*/
2028 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
2029 CPUFREQ_INCOMPATIBLE
, new_policy
);
2032 * verify the cpu speed can be set within this limit, which might be
2033 * different to the first one
2035 ret
= cpufreq_driver
->verify(new_policy
);
2039 /* notification of the new policy */
2040 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
2041 CPUFREQ_NOTIFY
, new_policy
);
2043 policy
->min
= new_policy
->min
;
2044 policy
->max
= new_policy
->max
;
2046 pr_debug("new min and max freqs are %u - %u kHz\n",
2047 policy
->min
, policy
->max
);
2049 if (cpufreq_driver
->setpolicy
) {
2050 policy
->policy
= new_policy
->policy
;
2051 pr_debug("setting range\n");
2052 return cpufreq_driver
->setpolicy(new_policy
);
2055 if (new_policy
->governor
== policy
->governor
)
2058 pr_debug("governor switch\n");
2060 /* save old, working values */
2061 old_gov
= policy
->governor
;
2062 /* end old governor */
2064 __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
2065 up_write(&policy
->rwsem
);
2066 __cpufreq_governor(policy
,CPUFREQ_GOV_POLICY_EXIT
);
2067 down_write(&policy
->rwsem
);
2070 /* start new governor */
2071 policy
->governor
= new_policy
->governor
;
2072 if (!__cpufreq_governor(policy
, CPUFREQ_GOV_POLICY_INIT
)) {
2073 if (!__cpufreq_governor(policy
, CPUFREQ_GOV_START
))
2076 up_write(&policy
->rwsem
);
2077 __cpufreq_governor(policy
, CPUFREQ_GOV_POLICY_EXIT
);
2078 down_write(&policy
->rwsem
);
2081 /* new governor failed, so re-start old one */
2082 pr_debug("starting governor %s failed\n", policy
->governor
->name
);
2084 policy
->governor
= old_gov
;
2085 __cpufreq_governor(policy
, CPUFREQ_GOV_POLICY_INIT
);
2086 __cpufreq_governor(policy
, CPUFREQ_GOV_START
);
2092 pr_debug("governor: change or update limits\n");
2093 return __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
);
2097 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2098 * @cpu: CPU which shall be re-evaluated
2100 * Useful for policy notifiers which have different necessities
2101 * at different times.
2103 int cpufreq_update_policy(unsigned int cpu
)
2105 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
2106 struct cpufreq_policy new_policy
;
2114 down_write(&policy
->rwsem
);
2116 pr_debug("updating policy for CPU %u\n", cpu
);
2117 memcpy(&new_policy
, policy
, sizeof(*policy
));
2118 new_policy
.min
= policy
->user_policy
.min
;
2119 new_policy
.max
= policy
->user_policy
.max
;
2120 new_policy
.policy
= policy
->user_policy
.policy
;
2121 new_policy
.governor
= policy
->user_policy
.governor
;
2124 * BIOS might change freq behind our back
2125 * -> ask driver for current freq and notify governors about a change
2127 if (cpufreq_driver
->get
) {
2128 new_policy
.cur
= cpufreq_driver
->get(cpu
);
2129 if (WARN_ON(!new_policy
.cur
)) {
2135 pr_debug("Driver did not initialize current freq");
2136 policy
->cur
= new_policy
.cur
;
2138 if (policy
->cur
!= new_policy
.cur
&& has_target())
2139 cpufreq_out_of_sync(cpu
, policy
->cur
,
2144 ret
= cpufreq_set_policy(policy
, &new_policy
);
2146 up_write(&policy
->rwsem
);
2148 cpufreq_cpu_put(policy
);
2152 EXPORT_SYMBOL(cpufreq_update_policy
);
2154 static int cpufreq_cpu_callback(struct notifier_block
*nfb
,
2155 unsigned long action
, void *hcpu
)
2157 unsigned int cpu
= (unsigned long)hcpu
;
2159 bool frozen
= false;
2161 dev
= get_cpu_device(cpu
);
2164 if (action
& CPU_TASKS_FROZEN
)
2167 switch (action
& ~CPU_TASKS_FROZEN
) {
2169 __cpufreq_add_dev(dev
, NULL
, frozen
);
2172 case CPU_DOWN_PREPARE
:
2173 __cpufreq_remove_dev_prepare(dev
, NULL
, frozen
);
2177 __cpufreq_remove_dev_finish(dev
, NULL
, frozen
);
2180 case CPU_DOWN_FAILED
:
2181 __cpufreq_add_dev(dev
, NULL
, frozen
);
2188 static struct notifier_block __refdata cpufreq_cpu_notifier
= {
2189 .notifier_call
= cpufreq_cpu_callback
,
2192 /*********************************************************************
2194 *********************************************************************/
2195 static int cpufreq_boost_set_sw(int state
)
2197 struct cpufreq_frequency_table
*freq_table
;
2198 struct cpufreq_policy
*policy
;
2201 list_for_each_entry(policy
, &cpufreq_policy_list
, policy_list
) {
2202 freq_table
= cpufreq_frequency_get_table(policy
->cpu
);
2204 ret
= cpufreq_frequency_table_cpuinfo(policy
,
2207 pr_err("%s: Policy frequency update failed\n",
2211 policy
->user_policy
.max
= policy
->max
;
2212 __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
);
2219 int cpufreq_boost_trigger_state(int state
)
2221 unsigned long flags
;
2224 if (cpufreq_driver
->boost_enabled
== state
)
2227 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2228 cpufreq_driver
->boost_enabled
= state
;
2229 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2231 ret
= cpufreq_driver
->set_boost(state
);
2233 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2234 cpufreq_driver
->boost_enabled
= !state
;
2235 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2237 pr_err("%s: Cannot %s BOOST\n", __func__
,
2238 state
? "enable" : "disable");
2244 int cpufreq_boost_supported(void)
2246 if (likely(cpufreq_driver
))
2247 return cpufreq_driver
->boost_supported
;
2251 EXPORT_SYMBOL_GPL(cpufreq_boost_supported
);
2253 int cpufreq_boost_enabled(void)
2255 return cpufreq_driver
->boost_enabled
;
2257 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled
);
2259 /*********************************************************************
2260 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2261 *********************************************************************/
2264 * cpufreq_register_driver - register a CPU Frequency driver
2265 * @driver_data: A struct cpufreq_driver containing the values#
2266 * submitted by the CPU Frequency driver.
2268 * Registers a CPU Frequency driver to this core code. This code
2269 * returns zero on success, -EBUSY when another driver got here first
2270 * (and isn't unregistered in the meantime).
2273 int cpufreq_register_driver(struct cpufreq_driver
*driver_data
)
2275 unsigned long flags
;
2278 if (cpufreq_disabled())
2281 if (!driver_data
|| !driver_data
->verify
|| !driver_data
->init
||
2282 !(driver_data
->setpolicy
|| driver_data
->target_index
||
2283 driver_data
->target
))
2286 pr_debug("trying to register driver %s\n", driver_data
->name
);
2288 if (driver_data
->setpolicy
)
2289 driver_data
->flags
|= CPUFREQ_CONST_LOOPS
;
2291 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2292 if (cpufreq_driver
) {
2293 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2296 cpufreq_driver
= driver_data
;
2297 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2299 if (cpufreq_boost_supported()) {
2301 * Check if driver provides function to enable boost -
2302 * if not, use cpufreq_boost_set_sw as default
2304 if (!cpufreq_driver
->set_boost
)
2305 cpufreq_driver
->set_boost
= cpufreq_boost_set_sw
;
2307 ret
= cpufreq_sysfs_create_file(&boost
.attr
);
2309 pr_err("%s: cannot register global BOOST sysfs file\n",
2311 goto err_null_driver
;
2315 ret
= subsys_interface_register(&cpufreq_interface
);
2317 goto err_boost_unreg
;
2319 if (!(cpufreq_driver
->flags
& CPUFREQ_STICKY
)) {
2323 /* check for at least one working CPU */
2324 for (i
= 0; i
< nr_cpu_ids
; i
++)
2325 if (cpu_possible(i
) && per_cpu(cpufreq_cpu_data
, i
)) {
2330 /* if all ->init() calls failed, unregister */
2332 pr_debug("no CPU initialized for driver %s\n",
2338 register_hotcpu_notifier(&cpufreq_cpu_notifier
);
2339 pr_debug("driver %s up and running\n", driver_data
->name
);
2343 subsys_interface_unregister(&cpufreq_interface
);
2345 if (cpufreq_boost_supported())
2346 cpufreq_sysfs_remove_file(&boost
.attr
);
2348 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2349 cpufreq_driver
= NULL
;
2350 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2353 EXPORT_SYMBOL_GPL(cpufreq_register_driver
);
2356 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2358 * Unregister the current CPUFreq driver. Only call this if you have
2359 * the right to do so, i.e. if you have succeeded in initialising before!
2360 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2361 * currently not initialised.
2363 int cpufreq_unregister_driver(struct cpufreq_driver
*driver
)
2365 unsigned long flags
;
2367 if (!cpufreq_driver
|| (driver
!= cpufreq_driver
))
2370 pr_debug("unregistering driver %s\n", driver
->name
);
2372 subsys_interface_unregister(&cpufreq_interface
);
2373 if (cpufreq_boost_supported())
2374 cpufreq_sysfs_remove_file(&boost
.attr
);
2376 unregister_hotcpu_notifier(&cpufreq_cpu_notifier
);
2378 down_write(&cpufreq_rwsem
);
2379 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2381 cpufreq_driver
= NULL
;
2383 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2384 up_write(&cpufreq_rwsem
);
2388 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver
);
2390 static int __init
cpufreq_core_init(void)
2392 if (cpufreq_disabled())
2395 cpufreq_global_kobject
= kobject_create();
2396 BUG_ON(!cpufreq_global_kobject
);
2400 core_initcall(cpufreq_core_init
);