2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9 * Added handling for CPU hotplug
10 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/cpu.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/syscore_ops.h>
30 #include <linux/tick.h>
31 #include <trace/events/power.h>
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 #ifdef CONFIG_HOTPLUG_CPU
46 /* This one keeps track of the previously set governor of a removed CPU */
47 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN
], cpufreq_cpu_governor
);
50 static inline bool has_target(void)
52 return cpufreq_driver
->target_index
|| cpufreq_driver
->target
;
56 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
59 static DECLARE_RWSEM(cpufreq_rwsem
);
61 /* internal prototypes */
62 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
64 static unsigned int __cpufreq_get(unsigned int cpu
);
65 static void handle_update(struct work_struct
*work
);
68 * Two notifier lists: the "policy" list is involved in the
69 * validation process for a new CPU frequency policy; the
70 * "transition" list for kernel code that needs to handle
71 * changes to devices when the CPU clock speed changes.
72 * The mutex locks both lists.
74 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list
);
75 static struct srcu_notifier_head cpufreq_transition_notifier_list
;
77 static bool init_cpufreq_transition_notifier_list_called
;
78 static int __init
init_cpufreq_transition_notifier_list(void)
80 srcu_init_notifier_head(&cpufreq_transition_notifier_list
);
81 init_cpufreq_transition_notifier_list_called
= true;
84 pure_initcall(init_cpufreq_transition_notifier_list
);
86 static int off __read_mostly
;
87 static int cpufreq_disabled(void)
91 void disable_cpufreq(void)
95 static LIST_HEAD(cpufreq_governor_list
);
96 static DEFINE_MUTEX(cpufreq_governor_mutex
);
98 bool have_governor_per_policy(void)
100 return !!(cpufreq_driver
->flags
& CPUFREQ_HAVE_GOVERNOR_PER_POLICY
);
102 EXPORT_SYMBOL_GPL(have_governor_per_policy
);
104 struct kobject
*get_governor_parent_kobj(struct cpufreq_policy
*policy
)
106 if (have_governor_per_policy())
107 return &policy
->kobj
;
109 return cpufreq_global_kobject
;
111 EXPORT_SYMBOL_GPL(get_governor_parent_kobj
);
113 static inline u64
get_cpu_idle_time_jiffy(unsigned int cpu
, u64
*wall
)
119 cur_wall_time
= jiffies64_to_cputime64(get_jiffies_64());
121 busy_time
= kcpustat_cpu(cpu
).cpustat
[CPUTIME_USER
];
122 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_SYSTEM
];
123 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_IRQ
];
124 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_SOFTIRQ
];
125 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_STEAL
];
126 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_NICE
];
128 idle_time
= cur_wall_time
- busy_time
;
130 *wall
= cputime_to_usecs(cur_wall_time
);
132 return cputime_to_usecs(idle_time
);
135 u64
get_cpu_idle_time(unsigned int cpu
, u64
*wall
, int io_busy
)
137 u64 idle_time
= get_cpu_idle_time_us(cpu
, io_busy
? wall
: NULL
);
139 if (idle_time
== -1ULL)
140 return get_cpu_idle_time_jiffy(cpu
, wall
);
142 idle_time
+= get_cpu_iowait_time_us(cpu
, wall
);
146 EXPORT_SYMBOL_GPL(get_cpu_idle_time
);
149 * This is a generic cpufreq init() routine which can be used by cpufreq
150 * drivers of SMP systems. It will do following:
151 * - validate & show freq table passed
152 * - set policies transition latency
153 * - policy->cpus with all possible CPUs
155 int cpufreq_generic_init(struct cpufreq_policy
*policy
,
156 struct cpufreq_frequency_table
*table
,
157 unsigned int transition_latency
)
161 ret
= cpufreq_table_validate_and_show(policy
, table
);
163 pr_err("%s: invalid frequency table: %d\n", __func__
, ret
);
167 policy
->cpuinfo
.transition_latency
= transition_latency
;
170 * The driver only supports the SMP configuartion where all processors
171 * share the clock and voltage and clock.
173 cpumask_setall(policy
->cpus
);
177 EXPORT_SYMBOL_GPL(cpufreq_generic_init
);
179 struct cpufreq_policy
*cpufreq_cpu_get(unsigned int cpu
)
181 struct cpufreq_policy
*policy
= NULL
;
184 if (cpufreq_disabled() || (cpu
>= nr_cpu_ids
))
187 if (!down_read_trylock(&cpufreq_rwsem
))
190 /* get the cpufreq driver */
191 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
193 if (cpufreq_driver
) {
195 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
197 kobject_get(&policy
->kobj
);
200 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
203 up_read(&cpufreq_rwsem
);
207 EXPORT_SYMBOL_GPL(cpufreq_cpu_get
);
209 void cpufreq_cpu_put(struct cpufreq_policy
*policy
)
211 if (cpufreq_disabled())
214 kobject_put(&policy
->kobj
);
215 up_read(&cpufreq_rwsem
);
217 EXPORT_SYMBOL_GPL(cpufreq_cpu_put
);
219 /*********************************************************************
220 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
221 *********************************************************************/
224 * adjust_jiffies - adjust the system "loops_per_jiffy"
226 * This function alters the system "loops_per_jiffy" for the clock
227 * speed change. Note that loops_per_jiffy cannot be updated on SMP
228 * systems as each CPU might be scaled differently. So, use the arch
229 * per-CPU loops_per_jiffy value wherever possible.
232 static unsigned long l_p_j_ref
;
233 static unsigned int l_p_j_ref_freq
;
235 static void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
237 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
240 if (!l_p_j_ref_freq
) {
241 l_p_j_ref
= loops_per_jiffy
;
242 l_p_j_ref_freq
= ci
->old
;
243 pr_debug("saving %lu as reference value for loops_per_jiffy; "
244 "freq is %u kHz\n", l_p_j_ref
, l_p_j_ref_freq
);
246 if ((val
== CPUFREQ_POSTCHANGE
&& ci
->old
!= ci
->new) ||
247 (val
== CPUFREQ_RESUMECHANGE
|| val
== CPUFREQ_SUSPENDCHANGE
)) {
248 loops_per_jiffy
= cpufreq_scale(l_p_j_ref
, l_p_j_ref_freq
,
250 pr_debug("scaling loops_per_jiffy to %lu "
251 "for frequency %u kHz\n", loops_per_jiffy
, ci
->new);
255 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
261 static void __cpufreq_notify_transition(struct cpufreq_policy
*policy
,
262 struct cpufreq_freqs
*freqs
, unsigned int state
)
264 BUG_ON(irqs_disabled());
266 if (cpufreq_disabled())
269 freqs
->flags
= cpufreq_driver
->flags
;
270 pr_debug("notification %u of frequency transition to %u kHz\n",
275 case CPUFREQ_PRECHANGE
:
276 /* detect if the driver reported a value as "old frequency"
277 * which is not equal to what the cpufreq core thinks is
280 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
281 if ((policy
) && (policy
->cpu
== freqs
->cpu
) &&
282 (policy
->cur
) && (policy
->cur
!= freqs
->old
)) {
283 pr_debug("Warning: CPU frequency is"
284 " %u, cpufreq assumed %u kHz.\n",
285 freqs
->old
, policy
->cur
);
286 freqs
->old
= policy
->cur
;
289 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
290 CPUFREQ_PRECHANGE
, freqs
);
291 adjust_jiffies(CPUFREQ_PRECHANGE
, freqs
);
294 case CPUFREQ_POSTCHANGE
:
295 adjust_jiffies(CPUFREQ_POSTCHANGE
, freqs
);
296 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs
->new,
297 (unsigned long)freqs
->cpu
);
298 trace_cpu_frequency(freqs
->new, freqs
->cpu
);
299 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
300 CPUFREQ_POSTCHANGE
, freqs
);
301 if (likely(policy
) && likely(policy
->cpu
== freqs
->cpu
))
302 policy
->cur
= freqs
->new;
308 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
309 * on frequency transition.
311 * This function calls the transition notifiers and the "adjust_jiffies"
312 * function. It is called twice on all CPU frequency changes that have
315 void cpufreq_notify_transition(struct cpufreq_policy
*policy
,
316 struct cpufreq_freqs
*freqs
, unsigned int state
)
318 for_each_cpu(freqs
->cpu
, policy
->cpus
)
319 __cpufreq_notify_transition(policy
, freqs
, state
);
321 EXPORT_SYMBOL_GPL(cpufreq_notify_transition
);
323 /* Do post notifications when there are chances that transition has failed */
324 void cpufreq_notify_post_transition(struct cpufreq_policy
*policy
,
325 struct cpufreq_freqs
*freqs
, int transition_failed
)
327 cpufreq_notify_transition(policy
, freqs
, CPUFREQ_POSTCHANGE
);
328 if (!transition_failed
)
331 swap(freqs
->old
, freqs
->new);
332 cpufreq_notify_transition(policy
, freqs
, CPUFREQ_PRECHANGE
);
333 cpufreq_notify_transition(policy
, freqs
, CPUFREQ_POSTCHANGE
);
335 EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition
);
338 /*********************************************************************
340 *********************************************************************/
342 static struct cpufreq_governor
*__find_governor(const char *str_governor
)
344 struct cpufreq_governor
*t
;
346 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
)
347 if (!strnicmp(str_governor
, t
->name
, CPUFREQ_NAME_LEN
))
354 * cpufreq_parse_governor - parse a governor string
356 static int cpufreq_parse_governor(char *str_governor
, unsigned int *policy
,
357 struct cpufreq_governor
**governor
)
364 if (cpufreq_driver
->setpolicy
) {
365 if (!strnicmp(str_governor
, "performance", CPUFREQ_NAME_LEN
)) {
366 *policy
= CPUFREQ_POLICY_PERFORMANCE
;
368 } else if (!strnicmp(str_governor
, "powersave",
370 *policy
= CPUFREQ_POLICY_POWERSAVE
;
373 } else if (has_target()) {
374 struct cpufreq_governor
*t
;
376 mutex_lock(&cpufreq_governor_mutex
);
378 t
= __find_governor(str_governor
);
383 mutex_unlock(&cpufreq_governor_mutex
);
384 ret
= request_module("cpufreq_%s", str_governor
);
385 mutex_lock(&cpufreq_governor_mutex
);
388 t
= __find_governor(str_governor
);
396 mutex_unlock(&cpufreq_governor_mutex
);
403 * cpufreq_per_cpu_attr_read() / show_##file_name() -
404 * print out cpufreq information
406 * Write out information from cpufreq_driver->policy[cpu]; object must be
410 #define show_one(file_name, object) \
411 static ssize_t show_##file_name \
412 (struct cpufreq_policy *policy, char *buf) \
414 return sprintf(buf, "%u\n", policy->object); \
417 show_one(cpuinfo_min_freq
, cpuinfo
.min_freq
);
418 show_one(cpuinfo_max_freq
, cpuinfo
.max_freq
);
419 show_one(cpuinfo_transition_latency
, cpuinfo
.transition_latency
);
420 show_one(scaling_min_freq
, min
);
421 show_one(scaling_max_freq
, max
);
422 show_one(scaling_cur_freq
, cur
);
424 static int cpufreq_set_policy(struct cpufreq_policy
*policy
,
425 struct cpufreq_policy
*new_policy
);
428 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
430 #define store_one(file_name, object) \
431 static ssize_t store_##file_name \
432 (struct cpufreq_policy *policy, const char *buf, size_t count) \
435 struct cpufreq_policy new_policy; \
437 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
441 ret = sscanf(buf, "%u", &new_policy.object); \
445 ret = cpufreq_set_policy(policy, &new_policy); \
446 policy->user_policy.object = policy->object; \
448 return ret ? ret : count; \
451 store_one(scaling_min_freq
, min
);
452 store_one(scaling_max_freq
, max
);
455 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
457 static ssize_t
show_cpuinfo_cur_freq(struct cpufreq_policy
*policy
,
460 unsigned int cur_freq
= __cpufreq_get(policy
->cpu
);
462 return sprintf(buf
, "<unknown>");
463 return sprintf(buf
, "%u\n", cur_freq
);
467 * show_scaling_governor - show the current policy for the specified CPU
469 static ssize_t
show_scaling_governor(struct cpufreq_policy
*policy
, char *buf
)
471 if (policy
->policy
== CPUFREQ_POLICY_POWERSAVE
)
472 return sprintf(buf
, "powersave\n");
473 else if (policy
->policy
== CPUFREQ_POLICY_PERFORMANCE
)
474 return sprintf(buf
, "performance\n");
475 else if (policy
->governor
)
476 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n",
477 policy
->governor
->name
);
482 * store_scaling_governor - store policy for the specified CPU
484 static ssize_t
store_scaling_governor(struct cpufreq_policy
*policy
,
485 const char *buf
, size_t count
)
488 char str_governor
[16];
489 struct cpufreq_policy new_policy
;
491 ret
= cpufreq_get_policy(&new_policy
, policy
->cpu
);
495 ret
= sscanf(buf
, "%15s", str_governor
);
499 if (cpufreq_parse_governor(str_governor
, &new_policy
.policy
,
500 &new_policy
.governor
))
503 ret
= cpufreq_set_policy(policy
, &new_policy
);
505 policy
->user_policy
.policy
= policy
->policy
;
506 policy
->user_policy
.governor
= policy
->governor
;
515 * show_scaling_driver - show the cpufreq driver currently loaded
517 static ssize_t
show_scaling_driver(struct cpufreq_policy
*policy
, char *buf
)
519 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n", cpufreq_driver
->name
);
523 * show_scaling_available_governors - show the available CPUfreq governors
525 static ssize_t
show_scaling_available_governors(struct cpufreq_policy
*policy
,
529 struct cpufreq_governor
*t
;
532 i
+= sprintf(buf
, "performance powersave");
536 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
537 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char))
538 - (CPUFREQ_NAME_LEN
+ 2)))
540 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_PLEN
, "%s ", t
->name
);
543 i
+= sprintf(&buf
[i
], "\n");
547 ssize_t
cpufreq_show_cpus(const struct cpumask
*mask
, char *buf
)
552 for_each_cpu(cpu
, mask
) {
554 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), " ");
555 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), "%u", cpu
);
556 if (i
>= (PAGE_SIZE
- 5))
559 i
+= sprintf(&buf
[i
], "\n");
562 EXPORT_SYMBOL_GPL(cpufreq_show_cpus
);
565 * show_related_cpus - show the CPUs affected by each transition even if
566 * hw coordination is in use
568 static ssize_t
show_related_cpus(struct cpufreq_policy
*policy
, char *buf
)
570 return cpufreq_show_cpus(policy
->related_cpus
, buf
);
574 * show_affected_cpus - show the CPUs affected by each transition
576 static ssize_t
show_affected_cpus(struct cpufreq_policy
*policy
, char *buf
)
578 return cpufreq_show_cpus(policy
->cpus
, buf
);
581 static ssize_t
store_scaling_setspeed(struct cpufreq_policy
*policy
,
582 const char *buf
, size_t count
)
584 unsigned int freq
= 0;
587 if (!policy
->governor
|| !policy
->governor
->store_setspeed
)
590 ret
= sscanf(buf
, "%u", &freq
);
594 policy
->governor
->store_setspeed(policy
, freq
);
599 static ssize_t
show_scaling_setspeed(struct cpufreq_policy
*policy
, char *buf
)
601 if (!policy
->governor
|| !policy
->governor
->show_setspeed
)
602 return sprintf(buf
, "<unsupported>\n");
604 return policy
->governor
->show_setspeed(policy
, buf
);
608 * show_bios_limit - show the current cpufreq HW/BIOS limitation
610 static ssize_t
show_bios_limit(struct cpufreq_policy
*policy
, char *buf
)
614 if (cpufreq_driver
->bios_limit
) {
615 ret
= cpufreq_driver
->bios_limit(policy
->cpu
, &limit
);
617 return sprintf(buf
, "%u\n", limit
);
619 return sprintf(buf
, "%u\n", policy
->cpuinfo
.max_freq
);
622 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq
, 0400);
623 cpufreq_freq_attr_ro(cpuinfo_min_freq
);
624 cpufreq_freq_attr_ro(cpuinfo_max_freq
);
625 cpufreq_freq_attr_ro(cpuinfo_transition_latency
);
626 cpufreq_freq_attr_ro(scaling_available_governors
);
627 cpufreq_freq_attr_ro(scaling_driver
);
628 cpufreq_freq_attr_ro(scaling_cur_freq
);
629 cpufreq_freq_attr_ro(bios_limit
);
630 cpufreq_freq_attr_ro(related_cpus
);
631 cpufreq_freq_attr_ro(affected_cpus
);
632 cpufreq_freq_attr_rw(scaling_min_freq
);
633 cpufreq_freq_attr_rw(scaling_max_freq
);
634 cpufreq_freq_attr_rw(scaling_governor
);
635 cpufreq_freq_attr_rw(scaling_setspeed
);
637 static struct attribute
*default_attrs
[] = {
638 &cpuinfo_min_freq
.attr
,
639 &cpuinfo_max_freq
.attr
,
640 &cpuinfo_transition_latency
.attr
,
641 &scaling_min_freq
.attr
,
642 &scaling_max_freq
.attr
,
645 &scaling_governor
.attr
,
646 &scaling_driver
.attr
,
647 &scaling_available_governors
.attr
,
648 &scaling_setspeed
.attr
,
652 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
653 #define to_attr(a) container_of(a, struct freq_attr, attr)
655 static ssize_t
show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
657 struct cpufreq_policy
*policy
= to_policy(kobj
);
658 struct freq_attr
*fattr
= to_attr(attr
);
661 if (!down_read_trylock(&cpufreq_rwsem
))
664 down_read(&policy
->rwsem
);
667 ret
= fattr
->show(policy
, buf
);
671 up_read(&policy
->rwsem
);
672 up_read(&cpufreq_rwsem
);
677 static ssize_t
store(struct kobject
*kobj
, struct attribute
*attr
,
678 const char *buf
, size_t count
)
680 struct cpufreq_policy
*policy
= to_policy(kobj
);
681 struct freq_attr
*fattr
= to_attr(attr
);
682 ssize_t ret
= -EINVAL
;
686 if (!cpu_online(policy
->cpu
))
689 if (!down_read_trylock(&cpufreq_rwsem
))
692 down_write(&policy
->rwsem
);
695 ret
= fattr
->store(policy
, buf
, count
);
699 up_write(&policy
->rwsem
);
701 up_read(&cpufreq_rwsem
);
708 static void cpufreq_sysfs_release(struct kobject
*kobj
)
710 struct cpufreq_policy
*policy
= to_policy(kobj
);
711 pr_debug("last reference is dropped\n");
712 complete(&policy
->kobj_unregister
);
715 static const struct sysfs_ops sysfs_ops
= {
720 static struct kobj_type ktype_cpufreq
= {
721 .sysfs_ops
= &sysfs_ops
,
722 .default_attrs
= default_attrs
,
723 .release
= cpufreq_sysfs_release
,
726 struct kobject
*cpufreq_global_kobject
;
727 EXPORT_SYMBOL(cpufreq_global_kobject
);
729 static int cpufreq_global_kobject_usage
;
731 int cpufreq_get_global_kobject(void)
733 if (!cpufreq_global_kobject_usage
++)
734 return kobject_add(cpufreq_global_kobject
,
735 &cpu_subsys
.dev_root
->kobj
, "%s", "cpufreq");
739 EXPORT_SYMBOL(cpufreq_get_global_kobject
);
741 void cpufreq_put_global_kobject(void)
743 if (!--cpufreq_global_kobject_usage
)
744 kobject_del(cpufreq_global_kobject
);
746 EXPORT_SYMBOL(cpufreq_put_global_kobject
);
748 int cpufreq_sysfs_create_file(const struct attribute
*attr
)
750 int ret
= cpufreq_get_global_kobject();
753 ret
= sysfs_create_file(cpufreq_global_kobject
, attr
);
755 cpufreq_put_global_kobject();
760 EXPORT_SYMBOL(cpufreq_sysfs_create_file
);
762 void cpufreq_sysfs_remove_file(const struct attribute
*attr
)
764 sysfs_remove_file(cpufreq_global_kobject
, attr
);
765 cpufreq_put_global_kobject();
767 EXPORT_SYMBOL(cpufreq_sysfs_remove_file
);
769 /* symlink affected CPUs */
770 static int cpufreq_add_dev_symlink(struct cpufreq_policy
*policy
)
775 for_each_cpu(j
, policy
->cpus
) {
776 struct device
*cpu_dev
;
778 if (j
== policy
->cpu
)
781 pr_debug("Adding link for CPU: %u\n", j
);
782 cpu_dev
= get_cpu_device(j
);
783 ret
= sysfs_create_link(&cpu_dev
->kobj
, &policy
->kobj
,
791 static int cpufreq_add_dev_interface(struct cpufreq_policy
*policy
,
794 struct freq_attr
**drv_attr
;
797 /* prepare interface data */
798 ret
= kobject_init_and_add(&policy
->kobj
, &ktype_cpufreq
,
799 &dev
->kobj
, "cpufreq");
803 /* set up files for this cpu device */
804 drv_attr
= cpufreq_driver
->attr
;
805 while ((drv_attr
) && (*drv_attr
)) {
806 ret
= sysfs_create_file(&policy
->kobj
, &((*drv_attr
)->attr
));
808 goto err_out_kobj_put
;
811 if (cpufreq_driver
->get
) {
812 ret
= sysfs_create_file(&policy
->kobj
, &cpuinfo_cur_freq
.attr
);
814 goto err_out_kobj_put
;
817 ret
= sysfs_create_file(&policy
->kobj
, &scaling_cur_freq
.attr
);
819 goto err_out_kobj_put
;
821 if (cpufreq_driver
->bios_limit
) {
822 ret
= sysfs_create_file(&policy
->kobj
, &bios_limit
.attr
);
824 goto err_out_kobj_put
;
827 ret
= cpufreq_add_dev_symlink(policy
);
829 goto err_out_kobj_put
;
834 kobject_put(&policy
->kobj
);
835 wait_for_completion(&policy
->kobj_unregister
);
839 static void cpufreq_init_policy(struct cpufreq_policy
*policy
)
841 struct cpufreq_policy new_policy
;
844 memcpy(&new_policy
, policy
, sizeof(*policy
));
846 /* Use the default policy if its valid. */
847 if (cpufreq_driver
->setpolicy
)
848 cpufreq_parse_governor(policy
->governor
->name
,
849 &new_policy
.policy
, NULL
);
851 /* assure that the starting sequence is run in cpufreq_set_policy */
852 policy
->governor
= NULL
;
854 /* set default policy */
855 ret
= cpufreq_set_policy(policy
, &new_policy
);
857 pr_debug("setting policy failed\n");
858 if (cpufreq_driver
->exit
)
859 cpufreq_driver
->exit(policy
);
863 #ifdef CONFIG_HOTPLUG_CPU
864 static int cpufreq_add_policy_cpu(struct cpufreq_policy
*policy
,
865 unsigned int cpu
, struct device
*dev
)
871 ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
873 pr_err("%s: Failed to stop governor\n", __func__
);
878 down_write(&policy
->rwsem
);
880 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
882 cpumask_set_cpu(cpu
, policy
->cpus
);
883 per_cpu(cpufreq_cpu_data
, cpu
) = policy
;
884 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
886 up_write(&policy
->rwsem
);
889 if ((ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_START
)) ||
890 (ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
))) {
891 pr_err("%s: Failed to start governor\n", __func__
);
896 return sysfs_create_link(&dev
->kobj
, &policy
->kobj
, "cpufreq");
900 static struct cpufreq_policy
*cpufreq_policy_restore(unsigned int cpu
)
902 struct cpufreq_policy
*policy
;
905 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
907 policy
= per_cpu(cpufreq_cpu_data_fallback
, cpu
);
909 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
914 static struct cpufreq_policy
*cpufreq_policy_alloc(void)
916 struct cpufreq_policy
*policy
;
918 policy
= kzalloc(sizeof(*policy
), GFP_KERNEL
);
922 if (!alloc_cpumask_var(&policy
->cpus
, GFP_KERNEL
))
923 goto err_free_policy
;
925 if (!zalloc_cpumask_var(&policy
->related_cpus
, GFP_KERNEL
))
926 goto err_free_cpumask
;
928 INIT_LIST_HEAD(&policy
->policy_list
);
929 init_rwsem(&policy
->rwsem
);
934 free_cpumask_var(policy
->cpus
);
941 static void cpufreq_policy_put_kobj(struct cpufreq_policy
*policy
)
943 struct kobject
*kobj
;
944 struct completion
*cmp
;
946 down_read(&policy
->rwsem
);
947 kobj
= &policy
->kobj
;
948 cmp
= &policy
->kobj_unregister
;
949 up_read(&policy
->rwsem
);
953 * We need to make sure that the underlying kobj is
954 * actually not referenced anymore by anybody before we
955 * proceed with unloading.
957 pr_debug("waiting for dropping of refcount\n");
958 wait_for_completion(cmp
);
959 pr_debug("wait complete\n");
962 static void cpufreq_policy_free(struct cpufreq_policy
*policy
)
964 free_cpumask_var(policy
->related_cpus
);
965 free_cpumask_var(policy
->cpus
);
969 static void update_policy_cpu(struct cpufreq_policy
*policy
, unsigned int cpu
)
971 if (WARN_ON(cpu
== policy
->cpu
))
974 down_write(&policy
->rwsem
);
976 policy
->last_cpu
= policy
->cpu
;
979 up_write(&policy
->rwsem
);
981 cpufreq_frequency_table_update_policy_cpu(policy
);
982 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
983 CPUFREQ_UPDATE_POLICY_CPU
, policy
);
986 static int __cpufreq_add_dev(struct device
*dev
, struct subsys_interface
*sif
,
989 unsigned int j
, cpu
= dev
->id
;
991 struct cpufreq_policy
*policy
;
993 #ifdef CONFIG_HOTPLUG_CPU
994 struct cpufreq_policy
*tpolicy
;
995 struct cpufreq_governor
*gov
;
998 if (cpu_is_offline(cpu
))
1001 pr_debug("adding CPU %u\n", cpu
);
1004 /* check whether a different CPU already registered this
1005 * CPU because it is in the same boat. */
1006 policy
= cpufreq_cpu_get(cpu
);
1007 if (unlikely(policy
)) {
1008 cpufreq_cpu_put(policy
);
1013 if (!down_read_trylock(&cpufreq_rwsem
))
1016 #ifdef CONFIG_HOTPLUG_CPU
1017 /* Check if this cpu was hot-unplugged earlier and has siblings */
1018 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1019 list_for_each_entry(tpolicy
, &cpufreq_policy_list
, policy_list
) {
1020 if (cpumask_test_cpu(cpu
, tpolicy
->related_cpus
)) {
1021 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1022 ret
= cpufreq_add_policy_cpu(tpolicy
, cpu
, dev
);
1023 up_read(&cpufreq_rwsem
);
1027 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1031 * Restore the saved policy when doing light-weight init and fall back
1032 * to the full init if that fails.
1034 policy
= frozen
? cpufreq_policy_restore(cpu
) : NULL
;
1037 policy
= cpufreq_policy_alloc();
1043 * In the resume path, since we restore a saved policy, the assignment
1044 * to policy->cpu is like an update of the existing policy, rather than
1045 * the creation of a brand new one. So we need to perform this update
1046 * by invoking update_policy_cpu().
1048 if (frozen
&& cpu
!= policy
->cpu
)
1049 update_policy_cpu(policy
, cpu
);
1053 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
1054 cpumask_copy(policy
->cpus
, cpumask_of(cpu
));
1056 init_completion(&policy
->kobj_unregister
);
1057 INIT_WORK(&policy
->update
, handle_update
);
1059 /* call driver. From then on the cpufreq must be able
1060 * to accept all calls to ->verify and ->setpolicy for this CPU
1062 ret
= cpufreq_driver
->init(policy
);
1064 pr_debug("initialization failed\n");
1065 goto err_set_policy_cpu
;
1068 if (cpufreq_driver
->get
) {
1069 policy
->cur
= cpufreq_driver
->get(policy
->cpu
);
1071 pr_err("%s: ->get() failed\n", __func__
);
1077 * Sometimes boot loaders set CPU frequency to a value outside of
1078 * frequency table present with cpufreq core. In such cases CPU might be
1079 * unstable if it has to run on that frequency for long duration of time
1080 * and so its better to set it to a frequency which is specified in
1081 * freq-table. This also makes cpufreq stats inconsistent as
1082 * cpufreq-stats would fail to register because current frequency of CPU
1083 * isn't found in freq-table.
1085 * Because we don't want this change to effect boot process badly, we go
1086 * for the next freq which is >= policy->cur ('cur' must be set by now,
1087 * otherwise we will end up setting freq to lowest of the table as 'cur'
1088 * is initialized to zero).
1090 * We are passing target-freq as "policy->cur - 1" otherwise
1091 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1092 * equal to target-freq.
1094 if ((cpufreq_driver
->flags
& CPUFREQ_NEED_INITIAL_FREQ_CHECK
)
1096 /* Are we running at unknown frequency ? */
1097 ret
= cpufreq_frequency_table_get_index(policy
, policy
->cur
);
1098 if (ret
== -EINVAL
) {
1099 /* Warn user and fix it */
1100 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1101 __func__
, policy
->cpu
, policy
->cur
);
1102 ret
= __cpufreq_driver_target(policy
, policy
->cur
- 1,
1103 CPUFREQ_RELATION_L
);
1106 * Reaching here after boot in a few seconds may not
1107 * mean that system will remain stable at "unknown"
1108 * frequency for longer duration. Hence, a BUG_ON().
1111 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1112 __func__
, policy
->cpu
, policy
->cur
);
1116 /* related cpus should atleast have policy->cpus */
1117 cpumask_or(policy
->related_cpus
, policy
->related_cpus
, policy
->cpus
);
1120 * affected cpus must always be the one, which are online. We aren't
1121 * managing offline cpus here.
1123 cpumask_and(policy
->cpus
, policy
->cpus
, cpu_online_mask
);
1126 policy
->user_policy
.min
= policy
->min
;
1127 policy
->user_policy
.max
= policy
->max
;
1130 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1131 CPUFREQ_START
, policy
);
1133 #ifdef CONFIG_HOTPLUG_CPU
1134 gov
= __find_governor(per_cpu(cpufreq_cpu_governor
, cpu
));
1136 policy
->governor
= gov
;
1137 pr_debug("Restoring governor %s for cpu %d\n",
1138 policy
->governor
->name
, cpu
);
1142 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1143 for_each_cpu(j
, policy
->cpus
)
1144 per_cpu(cpufreq_cpu_data
, j
) = policy
;
1145 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1148 ret
= cpufreq_add_dev_interface(policy
, dev
);
1150 goto err_out_unregister
;
1153 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1154 list_add(&policy
->policy_list
, &cpufreq_policy_list
);
1155 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1157 cpufreq_init_policy(policy
);
1160 policy
->user_policy
.policy
= policy
->policy
;
1161 policy
->user_policy
.governor
= policy
->governor
;
1164 kobject_uevent(&policy
->kobj
, KOBJ_ADD
);
1165 up_read(&cpufreq_rwsem
);
1167 pr_debug("initialization complete\n");
1172 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1173 for_each_cpu(j
, policy
->cpus
)
1174 per_cpu(cpufreq_cpu_data
, j
) = NULL
;
1175 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1178 if (cpufreq_driver
->exit
)
1179 cpufreq_driver
->exit(policy
);
1182 /* Do not leave stale fallback data behind. */
1183 per_cpu(cpufreq_cpu_data_fallback
, cpu
) = NULL
;
1184 cpufreq_policy_put_kobj(policy
);
1186 cpufreq_policy_free(policy
);
1189 up_read(&cpufreq_rwsem
);
1195 * cpufreq_add_dev - add a CPU device
1197 * Adds the cpufreq interface for a CPU device.
1199 * The Oracle says: try running cpufreq registration/unregistration concurrently
1200 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1201 * mess up, but more thorough testing is needed. - Mathieu
1203 static int cpufreq_add_dev(struct device
*dev
, struct subsys_interface
*sif
)
1205 return __cpufreq_add_dev(dev
, sif
, false);
1208 static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy
*policy
,
1209 unsigned int old_cpu
)
1211 struct device
*cpu_dev
;
1214 /* first sibling now owns the new sysfs dir */
1215 cpu_dev
= get_cpu_device(cpumask_any_but(policy
->cpus
, old_cpu
));
1217 sysfs_remove_link(&cpu_dev
->kobj
, "cpufreq");
1218 ret
= kobject_move(&policy
->kobj
, &cpu_dev
->kobj
);
1220 pr_err("%s: Failed to move kobj: %d", __func__
, ret
);
1222 down_write(&policy
->rwsem
);
1223 cpumask_set_cpu(old_cpu
, policy
->cpus
);
1224 up_write(&policy
->rwsem
);
1226 ret
= sysfs_create_link(&cpu_dev
->kobj
, &policy
->kobj
,
1235 static int __cpufreq_remove_dev_prepare(struct device
*dev
,
1236 struct subsys_interface
*sif
,
1239 unsigned int cpu
= dev
->id
, cpus
;
1241 unsigned long flags
;
1242 struct cpufreq_policy
*policy
;
1244 pr_debug("%s: unregistering CPU %u\n", __func__
, cpu
);
1246 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1248 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1250 /* Save the policy somewhere when doing a light-weight tear-down */
1252 per_cpu(cpufreq_cpu_data_fallback
, cpu
) = policy
;
1254 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1257 pr_debug("%s: No cpu_data found\n", __func__
);
1262 ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
1264 pr_err("%s: Failed to stop governor\n", __func__
);
1269 #ifdef CONFIG_HOTPLUG_CPU
1270 if (!cpufreq_driver
->setpolicy
)
1271 strncpy(per_cpu(cpufreq_cpu_governor
, cpu
),
1272 policy
->governor
->name
, CPUFREQ_NAME_LEN
);
1275 down_read(&policy
->rwsem
);
1276 cpus
= cpumask_weight(policy
->cpus
);
1277 up_read(&policy
->rwsem
);
1279 if (cpu
!= policy
->cpu
) {
1281 sysfs_remove_link(&dev
->kobj
, "cpufreq");
1282 } else if (cpus
> 1) {
1283 new_cpu
= cpufreq_nominate_new_policy_cpu(policy
, cpu
);
1285 update_policy_cpu(policy
, new_cpu
);
1288 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1289 __func__
, new_cpu
, cpu
);
1297 static int __cpufreq_remove_dev_finish(struct device
*dev
,
1298 struct subsys_interface
*sif
,
1301 unsigned int cpu
= dev
->id
, cpus
;
1303 unsigned long flags
;
1304 struct cpufreq_policy
*policy
;
1306 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1307 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1308 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1311 pr_debug("%s: No cpu_data found\n", __func__
);
1315 down_write(&policy
->rwsem
);
1316 cpus
= cpumask_weight(policy
->cpus
);
1319 cpumask_clear_cpu(cpu
, policy
->cpus
);
1320 up_write(&policy
->rwsem
);
1322 /* If cpu is last user of policy, free policy */
1325 ret
= __cpufreq_governor(policy
,
1326 CPUFREQ_GOV_POLICY_EXIT
);
1328 pr_err("%s: Failed to exit governor\n",
1335 cpufreq_policy_put_kobj(policy
);
1338 * Perform the ->exit() even during light-weight tear-down,
1339 * since this is a core component, and is essential for the
1340 * subsequent light-weight ->init() to succeed.
1342 if (cpufreq_driver
->exit
)
1343 cpufreq_driver
->exit(policy
);
1345 /* Remove policy from list of active policies */
1346 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1347 list_del(&policy
->policy_list
);
1348 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1351 cpufreq_policy_free(policy
);
1354 if ((ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_START
)) ||
1355 (ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
))) {
1356 pr_err("%s: Failed to start governor\n",
1363 per_cpu(cpufreq_cpu_data
, cpu
) = NULL
;
1368 * cpufreq_remove_dev - remove a CPU device
1370 * Removes the cpufreq interface for a CPU device.
1372 static int cpufreq_remove_dev(struct device
*dev
, struct subsys_interface
*sif
)
1374 unsigned int cpu
= dev
->id
;
1377 if (cpu_is_offline(cpu
))
1380 ret
= __cpufreq_remove_dev_prepare(dev
, sif
, false);
1383 ret
= __cpufreq_remove_dev_finish(dev
, sif
, false);
1388 static void handle_update(struct work_struct
*work
)
1390 struct cpufreq_policy
*policy
=
1391 container_of(work
, struct cpufreq_policy
, update
);
1392 unsigned int cpu
= policy
->cpu
;
1393 pr_debug("handle_update for cpu %u called\n", cpu
);
1394 cpufreq_update_policy(cpu
);
1398 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1401 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1402 * @new_freq: CPU frequency the CPU actually runs at
1404 * We adjust to current frequency first, and need to clean up later.
1405 * So either call to cpufreq_update_policy() or schedule handle_update()).
1407 static void cpufreq_out_of_sync(unsigned int cpu
, unsigned int old_freq
,
1408 unsigned int new_freq
)
1410 struct cpufreq_policy
*policy
;
1411 struct cpufreq_freqs freqs
;
1412 unsigned long flags
;
1414 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1415 "core thinks of %u, is %u kHz.\n", old_freq
, new_freq
);
1417 freqs
.old
= old_freq
;
1418 freqs
.new = new_freq
;
1420 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1421 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1422 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1424 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_PRECHANGE
);
1425 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_POSTCHANGE
);
1429 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1432 * This is the last known freq, without actually getting it from the driver.
1433 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1435 unsigned int cpufreq_quick_get(unsigned int cpu
)
1437 struct cpufreq_policy
*policy
;
1438 unsigned int ret_freq
= 0;
1440 if (cpufreq_driver
&& cpufreq_driver
->setpolicy
&& cpufreq_driver
->get
)
1441 return cpufreq_driver
->get(cpu
);
1443 policy
= cpufreq_cpu_get(cpu
);
1445 ret_freq
= policy
->cur
;
1446 cpufreq_cpu_put(policy
);
1451 EXPORT_SYMBOL(cpufreq_quick_get
);
1454 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1457 * Just return the max possible frequency for a given CPU.
1459 unsigned int cpufreq_quick_get_max(unsigned int cpu
)
1461 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1462 unsigned int ret_freq
= 0;
1465 ret_freq
= policy
->max
;
1466 cpufreq_cpu_put(policy
);
1471 EXPORT_SYMBOL(cpufreq_quick_get_max
);
1473 static unsigned int __cpufreq_get(unsigned int cpu
)
1475 struct cpufreq_policy
*policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1476 unsigned int ret_freq
= 0;
1478 if (!cpufreq_driver
->get
)
1481 ret_freq
= cpufreq_driver
->get(cpu
);
1483 if (ret_freq
&& policy
->cur
&&
1484 !(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
1485 /* verify no discrepancy between actual and
1486 saved value exists */
1487 if (unlikely(ret_freq
!= policy
->cur
)) {
1488 cpufreq_out_of_sync(cpu
, policy
->cur
, ret_freq
);
1489 schedule_work(&policy
->update
);
1497 * cpufreq_get - get the current CPU frequency (in kHz)
1500 * Get the CPU current (static) CPU frequency
1502 unsigned int cpufreq_get(unsigned int cpu
)
1504 struct cpufreq_policy
*policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1505 unsigned int ret_freq
= 0;
1507 if (cpufreq_disabled() || !cpufreq_driver
)
1512 if (!down_read_trylock(&cpufreq_rwsem
))
1515 down_read(&policy
->rwsem
);
1517 ret_freq
= __cpufreq_get(cpu
);
1519 up_read(&policy
->rwsem
);
1520 up_read(&cpufreq_rwsem
);
1524 EXPORT_SYMBOL(cpufreq_get
);
1526 static struct subsys_interface cpufreq_interface
= {
1528 .subsys
= &cpu_subsys
,
1529 .add_dev
= cpufreq_add_dev
,
1530 .remove_dev
= cpufreq_remove_dev
,
1534 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1536 * This function is only executed for the boot processor. The other CPUs
1537 * have been put offline by means of CPU hotplug.
1539 static int cpufreq_bp_suspend(void)
1543 int cpu
= smp_processor_id();
1544 struct cpufreq_policy
*policy
;
1546 pr_debug("suspending cpu %u\n", cpu
);
1548 /* If there's no policy for the boot CPU, we have nothing to do. */
1549 policy
= cpufreq_cpu_get(cpu
);
1553 if (cpufreq_driver
->suspend
) {
1554 ret
= cpufreq_driver
->suspend(policy
);
1556 printk(KERN_ERR
"cpufreq: suspend failed in ->suspend "
1557 "step on CPU %u\n", policy
->cpu
);
1560 cpufreq_cpu_put(policy
);
1565 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1567 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1568 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1569 * restored. It will verify that the current freq is in sync with
1570 * what we believe it to be. This is a bit later than when it
1571 * should be, but nonethteless it's better than calling
1572 * cpufreq_driver->get() here which might re-enable interrupts...
1574 * This function is only executed for the boot CPU. The other CPUs have not
1575 * been turned on yet.
1577 static void cpufreq_bp_resume(void)
1581 int cpu
= smp_processor_id();
1582 struct cpufreq_policy
*policy
;
1584 pr_debug("resuming cpu %u\n", cpu
);
1586 /* If there's no policy for the boot CPU, we have nothing to do. */
1587 policy
= cpufreq_cpu_get(cpu
);
1591 if (cpufreq_driver
->resume
) {
1592 ret
= cpufreq_driver
->resume(policy
);
1594 printk(KERN_ERR
"cpufreq: resume failed in ->resume "
1595 "step on CPU %u\n", policy
->cpu
);
1600 schedule_work(&policy
->update
);
1603 cpufreq_cpu_put(policy
);
1606 static struct syscore_ops cpufreq_syscore_ops
= {
1607 .suspend
= cpufreq_bp_suspend
,
1608 .resume
= cpufreq_bp_resume
,
1612 * cpufreq_get_current_driver - return current driver's name
1614 * Return the name string of the currently loaded cpufreq driver
1617 const char *cpufreq_get_current_driver(void)
1620 return cpufreq_driver
->name
;
1624 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver
);
1626 /*********************************************************************
1627 * NOTIFIER LISTS INTERFACE *
1628 *********************************************************************/
1631 * cpufreq_register_notifier - register a driver with cpufreq
1632 * @nb: notifier function to register
1633 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1635 * Add a driver to one of two lists: either a list of drivers that
1636 * are notified about clock rate changes (once before and once after
1637 * the transition), or a list of drivers that are notified about
1638 * changes in cpufreq policy.
1640 * This function may sleep, and has the same return conditions as
1641 * blocking_notifier_chain_register.
1643 int cpufreq_register_notifier(struct notifier_block
*nb
, unsigned int list
)
1647 if (cpufreq_disabled())
1650 WARN_ON(!init_cpufreq_transition_notifier_list_called
);
1653 case CPUFREQ_TRANSITION_NOTIFIER
:
1654 ret
= srcu_notifier_chain_register(
1655 &cpufreq_transition_notifier_list
, nb
);
1657 case CPUFREQ_POLICY_NOTIFIER
:
1658 ret
= blocking_notifier_chain_register(
1659 &cpufreq_policy_notifier_list
, nb
);
1667 EXPORT_SYMBOL(cpufreq_register_notifier
);
1670 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1671 * @nb: notifier block to be unregistered
1672 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1674 * Remove a driver from the CPU frequency notifier list.
1676 * This function may sleep, and has the same return conditions as
1677 * blocking_notifier_chain_unregister.
1679 int cpufreq_unregister_notifier(struct notifier_block
*nb
, unsigned int list
)
1683 if (cpufreq_disabled())
1687 case CPUFREQ_TRANSITION_NOTIFIER
:
1688 ret
= srcu_notifier_chain_unregister(
1689 &cpufreq_transition_notifier_list
, nb
);
1691 case CPUFREQ_POLICY_NOTIFIER
:
1692 ret
= blocking_notifier_chain_unregister(
1693 &cpufreq_policy_notifier_list
, nb
);
1701 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1704 /*********************************************************************
1706 *********************************************************************/
1708 int __cpufreq_driver_target(struct cpufreq_policy
*policy
,
1709 unsigned int target_freq
,
1710 unsigned int relation
)
1712 int retval
= -EINVAL
;
1713 unsigned int old_target_freq
= target_freq
;
1715 if (cpufreq_disabled())
1718 /* Make sure that target_freq is within supported range */
1719 if (target_freq
> policy
->max
)
1720 target_freq
= policy
->max
;
1721 if (target_freq
< policy
->min
)
1722 target_freq
= policy
->min
;
1724 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1725 policy
->cpu
, target_freq
, relation
, old_target_freq
);
1728 * This might look like a redundant call as we are checking it again
1729 * after finding index. But it is left intentionally for cases where
1730 * exactly same freq is called again and so we can save on few function
1733 if (target_freq
== policy
->cur
)
1736 if (cpufreq_driver
->target
)
1737 retval
= cpufreq_driver
->target(policy
, target_freq
, relation
);
1738 else if (cpufreq_driver
->target_index
) {
1739 struct cpufreq_frequency_table
*freq_table
;
1740 struct cpufreq_freqs freqs
;
1744 freq_table
= cpufreq_frequency_get_table(policy
->cpu
);
1745 if (unlikely(!freq_table
)) {
1746 pr_err("%s: Unable to find freq_table\n", __func__
);
1750 retval
= cpufreq_frequency_table_target(policy
, freq_table
,
1751 target_freq
, relation
, &index
);
1752 if (unlikely(retval
)) {
1753 pr_err("%s: Unable to find matching freq\n", __func__
);
1757 if (freq_table
[index
].frequency
== policy
->cur
) {
1762 notify
= !(cpufreq_driver
->flags
& CPUFREQ_ASYNC_NOTIFICATION
);
1765 freqs
.old
= policy
->cur
;
1766 freqs
.new = freq_table
[index
].frequency
;
1769 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1770 __func__
, policy
->cpu
, freqs
.old
,
1773 cpufreq_notify_transition(policy
, &freqs
,
1777 retval
= cpufreq_driver
->target_index(policy
, index
);
1779 pr_err("%s: Failed to change cpu frequency: %d\n",
1783 cpufreq_notify_post_transition(policy
, &freqs
, retval
);
1789 EXPORT_SYMBOL_GPL(__cpufreq_driver_target
);
1791 int cpufreq_driver_target(struct cpufreq_policy
*policy
,
1792 unsigned int target_freq
,
1793 unsigned int relation
)
1797 down_write(&policy
->rwsem
);
1799 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
1801 up_write(&policy
->rwsem
);
1805 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
1808 * when "event" is CPUFREQ_GOV_LIMITS
1811 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
1816 /* Only must be defined when default governor is known to have latency
1817 restrictions, like e.g. conservative or ondemand.
1818 That this is the case is already ensured in Kconfig
1820 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1821 struct cpufreq_governor
*gov
= &cpufreq_gov_performance
;
1823 struct cpufreq_governor
*gov
= NULL
;
1826 if (policy
->governor
->max_transition_latency
&&
1827 policy
->cpuinfo
.transition_latency
>
1828 policy
->governor
->max_transition_latency
) {
1832 printk(KERN_WARNING
"%s governor failed, too long"
1833 " transition latency of HW, fallback"
1834 " to %s governor\n",
1835 policy
->governor
->name
,
1837 policy
->governor
= gov
;
1841 if (event
== CPUFREQ_GOV_POLICY_INIT
)
1842 if (!try_module_get(policy
->governor
->owner
))
1845 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1846 policy
->cpu
, event
);
1848 mutex_lock(&cpufreq_governor_lock
);
1849 if ((policy
->governor_enabled
&& event
== CPUFREQ_GOV_START
)
1850 || (!policy
->governor_enabled
1851 && (event
== CPUFREQ_GOV_LIMITS
|| event
== CPUFREQ_GOV_STOP
))) {
1852 mutex_unlock(&cpufreq_governor_lock
);
1856 if (event
== CPUFREQ_GOV_STOP
)
1857 policy
->governor_enabled
= false;
1858 else if (event
== CPUFREQ_GOV_START
)
1859 policy
->governor_enabled
= true;
1861 mutex_unlock(&cpufreq_governor_lock
);
1863 ret
= policy
->governor
->governor(policy
, event
);
1866 if (event
== CPUFREQ_GOV_POLICY_INIT
)
1867 policy
->governor
->initialized
++;
1868 else if (event
== CPUFREQ_GOV_POLICY_EXIT
)
1869 policy
->governor
->initialized
--;
1871 /* Restore original values */
1872 mutex_lock(&cpufreq_governor_lock
);
1873 if (event
== CPUFREQ_GOV_STOP
)
1874 policy
->governor_enabled
= true;
1875 else if (event
== CPUFREQ_GOV_START
)
1876 policy
->governor_enabled
= false;
1877 mutex_unlock(&cpufreq_governor_lock
);
1880 if (((event
== CPUFREQ_GOV_POLICY_INIT
) && ret
) ||
1881 ((event
== CPUFREQ_GOV_POLICY_EXIT
) && !ret
))
1882 module_put(policy
->governor
->owner
);
1887 int cpufreq_register_governor(struct cpufreq_governor
*governor
)
1894 if (cpufreq_disabled())
1897 mutex_lock(&cpufreq_governor_mutex
);
1899 governor
->initialized
= 0;
1901 if (__find_governor(governor
->name
) == NULL
) {
1903 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
1906 mutex_unlock(&cpufreq_governor_mutex
);
1909 EXPORT_SYMBOL_GPL(cpufreq_register_governor
);
1911 void cpufreq_unregister_governor(struct cpufreq_governor
*governor
)
1913 #ifdef CONFIG_HOTPLUG_CPU
1920 if (cpufreq_disabled())
1923 #ifdef CONFIG_HOTPLUG_CPU
1924 for_each_present_cpu(cpu
) {
1925 if (cpu_online(cpu
))
1927 if (!strcmp(per_cpu(cpufreq_cpu_governor
, cpu
), governor
->name
))
1928 strcpy(per_cpu(cpufreq_cpu_governor
, cpu
), "\0");
1932 mutex_lock(&cpufreq_governor_mutex
);
1933 list_del(&governor
->governor_list
);
1934 mutex_unlock(&cpufreq_governor_mutex
);
1937 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor
);
1940 /*********************************************************************
1941 * POLICY INTERFACE *
1942 *********************************************************************/
1945 * cpufreq_get_policy - get the current cpufreq_policy
1946 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1949 * Reads the current cpufreq policy.
1951 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
1953 struct cpufreq_policy
*cpu_policy
;
1957 cpu_policy
= cpufreq_cpu_get(cpu
);
1961 memcpy(policy
, cpu_policy
, sizeof(*policy
));
1963 cpufreq_cpu_put(cpu_policy
);
1966 EXPORT_SYMBOL(cpufreq_get_policy
);
1969 * policy : current policy.
1970 * new_policy: policy to be set.
1972 static int cpufreq_set_policy(struct cpufreq_policy
*policy
,
1973 struct cpufreq_policy
*new_policy
)
1975 int ret
= 0, failed
= 1;
1977 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy
->cpu
,
1978 new_policy
->min
, new_policy
->max
);
1980 memcpy(&new_policy
->cpuinfo
, &policy
->cpuinfo
, sizeof(policy
->cpuinfo
));
1982 if (new_policy
->min
> policy
->max
|| new_policy
->max
< policy
->min
) {
1987 /* verify the cpu speed can be set within this limit */
1988 ret
= cpufreq_driver
->verify(new_policy
);
1992 /* adjust if necessary - all reasons */
1993 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1994 CPUFREQ_ADJUST
, new_policy
);
1996 /* adjust if necessary - hardware incompatibility*/
1997 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1998 CPUFREQ_INCOMPATIBLE
, new_policy
);
2001 * verify the cpu speed can be set within this limit, which might be
2002 * different to the first one
2004 ret
= cpufreq_driver
->verify(new_policy
);
2008 /* notification of the new policy */
2009 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
2010 CPUFREQ_NOTIFY
, new_policy
);
2012 policy
->min
= new_policy
->min
;
2013 policy
->max
= new_policy
->max
;
2015 pr_debug("new min and max freqs are %u - %u kHz\n",
2016 policy
->min
, policy
->max
);
2018 if (cpufreq_driver
->setpolicy
) {
2019 policy
->policy
= new_policy
->policy
;
2020 pr_debug("setting range\n");
2021 ret
= cpufreq_driver
->setpolicy(new_policy
);
2023 if (new_policy
->governor
!= policy
->governor
) {
2024 /* save old, working values */
2025 struct cpufreq_governor
*old_gov
= policy
->governor
;
2027 pr_debug("governor switch\n");
2029 /* end old governor */
2030 if (policy
->governor
) {
2031 __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
2032 up_write(&policy
->rwsem
);
2033 __cpufreq_governor(policy
,
2034 CPUFREQ_GOV_POLICY_EXIT
);
2035 down_write(&policy
->rwsem
);
2038 /* start new governor */
2039 policy
->governor
= new_policy
->governor
;
2040 if (!__cpufreq_governor(policy
, CPUFREQ_GOV_POLICY_INIT
)) {
2041 if (!__cpufreq_governor(policy
, CPUFREQ_GOV_START
)) {
2044 up_write(&policy
->rwsem
);
2045 __cpufreq_governor(policy
,
2046 CPUFREQ_GOV_POLICY_EXIT
);
2047 down_write(&policy
->rwsem
);
2052 /* new governor failed, so re-start old one */
2053 pr_debug("starting governor %s failed\n",
2054 policy
->governor
->name
);
2056 policy
->governor
= old_gov
;
2057 __cpufreq_governor(policy
,
2058 CPUFREQ_GOV_POLICY_INIT
);
2059 __cpufreq_governor(policy
,
2065 /* might be a policy change, too, so fall through */
2067 pr_debug("governor: change or update limits\n");
2068 ret
= __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
);
2076 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2077 * @cpu: CPU which shall be re-evaluated
2079 * Useful for policy notifiers which have different necessities
2080 * at different times.
2082 int cpufreq_update_policy(unsigned int cpu
)
2084 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
2085 struct cpufreq_policy new_policy
;
2093 down_write(&policy
->rwsem
);
2095 pr_debug("updating policy for CPU %u\n", cpu
);
2096 memcpy(&new_policy
, policy
, sizeof(*policy
));
2097 new_policy
.min
= policy
->user_policy
.min
;
2098 new_policy
.max
= policy
->user_policy
.max
;
2099 new_policy
.policy
= policy
->user_policy
.policy
;
2100 new_policy
.governor
= policy
->user_policy
.governor
;
2103 * BIOS might change freq behind our back
2104 * -> ask driver for current freq and notify governors about a change
2106 if (cpufreq_driver
->get
) {
2107 new_policy
.cur
= cpufreq_driver
->get(cpu
);
2109 pr_debug("Driver did not initialize current freq");
2110 policy
->cur
= new_policy
.cur
;
2112 if (policy
->cur
!= new_policy
.cur
&& has_target())
2113 cpufreq_out_of_sync(cpu
, policy
->cur
,
2118 ret
= cpufreq_set_policy(policy
, &new_policy
);
2120 up_write(&policy
->rwsem
);
2122 cpufreq_cpu_put(policy
);
2126 EXPORT_SYMBOL(cpufreq_update_policy
);
2128 static int cpufreq_cpu_callback(struct notifier_block
*nfb
,
2129 unsigned long action
, void *hcpu
)
2131 unsigned int cpu
= (unsigned long)hcpu
;
2133 bool frozen
= false;
2135 dev
= get_cpu_device(cpu
);
2138 if (action
& CPU_TASKS_FROZEN
)
2141 switch (action
& ~CPU_TASKS_FROZEN
) {
2143 __cpufreq_add_dev(dev
, NULL
, frozen
);
2144 cpufreq_update_policy(cpu
);
2147 case CPU_DOWN_PREPARE
:
2148 __cpufreq_remove_dev_prepare(dev
, NULL
, frozen
);
2152 __cpufreq_remove_dev_finish(dev
, NULL
, frozen
);
2155 case CPU_DOWN_FAILED
:
2156 __cpufreq_add_dev(dev
, NULL
, frozen
);
2163 static struct notifier_block __refdata cpufreq_cpu_notifier
= {
2164 .notifier_call
= cpufreq_cpu_callback
,
2167 /*********************************************************************
2168 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2169 *********************************************************************/
2172 * cpufreq_register_driver - register a CPU Frequency driver
2173 * @driver_data: A struct cpufreq_driver containing the values#
2174 * submitted by the CPU Frequency driver.
2176 * Registers a CPU Frequency driver to this core code. This code
2177 * returns zero on success, -EBUSY when another driver got here first
2178 * (and isn't unregistered in the meantime).
2181 int cpufreq_register_driver(struct cpufreq_driver
*driver_data
)
2183 unsigned long flags
;
2186 if (cpufreq_disabled())
2189 if (!driver_data
|| !driver_data
->verify
|| !driver_data
->init
||
2190 !(driver_data
->setpolicy
|| driver_data
->target_index
||
2191 driver_data
->target
))
2194 pr_debug("trying to register driver %s\n", driver_data
->name
);
2196 if (driver_data
->setpolicy
)
2197 driver_data
->flags
|= CPUFREQ_CONST_LOOPS
;
2199 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2200 if (cpufreq_driver
) {
2201 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2204 cpufreq_driver
= driver_data
;
2205 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2207 ret
= subsys_interface_register(&cpufreq_interface
);
2209 goto err_null_driver
;
2211 if (!(cpufreq_driver
->flags
& CPUFREQ_STICKY
)) {
2215 /* check for at least one working CPU */
2216 for (i
= 0; i
< nr_cpu_ids
; i
++)
2217 if (cpu_possible(i
) && per_cpu(cpufreq_cpu_data
, i
)) {
2222 /* if all ->init() calls failed, unregister */
2224 pr_debug("no CPU initialized for driver %s\n",
2230 register_hotcpu_notifier(&cpufreq_cpu_notifier
);
2231 pr_debug("driver %s up and running\n", driver_data
->name
);
2235 subsys_interface_unregister(&cpufreq_interface
);
2237 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2238 cpufreq_driver
= NULL
;
2239 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2242 EXPORT_SYMBOL_GPL(cpufreq_register_driver
);
2245 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2247 * Unregister the current CPUFreq driver. Only call this if you have
2248 * the right to do so, i.e. if you have succeeded in initialising before!
2249 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2250 * currently not initialised.
2252 int cpufreq_unregister_driver(struct cpufreq_driver
*driver
)
2254 unsigned long flags
;
2256 if (!cpufreq_driver
|| (driver
!= cpufreq_driver
))
2259 pr_debug("unregistering driver %s\n", driver
->name
);
2261 subsys_interface_unregister(&cpufreq_interface
);
2262 unregister_hotcpu_notifier(&cpufreq_cpu_notifier
);
2264 down_write(&cpufreq_rwsem
);
2265 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2267 cpufreq_driver
= NULL
;
2269 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2270 up_write(&cpufreq_rwsem
);
2274 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver
);
2276 static int __init
cpufreq_core_init(void)
2278 if (cpufreq_disabled())
2281 cpufreq_global_kobject
= kobject_create();
2282 BUG_ON(!cpufreq_global_kobject
);
2283 register_syscore_ops(&cpufreq_syscore_ops
);
2287 core_initcall(cpufreq_core_init
);