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 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/notifier.h>
22 #include <linux/cpufreq.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/completion.h>
30 #include <linux/mutex.h>
32 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
36 * The "cpufreq driver" - the arch- or hardware-dependent low
37 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
40 static struct cpufreq_driver
*cpufreq_driver
;
41 static struct cpufreq_policy
*cpufreq_cpu_data
[NR_CPUS
];
42 static DEFINE_SPINLOCK(cpufreq_driver_lock
);
45 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
46 * all cpufreq/hotplug/workqueue/etc related lock issues.
48 * The rules for this semaphore:
49 * - Any routine that wants to read from the policy structure will
50 * do a down_read on this semaphore.
51 * - Any routine that will write to the policy structure and/or may take away
52 * the policy altogether (eg. CPU hotplug), will hold this lock in write
53 * mode before doing so.
56 * - All holders of the lock should check to make sure that the CPU they
57 * are concerned with are online after they get the lock.
58 * - Governor routines that can be called in cpufreq hotplug path should not
59 * take this sem as top level hotplug notifier handler takes this.
61 static DEFINE_PER_CPU(int, policy_cpu
);
62 static DEFINE_PER_CPU(struct rw_semaphore
, cpu_policy_rwsem
);
64 #define lock_policy_rwsem(mode, cpu) \
65 int lock_policy_rwsem_##mode \
68 int policy_cpu = per_cpu(policy_cpu, cpu); \
69 BUG_ON(policy_cpu == -1); \
70 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
71 if (unlikely(!cpu_online(cpu))) { \
72 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 lock_policy_rwsem(read
, cpu
);
80 EXPORT_SYMBOL_GPL(lock_policy_rwsem_read
);
82 lock_policy_rwsem(write
, cpu
);
83 EXPORT_SYMBOL_GPL(lock_policy_rwsem_write
);
85 void unlock_policy_rwsem_read(int cpu
)
87 int policy_cpu
= per_cpu(policy_cpu
, cpu
);
88 BUG_ON(policy_cpu
== -1);
89 up_read(&per_cpu(cpu_policy_rwsem
, policy_cpu
));
91 EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read
);
93 void unlock_policy_rwsem_write(int cpu
)
95 int policy_cpu
= per_cpu(policy_cpu
, cpu
);
96 BUG_ON(policy_cpu
== -1);
97 up_write(&per_cpu(cpu_policy_rwsem
, policy_cpu
));
99 EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write
);
102 /* internal prototypes */
103 static int __cpufreq_governor(struct cpufreq_policy
*policy
, unsigned int event
);
104 static unsigned int __cpufreq_get(unsigned int cpu
);
105 static void handle_update(struct work_struct
*work
);
108 * Two notifier lists: the "policy" list is involved in the
109 * validation process for a new CPU frequency policy; the
110 * "transition" list for kernel code that needs to handle
111 * changes to devices when the CPU clock speed changes.
112 * The mutex locks both lists.
114 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list
);
115 static struct srcu_notifier_head cpufreq_transition_notifier_list
;
117 static int __init
init_cpufreq_transition_notifier_list(void)
119 srcu_init_notifier_head(&cpufreq_transition_notifier_list
);
122 pure_initcall(init_cpufreq_transition_notifier_list
);
124 static LIST_HEAD(cpufreq_governor_list
);
125 static DEFINE_MUTEX (cpufreq_governor_mutex
);
127 struct cpufreq_policy
*cpufreq_cpu_get(unsigned int cpu
)
129 struct cpufreq_policy
*data
;
135 /* get the cpufreq driver */
136 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
141 if (!try_module_get(cpufreq_driver
->owner
))
146 data
= cpufreq_cpu_data
[cpu
];
149 goto err_out_put_module
;
151 if (!kobject_get(&data
->kobj
))
152 goto err_out_put_module
;
154 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
158 module_put(cpufreq_driver
->owner
);
160 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
164 EXPORT_SYMBOL_GPL(cpufreq_cpu_get
);
167 void cpufreq_cpu_put(struct cpufreq_policy
*data
)
169 kobject_put(&data
->kobj
);
170 module_put(cpufreq_driver
->owner
);
172 EXPORT_SYMBOL_GPL(cpufreq_cpu_put
);
175 /*********************************************************************
176 * UNIFIED DEBUG HELPERS *
177 *********************************************************************/
178 #ifdef CONFIG_CPU_FREQ_DEBUG
180 /* what part(s) of the CPUfreq subsystem are debugged? */
181 static unsigned int debug
;
183 /* is the debug output ratelimit'ed using printk_ratelimit? User can
184 * set or modify this value.
186 static unsigned int debug_ratelimit
= 1;
188 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
189 * loading of a cpufreq driver, temporarily disabled when a new policy
190 * is set, and disabled upon cpufreq driver removal
192 static unsigned int disable_ratelimit
= 1;
193 static DEFINE_SPINLOCK(disable_ratelimit_lock
);
195 static void cpufreq_debug_enable_ratelimit(void)
199 spin_lock_irqsave(&disable_ratelimit_lock
, flags
);
200 if (disable_ratelimit
)
202 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
205 static void cpufreq_debug_disable_ratelimit(void)
209 spin_lock_irqsave(&disable_ratelimit_lock
, flags
);
211 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
214 void cpufreq_debug_printk(unsigned int type
, const char *prefix
,
215 const char *fmt
, ...)
224 spin_lock_irqsave(&disable_ratelimit_lock
, flags
);
225 if (!disable_ratelimit
&& debug_ratelimit
226 && !printk_ratelimit()) {
227 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
230 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
232 len
= snprintf(s
, 256, KERN_DEBUG
"%s: ", prefix
);
235 len
+= vsnprintf(&s
[len
], (256 - len
), fmt
, args
);
243 EXPORT_SYMBOL(cpufreq_debug_printk
);
246 module_param(debug
, uint
, 0644);
247 MODULE_PARM_DESC(debug
, "CPUfreq debugging: add 1 to debug core,"
248 " 2 to debug drivers, and 4 to debug governors.");
250 module_param(debug_ratelimit
, uint
, 0644);
251 MODULE_PARM_DESC(debug_ratelimit
, "CPUfreq debugging:"
252 " set to 0 to disable ratelimiting.");
254 #else /* !CONFIG_CPU_FREQ_DEBUG */
256 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
257 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
259 #endif /* CONFIG_CPU_FREQ_DEBUG */
262 /*********************************************************************
263 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
264 *********************************************************************/
267 * adjust_jiffies - adjust the system "loops_per_jiffy"
269 * This function alters the system "loops_per_jiffy" for the clock
270 * speed change. Note that loops_per_jiffy cannot be updated on SMP
271 * systems as each CPU might be scaled differently. So, use the arch
272 * per-CPU loops_per_jiffy value wherever possible.
275 static unsigned long l_p_j_ref
;
276 static unsigned int l_p_j_ref_freq
;
278 static void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
280 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
283 if (!l_p_j_ref_freq
) {
284 l_p_j_ref
= loops_per_jiffy
;
285 l_p_j_ref_freq
= ci
->old
;
286 dprintk("saving %lu as reference value for loops_per_jiffy;"
287 "freq is %u kHz\n", l_p_j_ref
, l_p_j_ref_freq
);
289 if ((val
== CPUFREQ_PRECHANGE
&& ci
->old
< ci
->new) ||
290 (val
== CPUFREQ_POSTCHANGE
&& ci
->old
> ci
->new) ||
291 (val
== CPUFREQ_RESUMECHANGE
|| val
== CPUFREQ_SUSPENDCHANGE
)) {
292 loops_per_jiffy
= cpufreq_scale(l_p_j_ref
, l_p_j_ref_freq
,
294 dprintk("scaling loops_per_jiffy to %lu"
295 "for frequency %u kHz\n", loops_per_jiffy
, ci
->new);
299 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
307 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
308 * on frequency transition.
310 * This function calls the transition notifiers and the "adjust_jiffies"
311 * function. It is called twice on all CPU frequency changes that have
314 void cpufreq_notify_transition(struct cpufreq_freqs
*freqs
, unsigned int state
)
316 struct cpufreq_policy
*policy
;
318 BUG_ON(irqs_disabled());
320 freqs
->flags
= cpufreq_driver
->flags
;
321 dprintk("notification %u of frequency transition to %u kHz\n",
324 policy
= cpufreq_cpu_data
[freqs
->cpu
];
327 case CPUFREQ_PRECHANGE
:
328 /* detect if the driver reported a value as "old frequency"
329 * which is not equal to what the cpufreq core thinks is
332 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
333 if ((policy
) && (policy
->cpu
== freqs
->cpu
) &&
334 (policy
->cur
) && (policy
->cur
!= freqs
->old
)) {
335 dprintk("Warning: CPU frequency is"
336 " %u, cpufreq assumed %u kHz.\n",
337 freqs
->old
, policy
->cur
);
338 freqs
->old
= policy
->cur
;
341 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
342 CPUFREQ_PRECHANGE
, freqs
);
343 adjust_jiffies(CPUFREQ_PRECHANGE
, freqs
);
346 case CPUFREQ_POSTCHANGE
:
347 adjust_jiffies(CPUFREQ_POSTCHANGE
, freqs
);
348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
349 CPUFREQ_POSTCHANGE
, freqs
);
350 if (likely(policy
) && likely(policy
->cpu
== freqs
->cpu
))
351 policy
->cur
= freqs
->new;
355 EXPORT_SYMBOL_GPL(cpufreq_notify_transition
);
359 /*********************************************************************
361 *********************************************************************/
363 static struct cpufreq_governor
*__find_governor(const char *str_governor
)
365 struct cpufreq_governor
*t
;
367 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
)
368 if (!strnicmp(str_governor
,t
->name
,CPUFREQ_NAME_LEN
))
375 * cpufreq_parse_governor - parse a governor string
377 static int cpufreq_parse_governor (char *str_governor
, unsigned int *policy
,
378 struct cpufreq_governor
**governor
)
385 if (cpufreq_driver
->setpolicy
) {
386 if (!strnicmp(str_governor
, "performance", CPUFREQ_NAME_LEN
)) {
387 *policy
= CPUFREQ_POLICY_PERFORMANCE
;
389 } else if (!strnicmp(str_governor
, "powersave",
391 *policy
= CPUFREQ_POLICY_POWERSAVE
;
394 } else if (cpufreq_driver
->target
) {
395 struct cpufreq_governor
*t
;
397 mutex_lock(&cpufreq_governor_mutex
);
399 t
= __find_governor(str_governor
);
402 char *name
= kasprintf(GFP_KERNEL
, "cpufreq_%s",
408 mutex_unlock(&cpufreq_governor_mutex
);
409 ret
= request_module(name
);
410 mutex_lock(&cpufreq_governor_mutex
);
413 t
= __find_governor(str_governor
);
424 mutex_unlock(&cpufreq_governor_mutex
);
431 /* drivers/base/cpu.c */
432 extern struct sysdev_class cpu_sysdev_class
;
436 * cpufreq_per_cpu_attr_read() / show_##file_name() -
437 * print out cpufreq information
439 * Write out information from cpufreq_driver->policy[cpu]; object must be
443 #define show_one(file_name, object) \
444 static ssize_t show_##file_name \
445 (struct cpufreq_policy * policy, char *buf) \
447 return sprintf (buf, "%u\n", policy->object); \
450 show_one(cpuinfo_min_freq
, cpuinfo
.min_freq
);
451 show_one(cpuinfo_max_freq
, cpuinfo
.max_freq
);
452 show_one(scaling_min_freq
, min
);
453 show_one(scaling_max_freq
, max
);
454 show_one(scaling_cur_freq
, cur
);
456 static int __cpufreq_set_policy(struct cpufreq_policy
*data
,
457 struct cpufreq_policy
*policy
);
460 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
462 #define store_one(file_name, object) \
463 static ssize_t store_##file_name \
464 (struct cpufreq_policy * policy, const char *buf, size_t count) \
466 unsigned int ret = -EINVAL; \
467 struct cpufreq_policy new_policy; \
469 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
473 ret = sscanf (buf, "%u", &new_policy.object); \
477 ret = __cpufreq_set_policy(policy, &new_policy); \
478 policy->user_policy.object = policy->object; \
480 return ret ? ret : count; \
483 store_one(scaling_min_freq
,min
);
484 store_one(scaling_max_freq
,max
);
487 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
489 static ssize_t
show_cpuinfo_cur_freq (struct cpufreq_policy
* policy
,
492 unsigned int cur_freq
= __cpufreq_get(policy
->cpu
);
494 return sprintf(buf
, "<unknown>");
495 return sprintf(buf
, "%u\n", cur_freq
);
500 * show_scaling_governor - show the current policy for the specified CPU
502 static ssize_t
show_scaling_governor (struct cpufreq_policy
* policy
,
505 if(policy
->policy
== CPUFREQ_POLICY_POWERSAVE
)
506 return sprintf(buf
, "powersave\n");
507 else if (policy
->policy
== CPUFREQ_POLICY_PERFORMANCE
)
508 return sprintf(buf
, "performance\n");
509 else if (policy
->governor
)
510 return scnprintf(buf
, CPUFREQ_NAME_LEN
, "%s\n", policy
->governor
->name
);
516 * store_scaling_governor - store policy for the specified CPU
518 static ssize_t
store_scaling_governor (struct cpufreq_policy
* policy
,
519 const char *buf
, size_t count
)
521 unsigned int ret
= -EINVAL
;
522 char str_governor
[16];
523 struct cpufreq_policy new_policy
;
525 ret
= cpufreq_get_policy(&new_policy
, policy
->cpu
);
529 ret
= sscanf (buf
, "%15s", str_governor
);
533 if (cpufreq_parse_governor(str_governor
, &new_policy
.policy
,
534 &new_policy
.governor
))
537 /* Do not use cpufreq_set_policy here or the user_policy.max
538 will be wrongly overridden */
539 ret
= __cpufreq_set_policy(policy
, &new_policy
);
541 policy
->user_policy
.policy
= policy
->policy
;
542 policy
->user_policy
.governor
= policy
->governor
;
551 * show_scaling_driver - show the cpufreq driver currently loaded
553 static ssize_t
show_scaling_driver (struct cpufreq_policy
* policy
, char *buf
)
555 return scnprintf(buf
, CPUFREQ_NAME_LEN
, "%s\n", cpufreq_driver
->name
);
559 * show_scaling_available_governors - show the available CPUfreq governors
561 static ssize_t
show_scaling_available_governors (struct cpufreq_policy
*policy
,
565 struct cpufreq_governor
*t
;
567 if (!cpufreq_driver
->target
) {
568 i
+= sprintf(buf
, "performance powersave");
572 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
573 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char)) - (CPUFREQ_NAME_LEN
+ 2)))
575 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_LEN
, "%s ", t
->name
);
578 i
+= sprintf(&buf
[i
], "\n");
582 * show_affected_cpus - show the CPUs affected by each transition
584 static ssize_t
show_affected_cpus (struct cpufreq_policy
* policy
, char *buf
)
589 for_each_cpu_mask(cpu
, policy
->cpus
) {
591 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), " ");
592 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), "%u", cpu
);
593 if (i
>= (PAGE_SIZE
- 5))
596 i
+= sprintf(&buf
[i
], "\n");
601 #define define_one_ro(_name) \
602 static struct freq_attr _name = \
603 __ATTR(_name, 0444, show_##_name, NULL)
605 #define define_one_ro0400(_name) \
606 static struct freq_attr _name = \
607 __ATTR(_name, 0400, show_##_name, NULL)
609 #define define_one_rw(_name) \
610 static struct freq_attr _name = \
611 __ATTR(_name, 0644, show_##_name, store_##_name)
613 define_one_ro0400(cpuinfo_cur_freq
);
614 define_one_ro(cpuinfo_min_freq
);
615 define_one_ro(cpuinfo_max_freq
);
616 define_one_ro(scaling_available_governors
);
617 define_one_ro(scaling_driver
);
618 define_one_ro(scaling_cur_freq
);
619 define_one_ro(affected_cpus
);
620 define_one_rw(scaling_min_freq
);
621 define_one_rw(scaling_max_freq
);
622 define_one_rw(scaling_governor
);
624 static struct attribute
* default_attrs
[] = {
625 &cpuinfo_min_freq
.attr
,
626 &cpuinfo_max_freq
.attr
,
627 &scaling_min_freq
.attr
,
628 &scaling_max_freq
.attr
,
630 &scaling_governor
.attr
,
631 &scaling_driver
.attr
,
632 &scaling_available_governors
.attr
,
636 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
637 #define to_attr(a) container_of(a,struct freq_attr,attr)
639 static ssize_t
show(struct kobject
* kobj
, struct attribute
* attr
,char * buf
)
641 struct cpufreq_policy
* policy
= to_policy(kobj
);
642 struct freq_attr
* fattr
= to_attr(attr
);
644 policy
= cpufreq_cpu_get(policy
->cpu
);
648 if (lock_policy_rwsem_read(policy
->cpu
) < 0)
652 ret
= fattr
->show(policy
, buf
);
656 unlock_policy_rwsem_read(policy
->cpu
);
658 cpufreq_cpu_put(policy
);
662 static ssize_t
store(struct kobject
* kobj
, struct attribute
* attr
,
663 const char * buf
, size_t count
)
665 struct cpufreq_policy
* policy
= to_policy(kobj
);
666 struct freq_attr
* fattr
= to_attr(attr
);
668 policy
= cpufreq_cpu_get(policy
->cpu
);
672 if (lock_policy_rwsem_write(policy
->cpu
) < 0)
676 ret
= fattr
->store(policy
, buf
, count
);
680 unlock_policy_rwsem_write(policy
->cpu
);
682 cpufreq_cpu_put(policy
);
686 static void cpufreq_sysfs_release(struct kobject
* kobj
)
688 struct cpufreq_policy
* policy
= to_policy(kobj
);
689 dprintk("last reference is dropped\n");
690 complete(&policy
->kobj_unregister
);
693 static struct sysfs_ops sysfs_ops
= {
698 static struct kobj_type ktype_cpufreq
= {
699 .sysfs_ops
= &sysfs_ops
,
700 .default_attrs
= default_attrs
,
701 .release
= cpufreq_sysfs_release
,
706 * cpufreq_add_dev - add a CPU device
708 * Adds the cpufreq interface for a CPU device.
710 static int cpufreq_add_dev (struct sys_device
* sys_dev
)
712 unsigned int cpu
= sys_dev
->id
;
714 struct cpufreq_policy new_policy
;
715 struct cpufreq_policy
*policy
;
716 struct freq_attr
**drv_attr
;
717 struct sys_device
*cpu_sys_dev
;
721 struct cpufreq_policy
*managed_policy
;
724 if (cpu_is_offline(cpu
))
727 cpufreq_debug_disable_ratelimit();
728 dprintk("adding CPU %u\n", cpu
);
731 /* check whether a different CPU already registered this
732 * CPU because it is in the same boat. */
733 policy
= cpufreq_cpu_get(cpu
);
734 if (unlikely(policy
)) {
735 cpufreq_cpu_put(policy
);
736 cpufreq_debug_enable_ratelimit();
741 if (!try_module_get(cpufreq_driver
->owner
)) {
746 policy
= kzalloc(sizeof(struct cpufreq_policy
), GFP_KERNEL
);
753 policy
->cpus
= cpumask_of_cpu(cpu
);
755 /* Initially set CPU itself as the policy_cpu */
756 per_cpu(policy_cpu
, cpu
) = cpu
;
757 lock_policy_rwsem_write(cpu
);
759 init_completion(&policy
->kobj_unregister
);
760 INIT_WORK(&policy
->update
, handle_update
);
762 /* call driver. From then on the cpufreq must be able
763 * to accept all calls to ->verify and ->setpolicy for this CPU
765 ret
= cpufreq_driver
->init(policy
);
767 dprintk("initialization failed\n");
768 unlock_policy_rwsem_write(cpu
);
771 policy
->user_policy
.min
= policy
->cpuinfo
.min_freq
;
772 policy
->user_policy
.max
= policy
->cpuinfo
.max_freq
;
773 policy
->user_policy
.governor
= policy
->governor
;
776 for_each_cpu_mask(j
, policy
->cpus
) {
780 /* check for existing affected CPUs. They may not be aware
781 * of it due to CPU Hotplug.
783 managed_policy
= cpufreq_cpu_get(j
);
784 if (unlikely(managed_policy
)) {
786 /* Set proper policy_cpu */
787 unlock_policy_rwsem_write(cpu
);
788 per_cpu(policy_cpu
, cpu
) = managed_policy
->cpu
;
790 if (lock_policy_rwsem_write(cpu
) < 0)
791 goto err_out_driver_exit
;
793 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
794 managed_policy
->cpus
= policy
->cpus
;
795 cpufreq_cpu_data
[cpu
] = managed_policy
;
796 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
798 dprintk("CPU already managed, adding link\n");
799 ret
= sysfs_create_link(&sys_dev
->kobj
,
800 &managed_policy
->kobj
,
803 unlock_policy_rwsem_write(cpu
);
804 goto err_out_driver_exit
;
807 cpufreq_debug_enable_ratelimit();
809 unlock_policy_rwsem_write(cpu
);
810 goto err_out_driver_exit
; /* call driver->exit() */
814 memcpy(&new_policy
, policy
, sizeof(struct cpufreq_policy
));
816 /* prepare interface data */
817 policy
->kobj
.parent
= &sys_dev
->kobj
;
818 policy
->kobj
.ktype
= &ktype_cpufreq
;
819 strlcpy(policy
->kobj
.name
, "cpufreq", KOBJ_NAME_LEN
);
821 ret
= kobject_register(&policy
->kobj
);
823 unlock_policy_rwsem_write(cpu
);
824 goto err_out_driver_exit
;
826 /* set up files for this cpu device */
827 drv_attr
= cpufreq_driver
->attr
;
828 while ((drv_attr
) && (*drv_attr
)) {
829 sysfs_create_file(&policy
->kobj
, &((*drv_attr
)->attr
));
832 if (cpufreq_driver
->get
)
833 sysfs_create_file(&policy
->kobj
, &cpuinfo_cur_freq
.attr
);
834 if (cpufreq_driver
->target
)
835 sysfs_create_file(&policy
->kobj
, &scaling_cur_freq
.attr
);
837 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
838 for_each_cpu_mask(j
, policy
->cpus
) {
839 cpufreq_cpu_data
[j
] = policy
;
840 per_cpu(policy_cpu
, j
) = policy
->cpu
;
842 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
844 /* symlink affected CPUs */
845 for_each_cpu_mask(j
, policy
->cpus
) {
851 dprintk("CPU %u already managed, adding link\n", j
);
852 cpufreq_cpu_get(cpu
);
853 cpu_sys_dev
= get_cpu_sysdev(j
);
854 ret
= sysfs_create_link(&cpu_sys_dev
->kobj
, &policy
->kobj
,
857 unlock_policy_rwsem_write(cpu
);
858 goto err_out_unregister
;
862 policy
->governor
= NULL
; /* to assure that the starting sequence is
863 * run in cpufreq_set_policy */
865 /* set default policy */
866 ret
= __cpufreq_set_policy(policy
, &new_policy
);
867 policy
->user_policy
.policy
= policy
->policy
;
869 unlock_policy_rwsem_write(cpu
);
872 dprintk("setting policy failed\n");
873 goto err_out_unregister
;
876 module_put(cpufreq_driver
->owner
);
877 dprintk("initialization complete\n");
878 cpufreq_debug_enable_ratelimit();
884 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
885 for_each_cpu_mask(j
, policy
->cpus
)
886 cpufreq_cpu_data
[j
] = NULL
;
887 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
889 kobject_unregister(&policy
->kobj
);
890 wait_for_completion(&policy
->kobj_unregister
);
893 if (cpufreq_driver
->exit
)
894 cpufreq_driver
->exit(policy
);
900 module_put(cpufreq_driver
->owner
);
902 cpufreq_debug_enable_ratelimit();
908 * __cpufreq_remove_dev - remove a CPU device
910 * Removes the cpufreq interface for a CPU device.
911 * Caller should already have policy_rwsem in write mode for this CPU.
912 * This routine frees the rwsem before returning.
914 static int __cpufreq_remove_dev (struct sys_device
* sys_dev
)
916 unsigned int cpu
= sys_dev
->id
;
918 struct cpufreq_policy
*data
;
920 struct sys_device
*cpu_sys_dev
;
924 cpufreq_debug_disable_ratelimit();
925 dprintk("unregistering CPU %u\n", cpu
);
927 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
928 data
= cpufreq_cpu_data
[cpu
];
931 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
932 cpufreq_debug_enable_ratelimit();
933 unlock_policy_rwsem_write(cpu
);
936 cpufreq_cpu_data
[cpu
] = NULL
;
940 /* if this isn't the CPU which is the parent of the kobj, we
941 * only need to unlink, put and exit
943 if (unlikely(cpu
!= data
->cpu
)) {
944 dprintk("removing link\n");
945 cpu_clear(cpu
, data
->cpus
);
946 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
947 sysfs_remove_link(&sys_dev
->kobj
, "cpufreq");
948 cpufreq_cpu_put(data
);
949 cpufreq_debug_enable_ratelimit();
950 unlock_policy_rwsem_write(cpu
);
956 if (!kobject_get(&data
->kobj
)) {
957 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
958 cpufreq_debug_enable_ratelimit();
959 unlock_policy_rwsem_write(cpu
);
964 /* if we have other CPUs still registered, we need to unlink them,
965 * or else wait_for_completion below will lock up. Clean the
966 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
969 if (unlikely(cpus_weight(data
->cpus
) > 1)) {
970 for_each_cpu_mask(j
, data
->cpus
) {
973 cpufreq_cpu_data
[j
] = NULL
;
977 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
979 if (unlikely(cpus_weight(data
->cpus
) > 1)) {
980 for_each_cpu_mask(j
, data
->cpus
) {
983 dprintk("removing link for cpu %u\n", j
);
984 cpu_sys_dev
= get_cpu_sysdev(j
);
985 sysfs_remove_link(&cpu_sys_dev
->kobj
, "cpufreq");
986 cpufreq_cpu_put(data
);
990 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
993 if (cpufreq_driver
->target
)
994 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
996 unlock_policy_rwsem_write(cpu
);
998 kobject_unregister(&data
->kobj
);
1000 kobject_put(&data
->kobj
);
1002 /* we need to make sure that the underlying kobj is actually
1003 * not referenced anymore by anybody before we proceed with
1006 dprintk("waiting for dropping of refcount\n");
1007 wait_for_completion(&data
->kobj_unregister
);
1008 dprintk("wait complete\n");
1010 if (cpufreq_driver
->exit
)
1011 cpufreq_driver
->exit(data
);
1015 cpufreq_debug_enable_ratelimit();
1020 static int cpufreq_remove_dev (struct sys_device
* sys_dev
)
1022 unsigned int cpu
= sys_dev
->id
;
1025 if (cpu_is_offline(cpu
))
1028 if (unlikely(lock_policy_rwsem_write(cpu
)))
1031 retval
= __cpufreq_remove_dev(sys_dev
);
1036 static void handle_update(struct work_struct
*work
)
1038 struct cpufreq_policy
*policy
=
1039 container_of(work
, struct cpufreq_policy
, update
);
1040 unsigned int cpu
= policy
->cpu
;
1041 dprintk("handle_update for cpu %u called\n", cpu
);
1042 cpufreq_update_policy(cpu
);
1046 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1048 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1049 * @new_freq: CPU frequency the CPU actually runs at
1051 * We adjust to current frequency first, and need to clean up later. So either call
1052 * to cpufreq_update_policy() or schedule handle_update()).
1054 static void cpufreq_out_of_sync(unsigned int cpu
, unsigned int old_freq
,
1055 unsigned int new_freq
)
1057 struct cpufreq_freqs freqs
;
1059 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
1060 "core thinks of %u, is %u kHz.\n", old_freq
, new_freq
);
1063 freqs
.old
= old_freq
;
1064 freqs
.new = new_freq
;
1065 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
1066 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
1071 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1074 * This is the last known freq, without actually getting it from the driver.
1075 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1077 unsigned int cpufreq_quick_get(unsigned int cpu
)
1079 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1080 unsigned int ret_freq
= 0;
1083 if (unlikely(lock_policy_rwsem_read(cpu
)))
1086 ret_freq
= policy
->cur
;
1088 unlock_policy_rwsem_read(cpu
);
1089 cpufreq_cpu_put(policy
);
1094 EXPORT_SYMBOL(cpufreq_quick_get
);
1097 static unsigned int __cpufreq_get(unsigned int cpu
)
1099 struct cpufreq_policy
*policy
= cpufreq_cpu_data
[cpu
];
1100 unsigned int ret_freq
= 0;
1102 if (!cpufreq_driver
->get
)
1105 ret_freq
= cpufreq_driver
->get(cpu
);
1107 if (ret_freq
&& policy
->cur
&&
1108 !(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
1109 /* verify no discrepancy between actual and
1110 saved value exists */
1111 if (unlikely(ret_freq
!= policy
->cur
)) {
1112 cpufreq_out_of_sync(cpu
, policy
->cur
, ret_freq
);
1113 schedule_work(&policy
->update
);
1121 * cpufreq_get - get the current CPU frequency (in kHz)
1124 * Get the CPU current (static) CPU frequency
1126 unsigned int cpufreq_get(unsigned int cpu
)
1128 unsigned int ret_freq
= 0;
1129 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1134 if (unlikely(lock_policy_rwsem_read(cpu
)))
1137 ret_freq
= __cpufreq_get(cpu
);
1139 unlock_policy_rwsem_read(cpu
);
1142 cpufreq_cpu_put(policy
);
1146 EXPORT_SYMBOL(cpufreq_get
);
1150 * cpufreq_suspend - let the low level driver prepare for suspend
1153 static int cpufreq_suspend(struct sys_device
* sysdev
, pm_message_t pmsg
)
1155 int cpu
= sysdev
->id
;
1157 unsigned int cur_freq
= 0;
1158 struct cpufreq_policy
*cpu_policy
;
1160 dprintk("suspending cpu %u\n", cpu
);
1162 if (!cpu_online(cpu
))
1165 /* we may be lax here as interrupts are off. Nonetheless
1166 * we need to grab the correct cpu policy, as to check
1167 * whether we really run on this CPU.
1170 cpu_policy
= cpufreq_cpu_get(cpu
);
1174 /* only handle each CPU group once */
1175 if (unlikely(cpu_policy
->cpu
!= cpu
)) {
1176 cpufreq_cpu_put(cpu_policy
);
1180 if (cpufreq_driver
->suspend
) {
1181 ret
= cpufreq_driver
->suspend(cpu_policy
, pmsg
);
1183 printk(KERN_ERR
"cpufreq: suspend failed in ->suspend "
1184 "step on CPU %u\n", cpu_policy
->cpu
);
1185 cpufreq_cpu_put(cpu_policy
);
1191 if (cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)
1194 if (cpufreq_driver
->get
)
1195 cur_freq
= cpufreq_driver
->get(cpu_policy
->cpu
);
1197 if (!cur_freq
|| !cpu_policy
->cur
) {
1198 printk(KERN_ERR
"cpufreq: suspend failed to assert current "
1199 "frequency is what timing core thinks it is.\n");
1203 if (unlikely(cur_freq
!= cpu_policy
->cur
)) {
1204 struct cpufreq_freqs freqs
;
1206 if (!(cpufreq_driver
->flags
& CPUFREQ_PM_NO_WARN
))
1207 dprintk("Warning: CPU frequency is %u, "
1208 "cpufreq assumed %u kHz.\n",
1209 cur_freq
, cpu_policy
->cur
);
1212 freqs
.old
= cpu_policy
->cur
;
1213 freqs
.new = cur_freq
;
1215 srcu_notifier_call_chain(&cpufreq_transition_notifier_list
,
1216 CPUFREQ_SUSPENDCHANGE
, &freqs
);
1217 adjust_jiffies(CPUFREQ_SUSPENDCHANGE
, &freqs
);
1219 cpu_policy
->cur
= cur_freq
;
1223 cpufreq_cpu_put(cpu_policy
);
1228 * cpufreq_resume - restore proper CPU frequency handling after resume
1230 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1231 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1232 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1235 static int cpufreq_resume(struct sys_device
* sysdev
)
1237 int cpu
= sysdev
->id
;
1239 struct cpufreq_policy
*cpu_policy
;
1241 dprintk("resuming cpu %u\n", cpu
);
1243 if (!cpu_online(cpu
))
1246 /* we may be lax here as interrupts are off. Nonetheless
1247 * we need to grab the correct cpu policy, as to check
1248 * whether we really run on this CPU.
1251 cpu_policy
= cpufreq_cpu_get(cpu
);
1255 /* only handle each CPU group once */
1256 if (unlikely(cpu_policy
->cpu
!= cpu
)) {
1257 cpufreq_cpu_put(cpu_policy
);
1261 if (cpufreq_driver
->resume
) {
1262 ret
= cpufreq_driver
->resume(cpu_policy
);
1264 printk(KERN_ERR
"cpufreq: resume failed in ->resume "
1265 "step on CPU %u\n", cpu_policy
->cpu
);
1266 cpufreq_cpu_put(cpu_policy
);
1271 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
1272 unsigned int cur_freq
= 0;
1274 if (cpufreq_driver
->get
)
1275 cur_freq
= cpufreq_driver
->get(cpu_policy
->cpu
);
1277 if (!cur_freq
|| !cpu_policy
->cur
) {
1278 printk(KERN_ERR
"cpufreq: resume failed to assert "
1279 "current frequency is what timing core "
1284 if (unlikely(cur_freq
!= cpu_policy
->cur
)) {
1285 struct cpufreq_freqs freqs
;
1287 if (!(cpufreq_driver
->flags
& CPUFREQ_PM_NO_WARN
))
1288 dprintk("Warning: CPU frequency"
1289 "is %u, cpufreq assumed %u kHz.\n",
1290 cur_freq
, cpu_policy
->cur
);
1293 freqs
.old
= cpu_policy
->cur
;
1294 freqs
.new = cur_freq
;
1296 srcu_notifier_call_chain(
1297 &cpufreq_transition_notifier_list
,
1298 CPUFREQ_RESUMECHANGE
, &freqs
);
1299 adjust_jiffies(CPUFREQ_RESUMECHANGE
, &freqs
);
1301 cpu_policy
->cur
= cur_freq
;
1306 schedule_work(&cpu_policy
->update
);
1307 cpufreq_cpu_put(cpu_policy
);
1311 static struct sysdev_driver cpufreq_sysdev_driver
= {
1312 .add
= cpufreq_add_dev
,
1313 .remove
= cpufreq_remove_dev
,
1314 .suspend
= cpufreq_suspend
,
1315 .resume
= cpufreq_resume
,
1319 /*********************************************************************
1320 * NOTIFIER LISTS INTERFACE *
1321 *********************************************************************/
1324 * cpufreq_register_notifier - register a driver with cpufreq
1325 * @nb: notifier function to register
1326 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1328 * Add a driver to one of two lists: either a list of drivers that
1329 * are notified about clock rate changes (once before and once after
1330 * the transition), or a list of drivers that are notified about
1331 * changes in cpufreq policy.
1333 * This function may sleep, and has the same return conditions as
1334 * blocking_notifier_chain_register.
1336 int cpufreq_register_notifier(struct notifier_block
*nb
, unsigned int list
)
1341 case CPUFREQ_TRANSITION_NOTIFIER
:
1342 ret
= srcu_notifier_chain_register(
1343 &cpufreq_transition_notifier_list
, nb
);
1345 case CPUFREQ_POLICY_NOTIFIER
:
1346 ret
= blocking_notifier_chain_register(
1347 &cpufreq_policy_notifier_list
, nb
);
1355 EXPORT_SYMBOL(cpufreq_register_notifier
);
1359 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1360 * @nb: notifier block to be unregistered
1361 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1363 * Remove a driver from the CPU frequency notifier list.
1365 * This function may sleep, and has the same return conditions as
1366 * blocking_notifier_chain_unregister.
1368 int cpufreq_unregister_notifier(struct notifier_block
*nb
, unsigned int list
)
1373 case CPUFREQ_TRANSITION_NOTIFIER
:
1374 ret
= srcu_notifier_chain_unregister(
1375 &cpufreq_transition_notifier_list
, nb
);
1377 case CPUFREQ_POLICY_NOTIFIER
:
1378 ret
= blocking_notifier_chain_unregister(
1379 &cpufreq_policy_notifier_list
, nb
);
1387 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1390 /*********************************************************************
1392 *********************************************************************/
1395 int __cpufreq_driver_target(struct cpufreq_policy
*policy
,
1396 unsigned int target_freq
,
1397 unsigned int relation
)
1399 int retval
= -EINVAL
;
1401 dprintk("target for CPU %u: %u kHz, relation %u\n", policy
->cpu
,
1402 target_freq
, relation
);
1403 if (cpu_online(policy
->cpu
) && cpufreq_driver
->target
)
1404 retval
= cpufreq_driver
->target(policy
, target_freq
, relation
);
1408 EXPORT_SYMBOL_GPL(__cpufreq_driver_target
);
1410 int cpufreq_driver_target(struct cpufreq_policy
*policy
,
1411 unsigned int target_freq
,
1412 unsigned int relation
)
1416 policy
= cpufreq_cpu_get(policy
->cpu
);
1420 if (unlikely(lock_policy_rwsem_write(policy
->cpu
)))
1423 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
1425 unlock_policy_rwsem_write(policy
->cpu
);
1427 cpufreq_cpu_put(policy
);
1430 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
1432 int __cpufreq_driver_getavg(struct cpufreq_policy
*policy
)
1436 policy
= cpufreq_cpu_get(policy
->cpu
);
1440 if (cpu_online(policy
->cpu
) && cpufreq_driver
->getavg
)
1441 ret
= cpufreq_driver
->getavg(policy
->cpu
);
1443 cpufreq_cpu_put(policy
);
1446 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg
);
1449 * when "event" is CPUFREQ_GOV_LIMITS
1452 static int __cpufreq_governor(struct cpufreq_policy
*policy
,
1457 if (!try_module_get(policy
->governor
->owner
))
1460 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1461 policy
->cpu
, event
);
1462 ret
= policy
->governor
->governor(policy
, event
);
1464 /* we keep one module reference alive for
1465 each CPU governed by this CPU */
1466 if ((event
!= CPUFREQ_GOV_START
) || ret
)
1467 module_put(policy
->governor
->owner
);
1468 if ((event
== CPUFREQ_GOV_STOP
) && !ret
)
1469 module_put(policy
->governor
->owner
);
1475 int cpufreq_register_governor(struct cpufreq_governor
*governor
)
1482 mutex_lock(&cpufreq_governor_mutex
);
1485 if (__find_governor(governor
->name
) == NULL
) {
1487 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
1490 mutex_unlock(&cpufreq_governor_mutex
);
1493 EXPORT_SYMBOL_GPL(cpufreq_register_governor
);
1496 void cpufreq_unregister_governor(struct cpufreq_governor
*governor
)
1501 mutex_lock(&cpufreq_governor_mutex
);
1502 list_del(&governor
->governor_list
);
1503 mutex_unlock(&cpufreq_governor_mutex
);
1506 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor
);
1510 /*********************************************************************
1511 * POLICY INTERFACE *
1512 *********************************************************************/
1515 * cpufreq_get_policy - get the current cpufreq_policy
1516 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1518 * Reads the current cpufreq policy.
1520 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
1522 struct cpufreq_policy
*cpu_policy
;
1526 cpu_policy
= cpufreq_cpu_get(cpu
);
1530 memcpy(policy
, cpu_policy
, sizeof(struct cpufreq_policy
));
1532 cpufreq_cpu_put(cpu_policy
);
1535 EXPORT_SYMBOL(cpufreq_get_policy
);
1539 * data : current policy.
1540 * policy : policy to be set.
1542 static int __cpufreq_set_policy(struct cpufreq_policy
*data
,
1543 struct cpufreq_policy
*policy
)
1547 cpufreq_debug_disable_ratelimit();
1548 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy
->cpu
,
1549 policy
->min
, policy
->max
);
1551 memcpy(&policy
->cpuinfo
, &data
->cpuinfo
,
1552 sizeof(struct cpufreq_cpuinfo
));
1554 if (policy
->min
> data
->min
&& policy
->min
> policy
->max
) {
1559 /* verify the cpu speed can be set within this limit */
1560 ret
= cpufreq_driver
->verify(policy
);
1564 /* adjust if necessary - all reasons */
1565 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1566 CPUFREQ_ADJUST
, policy
);
1568 /* adjust if necessary - hardware incompatibility*/
1569 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1570 CPUFREQ_INCOMPATIBLE
, policy
);
1572 /* verify the cpu speed can be set within this limit,
1573 which might be different to the first one */
1574 ret
= cpufreq_driver
->verify(policy
);
1578 /* notification of the new policy */
1579 blocking_notifier_call_chain(&cpufreq_policy_notifier_list
,
1580 CPUFREQ_NOTIFY
, policy
);
1582 data
->min
= policy
->min
;
1583 data
->max
= policy
->max
;
1585 dprintk("new min and max freqs are %u - %u kHz\n",
1586 data
->min
, data
->max
);
1588 if (cpufreq_driver
->setpolicy
) {
1589 data
->policy
= policy
->policy
;
1590 dprintk("setting range\n");
1591 ret
= cpufreq_driver
->setpolicy(policy
);
1593 if (policy
->governor
!= data
->governor
) {
1594 /* save old, working values */
1595 struct cpufreq_governor
*old_gov
= data
->governor
;
1597 dprintk("governor switch\n");
1599 /* end old governor */
1601 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
1603 /* start new governor */
1604 data
->governor
= policy
->governor
;
1605 if (__cpufreq_governor(data
, CPUFREQ_GOV_START
)) {
1606 /* new governor failed, so re-start old one */
1607 dprintk("starting governor %s failed\n",
1608 data
->governor
->name
);
1610 data
->governor
= old_gov
;
1611 __cpufreq_governor(data
,
1617 /* might be a policy change, too, so fall through */
1619 dprintk("governor: change or update limits\n");
1620 __cpufreq_governor(data
, CPUFREQ_GOV_LIMITS
);
1624 cpufreq_debug_enable_ratelimit();
1629 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1630 * @cpu: CPU which shall be re-evaluated
1632 * Usefull for policy notifiers which have different necessities
1633 * at different times.
1635 int cpufreq_update_policy(unsigned int cpu
)
1637 struct cpufreq_policy
*data
= cpufreq_cpu_get(cpu
);
1638 struct cpufreq_policy policy
;
1644 if (unlikely(lock_policy_rwsem_write(cpu
)))
1647 dprintk("updating policy for CPU %u\n", cpu
);
1648 memcpy(&policy
, data
, sizeof(struct cpufreq_policy
));
1649 policy
.min
= data
->user_policy
.min
;
1650 policy
.max
= data
->user_policy
.max
;
1651 policy
.policy
= data
->user_policy
.policy
;
1652 policy
.governor
= data
->user_policy
.governor
;
1654 /* BIOS might change freq behind our back
1655 -> ask driver for current freq and notify governors about a change */
1656 if (cpufreq_driver
->get
) {
1657 policy
.cur
= cpufreq_driver
->get(cpu
);
1659 dprintk("Driver did not initialize current freq");
1660 data
->cur
= policy
.cur
;
1662 if (data
->cur
!= policy
.cur
)
1663 cpufreq_out_of_sync(cpu
, data
->cur
,
1668 ret
= __cpufreq_set_policy(data
, &policy
);
1670 unlock_policy_rwsem_write(cpu
);
1672 cpufreq_cpu_put(data
);
1675 EXPORT_SYMBOL(cpufreq_update_policy
);
1677 static int cpufreq_cpu_callback(struct notifier_block
*nfb
,
1678 unsigned long action
, void *hcpu
)
1680 unsigned int cpu
= (unsigned long)hcpu
;
1681 struct sys_device
*sys_dev
;
1682 struct cpufreq_policy
*policy
;
1684 sys_dev
= get_cpu_sysdev(cpu
);
1688 case CPU_ONLINE_FROZEN
:
1689 cpufreq_add_dev(sys_dev
);
1691 case CPU_DOWN_PREPARE
:
1692 case CPU_DOWN_PREPARE_FROZEN
:
1693 if (unlikely(lock_policy_rwsem_write(cpu
)))
1696 policy
= cpufreq_cpu_data
[cpu
];
1698 __cpufreq_driver_target(policy
, policy
->min
,
1699 CPUFREQ_RELATION_H
);
1701 __cpufreq_remove_dev(sys_dev
);
1703 case CPU_DOWN_FAILED
:
1704 case CPU_DOWN_FAILED_FROZEN
:
1705 cpufreq_add_dev(sys_dev
);
1712 static struct notifier_block __cpuinitdata cpufreq_cpu_notifier
=
1714 .notifier_call
= cpufreq_cpu_callback
,
1717 /*********************************************************************
1718 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1719 *********************************************************************/
1722 * cpufreq_register_driver - register a CPU Frequency driver
1723 * @driver_data: A struct cpufreq_driver containing the values#
1724 * submitted by the CPU Frequency driver.
1726 * Registers a CPU Frequency driver to this core code. This code
1727 * returns zero on success, -EBUSY when another driver got here first
1728 * (and isn't unregistered in the meantime).
1731 int cpufreq_register_driver(struct cpufreq_driver
*driver_data
)
1733 unsigned long flags
;
1736 if (!driver_data
|| !driver_data
->verify
|| !driver_data
->init
||
1737 ((!driver_data
->setpolicy
) && (!driver_data
->target
)))
1740 dprintk("trying to register driver %s\n", driver_data
->name
);
1742 if (driver_data
->setpolicy
)
1743 driver_data
->flags
|= CPUFREQ_CONST_LOOPS
;
1745 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1746 if (cpufreq_driver
) {
1747 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1750 cpufreq_driver
= driver_data
;
1751 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1753 ret
= sysdev_driver_register(&cpu_sysdev_class
,&cpufreq_sysdev_driver
);
1755 if ((!ret
) && !(cpufreq_driver
->flags
& CPUFREQ_STICKY
)) {
1759 /* check for at least one working CPU */
1760 for (i
=0; i
<NR_CPUS
; i
++)
1761 if (cpufreq_cpu_data
[i
])
1764 /* if all ->init() calls failed, unregister */
1766 dprintk("no CPU initialized for driver %s\n",
1768 sysdev_driver_unregister(&cpu_sysdev_class
,
1769 &cpufreq_sysdev_driver
);
1771 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1772 cpufreq_driver
= NULL
;
1773 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1778 register_hotcpu_notifier(&cpufreq_cpu_notifier
);
1779 dprintk("driver %s up and running\n", driver_data
->name
);
1780 cpufreq_debug_enable_ratelimit();
1785 EXPORT_SYMBOL_GPL(cpufreq_register_driver
);
1789 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1791 * Unregister the current CPUFreq driver. Only call this if you have
1792 * the right to do so, i.e. if you have succeeded in initialising before!
1793 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1794 * currently not initialised.
1796 int cpufreq_unregister_driver(struct cpufreq_driver
*driver
)
1798 unsigned long flags
;
1800 cpufreq_debug_disable_ratelimit();
1802 if (!cpufreq_driver
|| (driver
!= cpufreq_driver
)) {
1803 cpufreq_debug_enable_ratelimit();
1807 dprintk("unregistering driver %s\n", driver
->name
);
1809 sysdev_driver_unregister(&cpu_sysdev_class
, &cpufreq_sysdev_driver
);
1810 unregister_hotcpu_notifier(&cpufreq_cpu_notifier
);
1812 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1813 cpufreq_driver
= NULL
;
1814 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1818 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver
);
1820 static int __init
cpufreq_core_init(void)
1824 for_each_possible_cpu(cpu
) {
1825 per_cpu(policy_cpu
, cpu
) = -1;
1826 init_rwsem(&per_cpu(cpu_policy_rwsem
, cpu
));
1831 core_initcall(cpufreq_core_init
);