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
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/notifier.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/cpu.h>
28 #include <linux/completion.h>
30 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
33 * The "cpufreq driver" - the arch- or hardware-dependend low
34 * level driver of CPUFreq support, and its spinlock. This lock
35 * also protects the cpufreq_cpu_data array.
37 static struct cpufreq_driver
*cpufreq_driver
;
38 static struct cpufreq_policy
*cpufreq_cpu_data
[NR_CPUS
];
39 static DEFINE_SPINLOCK(cpufreq_driver_lock
);
42 /* internal prototypes */
43 static int __cpufreq_governor(struct cpufreq_policy
*policy
, unsigned int event
);
44 static void handle_update(void *data
);
45 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
);
48 * Two notifier lists: the "policy" list is involved in the
49 * validation process for a new CPU frequency policy; the
50 * "transition" list for kernel code that needs to handle
51 * changes to devices when the CPU clock speed changes.
52 * The mutex locks both lists.
54 static struct notifier_block
*cpufreq_policy_notifier_list
;
55 static struct notifier_block
*cpufreq_transition_notifier_list
;
56 static DECLARE_RWSEM (cpufreq_notifier_rwsem
);
59 static LIST_HEAD(cpufreq_governor_list
);
60 static DECLARE_MUTEX (cpufreq_governor_sem
);
62 struct cpufreq_policy
* cpufreq_cpu_get(unsigned int cpu
)
64 struct cpufreq_policy
*data
;
70 /* get the cpufreq driver */
71 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
76 if (!try_module_get(cpufreq_driver
->owner
))
81 data
= cpufreq_cpu_data
[cpu
];
84 goto err_out_put_module
;
86 if (!kobject_get(&data
->kobj
))
87 goto err_out_put_module
;
90 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
95 module_put(cpufreq_driver
->owner
);
97 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
101 EXPORT_SYMBOL_GPL(cpufreq_cpu_get
);
103 void cpufreq_cpu_put(struct cpufreq_policy
*data
)
105 kobject_put(&data
->kobj
);
106 module_put(cpufreq_driver
->owner
);
108 EXPORT_SYMBOL_GPL(cpufreq_cpu_put
);
111 /*********************************************************************
112 * UNIFIED DEBUG HELPERS *
113 *********************************************************************/
114 #ifdef CONFIG_CPU_FREQ_DEBUG
116 /* what part(s) of the CPUfreq subsystem are debugged? */
117 static unsigned int debug
;
119 /* is the debug output ratelimit'ed using printk_ratelimit? User can
120 * set or modify this value.
122 static unsigned int debug_ratelimit
= 1;
124 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
125 * loading of a cpufreq driver, temporarily disabled when a new policy
126 * is set, and disabled upon cpufreq driver removal
128 static unsigned int disable_ratelimit
= 1;
129 static DEFINE_SPINLOCK(disable_ratelimit_lock
);
131 static inline void cpufreq_debug_enable_ratelimit(void)
135 spin_lock_irqsave(&disable_ratelimit_lock
, flags
);
136 if (disable_ratelimit
)
138 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
141 static inline void cpufreq_debug_disable_ratelimit(void)
145 spin_lock_irqsave(&disable_ratelimit_lock
, flags
);
147 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
150 void cpufreq_debug_printk(unsigned int type
, const char *prefix
, const char *fmt
, ...)
159 spin_lock_irqsave(&disable_ratelimit_lock
, flags
);
160 if (!disable_ratelimit
&& debug_ratelimit
&& !printk_ratelimit()) {
161 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
164 spin_unlock_irqrestore(&disable_ratelimit_lock
, flags
);
166 len
= snprintf(s
, 256, KERN_DEBUG
"%s: ", prefix
);
169 len
+= vsnprintf(&s
[len
], (256 - len
), fmt
, args
);
177 EXPORT_SYMBOL(cpufreq_debug_printk
);
180 module_param(debug
, uint
, 0644);
181 MODULE_PARM_DESC(debug
, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
183 module_param(debug_ratelimit
, uint
, 0644);
184 MODULE_PARM_DESC(debug_ratelimit
, "CPUfreq debugging: set to 0 to disable ratelimiting.");
186 #else /* !CONFIG_CPU_FREQ_DEBUG */
188 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
189 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
191 #endif /* CONFIG_CPU_FREQ_DEBUG */
194 /*********************************************************************
195 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
196 *********************************************************************/
199 * adjust_jiffies - adjust the system "loops_per_jiffy"
201 * This function alters the system "loops_per_jiffy" for the clock
202 * speed change. Note that loops_per_jiffy cannot be updated on SMP
203 * systems as each CPU might be scaled differently. So, use the arch
204 * per-CPU loops_per_jiffy value wherever possible.
207 static unsigned long l_p_j_ref
;
208 static unsigned int l_p_j_ref_freq
;
210 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
)
212 if (ci
->flags
& CPUFREQ_CONST_LOOPS
)
215 if (!l_p_j_ref_freq
) {
216 l_p_j_ref
= loops_per_jiffy
;
217 l_p_j_ref_freq
= ci
->old
;
218 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref
, l_p_j_ref_freq
);
220 if ((val
== CPUFREQ_PRECHANGE
&& ci
->old
< ci
->new) ||
221 (val
== CPUFREQ_POSTCHANGE
&& ci
->old
> ci
->new) ||
222 (val
== CPUFREQ_RESUMECHANGE
|| val
== CPUFREQ_SUSPENDCHANGE
)) {
223 loops_per_jiffy
= cpufreq_scale(l_p_j_ref
, l_p_j_ref_freq
, ci
->new);
224 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy
, ci
->new);
228 static inline void adjust_jiffies(unsigned long val
, struct cpufreq_freqs
*ci
) { return; }
233 * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
235 * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
236 * twice on all CPU frequency changes that have external effects.
238 void cpufreq_notify_transition(struct cpufreq_freqs
*freqs
, unsigned int state
)
240 BUG_ON(irqs_disabled());
242 freqs
->flags
= cpufreq_driver
->flags
;
243 dprintk("notification %u of frequency transition to %u kHz\n", state
, freqs
->new);
245 down_read(&cpufreq_notifier_rwsem
);
247 case CPUFREQ_PRECHANGE
:
248 /* detect if the driver reported a value as "old frequency" which
249 * is not equal to what the cpufreq core thinks is "old frequency".
251 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
252 if ((likely(cpufreq_cpu_data
[freqs
->cpu
])) &&
253 (likely(cpufreq_cpu_data
[freqs
->cpu
]->cpu
== freqs
->cpu
)) &&
254 (likely(cpufreq_cpu_data
[freqs
->cpu
]->cur
)) &&
255 (unlikely(freqs
->old
!= cpufreq_cpu_data
[freqs
->cpu
]->cur
)))
257 dprintk(KERN_WARNING
"Warning: CPU frequency is %u, "
258 "cpufreq assumed %u kHz.\n", freqs
->old
, cpufreq_cpu_data
[freqs
->cpu
]->cur
);
259 freqs
->old
= cpufreq_cpu_data
[freqs
->cpu
]->cur
;
262 notifier_call_chain(&cpufreq_transition_notifier_list
, CPUFREQ_PRECHANGE
, freqs
);
263 adjust_jiffies(CPUFREQ_PRECHANGE
, freqs
);
265 case CPUFREQ_POSTCHANGE
:
266 adjust_jiffies(CPUFREQ_POSTCHANGE
, freqs
);
267 notifier_call_chain(&cpufreq_transition_notifier_list
, CPUFREQ_POSTCHANGE
, freqs
);
268 if ((likely(cpufreq_cpu_data
[freqs
->cpu
])) &&
269 (likely(cpufreq_cpu_data
[freqs
->cpu
]->cpu
== freqs
->cpu
)))
270 cpufreq_cpu_data
[freqs
->cpu
]->cur
= freqs
->new;
273 up_read(&cpufreq_notifier_rwsem
);
275 EXPORT_SYMBOL_GPL(cpufreq_notify_transition
);
279 /*********************************************************************
281 *********************************************************************/
284 * cpufreq_parse_governor - parse a governor string
286 static int cpufreq_parse_governor (char *str_governor
, unsigned int *policy
,
287 struct cpufreq_governor
**governor
)
291 if (cpufreq_driver
->setpolicy
) {
292 if (!strnicmp(str_governor
, "performance", CPUFREQ_NAME_LEN
)) {
293 *policy
= CPUFREQ_POLICY_PERFORMANCE
;
295 } else if (!strnicmp(str_governor
, "powersave", CPUFREQ_NAME_LEN
)) {
296 *policy
= CPUFREQ_POLICY_POWERSAVE
;
301 struct cpufreq_governor
*t
;
302 down(&cpufreq_governor_sem
);
303 if (!cpufreq_driver
|| !cpufreq_driver
->target
)
305 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
306 if (!strnicmp(str_governor
,t
->name
,CPUFREQ_NAME_LEN
)) {
308 up(&cpufreq_governor_sem
);
313 up(&cpufreq_governor_sem
);
317 EXPORT_SYMBOL_GPL(cpufreq_parse_governor
);
320 /* drivers/base/cpu.c */
321 extern struct sysdev_class cpu_sysdev_class
;
325 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
327 * Write out information from cpufreq_driver->policy[cpu]; object must be
331 #define show_one(file_name, object) \
332 static ssize_t show_##file_name \
333 (struct cpufreq_policy * policy, char *buf) \
335 return sprintf (buf, "%u\n", policy->object); \
338 show_one(cpuinfo_min_freq
, cpuinfo
.min_freq
);
339 show_one(cpuinfo_max_freq
, cpuinfo
.max_freq
);
340 show_one(scaling_min_freq
, min
);
341 show_one(scaling_max_freq
, max
);
342 show_one(scaling_cur_freq
, cur
);
345 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
347 #define store_one(file_name, object) \
348 static ssize_t store_##file_name \
349 (struct cpufreq_policy * policy, const char *buf, size_t count) \
351 unsigned int ret = -EINVAL; \
352 struct cpufreq_policy new_policy; \
354 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
358 ret = sscanf (buf, "%u", &new_policy.object); \
362 ret = cpufreq_set_policy(&new_policy); \
364 return ret ? ret : count; \
367 store_one(scaling_min_freq
,min
);
368 store_one(scaling_max_freq
,max
);
371 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
373 static ssize_t
show_cpuinfo_cur_freq (struct cpufreq_policy
* policy
, char *buf
)
375 unsigned int cur_freq
= cpufreq_get(policy
->cpu
);
377 return sprintf(buf
, "<unknown>");
378 return sprintf(buf
, "%u\n", cur_freq
);
383 * show_scaling_governor - show the current policy for the specified CPU
385 static ssize_t
show_scaling_governor (struct cpufreq_policy
* policy
, char *buf
)
387 if(policy
->policy
== CPUFREQ_POLICY_POWERSAVE
)
388 return sprintf(buf
, "powersave\n");
389 else if (policy
->policy
== CPUFREQ_POLICY_PERFORMANCE
)
390 return sprintf(buf
, "performance\n");
391 else if (policy
->governor
)
392 return scnprintf(buf
, CPUFREQ_NAME_LEN
, "%s\n", policy
->governor
->name
);
398 * store_scaling_governor - store policy for the specified CPU
400 static ssize_t
store_scaling_governor (struct cpufreq_policy
* policy
,
401 const char *buf
, size_t count
)
403 unsigned int ret
= -EINVAL
;
404 char str_governor
[16];
405 struct cpufreq_policy new_policy
;
407 ret
= cpufreq_get_policy(&new_policy
, policy
->cpu
);
411 ret
= sscanf (buf
, "%15s", str_governor
);
415 if (cpufreq_parse_governor(str_governor
, &new_policy
.policy
, &new_policy
.governor
))
418 ret
= cpufreq_set_policy(&new_policy
);
420 return ret
? ret
: count
;
424 * show_scaling_driver - show the cpufreq driver currently loaded
426 static ssize_t
show_scaling_driver (struct cpufreq_policy
* policy
, char *buf
)
428 return scnprintf(buf
, CPUFREQ_NAME_LEN
, "%s\n", cpufreq_driver
->name
);
432 * show_scaling_available_governors - show the available CPUfreq governors
434 static ssize_t
show_scaling_available_governors (struct cpufreq_policy
* policy
,
438 struct cpufreq_governor
*t
;
440 if (!cpufreq_driver
->target
) {
441 i
+= sprintf(buf
, "performance powersave");
445 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
446 if (i
>= (ssize_t
) ((PAGE_SIZE
/ sizeof(char)) - (CPUFREQ_NAME_LEN
+ 2)))
448 i
+= scnprintf(&buf
[i
], CPUFREQ_NAME_LEN
, "%s ", t
->name
);
451 i
+= sprintf(&buf
[i
], "\n");
455 * show_affected_cpus - show the CPUs affected by each transition
457 static ssize_t
show_affected_cpus (struct cpufreq_policy
* policy
, char *buf
)
462 for_each_cpu_mask(cpu
, policy
->cpus
) {
464 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), " ");
465 i
+= scnprintf(&buf
[i
], (PAGE_SIZE
- i
- 2), "%u", cpu
);
466 if (i
>= (PAGE_SIZE
- 5))
469 i
+= sprintf(&buf
[i
], "\n");
474 #define define_one_ro(_name) \
475 static struct freq_attr _name = \
476 __ATTR(_name, 0444, show_##_name, NULL)
478 #define define_one_ro0400(_name) \
479 static struct freq_attr _name = \
480 __ATTR(_name, 0400, show_##_name, NULL)
482 #define define_one_rw(_name) \
483 static struct freq_attr _name = \
484 __ATTR(_name, 0644, show_##_name, store_##_name)
486 define_one_ro0400(cpuinfo_cur_freq
);
487 define_one_ro(cpuinfo_min_freq
);
488 define_one_ro(cpuinfo_max_freq
);
489 define_one_ro(scaling_available_governors
);
490 define_one_ro(scaling_driver
);
491 define_one_ro(scaling_cur_freq
);
492 define_one_ro(affected_cpus
);
493 define_one_rw(scaling_min_freq
);
494 define_one_rw(scaling_max_freq
);
495 define_one_rw(scaling_governor
);
497 static struct attribute
* default_attrs
[] = {
498 &cpuinfo_min_freq
.attr
,
499 &cpuinfo_max_freq
.attr
,
500 &scaling_min_freq
.attr
,
501 &scaling_max_freq
.attr
,
503 &scaling_governor
.attr
,
504 &scaling_driver
.attr
,
505 &scaling_available_governors
.attr
,
509 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
510 #define to_attr(a) container_of(a,struct freq_attr,attr)
512 static ssize_t
show(struct kobject
* kobj
, struct attribute
* attr
,char * buf
)
514 struct cpufreq_policy
* policy
= to_policy(kobj
);
515 struct freq_attr
* fattr
= to_attr(attr
);
517 policy
= cpufreq_cpu_get(policy
->cpu
);
520 ret
= fattr
->show
? fattr
->show(policy
,buf
) : -EIO
;
521 cpufreq_cpu_put(policy
);
525 static ssize_t
store(struct kobject
* kobj
, struct attribute
* attr
,
526 const char * buf
, size_t count
)
528 struct cpufreq_policy
* policy
= to_policy(kobj
);
529 struct freq_attr
* fattr
= to_attr(attr
);
531 policy
= cpufreq_cpu_get(policy
->cpu
);
534 ret
= fattr
->store
? fattr
->store(policy
,buf
,count
) : -EIO
;
535 cpufreq_cpu_put(policy
);
539 static void cpufreq_sysfs_release(struct kobject
* kobj
)
541 struct cpufreq_policy
* policy
= to_policy(kobj
);
542 dprintk("last reference is dropped\n");
543 complete(&policy
->kobj_unregister
);
546 static struct sysfs_ops sysfs_ops
= {
551 static struct kobj_type ktype_cpufreq
= {
552 .sysfs_ops
= &sysfs_ops
,
553 .default_attrs
= default_attrs
,
554 .release
= cpufreq_sysfs_release
,
559 * cpufreq_add_dev - add a CPU device
561 * Adds the cpufreq interface for a CPU device.
563 static int cpufreq_add_dev (struct sys_device
* sys_dev
)
565 unsigned int cpu
= sys_dev
->id
;
567 struct cpufreq_policy new_policy
;
568 struct cpufreq_policy
*policy
;
569 struct freq_attr
**drv_attr
;
573 if (cpu_is_offline(cpu
))
576 cpufreq_debug_disable_ratelimit();
577 dprintk("adding CPU %u\n", cpu
);
580 /* check whether a different CPU already registered this
581 * CPU because it is in the same boat. */
582 policy
= cpufreq_cpu_get(cpu
);
583 if (unlikely(policy
)) {
584 dprintk("CPU already managed, adding link\n");
585 sysfs_create_link(&sys_dev
->kobj
, &policy
->kobj
, "cpufreq");
586 cpufreq_debug_enable_ratelimit();
591 if (!try_module_get(cpufreq_driver
->owner
)) {
596 policy
= kzalloc(sizeof(struct cpufreq_policy
), GFP_KERNEL
);
603 policy
->cpus
= cpumask_of_cpu(cpu
);
605 init_MUTEX_LOCKED(&policy
->lock
);
606 init_completion(&policy
->kobj_unregister
);
607 INIT_WORK(&policy
->update
, handle_update
, (void *)(long)cpu
);
609 /* call driver. From then on the cpufreq must be able
610 * to accept all calls to ->verify and ->setpolicy for this CPU
612 ret
= cpufreq_driver
->init(policy
);
614 dprintk("initialization failed\n");
618 memcpy(&new_policy
, policy
, sizeof(struct cpufreq_policy
));
620 /* prepare interface data */
621 policy
->kobj
.parent
= &sys_dev
->kobj
;
622 policy
->kobj
.ktype
= &ktype_cpufreq
;
623 strlcpy(policy
->kobj
.name
, "cpufreq", KOBJ_NAME_LEN
);
625 ret
= kobject_register(&policy
->kobj
);
627 goto err_out_driver_exit
;
629 /* set up files for this cpu device */
630 drv_attr
= cpufreq_driver
->attr
;
631 while ((drv_attr
) && (*drv_attr
)) {
632 sysfs_create_file(&policy
->kobj
, &((*drv_attr
)->attr
));
635 if (cpufreq_driver
->get
)
636 sysfs_create_file(&policy
->kobj
, &cpuinfo_cur_freq
.attr
);
637 if (cpufreq_driver
->target
)
638 sysfs_create_file(&policy
->kobj
, &scaling_cur_freq
.attr
);
640 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
641 for_each_cpu_mask(j
, policy
->cpus
)
642 cpufreq_cpu_data
[j
] = policy
;
643 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
644 policy
->governor
= NULL
; /* to assure that the starting sequence is
645 * run in cpufreq_set_policy */
648 /* set default policy */
650 ret
= cpufreq_set_policy(&new_policy
);
652 dprintk("setting policy failed\n");
653 goto err_out_unregister
;
656 module_put(cpufreq_driver
->owner
);
657 dprintk("initialization complete\n");
658 cpufreq_debug_enable_ratelimit();
664 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
665 for_each_cpu_mask(j
, policy
->cpus
)
666 cpufreq_cpu_data
[j
] = NULL
;
667 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
669 kobject_unregister(&policy
->kobj
);
670 wait_for_completion(&policy
->kobj_unregister
);
673 if (cpufreq_driver
->exit
)
674 cpufreq_driver
->exit(policy
);
680 module_put(cpufreq_driver
->owner
);
682 cpufreq_debug_enable_ratelimit();
688 * cpufreq_remove_dev - remove a CPU device
690 * Removes the cpufreq interface for a CPU device.
692 static int cpufreq_remove_dev (struct sys_device
* sys_dev
)
694 unsigned int cpu
= sys_dev
->id
;
696 struct cpufreq_policy
*data
;
697 struct sys_device
*cpu_sys_dev
;
702 cpufreq_debug_disable_ratelimit();
703 dprintk("unregistering CPU %u\n", cpu
);
705 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
706 data
= cpufreq_cpu_data
[cpu
];
709 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
710 cpufreq_debug_enable_ratelimit();
713 cpufreq_cpu_data
[cpu
] = NULL
;
717 /* if this isn't the CPU which is the parent of the kobj, we
718 * only need to unlink, put and exit
720 if (unlikely(cpu
!= data
->cpu
)) {
721 dprintk("removing link\n");
722 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
723 sysfs_remove_link(&sys_dev
->kobj
, "cpufreq");
724 cpufreq_cpu_put(data
);
725 cpufreq_debug_enable_ratelimit();
731 if (!kobject_get(&data
->kobj
)) {
732 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
733 cpufreq_debug_enable_ratelimit();
738 /* if we have other CPUs still registered, we need to unlink them,
739 * or else wait_for_completion below will lock up. Clean the
740 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
743 if (unlikely(cpus_weight(data
->cpus
) > 1)) {
744 for_each_cpu_mask(j
, data
->cpus
) {
747 cpufreq_cpu_data
[j
] = NULL
;
751 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
753 if (unlikely(cpus_weight(data
->cpus
) > 1)) {
754 for_each_cpu_mask(j
, data
->cpus
) {
757 dprintk("removing link for cpu %u\n", j
);
758 cpu_sys_dev
= get_cpu_sysdev(j
);
759 sysfs_remove_link(&cpu_sys_dev
->kobj
, "cpufreq");
760 cpufreq_cpu_put(data
);
764 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
768 if (cpufreq_driver
->target
)
769 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
772 kobject_unregister(&data
->kobj
);
774 kobject_put(&data
->kobj
);
776 /* we need to make sure that the underlying kobj is actually
777 * not referenced anymore by anybody before we proceed with
780 dprintk("waiting for dropping of refcount\n");
781 wait_for_completion(&data
->kobj_unregister
);
782 dprintk("wait complete\n");
784 if (cpufreq_driver
->exit
)
785 cpufreq_driver
->exit(data
);
789 cpufreq_debug_enable_ratelimit();
795 static void handle_update(void *data
)
797 unsigned int cpu
= (unsigned int)(long)data
;
798 dprintk("handle_update for cpu %u called\n", cpu
);
799 cpufreq_update_policy(cpu
);
803 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
805 * @old_freq: CPU frequency the kernel thinks the CPU runs at
806 * @new_freq: CPU frequency the CPU actually runs at
808 * We adjust to current frequency first, and need to clean up later. So either call
809 * to cpufreq_update_policy() or schedule handle_update()).
811 static void cpufreq_out_of_sync(unsigned int cpu
, unsigned int old_freq
, unsigned int new_freq
)
813 struct cpufreq_freqs freqs
;
815 dprintk(KERN_WARNING
"Warning: CPU frequency out of sync: cpufreq and timing "
816 "core thinks of %u, is %u kHz.\n", old_freq
, new_freq
);
819 freqs
.old
= old_freq
;
820 freqs
.new = new_freq
;
821 cpufreq_notify_transition(&freqs
, CPUFREQ_PRECHANGE
);
822 cpufreq_notify_transition(&freqs
, CPUFREQ_POSTCHANGE
);
827 * cpufreq_get - get the current CPU frequency (in kHz)
830 * Get the CPU current (static) CPU frequency
832 unsigned int cpufreq_get(unsigned int cpu
)
834 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
835 unsigned int ret
= 0;
840 if (!cpufreq_driver
->get
)
845 ret
= cpufreq_driver
->get(cpu
);
847 if (ret
&& policy
->cur
&& !(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
))
849 /* verify no discrepancy between actual and saved value exists */
850 if (unlikely(ret
!= policy
->cur
)) {
851 cpufreq_out_of_sync(cpu
, policy
->cur
, ret
);
852 schedule_work(&policy
->update
);
859 cpufreq_cpu_put(policy
);
863 EXPORT_SYMBOL(cpufreq_get
);
867 * cpufreq_suspend - let the low level driver prepare for suspend
870 static int cpufreq_suspend(struct sys_device
* sysdev
, pm_message_t pmsg
)
872 int cpu
= sysdev
->id
;
873 unsigned int ret
= 0;
874 unsigned int cur_freq
= 0;
875 struct cpufreq_policy
*cpu_policy
;
877 dprintk("resuming cpu %u\n", cpu
);
879 if (!cpu_online(cpu
))
882 /* we may be lax here as interrupts are off. Nonetheless
883 * we need to grab the correct cpu policy, as to check
884 * whether we really run on this CPU.
887 cpu_policy
= cpufreq_cpu_get(cpu
);
891 /* only handle each CPU group once */
892 if (unlikely(cpu_policy
->cpu
!= cpu
)) {
893 cpufreq_cpu_put(cpu_policy
);
897 if (cpufreq_driver
->suspend
) {
898 ret
= cpufreq_driver
->suspend(cpu_policy
, pmsg
);
900 printk(KERN_ERR
"cpufreq: suspend failed in ->suspend "
901 "step on CPU %u\n", cpu_policy
->cpu
);
902 cpufreq_cpu_put(cpu_policy
);
908 if (cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)
911 if (cpufreq_driver
->get
)
912 cur_freq
= cpufreq_driver
->get(cpu_policy
->cpu
);
914 if (!cur_freq
|| !cpu_policy
->cur
) {
915 printk(KERN_ERR
"cpufreq: suspend failed to assert current "
916 "frequency is what timing core thinks it is.\n");
920 if (unlikely(cur_freq
!= cpu_policy
->cur
)) {
921 struct cpufreq_freqs freqs
;
923 if (!(cpufreq_driver
->flags
& CPUFREQ_PM_NO_WARN
))
924 dprintk(KERN_DEBUG
"Warning: CPU frequency is %u, "
925 "cpufreq assumed %u kHz.\n",
926 cur_freq
, cpu_policy
->cur
);
929 freqs
.old
= cpu_policy
->cur
;
930 freqs
.new = cur_freq
;
932 notifier_call_chain(&cpufreq_transition_notifier_list
,
933 CPUFREQ_SUSPENDCHANGE
, &freqs
);
934 adjust_jiffies(CPUFREQ_SUSPENDCHANGE
, &freqs
);
936 cpu_policy
->cur
= cur_freq
;
940 cpufreq_cpu_put(cpu_policy
);
945 * cpufreq_resume - restore proper CPU frequency handling after resume
947 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
948 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
949 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
952 static int cpufreq_resume(struct sys_device
* sysdev
)
954 int cpu
= sysdev
->id
;
955 unsigned int ret
= 0;
956 struct cpufreq_policy
*cpu_policy
;
958 dprintk("resuming cpu %u\n", cpu
);
960 if (!cpu_online(cpu
))
963 /* we may be lax here as interrupts are off. Nonetheless
964 * we need to grab the correct cpu policy, as to check
965 * whether we really run on this CPU.
968 cpu_policy
= cpufreq_cpu_get(cpu
);
972 /* only handle each CPU group once */
973 if (unlikely(cpu_policy
->cpu
!= cpu
)) {
974 cpufreq_cpu_put(cpu_policy
);
978 if (cpufreq_driver
->resume
) {
979 ret
= cpufreq_driver
->resume(cpu_policy
);
981 printk(KERN_ERR
"cpufreq: resume failed in ->resume "
982 "step on CPU %u\n", cpu_policy
->cpu
);
983 cpufreq_cpu_put(cpu_policy
);
988 if (!(cpufreq_driver
->flags
& CPUFREQ_CONST_LOOPS
)) {
989 unsigned int cur_freq
= 0;
991 if (cpufreq_driver
->get
)
992 cur_freq
= cpufreq_driver
->get(cpu_policy
->cpu
);
994 if (!cur_freq
|| !cpu_policy
->cur
) {
995 printk(KERN_ERR
"cpufreq: resume failed to assert "
996 "current frequency is what timing core "
1001 if (unlikely(cur_freq
!= cpu_policy
->cur
)) {
1002 struct cpufreq_freqs freqs
;
1004 if (!(cpufreq_driver
->flags
& CPUFREQ_PM_NO_WARN
))
1005 dprintk(KERN_WARNING
"Warning: CPU frequency"
1006 "is %u, cpufreq assumed %u kHz.\n",
1007 cur_freq
, cpu_policy
->cur
);
1010 freqs
.old
= cpu_policy
->cur
;
1011 freqs
.new = cur_freq
;
1013 notifier_call_chain(&cpufreq_transition_notifier_list
,
1014 CPUFREQ_RESUMECHANGE
, &freqs
);
1015 adjust_jiffies(CPUFREQ_RESUMECHANGE
, &freqs
);
1017 cpu_policy
->cur
= cur_freq
;
1022 schedule_work(&cpu_policy
->update
);
1023 cpufreq_cpu_put(cpu_policy
);
1027 static struct sysdev_driver cpufreq_sysdev_driver
= {
1028 .add
= cpufreq_add_dev
,
1029 .remove
= cpufreq_remove_dev
,
1030 .suspend
= cpufreq_suspend
,
1031 .resume
= cpufreq_resume
,
1035 /*********************************************************************
1036 * NOTIFIER LISTS INTERFACE *
1037 *********************************************************************/
1040 * cpufreq_register_notifier - register a driver with cpufreq
1041 * @nb: notifier function to register
1042 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1044 * Add a driver to one of two lists: either a list of drivers that
1045 * are notified about clock rate changes (once before and once after
1046 * the transition), or a list of drivers that are notified about
1047 * changes in cpufreq policy.
1049 * This function may sleep, and has the same return conditions as
1050 * notifier_chain_register.
1052 int cpufreq_register_notifier(struct notifier_block
*nb
, unsigned int list
)
1056 down_write(&cpufreq_notifier_rwsem
);
1058 case CPUFREQ_TRANSITION_NOTIFIER
:
1059 ret
= notifier_chain_register(&cpufreq_transition_notifier_list
, nb
);
1061 case CPUFREQ_POLICY_NOTIFIER
:
1062 ret
= notifier_chain_register(&cpufreq_policy_notifier_list
, nb
);
1067 up_write(&cpufreq_notifier_rwsem
);
1071 EXPORT_SYMBOL(cpufreq_register_notifier
);
1075 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1076 * @nb: notifier block to be unregistered
1077 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1079 * Remove a driver from the CPU frequency notifier list.
1081 * This function may sleep, and has the same return conditions as
1082 * notifier_chain_unregister.
1084 int cpufreq_unregister_notifier(struct notifier_block
*nb
, unsigned int list
)
1088 down_write(&cpufreq_notifier_rwsem
);
1090 case CPUFREQ_TRANSITION_NOTIFIER
:
1091 ret
= notifier_chain_unregister(&cpufreq_transition_notifier_list
, nb
);
1093 case CPUFREQ_POLICY_NOTIFIER
:
1094 ret
= notifier_chain_unregister(&cpufreq_policy_notifier_list
, nb
);
1099 up_write(&cpufreq_notifier_rwsem
);
1103 EXPORT_SYMBOL(cpufreq_unregister_notifier
);
1106 /*********************************************************************
1108 *********************************************************************/
1111 int __cpufreq_driver_target(struct cpufreq_policy
*policy
,
1112 unsigned int target_freq
,
1113 unsigned int relation
)
1115 int retval
= -EINVAL
;
1118 * Converted the lock_cpu_hotplug to preempt_disable()
1119 * and preempt_enable(). This is a bit kludgy and relies on how cpu
1120 * hotplug works. All we need is a guarantee that cpu hotplug won't make
1121 * progress on any cpu. Once we do preempt_disable(), this would ensure
1122 * that hotplug threads don't get onto this cpu, thereby delaying
1123 * the cpu remove process.
1125 * We removed the lock_cpu_hotplug since we need to call this function
1126 * via cpu hotplug callbacks, which result in locking the cpu hotplug
1127 * thread itself. Agree this is not very clean, cpufreq community
1128 * could improve this if required. - Ashok Raj <ashok.raj@intel.com>
1131 dprintk("target for CPU %u: %u kHz, relation %u\n", policy
->cpu
,
1132 target_freq
, relation
);
1133 if (cpu_online(policy
->cpu
) && cpufreq_driver
->target
)
1134 retval
= cpufreq_driver
->target(policy
, target_freq
, relation
);
1138 EXPORT_SYMBOL_GPL(__cpufreq_driver_target
);
1140 int cpufreq_driver_target(struct cpufreq_policy
*policy
,
1141 unsigned int target_freq
,
1142 unsigned int relation
)
1146 policy
= cpufreq_cpu_get(policy
->cpu
);
1150 down(&policy
->lock
);
1152 ret
= __cpufreq_driver_target(policy
, target_freq
, relation
);
1156 cpufreq_cpu_put(policy
);
1160 EXPORT_SYMBOL_GPL(cpufreq_driver_target
);
1163 static int __cpufreq_governor(struct cpufreq_policy
*policy
, unsigned int event
)
1167 if (!try_module_get(policy
->governor
->owner
))
1170 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy
->cpu
, event
);
1171 ret
= policy
->governor
->governor(policy
, event
);
1173 /* we keep one module reference alive for each CPU governed by this CPU */
1174 if ((event
!= CPUFREQ_GOV_START
) || ret
)
1175 module_put(policy
->governor
->owner
);
1176 if ((event
== CPUFREQ_GOV_STOP
) && !ret
)
1177 module_put(policy
->governor
->owner
);
1183 int cpufreq_governor(unsigned int cpu
, unsigned int event
)
1186 struct cpufreq_policy
*policy
= cpufreq_cpu_get(cpu
);
1191 down(&policy
->lock
);
1192 ret
= __cpufreq_governor(policy
, event
);
1195 cpufreq_cpu_put(policy
);
1199 EXPORT_SYMBOL_GPL(cpufreq_governor
);
1202 int cpufreq_register_governor(struct cpufreq_governor
*governor
)
1204 struct cpufreq_governor
*t
;
1209 down(&cpufreq_governor_sem
);
1211 list_for_each_entry(t
, &cpufreq_governor_list
, governor_list
) {
1212 if (!strnicmp(governor
->name
,t
->name
,CPUFREQ_NAME_LEN
)) {
1213 up(&cpufreq_governor_sem
);
1217 list_add(&governor
->governor_list
, &cpufreq_governor_list
);
1219 up(&cpufreq_governor_sem
);
1223 EXPORT_SYMBOL_GPL(cpufreq_register_governor
);
1226 void cpufreq_unregister_governor(struct cpufreq_governor
*governor
)
1231 down(&cpufreq_governor_sem
);
1232 list_del(&governor
->governor_list
);
1233 up(&cpufreq_governor_sem
);
1236 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor
);
1240 /*********************************************************************
1241 * POLICY INTERFACE *
1242 *********************************************************************/
1245 * cpufreq_get_policy - get the current cpufreq_policy
1246 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1248 * Reads the current cpufreq policy.
1250 int cpufreq_get_policy(struct cpufreq_policy
*policy
, unsigned int cpu
)
1252 struct cpufreq_policy
*cpu_policy
;
1256 cpu_policy
= cpufreq_cpu_get(cpu
);
1260 down(&cpu_policy
->lock
);
1261 memcpy(policy
, cpu_policy
, sizeof(struct cpufreq_policy
));
1262 up(&cpu_policy
->lock
);
1264 cpufreq_cpu_put(cpu_policy
);
1268 EXPORT_SYMBOL(cpufreq_get_policy
);
1271 static int __cpufreq_set_policy(struct cpufreq_policy
*data
, struct cpufreq_policy
*policy
)
1275 cpufreq_debug_disable_ratelimit();
1276 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy
->cpu
,
1277 policy
->min
, policy
->max
);
1279 memcpy(&policy
->cpuinfo
,
1281 sizeof(struct cpufreq_cpuinfo
));
1283 /* verify the cpu speed can be set within this limit */
1284 ret
= cpufreq_driver
->verify(policy
);
1288 down_read(&cpufreq_notifier_rwsem
);
1290 /* adjust if necessary - all reasons */
1291 notifier_call_chain(&cpufreq_policy_notifier_list
, CPUFREQ_ADJUST
,
1294 /* adjust if necessary - hardware incompatibility*/
1295 notifier_call_chain(&cpufreq_policy_notifier_list
, CPUFREQ_INCOMPATIBLE
,
1298 /* verify the cpu speed can be set within this limit,
1299 which might be different to the first one */
1300 ret
= cpufreq_driver
->verify(policy
);
1302 up_read(&cpufreq_notifier_rwsem
);
1306 /* notification of the new policy */
1307 notifier_call_chain(&cpufreq_policy_notifier_list
, CPUFREQ_NOTIFY
,
1310 up_read(&cpufreq_notifier_rwsem
);
1312 data
->min
= policy
->min
;
1313 data
->max
= policy
->max
;
1315 dprintk("new min and max freqs are %u - %u kHz\n", data
->min
, data
->max
);
1317 if (cpufreq_driver
->setpolicy
) {
1318 data
->policy
= policy
->policy
;
1319 dprintk("setting range\n");
1320 ret
= cpufreq_driver
->setpolicy(policy
);
1322 if (policy
->governor
!= data
->governor
) {
1323 /* save old, working values */
1324 struct cpufreq_governor
*old_gov
= data
->governor
;
1326 dprintk("governor switch\n");
1328 /* end old governor */
1330 __cpufreq_governor(data
, CPUFREQ_GOV_STOP
);
1332 /* start new governor */
1333 data
->governor
= policy
->governor
;
1334 if (__cpufreq_governor(data
, CPUFREQ_GOV_START
)) {
1335 /* new governor failed, so re-start old one */
1336 dprintk("starting governor %s failed\n", data
->governor
->name
);
1338 data
->governor
= old_gov
;
1339 __cpufreq_governor(data
, CPUFREQ_GOV_START
);
1344 /* might be a policy change, too, so fall through */
1346 dprintk("governor: change or update limits\n");
1347 __cpufreq_governor(data
, CPUFREQ_GOV_LIMITS
);
1351 cpufreq_debug_enable_ratelimit();
1356 * cpufreq_set_policy - set a new CPUFreq policy
1357 * @policy: policy to be set.
1359 * Sets a new CPU frequency and voltage scaling policy.
1361 int cpufreq_set_policy(struct cpufreq_policy
*policy
)
1364 struct cpufreq_policy
*data
;
1369 data
= cpufreq_cpu_get(policy
->cpu
);
1376 ret
= __cpufreq_set_policy(data
, policy
);
1377 data
->user_policy
.min
= data
->min
;
1378 data
->user_policy
.max
= data
->max
;
1379 data
->user_policy
.policy
= data
->policy
;
1380 data
->user_policy
.governor
= data
->governor
;
1383 cpufreq_cpu_put(data
);
1387 EXPORT_SYMBOL(cpufreq_set_policy
);
1391 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1392 * @cpu: CPU which shall be re-evaluated
1394 * Usefull for policy notifiers which have different necessities
1395 * at different times.
1397 int cpufreq_update_policy(unsigned int cpu
)
1399 struct cpufreq_policy
*data
= cpufreq_cpu_get(cpu
);
1400 struct cpufreq_policy policy
;
1408 dprintk("updating policy for CPU %u\n", cpu
);
1411 sizeof(struct cpufreq_policy
));
1412 policy
.min
= data
->user_policy
.min
;
1413 policy
.max
= data
->user_policy
.max
;
1414 policy
.policy
= data
->user_policy
.policy
;
1415 policy
.governor
= data
->user_policy
.governor
;
1417 ret
= __cpufreq_set_policy(data
, &policy
);
1421 cpufreq_cpu_put(data
);
1424 EXPORT_SYMBOL(cpufreq_update_policy
);
1426 static int __cpuinit
cpufreq_cpu_callback(struct notifier_block
*nfb
,
1427 unsigned long action
, void *hcpu
)
1429 unsigned int cpu
= (unsigned long)hcpu
;
1430 struct cpufreq_policy
*policy
;
1431 struct sys_device
*sys_dev
;
1433 sys_dev
= get_cpu_sysdev(cpu
);
1438 cpufreq_add_dev(sys_dev
);
1440 case CPU_DOWN_PREPARE
:
1442 * We attempt to put this cpu in lowest frequency
1443 * possible before going down. This will permit
1444 * hardware-managed P-State to switch other related
1445 * threads to min or higher speeds if possible.
1447 policy
= cpufreq_cpu_data
[cpu
];
1449 cpufreq_driver_target(policy
, policy
->min
,
1450 CPUFREQ_RELATION_H
);
1454 cpufreq_remove_dev(sys_dev
);
1461 static struct notifier_block cpufreq_cpu_notifier
=
1463 .notifier_call
= cpufreq_cpu_callback
,
1466 /*********************************************************************
1467 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1468 *********************************************************************/
1471 * cpufreq_register_driver - register a CPU Frequency driver
1472 * @driver_data: A struct cpufreq_driver containing the values#
1473 * submitted by the CPU Frequency driver.
1475 * Registers a CPU Frequency driver to this core code. This code
1476 * returns zero on success, -EBUSY when another driver got here first
1477 * (and isn't unregistered in the meantime).
1480 int cpufreq_register_driver(struct cpufreq_driver
*driver_data
)
1482 unsigned long flags
;
1485 if (!driver_data
|| !driver_data
->verify
|| !driver_data
->init
||
1486 ((!driver_data
->setpolicy
) && (!driver_data
->target
)))
1489 dprintk("trying to register driver %s\n", driver_data
->name
);
1491 if (driver_data
->setpolicy
)
1492 driver_data
->flags
|= CPUFREQ_CONST_LOOPS
;
1494 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1495 if (cpufreq_driver
) {
1496 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1499 cpufreq_driver
= driver_data
;
1500 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1502 ret
= sysdev_driver_register(&cpu_sysdev_class
,&cpufreq_sysdev_driver
);
1504 if ((!ret
) && !(cpufreq_driver
->flags
& CPUFREQ_STICKY
)) {
1508 /* check for at least one working CPU */
1509 for (i
=0; i
<NR_CPUS
; i
++)
1510 if (cpufreq_cpu_data
[i
])
1513 /* if all ->init() calls failed, unregister */
1515 dprintk("no CPU initialized for driver %s\n", driver_data
->name
);
1516 sysdev_driver_unregister(&cpu_sysdev_class
, &cpufreq_sysdev_driver
);
1518 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1519 cpufreq_driver
= NULL
;
1520 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1525 register_cpu_notifier(&cpufreq_cpu_notifier
);
1526 dprintk("driver %s up and running\n", driver_data
->name
);
1527 cpufreq_debug_enable_ratelimit();
1532 EXPORT_SYMBOL_GPL(cpufreq_register_driver
);
1536 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1538 * Unregister the current CPUFreq driver. Only call this if you have
1539 * the right to do so, i.e. if you have succeeded in initialising before!
1540 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1541 * currently not initialised.
1543 int cpufreq_unregister_driver(struct cpufreq_driver
*driver
)
1545 unsigned long flags
;
1547 cpufreq_debug_disable_ratelimit();
1549 if (!cpufreq_driver
|| (driver
!= cpufreq_driver
)) {
1550 cpufreq_debug_enable_ratelimit();
1554 dprintk("unregistering driver %s\n", driver
->name
);
1556 sysdev_driver_unregister(&cpu_sysdev_class
, &cpufreq_sysdev_driver
);
1557 unregister_cpu_notifier(&cpufreq_cpu_notifier
);
1559 spin_lock_irqsave(&cpufreq_driver_lock
, flags
);
1560 cpufreq_driver
= NULL
;
1561 spin_unlock_irqrestore(&cpufreq_driver_lock
, flags
);
1565 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver
);