GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / cpufreq / cpufreq.c
blob199dcb9f0b836da1431a5791b05309d79f9c330c
1 /*
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 #include <trace/events/power.h>
34 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
35 "cpufreq-core", msg)
37 /**
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);
47 #endif
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.
61 * Additional rules:
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 \
74 (int cpu) \
75 { \
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)); \
81 return -1; \
82 } \
84 return 0; \
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,
108 unsigned int event);
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;
127 return 0;
129 pure_initcall(init_cpufreq_transition_notifier_list);
131 static LIST_HEAD(cpufreq_governor_list);
132 static DEFINE_MUTEX(cpufreq_governor_mutex);
134 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
136 struct cpufreq_policy *data;
137 unsigned long flags;
139 if (cpu >= nr_cpu_ids)
140 goto err_out;
142 /* get the cpufreq driver */
143 spin_lock_irqsave(&cpufreq_driver_lock, flags);
145 if (!cpufreq_driver)
146 goto err_out_unlock;
148 if (!try_module_get(cpufreq_driver->owner))
149 goto err_out_unlock;
152 /* get the CPU */
153 data = per_cpu(cpufreq_cpu_data, cpu);
155 if (!data)
156 goto err_out_put_module;
158 if (!kobject_get(&data->kobj))
159 goto err_out_put_module;
161 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
162 return data;
164 err_out_put_module:
165 module_put(cpufreq_driver->owner);
166 err_out_unlock:
167 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
168 err_out:
169 return NULL;
171 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
174 void cpufreq_cpu_put(struct cpufreq_policy *data)
176 kobject_put(&data->kobj);
177 module_put(cpufreq_driver->owner);
179 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
182 /*********************************************************************
183 * UNIFIED DEBUG HELPERS *
184 *********************************************************************/
185 #ifdef CONFIG_CPU_FREQ_DEBUG
187 /* what part(s) of the CPUfreq subsystem are debugged? */
188 static unsigned int debug;
190 /* is the debug output ratelimit'ed using printk_ratelimit? User can
191 * set or modify this value.
193 static unsigned int debug_ratelimit = 1;
195 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
196 * loading of a cpufreq driver, temporarily disabled when a new policy
197 * is set, and disabled upon cpufreq driver removal
199 static unsigned int disable_ratelimit = 1;
200 static DEFINE_SPINLOCK(disable_ratelimit_lock);
202 static void cpufreq_debug_enable_ratelimit(void)
204 unsigned long flags;
206 spin_lock_irqsave(&disable_ratelimit_lock, flags);
207 if (disable_ratelimit)
208 disable_ratelimit--;
209 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
212 static void cpufreq_debug_disable_ratelimit(void)
214 unsigned long flags;
216 spin_lock_irqsave(&disable_ratelimit_lock, flags);
217 disable_ratelimit++;
218 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
221 void cpufreq_debug_printk(unsigned int type, const char *prefix,
222 const char *fmt, ...)
224 char s[256];
225 va_list args;
226 unsigned int len;
227 unsigned long flags;
229 WARN_ON(!prefix);
230 if (type & debug) {
231 spin_lock_irqsave(&disable_ratelimit_lock, flags);
232 if (!disable_ratelimit && debug_ratelimit
233 && !printk_ratelimit()) {
234 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
235 return;
237 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
239 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
241 va_start(args, fmt);
242 len += vsnprintf(&s[len], (256 - len), fmt, args);
243 va_end(args);
245 printk(s);
247 WARN_ON(len < 5);
250 EXPORT_SYMBOL(cpufreq_debug_printk);
253 module_param(debug, uint, 0644);
254 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
255 " 2 to debug drivers, and 4 to debug governors.");
257 module_param(debug_ratelimit, uint, 0644);
258 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
259 " set to 0 to disable ratelimiting.");
261 #else /* !CONFIG_CPU_FREQ_DEBUG */
263 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
264 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
266 #endif /* CONFIG_CPU_FREQ_DEBUG */
269 /*********************************************************************
270 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
271 *********************************************************************/
274 * adjust_jiffies - adjust the system "loops_per_jiffy"
276 * This function alters the system "loops_per_jiffy" for the clock
277 * speed change. Note that loops_per_jiffy cannot be updated on SMP
278 * systems as each CPU might be scaled differently. So, use the arch
279 * per-CPU loops_per_jiffy value wherever possible.
281 #ifndef CONFIG_SMP
282 static unsigned long l_p_j_ref;
283 static unsigned int l_p_j_ref_freq;
285 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
287 if (ci->flags & CPUFREQ_CONST_LOOPS)
288 return;
290 if (!l_p_j_ref_freq) {
291 l_p_j_ref = loops_per_jiffy;
292 l_p_j_ref_freq = ci->old;
293 dprintk("saving %lu as reference value for loops_per_jiffy; "
294 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
296 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
297 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
298 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
299 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
300 ci->new);
301 dprintk("scaling loops_per_jiffy to %lu "
302 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
305 #else
306 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
308 return;
310 #endif
314 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
315 * on frequency transition.
317 * This function calls the transition notifiers and the "adjust_jiffies"
318 * function. It is called twice on all CPU frequency changes that have
319 * external effects.
321 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
323 struct cpufreq_policy *policy;
325 BUG_ON(irqs_disabled());
327 freqs->flags = cpufreq_driver->flags;
328 dprintk("notification %u of frequency transition to %u kHz\n",
329 state, freqs->new);
331 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
332 switch (state) {
334 case CPUFREQ_PRECHANGE:
335 /* detect if the driver reported a value as "old frequency"
336 * which is not equal to what the cpufreq core thinks is
337 * "old frequency".
339 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
340 if ((policy) && (policy->cpu == freqs->cpu) &&
341 (policy->cur) && (policy->cur != freqs->old)) {
342 dprintk("Warning: CPU frequency is"
343 " %u, cpufreq assumed %u kHz.\n",
344 freqs->old, policy->cur);
345 freqs->old = policy->cur;
348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
349 CPUFREQ_PRECHANGE, freqs);
350 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
351 break;
353 case CPUFREQ_POSTCHANGE:
354 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
355 dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
356 (unsigned long)freqs->cpu);
357 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
358 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
359 CPUFREQ_POSTCHANGE, freqs);
360 if (likely(policy) && likely(policy->cpu == freqs->cpu))
361 policy->cur = freqs->new;
362 break;
365 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
369 /*********************************************************************
370 * SYSFS INTERFACE *
371 *********************************************************************/
373 static struct cpufreq_governor *__find_governor(const char *str_governor)
375 struct cpufreq_governor *t;
377 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
378 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
379 return t;
381 return NULL;
385 * cpufreq_parse_governor - parse a governor string
387 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
388 struct cpufreq_governor **governor)
390 int err = -EINVAL;
392 if (!cpufreq_driver)
393 goto out;
395 if (cpufreq_driver->setpolicy) {
396 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
397 *policy = CPUFREQ_POLICY_PERFORMANCE;
398 err = 0;
399 } else if (!strnicmp(str_governor, "powersave",
400 CPUFREQ_NAME_LEN)) {
401 *policy = CPUFREQ_POLICY_POWERSAVE;
402 err = 0;
404 } else if (cpufreq_driver->target) {
405 struct cpufreq_governor *t;
407 mutex_lock(&cpufreq_governor_mutex);
409 t = __find_governor(str_governor);
411 if (t == NULL) {
412 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
413 str_governor);
415 if (name) {
416 int ret;
418 mutex_unlock(&cpufreq_governor_mutex);
419 ret = request_module("%s", name);
420 mutex_lock(&cpufreq_governor_mutex);
422 if (ret == 0)
423 t = __find_governor(str_governor);
426 kfree(name);
429 if (t != NULL) {
430 *governor = t;
431 err = 0;
434 mutex_unlock(&cpufreq_governor_mutex);
436 out:
437 return err;
442 * cpufreq_per_cpu_attr_read() / show_##file_name() -
443 * print out cpufreq information
445 * Write out information from cpufreq_driver->policy[cpu]; object must be
446 * "unsigned int".
449 #define show_one(file_name, object) \
450 static ssize_t show_##file_name \
451 (struct cpufreq_policy *policy, char *buf) \
453 return sprintf(buf, "%u\n", policy->object); \
456 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
457 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
458 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
459 show_one(scaling_min_freq, min);
460 show_one(scaling_max_freq, max);
461 show_one(scaling_cur_freq, cur);
463 static int __cpufreq_set_policy(struct cpufreq_policy *data,
464 struct cpufreq_policy *policy);
467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
469 #define store_one(file_name, object) \
470 static ssize_t store_##file_name \
471 (struct cpufreq_policy *policy, const char *buf, size_t count) \
473 unsigned int ret = -EINVAL; \
474 struct cpufreq_policy new_policy; \
476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
477 if (ret) \
478 return -EINVAL; \
480 ret = sscanf(buf, "%u", &new_policy.object); \
481 if (ret != 1) \
482 return -EINVAL; \
484 ret = __cpufreq_set_policy(policy, &new_policy); \
485 policy->user_policy.object = policy->object; \
487 return ret ? ret : count; \
490 store_one(scaling_min_freq, min);
491 store_one(scaling_max_freq, max);
494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
496 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
497 char *buf)
499 unsigned int cur_freq = __cpufreq_get(policy->cpu);
500 if (!cur_freq)
501 return sprintf(buf, "<unknown>");
502 return sprintf(buf, "%u\n", cur_freq);
507 * show_scaling_governor - show the current policy for the specified CPU
509 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
511 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
512 return sprintf(buf, "powersave\n");
513 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
514 return sprintf(buf, "performance\n");
515 else if (policy->governor)
516 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
517 policy->governor->name);
518 return -EINVAL;
523 * store_scaling_governor - store policy for the specified CPU
525 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
526 const char *buf, size_t count)
528 unsigned int ret = -EINVAL;
529 char str_governor[16];
530 struct cpufreq_policy new_policy;
532 ret = cpufreq_get_policy(&new_policy, policy->cpu);
533 if (ret)
534 return ret;
536 ret = sscanf(buf, "%15s", str_governor);
537 if (ret != 1)
538 return -EINVAL;
540 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
541 &new_policy.governor))
542 return -EINVAL;
544 /* Do not use cpufreq_set_policy here or the user_policy.max
545 will be wrongly overridden */
546 ret = __cpufreq_set_policy(policy, &new_policy);
548 policy->user_policy.policy = policy->policy;
549 policy->user_policy.governor = policy->governor;
551 if (ret)
552 return ret;
553 else
554 return count;
558 * show_scaling_driver - show the cpufreq driver currently loaded
560 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
562 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
566 * show_scaling_available_governors - show the available CPUfreq governors
568 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
569 char *buf)
571 ssize_t i = 0;
572 struct cpufreq_governor *t;
574 if (!cpufreq_driver->target) {
575 i += sprintf(buf, "performance powersave");
576 goto out;
579 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
580 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
581 - (CPUFREQ_NAME_LEN + 2)))
582 goto out;
583 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
585 out:
586 i += sprintf(&buf[i], "\n");
587 return i;
590 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
592 ssize_t i = 0;
593 unsigned int cpu;
595 for_each_cpu(cpu, mask) {
596 if (i)
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
598 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
599 if (i >= (PAGE_SIZE - 5))
600 break;
602 i += sprintf(&buf[i], "\n");
603 return i;
607 * show_related_cpus - show the CPUs affected by each transition even if
608 * hw coordination is in use
610 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
612 if (cpumask_empty(policy->related_cpus))
613 return show_cpus(policy->cpus, buf);
614 return show_cpus(policy->related_cpus, buf);
618 * show_affected_cpus - show the CPUs affected by each transition
620 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
622 return show_cpus(policy->cpus, buf);
625 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
626 const char *buf, size_t count)
628 unsigned int freq = 0;
629 unsigned int ret;
631 if (!policy->governor || !policy->governor->store_setspeed)
632 return -EINVAL;
634 ret = sscanf(buf, "%u", &freq);
635 if (ret != 1)
636 return -EINVAL;
638 policy->governor->store_setspeed(policy, freq);
640 return count;
643 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
645 if (!policy->governor || !policy->governor->show_setspeed)
646 return sprintf(buf, "<unsupported>\n");
648 return policy->governor->show_setspeed(policy, buf);
652 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
654 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
656 unsigned int limit;
657 int ret;
658 if (cpufreq_driver->bios_limit) {
659 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
660 if (!ret)
661 return sprintf(buf, "%u\n", limit);
663 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
666 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
667 cpufreq_freq_attr_ro(cpuinfo_min_freq);
668 cpufreq_freq_attr_ro(cpuinfo_max_freq);
669 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
670 cpufreq_freq_attr_ro(scaling_available_governors);
671 cpufreq_freq_attr_ro(scaling_driver);
672 cpufreq_freq_attr_ro(scaling_cur_freq);
673 cpufreq_freq_attr_ro(bios_limit);
674 cpufreq_freq_attr_ro(related_cpus);
675 cpufreq_freq_attr_ro(affected_cpus);
676 cpufreq_freq_attr_rw(scaling_min_freq);
677 cpufreq_freq_attr_rw(scaling_max_freq);
678 cpufreq_freq_attr_rw(scaling_governor);
679 cpufreq_freq_attr_rw(scaling_setspeed);
681 static struct attribute *default_attrs[] = {
682 &cpuinfo_min_freq.attr,
683 &cpuinfo_max_freq.attr,
684 &cpuinfo_transition_latency.attr,
685 &scaling_min_freq.attr,
686 &scaling_max_freq.attr,
687 &affected_cpus.attr,
688 &related_cpus.attr,
689 &scaling_governor.attr,
690 &scaling_driver.attr,
691 &scaling_available_governors.attr,
692 &scaling_setspeed.attr,
693 NULL
696 struct kobject *cpufreq_global_kobject;
697 EXPORT_SYMBOL(cpufreq_global_kobject);
699 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
700 #define to_attr(a) container_of(a, struct freq_attr, attr)
702 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
704 struct cpufreq_policy *policy = to_policy(kobj);
705 struct freq_attr *fattr = to_attr(attr);
706 ssize_t ret = -EINVAL;
707 policy = cpufreq_cpu_get(policy->cpu);
708 if (!policy)
709 goto no_policy;
711 if (lock_policy_rwsem_read(policy->cpu) < 0)
712 goto fail;
714 if (fattr->show)
715 ret = fattr->show(policy, buf);
716 else
717 ret = -EIO;
719 unlock_policy_rwsem_read(policy->cpu);
720 fail:
721 cpufreq_cpu_put(policy);
722 no_policy:
723 return ret;
726 static ssize_t store(struct kobject *kobj, struct attribute *attr,
727 const char *buf, size_t count)
729 struct cpufreq_policy *policy = to_policy(kobj);
730 struct freq_attr *fattr = to_attr(attr);
731 ssize_t ret = -EINVAL;
732 policy = cpufreq_cpu_get(policy->cpu);
733 if (!policy)
734 goto no_policy;
736 if (lock_policy_rwsem_write(policy->cpu) < 0)
737 goto fail;
739 if (fattr->store)
740 ret = fattr->store(policy, buf, count);
741 else
742 ret = -EIO;
744 unlock_policy_rwsem_write(policy->cpu);
745 fail:
746 cpufreq_cpu_put(policy);
747 no_policy:
748 return ret;
751 static void cpufreq_sysfs_release(struct kobject *kobj)
753 struct cpufreq_policy *policy = to_policy(kobj);
754 dprintk("last reference is dropped\n");
755 complete(&policy->kobj_unregister);
758 static const struct sysfs_ops sysfs_ops = {
759 .show = show,
760 .store = store,
763 static struct kobj_type ktype_cpufreq = {
764 .sysfs_ops = &sysfs_ops,
765 .default_attrs = default_attrs,
766 .release = cpufreq_sysfs_release,
770 * Returns:
771 * Negative: Failure
772 * 0: Success
773 * Positive: When we have a managed CPU and the sysfs got symlinked
775 static int cpufreq_add_dev_policy(unsigned int cpu,
776 struct cpufreq_policy *policy,
777 struct sys_device *sys_dev)
779 int ret = 0;
780 #ifdef CONFIG_SMP
781 unsigned long flags;
782 unsigned int j;
783 #ifdef CONFIG_HOTPLUG_CPU
784 struct cpufreq_governor *gov;
786 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
787 if (gov) {
788 policy->governor = gov;
789 dprintk("Restoring governor %s for cpu %d\n",
790 policy->governor->name, cpu);
792 #endif
794 for_each_cpu(j, policy->cpus) {
795 struct cpufreq_policy *managed_policy;
797 if (cpu == j)
798 continue;
800 /* Check for existing affected CPUs.
801 * They may not be aware of it due to CPU Hotplug.
802 * cpufreq_cpu_put is called when the device is removed
803 * in __cpufreq_remove_dev()
805 managed_policy = cpufreq_cpu_get(j);
806 if (unlikely(managed_policy)) {
808 /* Set proper policy_cpu */
809 unlock_policy_rwsem_write(cpu);
810 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
812 if (lock_policy_rwsem_write(cpu) < 0) {
813 /* Should not go through policy unlock path */
814 if (cpufreq_driver->exit)
815 cpufreq_driver->exit(policy);
816 cpufreq_cpu_put(managed_policy);
817 return -EBUSY;
820 spin_lock_irqsave(&cpufreq_driver_lock, flags);
821 cpumask_copy(managed_policy->cpus, policy->cpus);
822 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
823 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
825 dprintk("CPU already managed, adding link\n");
826 ret = sysfs_create_link(&sys_dev->kobj,
827 &managed_policy->kobj,
828 "cpufreq");
829 if (ret)
830 cpufreq_cpu_put(managed_policy);
832 * Success. We only needed to be added to the mask.
833 * Call driver->exit() because only the cpu parent of
834 * the kobj needed to call init().
836 if (cpufreq_driver->exit)
837 cpufreq_driver->exit(policy);
839 if (!ret)
840 return 1;
841 else
842 return ret;
845 #endif
846 return ret;
850 /* symlink affected CPUs */
851 static int cpufreq_add_dev_symlink(unsigned int cpu,
852 struct cpufreq_policy *policy)
854 unsigned int j;
855 int ret = 0;
857 for_each_cpu(j, policy->cpus) {
858 struct cpufreq_policy *managed_policy;
859 struct sys_device *cpu_sys_dev;
861 if (j == cpu)
862 continue;
863 if (!cpu_online(j))
864 continue;
866 dprintk("CPU %u already managed, adding link\n", j);
867 managed_policy = cpufreq_cpu_get(cpu);
868 cpu_sys_dev = get_cpu_sysdev(j);
869 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
870 "cpufreq");
871 if (ret) {
872 cpufreq_cpu_put(managed_policy);
873 return ret;
876 return ret;
879 static int cpufreq_add_dev_interface(unsigned int cpu,
880 struct cpufreq_policy *policy,
881 struct sys_device *sys_dev)
883 struct cpufreq_policy new_policy;
884 struct freq_attr **drv_attr;
885 unsigned long flags;
886 int ret = 0;
887 unsigned int j;
889 /* prepare interface data */
890 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
891 &sys_dev->kobj, "cpufreq");
892 if (ret)
893 return ret;
895 /* set up files for this cpu device */
896 drv_attr = cpufreq_driver->attr;
897 while ((drv_attr) && (*drv_attr)) {
898 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
899 if (ret)
900 goto err_out_kobj_put;
901 drv_attr++;
903 if (cpufreq_driver->get) {
904 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
905 if (ret)
906 goto err_out_kobj_put;
908 if (cpufreq_driver->target) {
909 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
910 if (ret)
911 goto err_out_kobj_put;
913 if (cpufreq_driver->bios_limit) {
914 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
915 if (ret)
916 goto err_out_kobj_put;
919 spin_lock_irqsave(&cpufreq_driver_lock, flags);
920 for_each_cpu(j, policy->cpus) {
921 if (!cpu_online(j))
922 continue;
923 per_cpu(cpufreq_cpu_data, j) = policy;
924 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
926 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
928 ret = cpufreq_add_dev_symlink(cpu, policy);
929 if (ret)
930 goto err_out_kobj_put;
932 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
933 /* assure that the starting sequence is run in __cpufreq_set_policy */
934 policy->governor = NULL;
936 /* set default policy */
937 ret = __cpufreq_set_policy(policy, &new_policy);
938 policy->user_policy.policy = policy->policy;
939 policy->user_policy.governor = policy->governor;
941 if (ret) {
942 dprintk("setting policy failed\n");
943 if (cpufreq_driver->exit)
944 cpufreq_driver->exit(policy);
946 return ret;
948 err_out_kobj_put:
949 kobject_put(&policy->kobj);
950 wait_for_completion(&policy->kobj_unregister);
951 return ret;
956 * cpufreq_add_dev - add a CPU device
958 * Adds the cpufreq interface for a CPU device.
960 * The Oracle says: try running cpufreq registration/unregistration concurrently
961 * with with cpu hotplugging and all hell will break loose. Tried to clean this
962 * mess up, but more thorough testing is needed. - Mathieu
964 static int cpufreq_add_dev(struct sys_device *sys_dev)
966 unsigned int cpu = sys_dev->id;
967 int ret = 0, found = 0;
968 struct cpufreq_policy *policy;
969 unsigned long flags;
970 unsigned int j;
971 #ifdef CONFIG_HOTPLUG_CPU
972 int sibling;
973 #endif
975 if (cpu_is_offline(cpu))
976 return 0;
978 cpufreq_debug_disable_ratelimit();
979 dprintk("adding CPU %u\n", cpu);
981 #ifdef CONFIG_SMP
982 /* check whether a different CPU already registered this
983 * CPU because it is in the same boat. */
984 policy = cpufreq_cpu_get(cpu);
985 if (unlikely(policy)) {
986 cpufreq_cpu_put(policy);
987 cpufreq_debug_enable_ratelimit();
988 return 0;
990 #endif
992 if (!try_module_get(cpufreq_driver->owner)) {
993 ret = -EINVAL;
994 goto module_out;
997 ret = -ENOMEM;
998 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
999 if (!policy)
1000 goto nomem_out;
1002 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1003 goto err_free_policy;
1005 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1006 goto err_free_cpumask;
1008 policy->cpu = cpu;
1009 cpumask_copy(policy->cpus, cpumask_of(cpu));
1011 /* Initially set CPU itself as the policy_cpu */
1012 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
1013 ret = (lock_policy_rwsem_write(cpu) < 0);
1014 WARN_ON(ret);
1016 init_completion(&policy->kobj_unregister);
1017 INIT_WORK(&policy->update, handle_update);
1019 /* Set governor before ->init, so that driver could check it */
1020 #ifdef CONFIG_HOTPLUG_CPU
1021 for_each_online_cpu(sibling) {
1022 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1023 if (cp && cp->governor &&
1024 (cpumask_test_cpu(cpu, cp->related_cpus))) {
1025 policy->governor = cp->governor;
1026 found = 1;
1027 break;
1030 #endif
1031 if (!found)
1032 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1033 /* call driver. From then on the cpufreq must be able
1034 * to accept all calls to ->verify and ->setpolicy for this CPU
1036 ret = cpufreq_driver->init(policy);
1037 if (ret) {
1038 dprintk("initialization failed\n");
1039 goto err_unlock_policy;
1041 policy->user_policy.min = policy->min;
1042 policy->user_policy.max = policy->max;
1044 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1045 CPUFREQ_START, policy);
1047 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
1048 if (ret) {
1049 if (ret > 0)
1050 /* This is a managed cpu, symlink created,
1051 exit with 0 */
1052 ret = 0;
1053 goto err_unlock_policy;
1056 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
1057 if (ret)
1058 goto err_out_unregister;
1060 unlock_policy_rwsem_write(cpu);
1062 kobject_uevent(&policy->kobj, KOBJ_ADD);
1063 module_put(cpufreq_driver->owner);
1064 dprintk("initialization complete\n");
1065 cpufreq_debug_enable_ratelimit();
1067 return 0;
1070 err_out_unregister:
1071 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1072 for_each_cpu(j, policy->cpus)
1073 per_cpu(cpufreq_cpu_data, j) = NULL;
1074 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1076 kobject_put(&policy->kobj);
1077 wait_for_completion(&policy->kobj_unregister);
1079 err_unlock_policy:
1080 unlock_policy_rwsem_write(cpu);
1081 free_cpumask_var(policy->related_cpus);
1082 err_free_cpumask:
1083 free_cpumask_var(policy->cpus);
1084 err_free_policy:
1085 kfree(policy);
1086 nomem_out:
1087 module_put(cpufreq_driver->owner);
1088 module_out:
1089 cpufreq_debug_enable_ratelimit();
1090 return ret;
1095 * __cpufreq_remove_dev - remove a CPU device
1097 * Removes the cpufreq interface for a CPU device.
1098 * Caller should already have policy_rwsem in write mode for this CPU.
1099 * This routine frees the rwsem before returning.
1101 static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1103 unsigned int cpu = sys_dev->id;
1104 unsigned long flags;
1105 struct cpufreq_policy *data;
1106 struct kobject *kobj;
1107 struct completion *cmp;
1108 #ifdef CONFIG_SMP
1109 struct sys_device *cpu_sys_dev;
1110 unsigned int j;
1111 #endif
1113 cpufreq_debug_disable_ratelimit();
1114 dprintk("unregistering CPU %u\n", cpu);
1116 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1117 data = per_cpu(cpufreq_cpu_data, cpu);
1119 if (!data) {
1120 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1121 cpufreq_debug_enable_ratelimit();
1122 unlock_policy_rwsem_write(cpu);
1123 return -EINVAL;
1125 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1128 #ifdef CONFIG_SMP
1129 /* if this isn't the CPU which is the parent of the kobj, we
1130 * only need to unlink, put and exit
1132 if (unlikely(cpu != data->cpu)) {
1133 dprintk("removing link\n");
1134 cpumask_clear_cpu(cpu, data->cpus);
1135 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1136 kobj = &sys_dev->kobj;
1137 cpufreq_cpu_put(data);
1138 cpufreq_debug_enable_ratelimit();
1139 unlock_policy_rwsem_write(cpu);
1140 sysfs_remove_link(kobj, "cpufreq");
1141 return 0;
1143 #endif
1145 #ifdef CONFIG_SMP
1147 #ifdef CONFIG_HOTPLUG_CPU
1148 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1149 CPUFREQ_NAME_LEN);
1150 #endif
1152 /* if we have other CPUs still registered, we need to unlink them,
1153 * or else wait_for_completion below will lock up. Clean the
1154 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1155 * the sysfs links afterwards.
1157 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1158 for_each_cpu(j, data->cpus) {
1159 if (j == cpu)
1160 continue;
1161 per_cpu(cpufreq_cpu_data, j) = NULL;
1165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1167 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1168 for_each_cpu(j, data->cpus) {
1169 if (j == cpu)
1170 continue;
1171 dprintk("removing link for cpu %u\n", j);
1172 #ifdef CONFIG_HOTPLUG_CPU
1173 strncpy(per_cpu(cpufreq_cpu_governor, j),
1174 data->governor->name, CPUFREQ_NAME_LEN);
1175 #endif
1176 cpu_sys_dev = get_cpu_sysdev(j);
1177 kobj = &cpu_sys_dev->kobj;
1178 unlock_policy_rwsem_write(cpu);
1179 sysfs_remove_link(kobj, "cpufreq");
1180 lock_policy_rwsem_write(cpu);
1181 cpufreq_cpu_put(data);
1184 #else
1185 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1186 #endif
1188 if (cpufreq_driver->target)
1189 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1191 kobj = &data->kobj;
1192 cmp = &data->kobj_unregister;
1193 unlock_policy_rwsem_write(cpu);
1194 kobject_put(kobj);
1196 /* we need to make sure that the underlying kobj is actually
1197 * not referenced anymore by anybody before we proceed with
1198 * unloading.
1200 dprintk("waiting for dropping of refcount\n");
1201 wait_for_completion(cmp);
1202 dprintk("wait complete\n");
1204 lock_policy_rwsem_write(cpu);
1205 if (cpufreq_driver->exit)
1206 cpufreq_driver->exit(data);
1207 unlock_policy_rwsem_write(cpu);
1209 free_cpumask_var(data->related_cpus);
1210 free_cpumask_var(data->cpus);
1211 kfree(data);
1212 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1214 cpufreq_debug_enable_ratelimit();
1215 return 0;
1219 static int cpufreq_remove_dev(struct sys_device *sys_dev)
1221 unsigned int cpu = sys_dev->id;
1222 int retval;
1224 if (cpu_is_offline(cpu))
1225 return 0;
1227 if (unlikely(lock_policy_rwsem_write(cpu)))
1228 BUG();
1230 retval = __cpufreq_remove_dev(sys_dev);
1231 return retval;
1235 static void handle_update(struct work_struct *work)
1237 struct cpufreq_policy *policy =
1238 container_of(work, struct cpufreq_policy, update);
1239 unsigned int cpu = policy->cpu;
1240 dprintk("handle_update for cpu %u called\n", cpu);
1241 cpufreq_update_policy(cpu);
1245 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1246 * @cpu: cpu number
1247 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1248 * @new_freq: CPU frequency the CPU actually runs at
1250 * We adjust to current frequency first, and need to clean up later.
1251 * So either call to cpufreq_update_policy() or schedule handle_update()).
1253 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1254 unsigned int new_freq)
1256 struct cpufreq_freqs freqs;
1258 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
1259 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1261 freqs.cpu = cpu;
1262 freqs.old = old_freq;
1263 freqs.new = new_freq;
1264 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1265 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1270 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1271 * @cpu: CPU number
1273 * This is the last known freq, without actually getting it from the driver.
1274 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1276 unsigned int cpufreq_quick_get(unsigned int cpu)
1278 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1279 unsigned int ret_freq = 0;
1281 if (policy) {
1282 ret_freq = policy->cur;
1283 cpufreq_cpu_put(policy);
1286 return ret_freq;
1288 EXPORT_SYMBOL(cpufreq_quick_get);
1291 static unsigned int __cpufreq_get(unsigned int cpu)
1293 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1294 unsigned int ret_freq = 0;
1296 if (!cpufreq_driver->get)
1297 return ret_freq;
1299 ret_freq = cpufreq_driver->get(cpu);
1301 if (ret_freq && policy->cur &&
1302 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1303 /* verify no discrepancy between actual and
1304 saved value exists */
1305 if (unlikely(ret_freq != policy->cur)) {
1306 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1307 schedule_work(&policy->update);
1311 return ret_freq;
1315 * cpufreq_get - get the current CPU frequency (in kHz)
1316 * @cpu: CPU number
1318 * Get the CPU current (static) CPU frequency
1320 unsigned int cpufreq_get(unsigned int cpu)
1322 unsigned int ret_freq = 0;
1323 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1325 if (!policy)
1326 goto out;
1328 if (unlikely(lock_policy_rwsem_read(cpu)))
1329 goto out_policy;
1331 ret_freq = __cpufreq_get(cpu);
1333 unlock_policy_rwsem_read(cpu);
1335 out_policy:
1336 cpufreq_cpu_put(policy);
1337 out:
1338 return ret_freq;
1340 EXPORT_SYMBOL(cpufreq_get);
1344 * cpufreq_suspend - let the low level driver prepare for suspend
1347 static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
1349 int ret = 0;
1351 int cpu = sysdev->id;
1352 struct cpufreq_policy *cpu_policy;
1354 dprintk("suspending cpu %u\n", cpu);
1356 if (!cpu_online(cpu))
1357 return 0;
1359 /* we may be lax here as interrupts are off. Nonetheless
1360 * we need to grab the correct cpu policy, as to check
1361 * whether we really run on this CPU.
1364 cpu_policy = cpufreq_cpu_get(cpu);
1365 if (!cpu_policy)
1366 return -EINVAL;
1368 /* only handle each CPU group once */
1369 if (unlikely(cpu_policy->cpu != cpu))
1370 goto out;
1372 if (cpufreq_driver->suspend) {
1373 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
1374 if (ret)
1375 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1376 "step on CPU %u\n", cpu_policy->cpu);
1379 out:
1380 cpufreq_cpu_put(cpu_policy);
1381 return ret;
1385 * cpufreq_resume - restore proper CPU frequency handling after resume
1387 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1388 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1389 * restored. It will verify that the current freq is in sync with
1390 * what we believe it to be. This is a bit later than when it
1391 * should be, but nonethteless it's better than calling
1392 * cpufreq_driver->get() here which might re-enable interrupts...
1394 static int cpufreq_resume(struct sys_device *sysdev)
1396 int ret = 0;
1398 int cpu = sysdev->id;
1399 struct cpufreq_policy *cpu_policy;
1401 dprintk("resuming cpu %u\n", cpu);
1403 if (!cpu_online(cpu))
1404 return 0;
1406 /* we may be lax here as interrupts are off. Nonetheless
1407 * we need to grab the correct cpu policy, as to check
1408 * whether we really run on this CPU.
1411 cpu_policy = cpufreq_cpu_get(cpu);
1412 if (!cpu_policy)
1413 return -EINVAL;
1415 /* only handle each CPU group once */
1416 if (unlikely(cpu_policy->cpu != cpu))
1417 goto fail;
1419 if (cpufreq_driver->resume) {
1420 ret = cpufreq_driver->resume(cpu_policy);
1421 if (ret) {
1422 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1423 "step on CPU %u\n", cpu_policy->cpu);
1424 goto fail;
1428 schedule_work(&cpu_policy->update);
1430 fail:
1431 cpufreq_cpu_put(cpu_policy);
1432 return ret;
1435 static struct sysdev_driver cpufreq_sysdev_driver = {
1436 .add = cpufreq_add_dev,
1437 .remove = cpufreq_remove_dev,
1438 .suspend = cpufreq_suspend,
1439 .resume = cpufreq_resume,
1443 /*********************************************************************
1444 * NOTIFIER LISTS INTERFACE *
1445 *********************************************************************/
1448 * cpufreq_register_notifier - register a driver with cpufreq
1449 * @nb: notifier function to register
1450 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1452 * Add a driver to one of two lists: either a list of drivers that
1453 * are notified about clock rate changes (once before and once after
1454 * the transition), or a list of drivers that are notified about
1455 * changes in cpufreq policy.
1457 * This function may sleep, and has the same return conditions as
1458 * blocking_notifier_chain_register.
1460 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1462 int ret;
1464 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1466 switch (list) {
1467 case CPUFREQ_TRANSITION_NOTIFIER:
1468 ret = srcu_notifier_chain_register(
1469 &cpufreq_transition_notifier_list, nb);
1470 break;
1471 case CPUFREQ_POLICY_NOTIFIER:
1472 ret = blocking_notifier_chain_register(
1473 &cpufreq_policy_notifier_list, nb);
1474 break;
1475 default:
1476 ret = -EINVAL;
1479 return ret;
1481 EXPORT_SYMBOL(cpufreq_register_notifier);
1485 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1486 * @nb: notifier block to be unregistered
1487 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1489 * Remove a driver from the CPU frequency notifier list.
1491 * This function may sleep, and has the same return conditions as
1492 * blocking_notifier_chain_unregister.
1494 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1496 int ret;
1498 switch (list) {
1499 case CPUFREQ_TRANSITION_NOTIFIER:
1500 ret = srcu_notifier_chain_unregister(
1501 &cpufreq_transition_notifier_list, nb);
1502 break;
1503 case CPUFREQ_POLICY_NOTIFIER:
1504 ret = blocking_notifier_chain_unregister(
1505 &cpufreq_policy_notifier_list, nb);
1506 break;
1507 default:
1508 ret = -EINVAL;
1511 return ret;
1513 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1516 /*********************************************************************
1517 * GOVERNORS *
1518 *********************************************************************/
1521 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1522 unsigned int target_freq,
1523 unsigned int relation)
1525 int retval = -EINVAL;
1527 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1528 target_freq, relation);
1529 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1530 retval = cpufreq_driver->target(policy, target_freq, relation);
1532 return retval;
1534 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1536 int cpufreq_driver_target(struct cpufreq_policy *policy,
1537 unsigned int target_freq,
1538 unsigned int relation)
1540 int ret = -EINVAL;
1542 policy = cpufreq_cpu_get(policy->cpu);
1543 if (!policy)
1544 goto no_policy;
1546 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1547 goto fail;
1549 ret = __cpufreq_driver_target(policy, target_freq, relation);
1551 unlock_policy_rwsem_write(policy->cpu);
1553 fail:
1554 cpufreq_cpu_put(policy);
1555 no_policy:
1556 return ret;
1558 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1560 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1562 int ret = 0;
1564 policy = cpufreq_cpu_get(policy->cpu);
1565 if (!policy)
1566 return -EINVAL;
1568 if (cpu_online(cpu) && cpufreq_driver->getavg)
1569 ret = cpufreq_driver->getavg(policy, cpu);
1571 cpufreq_cpu_put(policy);
1572 return ret;
1574 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1577 * when "event" is CPUFREQ_GOV_LIMITS
1580 static int __cpufreq_governor(struct cpufreq_policy *policy,
1581 unsigned int event)
1583 int ret;
1585 /* Only must be defined when default governor is known to have latency
1586 restrictions, like e.g. conservative or ondemand.
1587 That this is the case is already ensured in Kconfig
1589 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1590 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1591 #else
1592 struct cpufreq_governor *gov = NULL;
1593 #endif
1595 if (policy->governor->max_transition_latency &&
1596 policy->cpuinfo.transition_latency >
1597 policy->governor->max_transition_latency) {
1598 if (!gov)
1599 return -EINVAL;
1600 else {
1601 printk(KERN_WARNING "%s governor failed, too long"
1602 " transition latency of HW, fallback"
1603 " to %s governor\n",
1604 policy->governor->name,
1605 gov->name);
1606 policy->governor = gov;
1610 if (!try_module_get(policy->governor->owner))
1611 return -EINVAL;
1613 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1614 policy->cpu, event);
1615 ret = policy->governor->governor(policy, event);
1617 /* we keep one module reference alive for
1618 each CPU governed by this CPU */
1619 if ((event != CPUFREQ_GOV_START) || ret)
1620 module_put(policy->governor->owner);
1621 if ((event == CPUFREQ_GOV_STOP) && !ret)
1622 module_put(policy->governor->owner);
1624 return ret;
1628 int cpufreq_register_governor(struct cpufreq_governor *governor)
1630 int err;
1632 if (!governor)
1633 return -EINVAL;
1635 mutex_lock(&cpufreq_governor_mutex);
1637 err = -EBUSY;
1638 if (__find_governor(governor->name) == NULL) {
1639 err = 0;
1640 list_add(&governor->governor_list, &cpufreq_governor_list);
1643 mutex_unlock(&cpufreq_governor_mutex);
1644 return err;
1646 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1649 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1651 #ifdef CONFIG_HOTPLUG_CPU
1652 int cpu;
1653 #endif
1655 if (!governor)
1656 return;
1658 #ifdef CONFIG_HOTPLUG_CPU
1659 for_each_present_cpu(cpu) {
1660 if (cpu_online(cpu))
1661 continue;
1662 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1663 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1665 #endif
1667 mutex_lock(&cpufreq_governor_mutex);
1668 list_del(&governor->governor_list);
1669 mutex_unlock(&cpufreq_governor_mutex);
1670 return;
1672 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1676 /*********************************************************************
1677 * POLICY INTERFACE *
1678 *********************************************************************/
1681 * cpufreq_get_policy - get the current cpufreq_policy
1682 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1683 * is written
1685 * Reads the current cpufreq policy.
1687 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1689 struct cpufreq_policy *cpu_policy;
1690 if (!policy)
1691 return -EINVAL;
1693 cpu_policy = cpufreq_cpu_get(cpu);
1694 if (!cpu_policy)
1695 return -EINVAL;
1697 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1699 cpufreq_cpu_put(cpu_policy);
1700 return 0;
1702 EXPORT_SYMBOL(cpufreq_get_policy);
1706 * data : current policy.
1707 * policy : policy to be set.
1709 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1710 struct cpufreq_policy *policy)
1712 int ret = 0;
1714 cpufreq_debug_disable_ratelimit();
1715 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1716 policy->min, policy->max);
1718 memcpy(&policy->cpuinfo, &data->cpuinfo,
1719 sizeof(struct cpufreq_cpuinfo));
1721 if (policy->min > data->max || policy->max < data->min) {
1722 ret = -EINVAL;
1723 goto error_out;
1726 /* verify the cpu speed can be set within this limit */
1727 ret = cpufreq_driver->verify(policy);
1728 if (ret)
1729 goto error_out;
1731 /* adjust if necessary - all reasons */
1732 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1733 CPUFREQ_ADJUST, policy);
1735 /* adjust if necessary - hardware incompatibility*/
1736 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1737 CPUFREQ_INCOMPATIBLE, policy);
1739 /* verify the cpu speed can be set within this limit,
1740 which might be different to the first one */
1741 ret = cpufreq_driver->verify(policy);
1742 if (ret)
1743 goto error_out;
1745 /* notification of the new policy */
1746 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1747 CPUFREQ_NOTIFY, policy);
1749 data->min = policy->min;
1750 data->max = policy->max;
1752 dprintk("new min and max freqs are %u - %u kHz\n",
1753 data->min, data->max);
1755 if (cpufreq_driver->setpolicy) {
1756 data->policy = policy->policy;
1757 dprintk("setting range\n");
1758 ret = cpufreq_driver->setpolicy(policy);
1759 } else {
1760 if (policy->governor != data->governor) {
1761 /* save old, working values */
1762 struct cpufreq_governor *old_gov = data->governor;
1764 dprintk("governor switch\n");
1766 /* end old governor */
1767 if (data->governor)
1768 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1770 /* start new governor */
1771 data->governor = policy->governor;
1772 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1773 /* new governor failed, so re-start old one */
1774 dprintk("starting governor %s failed\n",
1775 data->governor->name);
1776 if (old_gov) {
1777 data->governor = old_gov;
1778 __cpufreq_governor(data,
1779 CPUFREQ_GOV_START);
1781 ret = -EINVAL;
1782 goto error_out;
1784 /* might be a policy change, too, so fall through */
1786 dprintk("governor: change or update limits\n");
1787 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1790 error_out:
1791 cpufreq_debug_enable_ratelimit();
1792 return ret;
1796 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1797 * @cpu: CPU which shall be re-evaluated
1799 * Usefull for policy notifiers which have different necessities
1800 * at different times.
1802 int cpufreq_update_policy(unsigned int cpu)
1804 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1805 struct cpufreq_policy policy;
1806 int ret;
1808 if (!data) {
1809 ret = -ENODEV;
1810 goto no_policy;
1813 if (unlikely(lock_policy_rwsem_write(cpu))) {
1814 ret = -EINVAL;
1815 goto fail;
1818 dprintk("updating policy for CPU %u\n", cpu);
1819 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1820 policy.min = data->user_policy.min;
1821 policy.max = data->user_policy.max;
1822 policy.policy = data->user_policy.policy;
1823 policy.governor = data->user_policy.governor;
1825 /* BIOS might change freq behind our back
1826 -> ask driver for current freq and notify governors about a change */
1827 if (cpufreq_driver->get) {
1828 policy.cur = cpufreq_driver->get(cpu);
1829 if (!data->cur) {
1830 dprintk("Driver did not initialize current freq");
1831 data->cur = policy.cur;
1832 } else {
1833 if (data->cur != policy.cur)
1834 cpufreq_out_of_sync(cpu, data->cur,
1835 policy.cur);
1839 ret = __cpufreq_set_policy(data, &policy);
1841 unlock_policy_rwsem_write(cpu);
1843 fail:
1844 cpufreq_cpu_put(data);
1845 no_policy:
1846 return ret;
1848 EXPORT_SYMBOL(cpufreq_update_policy);
1850 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1851 unsigned long action, void *hcpu)
1853 unsigned int cpu = (unsigned long)hcpu;
1854 struct sys_device *sys_dev;
1856 sys_dev = get_cpu_sysdev(cpu);
1857 if (sys_dev) {
1858 switch (action) {
1859 case CPU_ONLINE:
1860 case CPU_ONLINE_FROZEN:
1861 cpufreq_add_dev(sys_dev);
1862 break;
1863 case CPU_DOWN_PREPARE:
1864 case CPU_DOWN_PREPARE_FROZEN:
1865 if (unlikely(lock_policy_rwsem_write(cpu)))
1866 BUG();
1868 __cpufreq_remove_dev(sys_dev);
1869 break;
1870 case CPU_DOWN_FAILED:
1871 case CPU_DOWN_FAILED_FROZEN:
1872 cpufreq_add_dev(sys_dev);
1873 break;
1876 return NOTIFY_OK;
1879 static struct notifier_block __refdata cpufreq_cpu_notifier = {
1880 .notifier_call = cpufreq_cpu_callback,
1883 /*********************************************************************
1884 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1885 *********************************************************************/
1888 * cpufreq_register_driver - register a CPU Frequency driver
1889 * @driver_data: A struct cpufreq_driver containing the values#
1890 * submitted by the CPU Frequency driver.
1892 * Registers a CPU Frequency driver to this core code. This code
1893 * returns zero on success, -EBUSY when another driver got here first
1894 * (and isn't unregistered in the meantime).
1897 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1899 unsigned long flags;
1900 int ret;
1902 if (!driver_data || !driver_data->verify || !driver_data->init ||
1903 ((!driver_data->setpolicy) && (!driver_data->target)))
1904 return -EINVAL;
1906 dprintk("trying to register driver %s\n", driver_data->name);
1908 if (driver_data->setpolicy)
1909 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1911 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1912 if (cpufreq_driver) {
1913 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1914 return -EBUSY;
1916 cpufreq_driver = driver_data;
1917 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1919 ret = sysdev_driver_register(&cpu_sysdev_class,
1920 &cpufreq_sysdev_driver);
1922 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1923 int i;
1924 ret = -ENODEV;
1926 /* check for at least one working CPU */
1927 for (i = 0; i < nr_cpu_ids; i++)
1928 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1929 ret = 0;
1930 break;
1933 /* if all ->init() calls failed, unregister */
1934 if (ret) {
1935 dprintk("no CPU initialized for driver %s\n",
1936 driver_data->name);
1937 sysdev_driver_unregister(&cpu_sysdev_class,
1938 &cpufreq_sysdev_driver);
1940 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1941 cpufreq_driver = NULL;
1942 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1946 if (!ret) {
1947 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1948 dprintk("driver %s up and running\n", driver_data->name);
1949 cpufreq_debug_enable_ratelimit();
1952 return ret;
1954 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1958 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1960 * Unregister the current CPUFreq driver. Only call this if you have
1961 * the right to do so, i.e. if you have succeeded in initialising before!
1962 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1963 * currently not initialised.
1965 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1967 unsigned long flags;
1969 cpufreq_debug_disable_ratelimit();
1971 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1972 cpufreq_debug_enable_ratelimit();
1973 return -EINVAL;
1976 dprintk("unregistering driver %s\n", driver->name);
1978 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1979 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1981 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1982 cpufreq_driver = NULL;
1983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1985 return 0;
1987 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1989 static int __init cpufreq_core_init(void)
1991 int cpu;
1993 for_each_possible_cpu(cpu) {
1994 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1995 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1998 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1999 &cpu_sysdev_class.kset.kobj);
2000 BUG_ON(!cpufreq_global_kobject);
2002 return 0;
2004 core_initcall(cpufreq_core_init);