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 <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/notifier.h>
24 #include <linux/cpufreq.h>
25 #include <linux/delay.h>
26 #include <linux/interrupt.h>
27 #include <linux/spinlock.h>
28 #include <linux/device.h>
29 #include <linux/slab.h>
30 #include <linux/cpu.h>
31 #include <linux/completion.h>
32 #include <linux/mutex.h>
33 #include <linux/syscore_ops.h>
35 #include <trace/events/power.h>
38 * The "cpufreq driver" - the arch- or hardware-dependent low
39 * level driver of CPUFreq support, and its spinlock. This lock
40 * also protects the cpufreq_cpu_data array.
42 static struct cpufreq_driver
*cpufreq_driver
;
43 static DEFINE_PER_CPU(struct cpufreq_policy
*, cpufreq_cpu_data
);
44 #ifdef CONFIG_HOTPLUG_CPU
45 /* This one keeps track of the previously set governor of a removed CPU */
46 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN
], cpufreq_cpu_governor
);
48 static DEFINE_SPINLOCK(cpufreq_driver_lock
);
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
62 * - All holders of the lock should check to make sure that the CPU they
63 * are concerned with are online after they get the lock.
64 * - Governor routines that can be called in cpufreq hotplug path should not
65 * take this sem as top level hotplug notifier handler takes this.
66 * - Lock should not be held across
67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
69 static DEFINE_PER_CPU(int, cpufreq_policy_cpu
);
70 static DEFINE_PER_CPU(struct rw_semaphore
, cpu_policy_rwsem
);
72 #define lock_policy_rwsem(mode, cpu) \
73 static int lock_policy_rwsem_##mode \
76 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
77 BUG_ON(policy_cpu == -1); \
78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 if (unlikely(!cpu_online(cpu))) { \
80 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
87 lock_policy_rwsem(read
, cpu
);
89 lock_policy_rwsem(write
, cpu
);
91 static void unlock_policy_rwsem_read(int cpu
)
93 int policy_cpu
= per_cpu(cpufreq_policy_cpu
, cpu
);
94 BUG_ON(policy_cpu
== -1);
95 up_read(&per_cpu(cpu_policy_rwsem
, policy_cpu
));
98 static void unlock_policy_rwsem_write(int cpu
)
100 int policy_cpu
= per_cpu(cpufreq_policy_cpu
, cpu
);
101 BUG_ON(policy_cpu
== -1);
102 up_write(&per_cpu(cpu_policy_rwsem
, policy_cpu
));
106 /* internal prototypes */
107 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
109 static unsigned int __cpufreq_get(unsigned int cpu
);
110 static void handle_update(struct work_struct
*work
);
113 * Two notifier lists: the "policy" list is involved in the
114 * validation process for a new CPU frequency policy; the
115 * "transition" list for kernel code that needs to handle
116 * changes to devices when the CPU clock speed changes.
117 * The mutex locks both lists.
119 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list
);
120 static struct srcu_notifier_head cpufreq_transition_notifier_list
;
122 static bool init_cpufreq_transition_notifier_list_called
;
123 static int __init
init_cpufreq_transition_notifier_list(void)
125 srcu_init_notifier_head(&cpufreq_transition_notifier_list
);
126 init_cpufreq_transition_notifier_list_called
= true;
129 pure_initcall(init_cpufreq_transition_notifier_list
);
131 static int off __read_mostly
;
132 static int cpufreq_disabled(void)
136 void disable_cpufreq(void)
140 static LIST_HEAD(cpufreq_governor_list
);
141 static DEFINE_MUTEX(cpufreq_governor_mutex
);
143 static struct cpufreq_policy
*__cpufreq_cpu_get(unsigned int cpu
, bool sysfs
)
145 struct cpufreq_policy
*data
;
148 if (cpu
>= nr_cpu_ids
)
151 /* get the cpufreq driver */
152 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
157 if (!try_module_get(cpufreq_driver
->owner
))
162 data
= per_cpu(cpufreq_cpu_data
, cpu
);
165 goto err_out_put_module
;
167 if (!sysfs
&& !kobject_get(&data
->kobj
))
168 goto err_out_put_module
;
170 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
174 module_put(cpufreq_driver
->owner
);
176 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
181 struct cpufreq_policy
*cpufreq_cpu_get(unsigned int cpu
)
183 return __cpufreq_cpu_get(cpu
, false);
185 EXPORT_SYMBOL_GPL(cpufreq_cpu_get
);
187 static struct cpufreq_policy
*cpufreq_cpu_get_sysfs(unsigned int cpu
)
189 return __cpufreq_cpu_get(cpu
, true);
192 static void __cpufreq_cpu_put(struct cpufreq_policy
*data
, bool sysfs
)
195 kobject_put(&data
->kobj
);
196 module_put(cpufreq_driver
->owner
);
199 void cpufreq_cpu_put(struct cpufreq_policy
*data
)
201 __cpufreq_cpu_put(data
, false);
203 EXPORT_SYMBOL_GPL(cpufreq_cpu_put
);
205 static void cpufreq_cpu_put_sysfs(struct cpufreq_policy
*data
)
207 __cpufreq_cpu_put(data
, true);
210 /*********************************************************************
211 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
212 *********************************************************************/
215 * adjust_jiffies - adjust the system "loops_per_jiffy"
217 * This function alters the system "loops_per_jiffy" for the clock
218 * speed change. Note that loops_per_jiffy cannot be updated on SMP
219 * systems as each CPU might be scaled differently. So, use the arch
220 * per-CPU loops_per_jiffy value wherever possible.
223 static unsigned long l_p_j_ref
;
224 static unsigned int l_p_j_ref_freq
;
226 static void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
228 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
231 if (!l_p_j_ref_freq
) {
232 l_p_j_ref
= loops_per_jiffy
;
233 l_p_j_ref_freq
= ci
->old
;
234 pr_debug("saving %lu as reference value for loops_per_jiffy; "
235 "freq is %u kHz\n", l_p_j_ref
, l_p_j_ref_freq
);
237 if ((val
== CPUFREQ_POSTCHANGE
&& ci
->old
!= ci
->new) ||
238 (val
== CPUFREQ_RESUMECHANGE
|| val
== CPUFREQ_SUSPENDCHANGE
)) {
239 loops_per_jiffy
= cpufreq_scale(l_p_j_ref
, l_p_j_ref_freq
,
241 pr_debug("scaling loops_per_jiffy to %lu "
242 "for frequency %u kHz\n", loops_per_jiffy
, ci
->new);
246 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
254 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
255 * on frequency transition.
257 * This function calls the transition notifiers and the "adjust_jiffies"
258 * function. It is called twice on all CPU frequency changes that have
261 void cpufreq_notify_transition(struct cpufreq_freqs
*freqs
, unsigned int state
)
263 struct cpufreq_policy
*policy
;
265 BUG_ON(irqs_disabled());
267 freqs
->flags
= cpufreq_driver
->flags
;
268 pr_debug("notification %u of frequency transition to %u kHz\n",
271 policy
= per_cpu(cpufreq_cpu_data
, freqs
->cpu
);
274 case CPUFREQ_PRECHANGE
:
275 /* detect if the driver reported a value as "old frequency"
276 * which is not equal to what the cpufreq core thinks is
279 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
280 if ((policy
) && (policy
->cpu
== freqs
->cpu
) &&
281 (policy
->cur
) && (policy
->cur
!= freqs
->old
)) {
282 pr_debug("Warning: CPU frequency is"
283 " %u, cpufreq assumed %u kHz.\n",
284 freqs
->old
, policy
->cur
);
285 freqs
->old
= policy
->cur
;
288 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
289 CPUFREQ_PRECHANGE
, freqs
);
290 adjust_jiffies(CPUFREQ_PRECHANGE
, freqs
);
293 case CPUFREQ_POSTCHANGE
:
294 adjust_jiffies(CPUFREQ_POSTCHANGE
, freqs
);
295 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs
->new,
296 (unsigned long)freqs
->cpu
);
297 trace_power_frequency(POWER_PSTATE
, freqs
->new, 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;
306 EXPORT_SYMBOL_GPL(cpufreq_notify_transition
);
310 /*********************************************************************
312 *********************************************************************/
314 static struct cpufreq_governor
*__find_governor(const char *str_governor
)
316 struct cpufreq_governor
*t
;
318 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
)
319 if (!strnicmp(str_governor
, t
->name
, CPUFREQ_NAME_LEN
))
326 * cpufreq_parse_governor - parse a governor string
328 static int cpufreq_parse_governor(char *str_governor
, unsigned int *policy
,
329 struct cpufreq_governor
**governor
)
336 if (cpufreq_driver
->setpolicy
) {
337 if (!strnicmp(str_governor
, "performance", CPUFREQ_NAME_LEN
)) {
338 *policy
= CPUFREQ_POLICY_PERFORMANCE
;
340 } else if (!strnicmp(str_governor
, "powersave",
342 *policy
= CPUFREQ_POLICY_POWERSAVE
;
345 } else if (cpufreq_driver
->target
) {
346 struct cpufreq_governor
*t
;
348 mutex_lock(&cpufreq_governor_mutex
);
350 t
= __find_governor(str_governor
);
355 mutex_unlock(&cpufreq_governor_mutex
);
356 ret
= request_module("cpufreq_%s", str_governor
);
357 mutex_lock(&cpufreq_governor_mutex
);
360 t
= __find_governor(str_governor
);
368 mutex_unlock(&cpufreq_governor_mutex
);
376 * cpufreq_per_cpu_attr_read() / show_##file_name() -
377 * print out cpufreq information
379 * Write out information from cpufreq_driver->policy[cpu]; object must be
383 #define show_one(file_name, object) \
384 static ssize_t show_##file_name \
385 (struct cpufreq_policy *policy, char *buf) \
387 return sprintf(buf, "%u\n", policy->object); \
390 show_one(cpuinfo_min_freq
, cpuinfo
.min_freq
);
391 show_one(cpuinfo_max_freq
, cpuinfo
.max_freq
);
392 show_one(cpuinfo_transition_latency
, cpuinfo
.transition_latency
);
393 show_one(scaling_min_freq
, min
);
394 show_one(scaling_max_freq
, max
);
395 show_one(scaling_cur_freq
, cur
);
397 static int __cpufreq_set_policy(struct cpufreq_policy
*data
,
398 struct cpufreq_policy
*policy
);
401 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
403 #define store_one(file_name, object) \
404 static ssize_t store_##file_name \
405 (struct cpufreq_policy *policy, const char *buf, size_t count) \
408 struct cpufreq_policy new_policy; \
410 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
414 ret = sscanf(buf, "%u", &new_policy.object); \
418 ret = __cpufreq_set_policy(policy, &new_policy); \
419 policy->user_policy.object = policy->object; \
421 return ret ? ret : count; \
424 store_one(scaling_min_freq
, min
);
425 store_one(scaling_max_freq
, max
);
428 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
430 static ssize_t
show_cpuinfo_cur_freq(struct cpufreq_policy
*policy
,
433 unsigned int cur_freq
= __cpufreq_get(policy
->cpu
);
435 return sprintf(buf
, "<unknown>");
436 return sprintf(buf
, "%u\n", cur_freq
);
441 * show_scaling_governor - show the current policy for the specified CPU
443 static ssize_t
show_scaling_governor(struct cpufreq_policy
*policy
, char *buf
)
445 if (policy
->policy
== CPUFREQ_POLICY_POWERSAVE
)
446 return sprintf(buf
, "powersave\n");
447 else if (policy
->policy
== CPUFREQ_POLICY_PERFORMANCE
)
448 return sprintf(buf
, "performance\n");
449 else if (policy
->governor
)
450 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n",
451 policy
->governor
->name
);
457 * store_scaling_governor - store policy for the specified CPU
459 static ssize_t
store_scaling_governor(struct cpufreq_policy
*policy
,
460 const char *buf
, size_t count
)
463 char str_governor
[16];
464 struct cpufreq_policy new_policy
;
466 ret
= cpufreq_get_policy(&new_policy
, policy
->cpu
);
470 ret
= sscanf(buf
, "%15s", str_governor
);
474 if (cpufreq_parse_governor(str_governor
, &new_policy
.policy
,
475 &new_policy
.governor
))
478 /* Do not use cpufreq_set_policy here or the user_policy.max
479 will be wrongly overridden */
480 ret
= __cpufreq_set_policy(policy
, &new_policy
);
482 policy
->user_policy
.policy
= policy
->policy
;
483 policy
->user_policy
.governor
= policy
->governor
;
492 * show_scaling_driver - show the cpufreq driver currently loaded
494 static ssize_t
show_scaling_driver(struct cpufreq_policy
*policy
, char *buf
)
496 return scnprintf(buf
, CPUFREQ_NAME_PLEN
, "%s\n", cpufreq_driver
->name
);
500 * show_scaling_available_governors - show the available CPUfreq governors
502 static ssize_t
show_scaling_available_governors(struct cpufreq_policy
*policy
,
506 struct cpufreq_governor
*t
;
508 if (!cpufreq_driver
->target
) {
509 i
+= sprintf(buf
, "performance powersave");
513 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
514 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char))
515 - (CPUFREQ_NAME_LEN
+ 2)))
517 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_PLEN
, "%s ", t
->name
);
520 i
+= sprintf(&buf
[i
], "\n");
524 static ssize_t
show_cpus(const struct cpumask
*mask
, char *buf
)
529 for_each_cpu(cpu
, mask
) {
531 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), " ");
532 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), "%u", cpu
);
533 if (i
>= (PAGE_SIZE
- 5))
536 i
+= sprintf(&buf
[i
], "\n");
541 * show_related_cpus - show the CPUs affected by each transition even if
542 * hw coordination is in use
544 static ssize_t
show_related_cpus(struct cpufreq_policy
*policy
, char *buf
)
546 if (cpumask_empty(policy
->related_cpus
))
547 return show_cpus(policy
->cpus
, buf
);
548 return show_cpus(policy
->related_cpus
, buf
);
552 * show_affected_cpus - show the CPUs affected by each transition
554 static ssize_t
show_affected_cpus(struct cpufreq_policy
*policy
, char *buf
)
556 return show_cpus(policy
->cpus
, buf
);
559 static ssize_t
store_scaling_setspeed(struct cpufreq_policy
*policy
,
560 const char *buf
, size_t count
)
562 unsigned int freq
= 0;
565 if (!policy
->governor
|| !policy
->governor
->store_setspeed
)
568 ret
= sscanf(buf
, "%u", &freq
);
572 policy
->governor
->store_setspeed(policy
, freq
);
577 static ssize_t
show_scaling_setspeed(struct cpufreq_policy
*policy
, char *buf
)
579 if (!policy
->governor
|| !policy
->governor
->show_setspeed
)
580 return sprintf(buf
, "<unsupported>\n");
582 return policy
->governor
->show_setspeed(policy
, buf
);
586 * show_bios_limit - show the current cpufreq HW/BIOS limitation
588 static ssize_t
show_bios_limit(struct cpufreq_policy
*policy
, char *buf
)
592 if (cpufreq_driver
->bios_limit
) {
593 ret
= cpufreq_driver
->bios_limit(policy
->cpu
, &limit
);
595 return sprintf(buf
, "%u\n", limit
);
597 return sprintf(buf
, "%u\n", policy
->cpuinfo
.max_freq
);
600 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq
, 0400);
601 cpufreq_freq_attr_ro(cpuinfo_min_freq
);
602 cpufreq_freq_attr_ro(cpuinfo_max_freq
);
603 cpufreq_freq_attr_ro(cpuinfo_transition_latency
);
604 cpufreq_freq_attr_ro(scaling_available_governors
);
605 cpufreq_freq_attr_ro(scaling_driver
);
606 cpufreq_freq_attr_ro(scaling_cur_freq
);
607 cpufreq_freq_attr_ro(bios_limit
);
608 cpufreq_freq_attr_ro(related_cpus
);
609 cpufreq_freq_attr_ro(affected_cpus
);
610 cpufreq_freq_attr_rw(scaling_min_freq
);
611 cpufreq_freq_attr_rw(scaling_max_freq
);
612 cpufreq_freq_attr_rw(scaling_governor
);
613 cpufreq_freq_attr_rw(scaling_setspeed
);
615 static struct attribute
*default_attrs
[] = {
616 &cpuinfo_min_freq
.attr
,
617 &cpuinfo_max_freq
.attr
,
618 &cpuinfo_transition_latency
.attr
,
619 &scaling_min_freq
.attr
,
620 &scaling_max_freq
.attr
,
623 &scaling_governor
.attr
,
624 &scaling_driver
.attr
,
625 &scaling_available_governors
.attr
,
626 &scaling_setspeed
.attr
,
630 struct kobject
*cpufreq_global_kobject
;
631 EXPORT_SYMBOL(cpufreq_global_kobject
);
633 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
634 #define to_attr(a) container_of(a, struct freq_attr, attr)
636 static ssize_t
show(struct kobject
*kobj
, struct attribute
*attr
, char *buf
)
638 struct cpufreq_policy
*policy
= to_policy(kobj
);
639 struct freq_attr
*fattr
= to_attr(attr
);
640 ssize_t ret
= -EINVAL
;
641 policy
= cpufreq_cpu_get_sysfs(policy
->cpu
);
645 if (lock_policy_rwsem_read(policy
->cpu
) < 0)
649 ret
= fattr
->show(policy
, buf
);
653 unlock_policy_rwsem_read(policy
->cpu
);
655 cpufreq_cpu_put_sysfs(policy
);
660 static ssize_t
store(struct kobject
*kobj
, struct attribute
*attr
,
661 const char *buf
, size_t count
)
663 struct cpufreq_policy
*policy
= to_policy(kobj
);
664 struct freq_attr
*fattr
= to_attr(attr
);
665 ssize_t ret
= -EINVAL
;
666 policy
= cpufreq_cpu_get_sysfs(policy
->cpu
);
670 if (lock_policy_rwsem_write(policy
->cpu
) < 0)
674 ret
= fattr
->store(policy
, buf
, count
);
678 unlock_policy_rwsem_write(policy
->cpu
);
680 cpufreq_cpu_put_sysfs(policy
);
685 static void cpufreq_sysfs_release(struct kobject
*kobj
)
687 struct cpufreq_policy
*policy
= to_policy(kobj
);
688 pr_debug("last reference is dropped\n");
689 complete(&policy
->kobj_unregister
);
692 static const struct sysfs_ops sysfs_ops
= {
697 static struct kobj_type ktype_cpufreq
= {
698 .sysfs_ops
= &sysfs_ops
,
699 .default_attrs
= default_attrs
,
700 .release
= cpufreq_sysfs_release
,
707 * Positive: When we have a managed CPU and the sysfs got symlinked
709 static int cpufreq_add_dev_policy(unsigned int cpu
,
710 struct cpufreq_policy
*policy
,
717 #ifdef CONFIG_HOTPLUG_CPU
718 struct cpufreq_governor
*gov
;
720 gov
= __find_governor(per_cpu(cpufreq_cpu_governor
, cpu
));
722 policy
->governor
= gov
;
723 pr_debug("Restoring governor %s for cpu %d\n",
724 policy
->governor
->name
, cpu
);
728 for_each_cpu(j
, policy
->cpus
) {
729 struct cpufreq_policy
*managed_policy
;
734 /* Check for existing affected CPUs.
735 * They may not be aware of it due to CPU Hotplug.
736 * cpufreq_cpu_put is called when the device is removed
737 * in __cpufreq_remove_dev()
739 managed_policy
= cpufreq_cpu_get(j
);
740 if (unlikely(managed_policy
)) {
742 /* Set proper policy_cpu */
743 unlock_policy_rwsem_write(cpu
);
744 per_cpu(cpufreq_policy_cpu
, cpu
) = managed_policy
->cpu
;
746 if (lock_policy_rwsem_write(cpu
) < 0) {
747 /* Should not go through policy unlock path */
748 if (cpufreq_driver
->exit
)
749 cpufreq_driver
->exit(policy
);
750 cpufreq_cpu_put(managed_policy
);
754 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
755 cpumask_copy(managed_policy
->cpus
, policy
->cpus
);
756 per_cpu(cpufreq_cpu_data
, cpu
) = managed_policy
;
757 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
759 pr_debug("CPU already managed, adding link\n");
760 ret
= sysfs_create_link(&dev
->kobj
,
761 &managed_policy
->kobj
,
764 cpufreq_cpu_put(managed_policy
);
766 * Success. We only needed to be added to the mask.
767 * Call driver->exit() because only the cpu parent of
768 * the kobj needed to call init().
770 if (cpufreq_driver
->exit
)
771 cpufreq_driver
->exit(policy
);
784 /* symlink affected CPUs */
785 static int cpufreq_add_dev_symlink(unsigned int cpu
,
786 struct cpufreq_policy
*policy
)
791 for_each_cpu(j
, policy
->cpus
) {
792 struct cpufreq_policy
*managed_policy
;
793 struct device
*cpu_dev
;
800 pr_debug("CPU %u already managed, adding link\n", j
);
801 managed_policy
= cpufreq_cpu_get(cpu
);
802 cpu_dev
= get_cpu_device(j
);
803 ret
= sysfs_create_link(&cpu_dev
->kobj
, &policy
->kobj
,
806 cpufreq_cpu_put(managed_policy
);
813 static int cpufreq_add_dev_interface(unsigned int cpu
,
814 struct cpufreq_policy
*policy
,
817 struct cpufreq_policy new_policy
;
818 struct freq_attr
**drv_attr
;
823 /* prepare interface data */
824 ret
= kobject_init_and_add(&policy
->kobj
, &ktype_cpufreq
,
825 &dev
->kobj
, "cpufreq");
829 /* set up files for this cpu device */
830 drv_attr
= cpufreq_driver
->attr
;
831 while ((drv_attr
) && (*drv_attr
)) {
832 ret
= sysfs_create_file(&policy
->kobj
, &((*drv_attr
)->attr
));
834 goto err_out_kobj_put
;
837 if (cpufreq_driver
->get
) {
838 ret
= sysfs_create_file(&policy
->kobj
, &cpuinfo_cur_freq
.attr
);
840 goto err_out_kobj_put
;
842 if (cpufreq_driver
->target
) {
843 ret
= sysfs_create_file(&policy
->kobj
, &scaling_cur_freq
.attr
);
845 goto err_out_kobj_put
;
847 if (cpufreq_driver
->bios_limit
) {
848 ret
= sysfs_create_file(&policy
->kobj
, &bios_limit
.attr
);
850 goto err_out_kobj_put
;
853 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
854 for_each_cpu(j
, policy
->cpus
) {
857 per_cpu(cpufreq_cpu_data
, j
) = policy
;
858 per_cpu(cpufreq_policy_cpu
, j
) = policy
->cpu
;
860 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
862 ret
= cpufreq_add_dev_symlink(cpu
, policy
);
864 goto err_out_kobj_put
;
866 memcpy(&new_policy
, policy
, sizeof(struct cpufreq_policy
));
867 /* assure that the starting sequence is run in __cpufreq_set_policy */
868 policy
->governor
= NULL
;
870 /* set default policy */
871 ret
= __cpufreq_set_policy(policy
, &new_policy
);
872 policy
->user_policy
.policy
= policy
->policy
;
873 policy
->user_policy
.governor
= policy
->governor
;
876 pr_debug("setting policy failed\n");
877 if (cpufreq_driver
->exit
)
878 cpufreq_driver
->exit(policy
);
883 kobject_put(&policy
->kobj
);
884 wait_for_completion(&policy
->kobj_unregister
);
890 * cpufreq_add_dev - add a CPU device
892 * Adds the cpufreq interface for a CPU device.
894 * The Oracle says: try running cpufreq registration/unregistration concurrently
895 * with with cpu hotplugging and all hell will break loose. Tried to clean this
896 * mess up, but more thorough testing is needed. - Mathieu
898 static int cpufreq_add_dev(struct device
*dev
, struct subsys_interface
*sif
)
900 unsigned int cpu
= dev
->id
;
901 int ret
= 0, found
= 0;
902 struct cpufreq_policy
*policy
;
905 #ifdef CONFIG_HOTPLUG_CPU
909 if (cpu_is_offline(cpu
))
912 pr_debug("adding CPU %u\n", cpu
);
915 /* check whether a different CPU already registered this
916 * CPU because it is in the same boat. */
917 policy
= cpufreq_cpu_get(cpu
);
918 if (unlikely(policy
)) {
919 cpufreq_cpu_put(policy
);
924 if (!try_module_get(cpufreq_driver
->owner
)) {
930 policy
= kzalloc(sizeof(struct cpufreq_policy
), GFP_KERNEL
);
934 if (!alloc_cpumask_var(&policy
->cpus
, GFP_KERNEL
))
935 goto err_free_policy
;
937 if (!zalloc_cpumask_var(&policy
->related_cpus
, GFP_KERNEL
))
938 goto err_free_cpumask
;
941 cpumask_copy(policy
->cpus
, cpumask_of(cpu
));
943 /* Initially set CPU itself as the policy_cpu */
944 per_cpu(cpufreq_policy_cpu
, cpu
) = cpu
;
945 ret
= (lock_policy_rwsem_write(cpu
) < 0);
948 init_completion(&policy
->kobj_unregister
);
949 INIT_WORK(&policy
->update
, handle_update
);
951 /* Set governor before ->init, so that driver could check it */
952 #ifdef CONFIG_HOTPLUG_CPU
953 for_each_online_cpu(sibling
) {
954 struct cpufreq_policy
*cp
= per_cpu(cpufreq_cpu_data
, sibling
);
955 if (cp
&& cp
->governor
&&
956 (cpumask_test_cpu(cpu
, cp
->related_cpus
))) {
957 policy
->governor
= cp
->governor
;
964 policy
->governor
= CPUFREQ_DEFAULT_GOVERNOR
;
965 /* call driver. From then on the cpufreq must be able
966 * to accept all calls to ->verify and ->setpolicy for this CPU
968 ret
= cpufreq_driver
->init(policy
);
970 pr_debug("initialization failed\n");
971 goto err_unlock_policy
;
973 policy
->user_policy
.min
= policy
->min
;
974 policy
->user_policy
.max
= policy
->max
;
976 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
977 CPUFREQ_START
, policy
);
979 ret
= cpufreq_add_dev_policy(cpu
, policy
, dev
);
982 /* This is a managed cpu, symlink created,
985 goto err_unlock_policy
;
988 ret
= cpufreq_add_dev_interface(cpu
, policy
, dev
);
990 goto err_out_unregister
;
992 unlock_policy_rwsem_write(cpu
);
994 kobject_uevent(&policy
->kobj
, KOBJ_ADD
);
995 module_put(cpufreq_driver
->owner
);
996 pr_debug("initialization complete\n");
1002 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1003 for_each_cpu(j
, policy
->cpus
)
1004 per_cpu(cpufreq_cpu_data
, j
) = NULL
;
1005 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1007 kobject_put(&policy
->kobj
);
1008 wait_for_completion(&policy
->kobj_unregister
);
1011 unlock_policy_rwsem_write(cpu
);
1012 free_cpumask_var(policy
->related_cpus
);
1014 free_cpumask_var(policy
->cpus
);
1018 module_put(cpufreq_driver
->owner
);
1025 * __cpufreq_remove_dev - remove a CPU device
1027 * Removes the cpufreq interface for a CPU device.
1028 * Caller should already have policy_rwsem in write mode for this CPU.
1029 * This routine frees the rwsem before returning.
1031 static int __cpufreq_remove_dev(struct device
*dev
, struct subsys_interface
*sif
)
1033 unsigned int cpu
= dev
->id
;
1034 unsigned long flags
;
1035 struct cpufreq_policy
*data
;
1036 struct kobject
*kobj
;
1037 struct completion
*cmp
;
1039 struct device
*cpu_dev
;
1043 pr_debug("unregistering CPU %u\n", cpu
);
1045 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1046 data
= per_cpu(cpufreq_cpu_data
, cpu
);
1049 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1050 unlock_policy_rwsem_write(cpu
);
1053 per_cpu(cpufreq_cpu_data
, cpu
) = NULL
;
1057 /* if this isn't the CPU which is the parent of the kobj, we
1058 * only need to unlink, put and exit
1060 if (unlikely(cpu
!= data
->cpu
)) {
1061 pr_debug("removing link\n");
1062 cpumask_clear_cpu(cpu
, data
->cpus
);
1063 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1065 cpufreq_cpu_put(data
);
1066 unlock_policy_rwsem_write(cpu
);
1067 sysfs_remove_link(kobj
, "cpufreq");
1074 #ifdef CONFIG_HOTPLUG_CPU
1075 strncpy(per_cpu(cpufreq_cpu_governor
, cpu
), data
->governor
->name
,
1079 /* if we have other CPUs still registered, we need to unlink them,
1080 * or else wait_for_completion below will lock up. Clean the
1081 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1082 * the sysfs links afterwards.
1084 if (unlikely(cpumask_weight(data
->cpus
) > 1)) {
1085 for_each_cpu(j
, data
->cpus
) {
1088 per_cpu(cpufreq_cpu_data
, j
) = NULL
;
1092 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1094 if (unlikely(cpumask_weight(data
->cpus
) > 1)) {
1095 for_each_cpu(j
, data
->cpus
) {
1098 pr_debug("removing link for cpu %u\n", j
);
1099 #ifdef CONFIG_HOTPLUG_CPU
1100 strncpy(per_cpu(cpufreq_cpu_governor
, j
),
1101 data
->governor
->name
, CPUFREQ_NAME_LEN
);
1103 cpu_dev
= get_cpu_device(j
);
1104 kobj
= &cpu_dev
->kobj
;
1105 unlock_policy_rwsem_write(cpu
);
1106 sysfs_remove_link(kobj
, "cpufreq");
1107 lock_policy_rwsem_write(cpu
);
1108 cpufreq_cpu_put(data
);
1112 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1115 if (cpufreq_driver
->target
)
1116 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
1119 cmp
= &data
->kobj_unregister
;
1120 unlock_policy_rwsem_write(cpu
);
1123 /* we need to make sure that the underlying kobj is actually
1124 * not referenced anymore by anybody before we proceed with
1127 pr_debug("waiting for dropping of refcount\n");
1128 wait_for_completion(cmp
);
1129 pr_debug("wait complete\n");
1131 lock_policy_rwsem_write(cpu
);
1132 if (cpufreq_driver
->exit
)
1133 cpufreq_driver
->exit(data
);
1134 unlock_policy_rwsem_write(cpu
);
1136 #ifdef CONFIG_HOTPLUG_CPU
1137 /* when the CPU which is the parent of the kobj is hotplugged
1138 * offline, check for siblings, and create cpufreq sysfs interface
1141 if (unlikely(cpumask_weight(data
->cpus
) > 1)) {
1142 /* first sibling now owns the new sysfs dir */
1143 cpumask_clear_cpu(cpu
, data
->cpus
);
1144 cpufreq_add_dev(get_cpu_device(cpumask_first(data
->cpus
)), NULL
);
1146 /* finally remove our own symlink */
1147 lock_policy_rwsem_write(cpu
);
1148 __cpufreq_remove_dev(dev
, sif
);
1152 free_cpumask_var(data
->related_cpus
);
1153 free_cpumask_var(data
->cpus
);
1160 static int cpufreq_remove_dev(struct device
*dev
, struct subsys_interface
*sif
)
1162 unsigned int cpu
= dev
->id
;
1165 if (cpu_is_offline(cpu
))
1168 if (unlikely(lock_policy_rwsem_write(cpu
)))
1171 retval
= __cpufreq_remove_dev(dev
, sif
);
1176 static void handle_update(struct work_struct
*work
)
1178 struct cpufreq_policy
*policy
=
1179 container_of(work
, struct cpufreq_policy
, update
);
1180 unsigned int cpu
= policy
->cpu
;
1181 pr_debug("handle_update for cpu %u called\n", cpu
);
1182 cpufreq_update_policy(cpu
);
1186 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1188 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1189 * @new_freq: CPU frequency the CPU actually runs at
1191 * We adjust to current frequency first, and need to clean up later.
1192 * So either call to cpufreq_update_policy() or schedule handle_update()).
1194 static void cpufreq_out_of_sync(unsigned int cpu
, unsigned int old_freq
,
1195 unsigned int new_freq
)
1197 struct cpufreq_freqs freqs
;
1199 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1200 "core thinks of %u, is %u kHz.\n", old_freq
, new_freq
);
1203 freqs
.old
= old_freq
;
1204 freqs
.new = new_freq
;
1205 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
1206 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
1211 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1214 * This is the last known freq, without actually getting it from the driver.
1215 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1217 unsigned int cpufreq_quick_get(unsigned int cpu
)
1219 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1220 unsigned int ret_freq
= 0;
1223 ret_freq
= policy
->cur
;
1224 cpufreq_cpu_put(policy
);
1229 EXPORT_SYMBOL(cpufreq_quick_get
);
1232 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1235 * Just return the max possible frequency for a given CPU.
1237 unsigned int cpufreq_quick_get_max(unsigned int cpu
)
1239 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1240 unsigned int ret_freq
= 0;
1243 ret_freq
= policy
->max
;
1244 cpufreq_cpu_put(policy
);
1249 EXPORT_SYMBOL(cpufreq_quick_get_max
);
1252 static unsigned int __cpufreq_get(unsigned int cpu
)
1254 struct cpufreq_policy
*policy
= per_cpu(cpufreq_cpu_data
, cpu
);
1255 unsigned int ret_freq
= 0;
1257 if (!cpufreq_driver
->get
)
1260 ret_freq
= cpufreq_driver
->get(cpu
);
1262 if (ret_freq
&& policy
->cur
&&
1263 !(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
1264 /* verify no discrepancy between actual and
1265 saved value exists */
1266 if (unlikely(ret_freq
!= policy
->cur
)) {
1267 cpufreq_out_of_sync(cpu
, policy
->cur
, ret_freq
);
1268 schedule_work(&policy
->update
);
1276 * cpufreq_get - get the current CPU frequency (in kHz)
1279 * Get the CPU current (static) CPU frequency
1281 unsigned int cpufreq_get(unsigned int cpu
)
1283 unsigned int ret_freq
= 0;
1284 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1289 if (unlikely(lock_policy_rwsem_read(cpu
)))
1292 ret_freq
= __cpufreq_get(cpu
);
1294 unlock_policy_rwsem_read(cpu
);
1297 cpufreq_cpu_put(policy
);
1301 EXPORT_SYMBOL(cpufreq_get
);
1303 static struct subsys_interface cpufreq_interface
= {
1305 .subsys
= &cpu_subsys
,
1306 .add_dev
= cpufreq_add_dev
,
1307 .remove_dev
= cpufreq_remove_dev
,
1312 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1314 * This function is only executed for the boot processor. The other CPUs
1315 * have been put offline by means of CPU hotplug.
1317 static int cpufreq_bp_suspend(void)
1321 int cpu
= smp_processor_id();
1322 struct cpufreq_policy
*cpu_policy
;
1324 pr_debug("suspending cpu %u\n", cpu
);
1326 /* If there's no policy for the boot CPU, we have nothing to do. */
1327 cpu_policy
= cpufreq_cpu_get(cpu
);
1331 if (cpufreq_driver
->suspend
) {
1332 ret
= cpufreq_driver
->suspend(cpu_policy
);
1334 printk(KERN_ERR
"cpufreq: suspend failed in ->suspend "
1335 "step on CPU %u\n", cpu_policy
->cpu
);
1338 cpufreq_cpu_put(cpu_policy
);
1343 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1345 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1346 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1347 * restored. It will verify that the current freq is in sync with
1348 * what we believe it to be. This is a bit later than when it
1349 * should be, but nonethteless it's better than calling
1350 * cpufreq_driver->get() here which might re-enable interrupts...
1352 * This function is only executed for the boot CPU. The other CPUs have not
1353 * been turned on yet.
1355 static void cpufreq_bp_resume(void)
1359 int cpu
= smp_processor_id();
1360 struct cpufreq_policy
*cpu_policy
;
1362 pr_debug("resuming cpu %u\n", cpu
);
1364 /* If there's no policy for the boot CPU, we have nothing to do. */
1365 cpu_policy
= cpufreq_cpu_get(cpu
);
1369 if (cpufreq_driver
->resume
) {
1370 ret
= cpufreq_driver
->resume(cpu_policy
);
1372 printk(KERN_ERR
"cpufreq: resume failed in ->resume "
1373 "step on CPU %u\n", cpu_policy
->cpu
);
1378 schedule_work(&cpu_policy
->update
);
1381 cpufreq_cpu_put(cpu_policy
);
1384 static struct syscore_ops cpufreq_syscore_ops
= {
1385 .suspend
= cpufreq_bp_suspend
,
1386 .resume
= cpufreq_bp_resume
,
1390 /*********************************************************************
1391 * NOTIFIER LISTS INTERFACE *
1392 *********************************************************************/
1395 * cpufreq_register_notifier - register a driver with cpufreq
1396 * @nb: notifier function to register
1397 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1399 * Add a driver to one of two lists: either a list of drivers that
1400 * are notified about clock rate changes (once before and once after
1401 * the transition), or a list of drivers that are notified about
1402 * changes in cpufreq policy.
1404 * This function may sleep, and has the same return conditions as
1405 * blocking_notifier_chain_register.
1407 int cpufreq_register_notifier(struct notifier_block
*nb
, unsigned int list
)
1411 WARN_ON(!init_cpufreq_transition_notifier_list_called
);
1414 case CPUFREQ_TRANSITION_NOTIFIER
:
1415 ret
= srcu_notifier_chain_register(
1416 &cpufreq_transition_notifier_list
, nb
);
1418 case CPUFREQ_POLICY_NOTIFIER
:
1419 ret
= blocking_notifier_chain_register(
1420 &cpufreq_policy_notifier_list
, nb
);
1428 EXPORT_SYMBOL(cpufreq_register_notifier
);
1432 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1433 * @nb: notifier block to be unregistered
1434 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1436 * Remove a driver from the CPU frequency notifier list.
1438 * This function may sleep, and has the same return conditions as
1439 * blocking_notifier_chain_unregister.
1441 int cpufreq_unregister_notifier(struct notifier_block
*nb
, unsigned int list
)
1446 case CPUFREQ_TRANSITION_NOTIFIER
:
1447 ret
= srcu_notifier_chain_unregister(
1448 &cpufreq_transition_notifier_list
, nb
);
1450 case CPUFREQ_POLICY_NOTIFIER
:
1451 ret
= blocking_notifier_chain_unregister(
1452 &cpufreq_policy_notifier_list
, nb
);
1460 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1463 /*********************************************************************
1465 *********************************************************************/
1468 int __cpufreq_driver_target(struct cpufreq_policy
*policy
,
1469 unsigned int target_freq
,
1470 unsigned int relation
)
1472 int retval
= -EINVAL
;
1473 unsigned int old_target_freq
= target_freq
;
1475 if (cpufreq_disabled())
1478 /* Make sure that target_freq is within supported range */
1479 if (target_freq
> policy
->max
)
1480 target_freq
= policy
->max
;
1481 if (target_freq
< policy
->min
)
1482 target_freq
= policy
->min
;
1484 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1485 policy
->cpu
, target_freq
, relation
, old_target_freq
);
1487 if (target_freq
== policy
->cur
)
1490 if (cpu_online(policy
->cpu
) && cpufreq_driver
->target
)
1491 retval
= cpufreq_driver
->target(policy
, target_freq
, relation
);
1495 EXPORT_SYMBOL_GPL(__cpufreq_driver_target
);
1497 int cpufreq_driver_target(struct cpufreq_policy
*policy
,
1498 unsigned int target_freq
,
1499 unsigned int relation
)
1503 policy
= cpufreq_cpu_get(policy
->cpu
);
1507 if (unlikely(lock_policy_rwsem_write(policy
->cpu
)))
1510 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
1512 unlock_policy_rwsem_write(policy
->cpu
);
1515 cpufreq_cpu_put(policy
);
1519 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
1521 int __cpufreq_driver_getavg(struct cpufreq_policy
*policy
, unsigned int cpu
)
1525 if (!(cpu_online(cpu
) && cpufreq_driver
->getavg
))
1528 policy
= cpufreq_cpu_get(policy
->cpu
);
1532 ret
= cpufreq_driver
->getavg(policy
, cpu
);
1534 cpufreq_cpu_put(policy
);
1537 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg
);
1540 * when "event" is CPUFREQ_GOV_LIMITS
1543 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
1548 /* Only must be defined when default governor is known to have latency
1549 restrictions, like e.g. conservative or ondemand.
1550 That this is the case is already ensured in Kconfig
1552 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1553 struct cpufreq_governor
*gov
= &cpufreq_gov_performance
;
1555 struct cpufreq_governor
*gov
= NULL
;
1558 if (policy
->governor
->max_transition_latency
&&
1559 policy
->cpuinfo
.transition_latency
>
1560 policy
->governor
->max_transition_latency
) {
1564 printk(KERN_WARNING
"%s governor failed, too long"
1565 " transition latency of HW, fallback"
1566 " to %s governor\n",
1567 policy
->governor
->name
,
1569 policy
->governor
= gov
;
1573 if (!try_module_get(policy
->governor
->owner
))
1576 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1577 policy
->cpu
, event
);
1578 ret
= policy
->governor
->governor(policy
, event
);
1580 /* we keep one module reference alive for
1581 each CPU governed by this CPU */
1582 if ((event
!= CPUFREQ_GOV_START
) || ret
)
1583 module_put(policy
->governor
->owner
);
1584 if ((event
== CPUFREQ_GOV_STOP
) && !ret
)
1585 module_put(policy
->governor
->owner
);
1591 int cpufreq_register_governor(struct cpufreq_governor
*governor
)
1598 if (cpufreq_disabled())
1601 mutex_lock(&cpufreq_governor_mutex
);
1604 if (__find_governor(governor
->name
) == NULL
) {
1606 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
1609 mutex_unlock(&cpufreq_governor_mutex
);
1612 EXPORT_SYMBOL_GPL(cpufreq_register_governor
);
1615 void cpufreq_unregister_governor(struct cpufreq_governor
*governor
)
1617 #ifdef CONFIG_HOTPLUG_CPU
1624 if (cpufreq_disabled())
1627 #ifdef CONFIG_HOTPLUG_CPU
1628 for_each_present_cpu(cpu
) {
1629 if (cpu_online(cpu
))
1631 if (!strcmp(per_cpu(cpufreq_cpu_governor
, cpu
), governor
->name
))
1632 strcpy(per_cpu(cpufreq_cpu_governor
, cpu
), "\0");
1636 mutex_lock(&cpufreq_governor_mutex
);
1637 list_del(&governor
->governor_list
);
1638 mutex_unlock(&cpufreq_governor_mutex
);
1641 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor
);
1645 /*********************************************************************
1646 * POLICY INTERFACE *
1647 *********************************************************************/
1650 * cpufreq_get_policy - get the current cpufreq_policy
1651 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1654 * Reads the current cpufreq policy.
1656 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
1658 struct cpufreq_policy
*cpu_policy
;
1662 cpu_policy
= cpufreq_cpu_get(cpu
);
1666 memcpy(policy
, cpu_policy
, sizeof(struct cpufreq_policy
));
1668 cpufreq_cpu_put(cpu_policy
);
1671 EXPORT_SYMBOL(cpufreq_get_policy
);
1675 * data : current policy.
1676 * policy : policy to be set.
1678 static int __cpufreq_set_policy(struct cpufreq_policy
*data
,
1679 struct cpufreq_policy
*policy
)
1683 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy
->cpu
,
1684 policy
->min
, policy
->max
);
1686 memcpy(&policy
->cpuinfo
, &data
->cpuinfo
,
1687 sizeof(struct cpufreq_cpuinfo
));
1689 if (policy
->min
> data
->max
|| policy
->max
< data
->min
) {
1694 /* verify the cpu speed can be set within this limit */
1695 ret
= cpufreq_driver
->verify(policy
);
1699 /* adjust if necessary - all reasons */
1700 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1701 CPUFREQ_ADJUST
, policy
);
1703 /* adjust if necessary - hardware incompatibility*/
1704 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1705 CPUFREQ_INCOMPATIBLE
, policy
);
1707 /* verify the cpu speed can be set within this limit,
1708 which might be different to the first one */
1709 ret
= cpufreq_driver
->verify(policy
);
1713 /* notification of the new policy */
1714 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1715 CPUFREQ_NOTIFY
, policy
);
1717 data
->min
= policy
->min
;
1718 data
->max
= policy
->max
;
1720 pr_debug("new min and max freqs are %u - %u kHz\n",
1721 data
->min
, data
->max
);
1723 if (cpufreq_driver
->setpolicy
) {
1724 data
->policy
= policy
->policy
;
1725 pr_debug("setting range\n");
1726 ret
= cpufreq_driver
->setpolicy(policy
);
1728 if (policy
->governor
!= data
->governor
) {
1729 /* save old, working values */
1730 struct cpufreq_governor
*old_gov
= data
->governor
;
1732 pr_debug("governor switch\n");
1734 /* end old governor */
1736 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
1738 /* start new governor */
1739 data
->governor
= policy
->governor
;
1740 if (__cpufreq_governor(data
, CPUFREQ_GOV_START
)) {
1741 /* new governor failed, so re-start old one */
1742 pr_debug("starting governor %s failed\n",
1743 data
->governor
->name
);
1745 data
->governor
= old_gov
;
1746 __cpufreq_governor(data
,
1752 /* might be a policy change, too, so fall through */
1754 pr_debug("governor: change or update limits\n");
1755 __cpufreq_governor(data
, CPUFREQ_GOV_LIMITS
);
1763 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1764 * @cpu: CPU which shall be re-evaluated
1766 * Useful for policy notifiers which have different necessities
1767 * at different times.
1769 int cpufreq_update_policy(unsigned int cpu
)
1771 struct cpufreq_policy
*data
= cpufreq_cpu_get(cpu
);
1772 struct cpufreq_policy policy
;
1780 if (unlikely(lock_policy_rwsem_write(cpu
))) {
1785 pr_debug("updating policy for CPU %u\n", cpu
);
1786 memcpy(&policy
, data
, sizeof(struct cpufreq_policy
));
1787 policy
.min
= data
->user_policy
.min
;
1788 policy
.max
= data
->user_policy
.max
;
1789 policy
.policy
= data
->user_policy
.policy
;
1790 policy
.governor
= data
->user_policy
.governor
;
1792 /* BIOS might change freq behind our back
1793 -> ask driver for current freq and notify governors about a change */
1794 if (cpufreq_driver
->get
) {
1795 policy
.cur
= cpufreq_driver
->get(cpu
);
1797 pr_debug("Driver did not initialize current freq");
1798 data
->cur
= policy
.cur
;
1800 if (data
->cur
!= policy
.cur
)
1801 cpufreq_out_of_sync(cpu
, data
->cur
,
1806 ret
= __cpufreq_set_policy(data
, &policy
);
1808 unlock_policy_rwsem_write(cpu
);
1811 cpufreq_cpu_put(data
);
1815 EXPORT_SYMBOL(cpufreq_update_policy
);
1817 static int __cpuinit
cpufreq_cpu_callback(struct notifier_block
*nfb
,
1818 unsigned long action
, void *hcpu
)
1820 unsigned int cpu
= (unsigned long)hcpu
;
1823 dev
= get_cpu_device(cpu
);
1827 case CPU_ONLINE_FROZEN
:
1828 cpufreq_add_dev(dev
, NULL
);
1830 case CPU_DOWN_PREPARE
:
1831 case CPU_DOWN_PREPARE_FROZEN
:
1832 if (unlikely(lock_policy_rwsem_write(cpu
)))
1835 __cpufreq_remove_dev(dev
, NULL
);
1837 case CPU_DOWN_FAILED
:
1838 case CPU_DOWN_FAILED_FROZEN
:
1839 cpufreq_add_dev(dev
, NULL
);
1846 static struct notifier_block __refdata cpufreq_cpu_notifier
= {
1847 .notifier_call
= cpufreq_cpu_callback
,
1850 /*********************************************************************
1851 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1852 *********************************************************************/
1855 * cpufreq_register_driver - register a CPU Frequency driver
1856 * @driver_data: A struct cpufreq_driver containing the values#
1857 * submitted by the CPU Frequency driver.
1859 * Registers a CPU Frequency driver to this core code. This code
1860 * returns zero on success, -EBUSY when another driver got here first
1861 * (and isn't unregistered in the meantime).
1864 int cpufreq_register_driver(struct cpufreq_driver
*driver_data
)
1866 unsigned long flags
;
1869 if (cpufreq_disabled())
1872 if (!driver_data
|| !driver_data
->verify
|| !driver_data
->init
||
1873 ((!driver_data
->setpolicy
) && (!driver_data
->target
)))
1876 pr_debug("trying to register driver %s\n", driver_data
->name
);
1878 if (driver_data
->setpolicy
)
1879 driver_data
->flags
|= CPUFREQ_CONST_LOOPS
;
1881 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1882 if (cpufreq_driver
) {
1883 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1886 cpufreq_driver
= driver_data
;
1887 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1889 ret
= subsys_interface_register(&cpufreq_interface
);
1891 goto err_null_driver
;
1893 if (!(cpufreq_driver
->flags
& CPUFREQ_STICKY
)) {
1897 /* check for at least one working CPU */
1898 for (i
= 0; i
< nr_cpu_ids
; i
++)
1899 if (cpu_possible(i
) && per_cpu(cpufreq_cpu_data
, i
)) {
1904 /* if all ->init() calls failed, unregister */
1906 pr_debug("no CPU initialized for driver %s\n",
1912 register_hotcpu_notifier(&cpufreq_cpu_notifier
);
1913 pr_debug("driver %s up and running\n", driver_data
->name
);
1917 subsys_interface_unregister(&cpufreq_interface
);
1919 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1920 cpufreq_driver
= NULL
;
1921 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1924 EXPORT_SYMBOL_GPL(cpufreq_register_driver
);
1928 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1930 * Unregister the current CPUFreq driver. Only call this if you have
1931 * the right to do so, i.e. if you have succeeded in initialising before!
1932 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1933 * currently not initialised.
1935 int cpufreq_unregister_driver(struct cpufreq_driver
*driver
)
1937 unsigned long flags
;
1939 if (!cpufreq_driver
|| (driver
!= cpufreq_driver
))
1942 pr_debug("unregistering driver %s\n", driver
->name
);
1944 subsys_interface_unregister(&cpufreq_interface
);
1945 unregister_hotcpu_notifier(&cpufreq_cpu_notifier
);
1947 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1948 cpufreq_driver
= NULL
;
1949 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1953 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver
);
1955 static int __init
cpufreq_core_init(void)
1959 if (cpufreq_disabled())
1962 for_each_possible_cpu(cpu
) {
1963 per_cpu(cpufreq_policy_cpu
, cpu
) = -1;
1964 init_rwsem(&per_cpu(cpu_policy_rwsem
, cpu
));
1967 cpufreq_global_kobject
= kobject_create_and_add("cpufreq", &cpu_subsys
.dev_root
->kobj
);
1968 BUG_ON(!cpufreq_global_kobject
);
1969 register_syscore_ops(&cpufreq_syscore_ops
);
1973 core_initcall(cpufreq_core_init
);