2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <asm/cputime.h>
21 #include <linux/kernel.h>
22 #include <linux/kernel_stat.h>
23 #include <linux/module.h>
24 #include <linux/init.h>
25 #include <linux/notifier.h>
26 #include <linux/cpufreq.h>
27 #include <linux/delay.h>
28 #include <linux/interrupt.h>
29 #include <linux/spinlock.h>
30 #include <linux/tick.h>
31 #include <linux/device.h>
32 #include <linux/slab.h>
33 #include <linux/cpu.h>
34 #include <linux/completion.h>
35 #include <linux/mutex.h>
36 #include <linux/syscore_ops.h>
38 #include <trace/events/power.h>
41 * The "cpufreq driver" - the arch- or hardware-dependent low
42 * level driver of CPUFreq support, and its spinlock. This lock
43 * also protects the cpufreq_cpu_data array.
45 static struct cpufreq_driver
*cpufreq_driver
;
46 static DEFINE_PER_CPU(struct cpufreq_policy
*, cpufreq_cpu_data
);
47 #ifdef CONFIG_HOTPLUG_CPU
48 /* This one keeps track of the previously set governor of a removed CPU */
49 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN
], cpufreq_cpu_governor
);
51 static DEFINE_RWLOCK(cpufreq_driver_lock
);
52 static DEFINE_MUTEX(cpufreq_governor_lock
);
55 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
56 * all cpufreq/hotplug/workqueue/etc related lock issues.
58 * The rules for this semaphore:
59 * - Any routine that wants to read from the policy structure will
60 * do a down_read on this semaphore.
61 * - Any routine that will write to the policy structure and/or may take away
62 * the policy altogether (eg. CPU hotplug), will hold this lock in write
63 * mode before doing so.
66 * - Governor routines that can be called in cpufreq hotplug path should not
67 * take this sem as top level hotplug notifier handler takes this.
68 * - Lock should not be held across
69 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
71 static DEFINE_PER_CPU(int, cpufreq_policy_cpu
);
72 static DEFINE_PER_CPU(struct rw_semaphore
, cpu_policy_rwsem
);
74 #define lock_policy_rwsem(mode, cpu) \
75 static int lock_policy_rwsem_##mode(int cpu) \
77 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
78 BUG_ON(policy_cpu == -1); \
79 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
84 lock_policy_rwsem(read
, cpu
);
85 lock_policy_rwsem(write
, cpu
);
87 #define unlock_policy_rwsem(mode, cpu) \
88 static void unlock_policy_rwsem_##mode(int cpu) \
90 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
91 BUG_ON(policy_cpu == -1); \
92 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
95 unlock_policy_rwsem(read
, cpu
);
96 unlock_policy_rwsem(write
, cpu
);
98 /* internal prototypes */
99 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
101 static unsigned int __cpufreq_get(unsigned int cpu
);
102 static void handle_update(struct work_struct
*work
);
105 * Two notifier lists: the "policy" list is involved in the
106 * validation process for a new CPU frequency policy; the
107 * "transition" list for kernel code that needs to handle
108 * changes to devices when the CPU clock speed changes.
109 * The mutex locks both lists.
111 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list
);
112 static struct srcu_notifier_head cpufreq_transition_notifier_list
;
114 static bool init_cpufreq_transition_notifier_list_called
;
115 static int __init
init_cpufreq_transition_notifier_list(void)
117 srcu_init_notifier_head(&cpufreq_transition_notifier_list
);
118 init_cpufreq_transition_notifier_list_called
= true;
121 pure_initcall(init_cpufreq_transition_notifier_list
);
123 static int off __read_mostly
;
124 static int cpufreq_disabled(void)
128 void disable_cpufreq(void)
132 static LIST_HEAD(cpufreq_governor_list
);
133 static DEFINE_MUTEX(cpufreq_governor_mutex
);
135 bool have_governor_per_policy(void)
137 return cpufreq_driver
->have_governor_per_policy
;
139 EXPORT_SYMBOL_GPL(have_governor_per_policy
);
141 struct kobject
*get_governor_parent_kobj(struct cpufreq_policy
*policy
)
143 if (have_governor_per_policy())
144 return &policy
->kobj
;
146 return cpufreq_global_kobject
;
148 EXPORT_SYMBOL_GPL(get_governor_parent_kobj
);
150 static inline u64
get_cpu_idle_time_jiffy(unsigned int cpu
, u64
*wall
)
156 cur_wall_time
= jiffies64_to_cputime64(get_jiffies_64());
158 busy_time
= kcpustat_cpu(cpu
).cpustat
[CPUTIME_USER
];
159 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_SYSTEM
];
160 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_IRQ
];
161 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_SOFTIRQ
];
162 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_STEAL
];
163 busy_time
+= kcpustat_cpu(cpu
).cpustat
[CPUTIME_NICE
];
165 idle_time
= cur_wall_time
- busy_time
;
167 *wall
= cputime_to_usecs(cur_wall_time
);
169 return cputime_to_usecs(idle_time
);
172 u64
get_cpu_idle_time(unsigned int cpu
, u64
*wall
, int io_busy
)
174 u64 idle_time
= get_cpu_idle_time_us(cpu
, io_busy
? wall
: NULL
);
176 if (idle_time
== -1ULL)
177 return get_cpu_idle_time_jiffy(cpu
, wall
);
179 idle_time
+= get_cpu_iowait_time_us(cpu
, wall
);
183 EXPORT_SYMBOL_GPL(get_cpu_idle_time
);
185 static struct cpufreq_policy
*__cpufreq_cpu_get(unsigned int cpu
, bool sysfs
)
187 struct cpufreq_policy
*data
;
190 if (cpu
>= nr_cpu_ids
)
193 /* get the cpufreq driver */
194 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
199 if (!try_module_get(cpufreq_driver
->owner
))
204 data
= per_cpu(cpufreq_cpu_data
, cpu
);
207 goto err_out_put_module
;
209 if (!sysfs
&& !kobject_get(&data
->kobj
))
210 goto err_out_put_module
;
212 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
216 module_put(cpufreq_driver
->owner
);
218 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
223 struct cpufreq_policy
*cpufreq_cpu_get(unsigned int cpu
)
225 if (cpufreq_disabled())
228 return __cpufreq_cpu_get(cpu
, false);
230 EXPORT_SYMBOL_GPL(cpufreq_cpu_get
);
232 static struct cpufreq_policy
*cpufreq_cpu_get_sysfs(unsigned int cpu
)
234 return __cpufreq_cpu_get(cpu
, true);
237 static void __cpufreq_cpu_put(struct cpufreq_policy
*data
, bool sysfs
)
240 kobject_put(&data
->kobj
);
241 module_put(cpufreq_driver
->owner
);
244 void cpufreq_cpu_put(struct cpufreq_policy
*data
)
246 if (cpufreq_disabled())
249 __cpufreq_cpu_put(data
, false);
251 EXPORT_SYMBOL_GPL(cpufreq_cpu_put
);
253 static void cpufreq_cpu_put_sysfs(struct cpufreq_policy
*data
)
255 __cpufreq_cpu_put(data
, true);
258 /*********************************************************************
259 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
260 *********************************************************************/
263 * adjust_jiffies - adjust the system "loops_per_jiffy"
265 * This function alters the system "loops_per_jiffy" for the clock
266 * speed change. Note that loops_per_jiffy cannot be updated on SMP
267 * systems as each CPU might be scaled differently. So, use the arch
268 * per-CPU loops_per_jiffy value wherever possible.
271 static unsigned long l_p_j_ref
;
272 static unsigned int l_p_j_ref_freq
;
274 static void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
276 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
279 if (!l_p_j_ref_freq
) {
280 l_p_j_ref
= loops_per_jiffy
;
281 l_p_j_ref_freq
= ci
->old
;
282 pr_debug("saving %lu as reference value for loops_per_jiffy; "
283 "freq is %u kHz\n", l_p_j_ref
, l_p_j_ref_freq
);
285 if ((val
== CPUFREQ_POSTCHANGE
&& ci
->old
!= ci
->new) ||
286 (val
== CPUFREQ_RESUMECHANGE
|| val
== CPUFREQ_SUSPENDCHANGE
)) {
287 loops_per_jiffy
= cpufreq_scale(l_p_j_ref
, l_p_j_ref_freq
,
289 pr_debug("scaling loops_per_jiffy to %lu "
290 "for frequency %u kHz\n", loops_per_jiffy
, ci
->new);
294 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
301 void __cpufreq_notify_transition(struct cpufreq_policy
*policy
,
302 struct cpufreq_freqs
*freqs
, unsigned int state
)
304 BUG_ON(irqs_disabled());
306 if (cpufreq_disabled())
309 freqs
->flags
= cpufreq_driver
->flags
;
310 pr_debug("notification %u of frequency transition to %u kHz\n",
315 case CPUFREQ_PRECHANGE
:
316 /* detect if the driver reported a value as "old frequency"
317 * which is not equal to what the cpufreq core thinks is
320 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
321 if ((policy
) && (policy
->cpu
== freqs
->cpu
) &&
322 (policy
->cur
) && (policy
->cur
!= freqs
->old
)) {
323 pr_debug("Warning: CPU frequency is"
324 " %u, cpufreq assumed %u kHz.\n",
325 freqs
->old
, policy
->cur
);
326 freqs
->old
= policy
->cur
;
329 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
330 CPUFREQ_PRECHANGE
, freqs
);
331 adjust_jiffies(CPUFREQ_PRECHANGE
, freqs
);
334 case CPUFREQ_POSTCHANGE
:
335 adjust_jiffies(CPUFREQ_POSTCHANGE
, freqs
);
336 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs
->new,
337 (unsigned long)freqs
->cpu
);
338 trace_cpu_frequency(freqs
->new, freqs
->cpu
);
339 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
340 CPUFREQ_POSTCHANGE
, freqs
);
341 if (likely(policy
) && likely(policy
->cpu
== freqs
->cpu
))
342 policy
->cur
= freqs
->new;
347 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
348 * on frequency transition.
350 * This function calls the transition notifiers and the "adjust_jiffies"
351 * function. It is called twice on all CPU frequency changes that have
354 void cpufreq_notify_transition(struct cpufreq_policy
*policy
,
355 struct cpufreq_freqs
*freqs
, unsigned int state
)
357 for_each_cpu(freqs
->cpu
, policy
->cpus
)
358 __cpufreq_notify_transition(policy
, freqs
, state
);
360 EXPORT_SYMBOL_GPL(cpufreq_notify_transition
);
364 /*********************************************************************
366 *********************************************************************/
368 static struct cpufreq_governor
*__find_governor(const char *str_governor
)
370 struct cpufreq_governor
*t
;
372 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
)
373 if (!strnicmp(str_governor
, t
->name
, CPUFREQ_NAME_LEN
))
380 * cpufreq_parse_governor - parse a governor string
382 static int cpufreq_parse_governor(char *str_governor
, unsigned int *policy
,
383 struct cpufreq_governor
**governor
)
390 if (cpufreq_driver
->setpolicy
) {
391 if (!strnicmp(str_governor
, "performance", CPUFREQ_NAME_LEN
)) {
392 *policy
= CPUFREQ_POLICY_PERFORMANCE
;
394 } else if (!strnicmp(str_governor
, "powersave",
396 *policy
= CPUFREQ_POLICY_POWERSAVE
;
399 } else if (cpufreq_driver
->target
) {
400 struct cpufreq_governor
*t
;
402 mutex_lock(&cpufreq_governor_mutex
);
404 t
= __find_governor(str_governor
);
409 mutex_unlock(&cpufreq_governor_mutex
);
410 ret
= request_module("cpufreq_%s", str_governor
);
411 mutex_lock(&cpufreq_governor_mutex
);
414 t
= __find_governor(str_governor
);
422 mutex_unlock(&cpufreq_governor_mutex
);
430 * cpufreq_per_cpu_attr_read() / show_##file_name() -
431 * print out cpufreq information
433 * Write out information from cpufreq_driver->policy[cpu]; object must be
437 #define show_one(file_name, object) \
438 static ssize_t show_##file_name \
439 (struct cpufreq_policy *policy, char *buf) \
441 return sprintf(buf, "%u\n", policy->object); \
444 show_one(cpuinfo_min_freq
, cpuinfo
.min_freq
);
445 show_one(cpuinfo_max_freq
, cpuinfo
.max_freq
);
446 show_one(cpuinfo_transition_latency
, cpuinfo
.transition_latency
);
447 show_one(scaling_min_freq
, min
);
448 show_one(scaling_max_freq
, max
);
449 show_one(scaling_cur_freq
, cur
);
451 static int __cpufreq_set_policy(struct cpufreq_policy
*data
,
452 struct cpufreq_policy
*policy
);
455 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
457 #define store_one(file_name, object) \
458 static ssize_t store_##file_name \
459 (struct cpufreq_policy *policy, const char *buf, size_t count) \
462 struct cpufreq_policy new_policy; \
464 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
468 ret = sscanf(buf, "%u", &new_policy.object); \
472 ret = __cpufreq_set_policy(policy, &new_policy); \
473 policy->user_policy.object = policy->object; \
475 return ret ? ret : count; \
478 store_one(scaling_min_freq
, min
);
479 store_one(scaling_max_freq
, max
);
482 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
484 static ssize_t
show_cpuinfo_cur_freq(struct cpufreq_policy
*policy
,
487 unsigned int cur_freq
= __cpufreq_get(policy
->cpu
);
490 return sprintf(buf
, "%u\n", cur_freq
);
492 return sprintf(buf
, "<unknown>\n");
497 * show_scaling_governor - show the current policy for the specified CPU
499 static ssize_t
show_scaling_governor(struct cpufreq_policy
*policy
, char *buf
)
501 if (policy
->policy
== CPUFREQ_POLICY_POWERSAVE
)
502 return sprintf(buf
, "powersave\n");
503 else if (policy
->policy
== CPUFREQ_POLICY_PERFORMANCE
)
504 return sprintf(buf
, "performance\n");
505 else if (policy
->governor
)
506 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n",
507 policy
->governor
->name
);
513 * store_scaling_governor - store policy for the specified CPU
515 static ssize_t
store_scaling_governor(struct cpufreq_policy
*policy
,
516 const char *buf
, size_t count
)
519 char str_governor
[16];
520 struct cpufreq_policy new_policy
;
522 ret
= cpufreq_get_policy(&new_policy
, policy
->cpu
);
526 ret
= sscanf(buf
, "%15s", str_governor
);
530 if (cpufreq_parse_governor(str_governor
, &new_policy
.policy
,
531 &new_policy
.governor
))
534 /* Do not use cpufreq_set_policy here or the user_policy.max
535 will be wrongly overridden */
536 ret
= __cpufreq_set_policy(policy
, &new_policy
);
538 policy
->user_policy
.policy
= policy
->policy
;
539 policy
->user_policy
.governor
= policy
->governor
;
548 * show_scaling_driver - show the cpufreq driver currently loaded
550 static ssize_t
show_scaling_driver(struct cpufreq_policy
*policy
, char *buf
)
552 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n", cpufreq_driver
->name
);
556 * show_scaling_available_governors - show the available CPUfreq governors
558 static ssize_t
show_scaling_available_governors(struct cpufreq_policy
*policy
,
562 struct cpufreq_governor
*t
;
564 if (!cpufreq_driver
->target
) {
565 i
+= sprintf(buf
, "performance powersave");
569 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
570 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char))
571 - (CPUFREQ_NAME_LEN
+ 2)))
573 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_PLEN
, "%s ", t
->name
);
576 i
+= sprintf(&buf
[i
], "\n");
580 static ssize_t
show_cpus(const struct cpumask
*mask
, char *buf
)
585 for_each_cpu(cpu
, mask
) {
587 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), " ");
588 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), "%u", cpu
);
589 if (i
>= (PAGE_SIZE
- 5))
592 i
+= sprintf(&buf
[i
], "\n");
597 * show_related_cpus - show the CPUs affected by each transition even if
598 * hw coordination is in use
600 static ssize_t
show_related_cpus(struct cpufreq_policy
*policy
, char *buf
)
602 return show_cpus(policy
->related_cpus
, buf
);
606 * show_affected_cpus - show the CPUs affected by each transition
608 static ssize_t
show_affected_cpus(struct cpufreq_policy
*policy
, char *buf
)
610 return show_cpus(policy
->cpus
, buf
);
613 static ssize_t
store_scaling_setspeed(struct cpufreq_policy
*policy
,
614 const char *buf
, size_t count
)
616 unsigned int freq
= 0;
619 if (!policy
->governor
|| !policy
->governor
->store_setspeed
)
622 ret
= sscanf(buf
, "%u", &freq
);
626 policy
->governor
->store_setspeed(policy
, freq
);
631 static ssize_t
show_scaling_setspeed(struct cpufreq_policy
*policy
, char *buf
)
633 if (!policy
->governor
|| !policy
->governor
->show_setspeed
)
634 return sprintf(buf
, "<unsupported>\n");
636 return policy
->governor
->show_setspeed(policy
, buf
);
640 * show_bios_limit - show the current cpufreq HW/BIOS limitation
642 static ssize_t
show_bios_limit(struct cpufreq_policy
*policy
, char *buf
)
646 if (cpufreq_driver
->bios_limit
) {
647 ret
= cpufreq_driver
->bios_limit(policy
->cpu
, &limit
);
649 return sprintf(buf
, "%u\n", limit
);
651 return sprintf(buf
, "%u\n", policy
->cpuinfo
.max_freq
);
654 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq
, 0400);
655 cpufreq_freq_attr_ro(cpuinfo_min_freq
);
656 cpufreq_freq_attr_ro(cpuinfo_max_freq
);
657 cpufreq_freq_attr_ro(cpuinfo_transition_latency
);
658 cpufreq_freq_attr_ro(scaling_available_governors
);
659 cpufreq_freq_attr_ro(scaling_driver
);
660 cpufreq_freq_attr_ro(scaling_cur_freq
);
661 cpufreq_freq_attr_ro(bios_limit
);
662 cpufreq_freq_attr_ro(related_cpus
);
663 cpufreq_freq_attr_ro(affected_cpus
);
664 cpufreq_freq_attr_rw(scaling_min_freq
);
665 cpufreq_freq_attr_rw(scaling_max_freq
);
666 cpufreq_freq_attr_rw(scaling_governor
);
667 cpufreq_freq_attr_rw(scaling_setspeed
);
669 static struct attribute
*default_attrs
[] = {
670 &cpuinfo_min_freq
.attr
,
671 &cpuinfo_max_freq
.attr
,
672 &cpuinfo_transition_latency
.attr
,
673 &scaling_min_freq
.attr
,
674 &scaling_max_freq
.attr
,
677 &scaling_governor
.attr
,
678 &scaling_driver
.attr
,
679 &scaling_available_governors
.attr
,
680 &scaling_setspeed
.attr
,
684 struct kobject
*cpufreq_global_kobject
;
685 EXPORT_SYMBOL(cpufreq_global_kobject
);
687 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
688 #define to_attr(a) container_of(a, struct freq_attr, attr)
690 static ssize_t
show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
692 struct cpufreq_policy
*policy
= to_policy(kobj
);
693 struct freq_attr
*fattr
= to_attr(attr
);
694 ssize_t ret
= -EINVAL
;
695 policy
= cpufreq_cpu_get_sysfs(policy
->cpu
);
699 if (lock_policy_rwsem_read(policy
->cpu
) < 0)
703 ret
= fattr
->show(policy
, buf
);
707 unlock_policy_rwsem_read(policy
->cpu
);
709 cpufreq_cpu_put_sysfs(policy
);
714 static ssize_t
store(struct kobject
*kobj
, struct attribute
*attr
,
715 const char *buf
, size_t count
)
717 struct cpufreq_policy
*policy
= to_policy(kobj
);
718 struct freq_attr
*fattr
= to_attr(attr
);
719 ssize_t ret
= -EINVAL
;
720 policy
= cpufreq_cpu_get_sysfs(policy
->cpu
);
724 if (lock_policy_rwsem_write(policy
->cpu
) < 0)
728 ret
= fattr
->store(policy
, buf
, count
);
732 unlock_policy_rwsem_write(policy
->cpu
);
734 cpufreq_cpu_put_sysfs(policy
);
739 static void cpufreq_sysfs_release(struct kobject
*kobj
)
741 struct cpufreq_policy
*policy
= to_policy(kobj
);
742 pr_debug("last reference is dropped\n");
743 complete(&policy
->kobj_unregister
);
746 static const struct sysfs_ops sysfs_ops
= {
751 static struct kobj_type ktype_cpufreq
= {
752 .sysfs_ops
= &sysfs_ops
,
753 .default_attrs
= default_attrs
,
754 .release
= cpufreq_sysfs_release
,
757 /* symlink affected CPUs */
758 static int cpufreq_add_dev_symlink(unsigned int cpu
,
759 struct cpufreq_policy
*policy
)
764 for_each_cpu(j
, policy
->cpus
) {
765 struct cpufreq_policy
*managed_policy
;
766 struct device
*cpu_dev
;
771 pr_debug("CPU %u already managed, adding link\n", j
);
772 managed_policy
= cpufreq_cpu_get(cpu
);
773 cpu_dev
= get_cpu_device(j
);
774 ret
= sysfs_create_link(&cpu_dev
->kobj
, &policy
->kobj
,
777 cpufreq_cpu_put(managed_policy
);
784 static int cpufreq_add_dev_interface(unsigned int cpu
,
785 struct cpufreq_policy
*policy
,
788 struct cpufreq_policy new_policy
;
789 struct freq_attr
**drv_attr
;
794 /* prepare interface data */
795 ret
= kobject_init_and_add(&policy
->kobj
, &ktype_cpufreq
,
796 &dev
->kobj
, "cpufreq");
800 /* set up files for this cpu device */
801 drv_attr
= cpufreq_driver
->attr
;
802 while ((drv_attr
) && (*drv_attr
)) {
803 ret
= sysfs_create_file(&policy
->kobj
, &((*drv_attr
)->attr
));
805 goto err_out_kobj_put
;
808 if (cpufreq_driver
->get
) {
809 ret
= sysfs_create_file(&policy
->kobj
, &cpuinfo_cur_freq
.attr
);
811 goto err_out_kobj_put
;
813 if (cpufreq_driver
->target
) {
814 ret
= sysfs_create_file(&policy
->kobj
, &scaling_cur_freq
.attr
);
816 goto err_out_kobj_put
;
818 if (cpufreq_driver
->bios_limit
) {
819 ret
= sysfs_create_file(&policy
->kobj
, &bios_limit
.attr
);
821 goto err_out_kobj_put
;
824 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
825 for_each_cpu(j
, policy
->cpus
) {
826 per_cpu(cpufreq_cpu_data
, j
) = policy
;
827 per_cpu(cpufreq_policy_cpu
, j
) = policy
->cpu
;
829 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
831 ret
= cpufreq_add_dev_symlink(cpu
, policy
);
833 goto err_out_kobj_put
;
835 memcpy(&new_policy
, policy
, sizeof(struct cpufreq_policy
));
836 /* assure that the starting sequence is run in __cpufreq_set_policy */
837 policy
->governor
= NULL
;
839 /* set default policy */
840 ret
= __cpufreq_set_policy(policy
, &new_policy
);
841 policy
->user_policy
.policy
= policy
->policy
;
842 policy
->user_policy
.governor
= policy
->governor
;
845 pr_debug("setting policy failed\n");
846 if (cpufreq_driver
->exit
)
847 cpufreq_driver
->exit(policy
);
852 kobject_put(&policy
->kobj
);
853 wait_for_completion(&policy
->kobj_unregister
);
857 #ifdef CONFIG_HOTPLUG_CPU
858 static int cpufreq_add_policy_cpu(unsigned int cpu
, unsigned int sibling
,
861 struct cpufreq_policy
*policy
;
862 int ret
= 0, has_target
= !!cpufreq_driver
->target
;
865 policy
= cpufreq_cpu_get(sibling
);
869 __cpufreq_governor(policy
, CPUFREQ_GOV_STOP
);
871 lock_policy_rwsem_write(sibling
);
873 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
875 cpumask_set_cpu(cpu
, policy
->cpus
);
876 per_cpu(cpufreq_policy_cpu
, cpu
) = policy
->cpu
;
877 per_cpu(cpufreq_cpu_data
, cpu
) = policy
;
878 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
880 unlock_policy_rwsem_write(sibling
);
883 __cpufreq_governor(policy
, CPUFREQ_GOV_START
);
884 __cpufreq_governor(policy
, CPUFREQ_GOV_LIMITS
);
887 ret
= sysfs_create_link(&dev
->kobj
, &policy
->kobj
, "cpufreq");
889 cpufreq_cpu_put(policy
);
898 * cpufreq_add_dev - add a CPU device
900 * Adds the cpufreq interface for a CPU device.
902 * The Oracle says: try running cpufreq registration/unregistration concurrently
903 * with with cpu hotplugging and all hell will break loose. Tried to clean this
904 * mess up, but more thorough testing is needed. - Mathieu
906 static int cpufreq_add_dev(struct device
*dev
, struct subsys_interface
*sif
)
908 unsigned int j
, cpu
= dev
->id
;
910 struct cpufreq_policy
*policy
;
912 #ifdef CONFIG_HOTPLUG_CPU
913 struct cpufreq_governor
*gov
;
917 if (cpu_is_offline(cpu
))
920 pr_debug("adding CPU %u\n", cpu
);
923 /* check whether a different CPU already registered this
924 * CPU because it is in the same boat. */
925 policy
= cpufreq_cpu_get(cpu
);
926 if (unlikely(policy
)) {
927 cpufreq_cpu_put(policy
);
931 #ifdef CONFIG_HOTPLUG_CPU
932 /* Check if this cpu was hot-unplugged earlier and has siblings */
933 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
934 for_each_online_cpu(sibling
) {
935 struct cpufreq_policy
*cp
= per_cpu(cpufreq_cpu_data
, sibling
);
936 if (cp
&& cpumask_test_cpu(cpu
, cp
->related_cpus
)) {
937 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
938 return cpufreq_add_policy_cpu(cpu
, sibling
, dev
);
941 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
945 if (!try_module_get(cpufreq_driver
->owner
)) {
950 policy
= kzalloc(sizeof(struct cpufreq_policy
), GFP_KERNEL
);
954 if (!alloc_cpumask_var(&policy
->cpus
, GFP_KERNEL
))
955 goto err_free_policy
;
957 if (!zalloc_cpumask_var(&policy
->related_cpus
, GFP_KERNEL
))
958 goto err_free_cpumask
;
961 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
962 cpumask_copy(policy
->cpus
, cpumask_of(cpu
));
964 /* Initially set CPU itself as the policy_cpu */
965 per_cpu(cpufreq_policy_cpu
, cpu
) = cpu
;
967 init_completion(&policy
->kobj_unregister
);
968 INIT_WORK(&policy
->update
, handle_update
);
970 /* call driver. From then on the cpufreq must be able
971 * to accept all calls to ->verify and ->setpolicy for this CPU
973 ret
= cpufreq_driver
->init(policy
);
975 pr_debug("initialization failed\n");
976 goto err_set_policy_cpu
;
979 /* related cpus should atleast have policy->cpus */
980 cpumask_or(policy
->related_cpus
, policy
->related_cpus
, policy
->cpus
);
983 * affected cpus must always be the one, which are online. We aren't
984 * managing offline cpus here.
986 cpumask_and(policy
->cpus
, policy
->cpus
, cpu_online_mask
);
988 policy
->user_policy
.min
= policy
->min
;
989 policy
->user_policy
.max
= policy
->max
;
991 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
992 CPUFREQ_START
, policy
);
994 #ifdef CONFIG_HOTPLUG_CPU
995 gov
= __find_governor(per_cpu(cpufreq_cpu_governor
, cpu
));
997 policy
->governor
= gov
;
998 pr_debug("Restoring governor %s for cpu %d\n",
999 policy
->governor
->name
, cpu
);
1003 ret
= cpufreq_add_dev_interface(cpu
, policy
, dev
);
1005 goto err_out_unregister
;
1007 kobject_uevent(&policy
->kobj
, KOBJ_ADD
);
1008 module_put(cpufreq_driver
->owner
);
1009 pr_debug("initialization complete\n");
1014 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1015 for_each_cpu(j
, policy
->cpus
)
1016 per_cpu(cpufreq_cpu_data
, j
) = NULL
;
1017 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1019 kobject_put(&policy
->kobj
);
1020 wait_for_completion(&policy
->kobj_unregister
);
1023 per_cpu(cpufreq_policy_cpu
, cpu
) = -1;
1024 free_cpumask_var(policy
->related_cpus
);
1026 free_cpumask_var(policy
->cpus
);
1030 module_put(cpufreq_driver
->owner
);
1035 static void update_policy_cpu(struct cpufreq_policy
*policy
, unsigned int cpu
)
1039 policy
->last_cpu
= policy
->cpu
;
1042 for_each_cpu(j
, policy
->cpus
)
1043 per_cpu(cpufreq_policy_cpu
, j
) = cpu
;
1045 #ifdef CONFIG_CPU_FREQ_TABLE
1046 cpufreq_frequency_table_update_policy_cpu(policy
);
1048 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1049 CPUFREQ_UPDATE_POLICY_CPU
, policy
);
1053 * __cpufreq_remove_dev - remove a CPU device
1055 * Removes the cpufreq interface for a CPU device.
1056 * Caller should already have policy_rwsem in write mode for this CPU.
1057 * This routine frees the rwsem before returning.
1059 static int __cpufreq_remove_dev(struct device
*dev
, struct subsys_interface
*sif
)
1061 unsigned int cpu
= dev
->id
, ret
, cpus
;
1062 unsigned long flags
;
1063 struct cpufreq_policy
*data
;
1064 struct kobject
*kobj
;
1065 struct completion
*cmp
;
1066 struct device
*cpu_dev
;
1068 pr_debug("%s: unregistering CPU %u\n", __func__
, cpu
);
1070 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1072 data
= per_cpu(cpufreq_cpu_data
, cpu
);
1073 per_cpu(cpufreq_cpu_data
, cpu
) = NULL
;
1075 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1078 pr_debug("%s: No cpu_data found\n", __func__
);
1082 if (cpufreq_driver
->target
)
1083 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
1085 #ifdef CONFIG_HOTPLUG_CPU
1086 if (!cpufreq_driver
->setpolicy
)
1087 strncpy(per_cpu(cpufreq_cpu_governor
, cpu
),
1088 data
->governor
->name
, CPUFREQ_NAME_LEN
);
1091 WARN_ON(lock_policy_rwsem_write(cpu
));
1092 cpus
= cpumask_weight(data
->cpus
);
1095 cpumask_clear_cpu(cpu
, data
->cpus
);
1096 unlock_policy_rwsem_write(cpu
);
1098 if (cpu
!= data
->cpu
) {
1099 sysfs_remove_link(&dev
->kobj
, "cpufreq");
1100 } else if (cpus
> 1) {
1101 /* first sibling now owns the new sysfs dir */
1102 cpu_dev
= get_cpu_device(cpumask_first(data
->cpus
));
1103 sysfs_remove_link(&cpu_dev
->kobj
, "cpufreq");
1104 ret
= kobject_move(&data
->kobj
, &cpu_dev
->kobj
);
1106 pr_err("%s: Failed to move kobj: %d", __func__
, ret
);
1108 WARN_ON(lock_policy_rwsem_write(cpu
));
1109 cpumask_set_cpu(cpu
, data
->cpus
);
1111 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1112 per_cpu(cpufreq_cpu_data
, cpu
) = data
;
1113 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1115 unlock_policy_rwsem_write(cpu
);
1117 ret
= sysfs_create_link(&cpu_dev
->kobj
, &data
->kobj
,
1122 WARN_ON(lock_policy_rwsem_write(cpu
));
1123 update_policy_cpu(data
, cpu_dev
->id
);
1124 unlock_policy_rwsem_write(cpu
);
1125 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1126 __func__
, cpu_dev
->id
, cpu
);
1129 /* If cpu is last user of policy, free policy */
1131 if (cpufreq_driver
->target
)
1132 __cpufreq_governor(data
, CPUFREQ_GOV_POLICY_EXIT
);
1134 lock_policy_rwsem_read(cpu
);
1136 cmp
= &data
->kobj_unregister
;
1137 unlock_policy_rwsem_read(cpu
);
1140 /* we need to make sure that the underlying kobj is actually
1141 * not referenced anymore by anybody before we proceed with
1144 pr_debug("waiting for dropping of refcount\n");
1145 wait_for_completion(cmp
);
1146 pr_debug("wait complete\n");
1148 if (cpufreq_driver
->exit
)
1149 cpufreq_driver
->exit(data
);
1151 free_cpumask_var(data
->related_cpus
);
1152 free_cpumask_var(data
->cpus
);
1155 pr_debug("%s: removing link, cpu: %d\n", __func__
, cpu
);
1156 cpufreq_cpu_put(data
);
1157 if (cpufreq_driver
->target
) {
1158 __cpufreq_governor(data
, CPUFREQ_GOV_START
);
1159 __cpufreq_governor(data
, CPUFREQ_GOV_LIMITS
);
1163 per_cpu(cpufreq_policy_cpu
, cpu
) = -1;
1168 static int cpufreq_remove_dev(struct device
*dev
, struct subsys_interface
*sif
)
1170 unsigned int cpu
= dev
->id
;
1173 if (cpu_is_offline(cpu
))
1176 retval
= __cpufreq_remove_dev(dev
, sif
);
1181 static void handle_update(struct work_struct
*work
)
1183 struct cpufreq_policy
*policy
=
1184 container_of(work
, struct cpufreq_policy
, update
);
1185 unsigned int cpu
= policy
->cpu
;
1186 pr_debug("handle_update for cpu %u called\n", cpu
);
1187 cpufreq_update_policy(cpu
);
1191 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1193 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1194 * @new_freq: CPU frequency the CPU actually runs at
1196 * We adjust to current frequency first, and need to clean up later.
1197 * So either call to cpufreq_update_policy() or schedule handle_update()).
1199 static void cpufreq_out_of_sync(unsigned int cpu
, unsigned int old_freq
,
1200 unsigned int new_freq
)
1202 struct cpufreq_policy
*policy
;
1203 struct cpufreq_freqs freqs
;
1204 unsigned long flags
;
1207 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1208 "core thinks of %u, is %u kHz.\n", old_freq
, new_freq
);
1210 freqs
.old
= old_freq
;
1211 freqs
.new = new_freq
;
1213 read_lock_irqsave(&cpufreq_driver_lock
, flags
);
1214 policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1215 read_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1217 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_PRECHANGE
);
1218 cpufreq_notify_transition(policy
, &freqs
, CPUFREQ_POSTCHANGE
);
1223 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1226 * This is the last known freq, without actually getting it from the driver.
1227 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1229 unsigned int cpufreq_quick_get(unsigned int cpu
)
1231 struct cpufreq_policy
*policy
;
1232 unsigned int ret_freq
= 0;
1234 if (cpufreq_driver
&& cpufreq_driver
->setpolicy
&& cpufreq_driver
->get
)
1235 return cpufreq_driver
->get(cpu
);
1237 policy
= cpufreq_cpu_get(cpu
);
1239 ret_freq
= policy
->cur
;
1240 cpufreq_cpu_put(policy
);
1245 EXPORT_SYMBOL(cpufreq_quick_get
);
1248 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1251 * Just return the max possible frequency for a given CPU.
1253 unsigned int cpufreq_quick_get_max(unsigned int cpu
)
1255 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1256 unsigned int ret_freq
= 0;
1259 ret_freq
= policy
->max
;
1260 cpufreq_cpu_put(policy
);
1265 EXPORT_SYMBOL(cpufreq_quick_get_max
);
1268 static unsigned int __cpufreq_get(unsigned int cpu
)
1270 struct cpufreq_policy
*policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1271 unsigned int ret_freq
= 0;
1273 if (!cpufreq_driver
->get
)
1276 ret_freq
= cpufreq_driver
->get(cpu
);
1278 if (ret_freq
&& policy
->cur
&&
1279 !(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
1280 /* verify no discrepancy between actual and
1281 saved value exists */
1282 if (unlikely(ret_freq
!= policy
->cur
)) {
1283 cpufreq_out_of_sync(cpu
, policy
->cur
, ret_freq
);
1284 schedule_work(&policy
->update
);
1292 * cpufreq_get - get the current CPU frequency (in kHz)
1295 * Get the CPU current (static) CPU frequency
1297 unsigned int cpufreq_get(unsigned int cpu
)
1299 unsigned int ret_freq
= 0;
1300 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1305 if (unlikely(lock_policy_rwsem_read(cpu
)))
1308 ret_freq
= __cpufreq_get(cpu
);
1310 unlock_policy_rwsem_read(cpu
);
1313 cpufreq_cpu_put(policy
);
1317 EXPORT_SYMBOL(cpufreq_get
);
1319 static struct subsys_interface cpufreq_interface
= {
1321 .subsys
= &cpu_subsys
,
1322 .add_dev
= cpufreq_add_dev
,
1323 .remove_dev
= cpufreq_remove_dev
,
1328 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1330 * This function is only executed for the boot processor. The other CPUs
1331 * have been put offline by means of CPU hotplug.
1333 static int cpufreq_bp_suspend(void)
1337 int cpu
= smp_processor_id();
1338 struct cpufreq_policy
*cpu_policy
;
1340 pr_debug("suspending cpu %u\n", cpu
);
1342 /* If there's no policy for the boot CPU, we have nothing to do. */
1343 cpu_policy
= cpufreq_cpu_get(cpu
);
1347 if (cpufreq_driver
->suspend
) {
1348 ret
= cpufreq_driver
->suspend(cpu_policy
);
1350 printk(KERN_ERR
"cpufreq: suspend failed in ->suspend "
1351 "step on CPU %u\n", cpu_policy
->cpu
);
1354 cpufreq_cpu_put(cpu_policy
);
1359 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1361 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1362 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1363 * restored. It will verify that the current freq is in sync with
1364 * what we believe it to be. This is a bit later than when it
1365 * should be, but nonethteless it's better than calling
1366 * cpufreq_driver->get() here which might re-enable interrupts...
1368 * This function is only executed for the boot CPU. The other CPUs have not
1369 * been turned on yet.
1371 static void cpufreq_bp_resume(void)
1375 int cpu
= smp_processor_id();
1376 struct cpufreq_policy
*cpu_policy
;
1378 pr_debug("resuming cpu %u\n", cpu
);
1380 /* If there's no policy for the boot CPU, we have nothing to do. */
1381 cpu_policy
= cpufreq_cpu_get(cpu
);
1385 if (cpufreq_driver
->resume
) {
1386 ret
= cpufreq_driver
->resume(cpu_policy
);
1388 printk(KERN_ERR
"cpufreq: resume failed in ->resume "
1389 "step on CPU %u\n", cpu_policy
->cpu
);
1394 schedule_work(&cpu_policy
->update
);
1397 cpufreq_cpu_put(cpu_policy
);
1400 static struct syscore_ops cpufreq_syscore_ops
= {
1401 .suspend
= cpufreq_bp_suspend
,
1402 .resume
= cpufreq_bp_resume
,
1406 * cpufreq_get_current_driver - return current driver's name
1408 * Return the name string of the currently loaded cpufreq driver
1411 const char *cpufreq_get_current_driver(void)
1414 return cpufreq_driver
->name
;
1418 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver
);
1420 /*********************************************************************
1421 * NOTIFIER LISTS INTERFACE *
1422 *********************************************************************/
1425 * cpufreq_register_notifier - register a driver with cpufreq
1426 * @nb: notifier function to register
1427 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1429 * Add a driver to one of two lists: either a list of drivers that
1430 * are notified about clock rate changes (once before and once after
1431 * the transition), or a list of drivers that are notified about
1432 * changes in cpufreq policy.
1434 * This function may sleep, and has the same return conditions as
1435 * blocking_notifier_chain_register.
1437 int cpufreq_register_notifier(struct notifier_block
*nb
, unsigned int list
)
1441 if (cpufreq_disabled())
1444 WARN_ON(!init_cpufreq_transition_notifier_list_called
);
1447 case CPUFREQ_TRANSITION_NOTIFIER
:
1448 ret
= srcu_notifier_chain_register(
1449 &cpufreq_transition_notifier_list
, nb
);
1451 case CPUFREQ_POLICY_NOTIFIER
:
1452 ret
= blocking_notifier_chain_register(
1453 &cpufreq_policy_notifier_list
, nb
);
1461 EXPORT_SYMBOL(cpufreq_register_notifier
);
1465 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1466 * @nb: notifier block to be unregistered
1467 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1469 * Remove a driver from the CPU frequency notifier list.
1471 * This function may sleep, and has the same return conditions as
1472 * blocking_notifier_chain_unregister.
1474 int cpufreq_unregister_notifier(struct notifier_block
*nb
, unsigned int list
)
1478 if (cpufreq_disabled())
1482 case CPUFREQ_TRANSITION_NOTIFIER
:
1483 ret
= srcu_notifier_chain_unregister(
1484 &cpufreq_transition_notifier_list
, nb
);
1486 case CPUFREQ_POLICY_NOTIFIER
:
1487 ret
= blocking_notifier_chain_unregister(
1488 &cpufreq_policy_notifier_list
, nb
);
1496 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1499 /*********************************************************************
1501 *********************************************************************/
1504 int __cpufreq_driver_target(struct cpufreq_policy
*policy
,
1505 unsigned int target_freq
,
1506 unsigned int relation
)
1508 int retval
= -EINVAL
;
1509 unsigned int old_target_freq
= target_freq
;
1511 if (cpufreq_disabled())
1514 /* Make sure that target_freq is within supported range */
1515 if (target_freq
> policy
->max
)
1516 target_freq
= policy
->max
;
1517 if (target_freq
< policy
->min
)
1518 target_freq
= policy
->min
;
1520 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1521 policy
->cpu
, target_freq
, relation
, old_target_freq
);
1523 if (target_freq
== policy
->cur
)
1526 if (cpufreq_driver
->target
)
1527 retval
= cpufreq_driver
->target(policy
, target_freq
, relation
);
1531 EXPORT_SYMBOL_GPL(__cpufreq_driver_target
);
1533 int cpufreq_driver_target(struct cpufreq_policy
*policy
,
1534 unsigned int target_freq
,
1535 unsigned int relation
)
1539 policy
= cpufreq_cpu_get(policy
->cpu
);
1543 if (unlikely(lock_policy_rwsem_write(policy
->cpu
)))
1546 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
1548 unlock_policy_rwsem_write(policy
->cpu
);
1551 cpufreq_cpu_put(policy
);
1555 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
1557 int __cpufreq_driver_getavg(struct cpufreq_policy
*policy
, unsigned int cpu
)
1561 if (cpufreq_disabled())
1564 if (!cpufreq_driver
->getavg
)
1567 policy
= cpufreq_cpu_get(policy
->cpu
);
1571 ret
= cpufreq_driver
->getavg(policy
, cpu
);
1573 cpufreq_cpu_put(policy
);
1576 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg
);
1579 * when "event" is CPUFREQ_GOV_LIMITS
1582 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
1587 /* Only must be defined when default governor is known to have latency
1588 restrictions, like e.g. conservative or ondemand.
1589 That this is the case is already ensured in Kconfig
1591 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1592 struct cpufreq_governor
*gov
= &cpufreq_gov_performance
;
1594 struct cpufreq_governor
*gov
= NULL
;
1597 if (policy
->governor
->max_transition_latency
&&
1598 policy
->cpuinfo
.transition_latency
>
1599 policy
->governor
->max_transition_latency
) {
1603 printk(KERN_WARNING
"%s governor failed, too long"
1604 " transition latency of HW, fallback"
1605 " to %s governor\n",
1606 policy
->governor
->name
,
1608 policy
->governor
= gov
;
1612 if (!try_module_get(policy
->governor
->owner
))
1615 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1616 policy
->cpu
, event
);
1618 mutex_lock(&cpufreq_governor_lock
);
1619 if ((!policy
->governor_enabled
&& (event
== CPUFREQ_GOV_STOP
)) ||
1620 (policy
->governor_enabled
&& (event
== CPUFREQ_GOV_START
))) {
1621 mutex_unlock(&cpufreq_governor_lock
);
1625 if (event
== CPUFREQ_GOV_STOP
)
1626 policy
->governor_enabled
= false;
1627 else if (event
== CPUFREQ_GOV_START
)
1628 policy
->governor_enabled
= true;
1630 mutex_unlock(&cpufreq_governor_lock
);
1632 ret
= policy
->governor
->governor(policy
, event
);
1635 if (event
== CPUFREQ_GOV_POLICY_INIT
)
1636 policy
->governor
->initialized
++;
1637 else if (event
== CPUFREQ_GOV_POLICY_EXIT
)
1638 policy
->governor
->initialized
--;
1640 /* Restore original values */
1641 mutex_lock(&cpufreq_governor_lock
);
1642 if (event
== CPUFREQ_GOV_STOP
)
1643 policy
->governor_enabled
= true;
1644 else if (event
== CPUFREQ_GOV_START
)
1645 policy
->governor_enabled
= false;
1646 mutex_unlock(&cpufreq_governor_lock
);
1649 /* we keep one module reference alive for
1650 each CPU governed by this CPU */
1651 if ((event
!= CPUFREQ_GOV_START
) || ret
)
1652 module_put(policy
->governor
->owner
);
1653 if ((event
== CPUFREQ_GOV_STOP
) && !ret
)
1654 module_put(policy
->governor
->owner
);
1660 int cpufreq_register_governor(struct cpufreq_governor
*governor
)
1667 if (cpufreq_disabled())
1670 mutex_lock(&cpufreq_governor_mutex
);
1672 governor
->initialized
= 0;
1674 if (__find_governor(governor
->name
) == NULL
) {
1676 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
1679 mutex_unlock(&cpufreq_governor_mutex
);
1682 EXPORT_SYMBOL_GPL(cpufreq_register_governor
);
1685 void cpufreq_unregister_governor(struct cpufreq_governor
*governor
)
1687 #ifdef CONFIG_HOTPLUG_CPU
1694 if (cpufreq_disabled())
1697 #ifdef CONFIG_HOTPLUG_CPU
1698 for_each_present_cpu(cpu
) {
1699 if (cpu_online(cpu
))
1701 if (!strcmp(per_cpu(cpufreq_cpu_governor
, cpu
), governor
->name
))
1702 strcpy(per_cpu(cpufreq_cpu_governor
, cpu
), "\0");
1706 mutex_lock(&cpufreq_governor_mutex
);
1707 list_del(&governor
->governor_list
);
1708 mutex_unlock(&cpufreq_governor_mutex
);
1711 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor
);
1715 /*********************************************************************
1716 * POLICY INTERFACE *
1717 *********************************************************************/
1720 * cpufreq_get_policy - get the current cpufreq_policy
1721 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1724 * Reads the current cpufreq policy.
1726 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
1728 struct cpufreq_policy
*cpu_policy
;
1732 cpu_policy
= cpufreq_cpu_get(cpu
);
1736 memcpy(policy
, cpu_policy
, sizeof(struct cpufreq_policy
));
1738 cpufreq_cpu_put(cpu_policy
);
1741 EXPORT_SYMBOL(cpufreq_get_policy
);
1745 * data : current policy.
1746 * policy : policy to be set.
1748 static int __cpufreq_set_policy(struct cpufreq_policy
*data
,
1749 struct cpufreq_policy
*policy
)
1751 int ret
= 0, failed
= 1;
1753 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy
->cpu
,
1754 policy
->min
, policy
->max
);
1756 memcpy(&policy
->cpuinfo
, &data
->cpuinfo
,
1757 sizeof(struct cpufreq_cpuinfo
));
1759 if (policy
->min
> data
->max
|| policy
->max
< data
->min
) {
1764 /* verify the cpu speed can be set within this limit */
1765 ret
= cpufreq_driver
->verify(policy
);
1769 /* adjust if necessary - all reasons */
1770 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1771 CPUFREQ_ADJUST
, policy
);
1773 /* adjust if necessary - hardware incompatibility*/
1774 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1775 CPUFREQ_INCOMPATIBLE
, policy
);
1777 /* verify the cpu speed can be set within this limit,
1778 which might be different to the first one */
1779 ret
= cpufreq_driver
->verify(policy
);
1783 /* notification of the new policy */
1784 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1785 CPUFREQ_NOTIFY
, policy
);
1787 data
->min
= policy
->min
;
1788 data
->max
= policy
->max
;
1790 pr_debug("new min and max freqs are %u - %u kHz\n",
1791 data
->min
, data
->max
);
1793 if (cpufreq_driver
->setpolicy
) {
1794 data
->policy
= policy
->policy
;
1795 pr_debug("setting range\n");
1796 ret
= cpufreq_driver
->setpolicy(policy
);
1798 if (policy
->governor
!= data
->governor
) {
1799 /* save old, working values */
1800 struct cpufreq_governor
*old_gov
= data
->governor
;
1802 pr_debug("governor switch\n");
1804 /* end old governor */
1805 if (data
->governor
) {
1806 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
1807 unlock_policy_rwsem_write(policy
->cpu
);
1808 __cpufreq_governor(data
,
1809 CPUFREQ_GOV_POLICY_EXIT
);
1810 lock_policy_rwsem_write(policy
->cpu
);
1813 /* start new governor */
1814 data
->governor
= policy
->governor
;
1815 if (!__cpufreq_governor(data
, CPUFREQ_GOV_POLICY_INIT
)) {
1816 if (!__cpufreq_governor(data
, CPUFREQ_GOV_START
)) {
1819 unlock_policy_rwsem_write(policy
->cpu
);
1820 __cpufreq_governor(data
,
1821 CPUFREQ_GOV_POLICY_EXIT
);
1822 lock_policy_rwsem_write(policy
->cpu
);
1827 /* new governor failed, so re-start old one */
1828 pr_debug("starting governor %s failed\n",
1829 data
->governor
->name
);
1831 data
->governor
= old_gov
;
1832 __cpufreq_governor(data
,
1833 CPUFREQ_GOV_POLICY_INIT
);
1834 __cpufreq_governor(data
,
1840 /* might be a policy change, too, so fall through */
1842 pr_debug("governor: change or update limits\n");
1843 __cpufreq_governor(data
, CPUFREQ_GOV_LIMITS
);
1851 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1852 * @cpu: CPU which shall be re-evaluated
1854 * Useful for policy notifiers which have different necessities
1855 * at different times.
1857 int cpufreq_update_policy(unsigned int cpu
)
1859 struct cpufreq_policy
*data
= cpufreq_cpu_get(cpu
);
1860 struct cpufreq_policy policy
;
1868 if (unlikely(lock_policy_rwsem_write(cpu
))) {
1873 pr_debug("updating policy for CPU %u\n", cpu
);
1874 memcpy(&policy
, data
, sizeof(struct cpufreq_policy
));
1875 policy
.min
= data
->user_policy
.min
;
1876 policy
.max
= data
->user_policy
.max
;
1877 policy
.policy
= data
->user_policy
.policy
;
1878 policy
.governor
= data
->user_policy
.governor
;
1880 /* BIOS might change freq behind our back
1881 -> ask driver for current freq and notify governors about a change */
1882 if (cpufreq_driver
->get
) {
1883 policy
.cur
= cpufreq_driver
->get(cpu
);
1885 pr_debug("Driver did not initialize current freq");
1886 data
->cur
= policy
.cur
;
1888 if (data
->cur
!= policy
.cur
&& cpufreq_driver
->target
)
1889 cpufreq_out_of_sync(cpu
, data
->cur
,
1894 ret
= __cpufreq_set_policy(data
, &policy
);
1896 unlock_policy_rwsem_write(cpu
);
1899 cpufreq_cpu_put(data
);
1903 EXPORT_SYMBOL(cpufreq_update_policy
);
1905 static int __cpuinit
cpufreq_cpu_callback(struct notifier_block
*nfb
,
1906 unsigned long action
, void *hcpu
)
1908 unsigned int cpu
= (unsigned long)hcpu
;
1911 dev
= get_cpu_device(cpu
);
1915 case CPU_ONLINE_FROZEN
:
1916 cpufreq_add_dev(dev
, NULL
);
1918 case CPU_DOWN_PREPARE
:
1919 case CPU_DOWN_PREPARE_FROZEN
:
1920 __cpufreq_remove_dev(dev
, NULL
);
1922 case CPU_DOWN_FAILED
:
1923 case CPU_DOWN_FAILED_FROZEN
:
1924 cpufreq_add_dev(dev
, NULL
);
1931 static struct notifier_block __refdata cpufreq_cpu_notifier
= {
1932 .notifier_call
= cpufreq_cpu_callback
,
1935 /*********************************************************************
1936 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1937 *********************************************************************/
1940 * cpufreq_register_driver - register a CPU Frequency driver
1941 * @driver_data: A struct cpufreq_driver containing the values#
1942 * submitted by the CPU Frequency driver.
1944 * Registers a CPU Frequency driver to this core code. This code
1945 * returns zero on success, -EBUSY when another driver got here first
1946 * (and isn't unregistered in the meantime).
1949 int cpufreq_register_driver(struct cpufreq_driver
*driver_data
)
1951 unsigned long flags
;
1954 if (cpufreq_disabled())
1957 if (!driver_data
|| !driver_data
->verify
|| !driver_data
->init
||
1958 ((!driver_data
->setpolicy
) && (!driver_data
->target
)))
1961 pr_debug("trying to register driver %s\n", driver_data
->name
);
1963 if (driver_data
->setpolicy
)
1964 driver_data
->flags
|= CPUFREQ_CONST_LOOPS
;
1966 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
1967 if (cpufreq_driver
) {
1968 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1971 cpufreq_driver
= driver_data
;
1972 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1974 ret
= subsys_interface_register(&cpufreq_interface
);
1976 goto err_null_driver
;
1978 if (!(cpufreq_driver
->flags
& CPUFREQ_STICKY
)) {
1982 /* check for at least one working CPU */
1983 for (i
= 0; i
< nr_cpu_ids
; i
++)
1984 if (cpu_possible(i
) && per_cpu(cpufreq_cpu_data
, i
)) {
1989 /* if all ->init() calls failed, unregister */
1991 pr_debug("no CPU initialized for driver %s\n",
1997 register_hotcpu_notifier(&cpufreq_cpu_notifier
);
1998 pr_debug("driver %s up and running\n", driver_data
->name
);
2002 subsys_interface_unregister(&cpufreq_interface
);
2004 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2005 cpufreq_driver
= NULL
;
2006 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2009 EXPORT_SYMBOL_GPL(cpufreq_register_driver
);
2013 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2015 * Unregister the current CPUFreq driver. Only call this if you have
2016 * the right to do so, i.e. if you have succeeded in initialising before!
2017 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2018 * currently not initialised.
2020 int cpufreq_unregister_driver(struct cpufreq_driver
*driver
)
2022 unsigned long flags
;
2024 if (!cpufreq_driver
|| (driver
!= cpufreq_driver
))
2027 pr_debug("unregistering driver %s\n", driver
->name
);
2029 subsys_interface_unregister(&cpufreq_interface
);
2030 unregister_hotcpu_notifier(&cpufreq_cpu_notifier
);
2032 write_lock_irqsave(&cpufreq_driver_lock
, flags
);
2033 cpufreq_driver
= NULL
;
2034 write_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
2038 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver
);
2040 static int __init
cpufreq_core_init(void)
2044 if (cpufreq_disabled())
2047 for_each_possible_cpu(cpu
) {
2048 per_cpu(cpufreq_policy_cpu
, cpu
) = -1;
2049 init_rwsem(&per_cpu(cpu_policy_rwsem
, cpu
));
2052 cpufreq_global_kobject
= kobject_create_and_add("cpufreq", &cpu_subsys
.dev_root
->kobj
);
2053 BUG_ON(!cpufreq_global_kobject
);
2054 register_syscore_ops(&cpufreq_syscore_ops
);
2058 core_initcall(cpufreq_core_init
);