11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/cpufreq/cpufreq.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001 Russell King 51da177e4SLinus Torvalds * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> 61da177e4SLinus Torvalds * 7c32b6b8eSAshok Raj * Oct 2005 - Ashok Raj <ashok.raj@intel.com> 8c32b6b8eSAshok Raj * Added handling for CPU hotplug 9c32b6b8eSAshok Raj * 101da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 111da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 121da177e4SLinus Torvalds * published by the Free Software Foundation. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds */ 151da177e4SLinus Torvalds 161da177e4SLinus Torvalds #include <linux/config.h> 171da177e4SLinus Torvalds #include <linux/kernel.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/init.h> 201da177e4SLinus Torvalds #include <linux/notifier.h> 211da177e4SLinus Torvalds #include <linux/cpufreq.h> 221da177e4SLinus Torvalds #include <linux/delay.h> 231da177e4SLinus Torvalds #include <linux/interrupt.h> 241da177e4SLinus Torvalds #include <linux/spinlock.h> 251da177e4SLinus Torvalds #include <linux/device.h> 261da177e4SLinus Torvalds #include <linux/slab.h> 271da177e4SLinus Torvalds #include <linux/cpu.h> 281da177e4SLinus Torvalds #include <linux/completion.h> 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg) 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds /** 331da177e4SLinus Torvalds * The "cpufreq driver" - the arch- or hardware-dependend low 341da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 351da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds static struct cpufreq_driver *cpufreq_driver; 381da177e4SLinus Torvalds static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS]; 391da177e4SLinus Torvalds static DEFINE_SPINLOCK(cpufreq_driver_lock); 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds /* internal prototypes */ 421da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); 431da177e4SLinus Torvalds static void handle_update(void *data); 441da177e4SLinus Torvalds static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci); 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /** 471da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 481da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 491da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 501da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 511da177e4SLinus Torvalds * The mutex locks both lists. 521da177e4SLinus Torvalds */ 531da177e4SLinus Torvalds static struct notifier_block *cpufreq_policy_notifier_list; 541da177e4SLinus Torvalds static struct notifier_block *cpufreq_transition_notifier_list; 551da177e4SLinus Torvalds static DECLARE_RWSEM (cpufreq_notifier_rwsem); 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 591da177e4SLinus Torvalds static DECLARE_MUTEX (cpufreq_governor_sem); 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu) 621da177e4SLinus Torvalds { 631da177e4SLinus Torvalds struct cpufreq_policy *data; 641da177e4SLinus Torvalds unsigned long flags; 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds if (cpu >= NR_CPUS) 671da177e4SLinus Torvalds goto err_out; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* get the cpufreq driver */ 701da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds if (!cpufreq_driver) 731da177e4SLinus Torvalds goto err_out_unlock; 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) 761da177e4SLinus Torvalds goto err_out_unlock; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds /* get the CPU */ 801da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds if (!data) 831da177e4SLinus Torvalds goto err_out_put_module; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) 861da177e4SLinus Torvalds goto err_out_put_module; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds return data; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds err_out_put_module: 941da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 951da177e4SLinus Torvalds err_out_unlock: 961da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 971da177e4SLinus Torvalds err_out: 981da177e4SLinus Torvalds return NULL; 991da177e4SLinus Torvalds } 1001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds kobject_put(&data->kobj); 1051da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /********************************************************************* 1111da177e4SLinus Torvalds * UNIFIED DEBUG HELPERS * 1121da177e4SLinus Torvalds *********************************************************************/ 1131da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */ 1161da177e4SLinus Torvalds static unsigned int debug; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can 1191da177e4SLinus Torvalds * set or modify this value. 1201da177e4SLinus Torvalds */ 1211da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1; 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful 1241da177e4SLinus Torvalds * loading of a cpufreq driver, temporarily disabled when a new policy 1251da177e4SLinus Torvalds * is set, and disabled upon cpufreq driver removal 1261da177e4SLinus Torvalds */ 1271da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1; 1281da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock); 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) 1311da177e4SLinus Torvalds { 1321da177e4SLinus Torvalds unsigned long flags; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 1351da177e4SLinus Torvalds if (disable_ratelimit) 1361da177e4SLinus Torvalds disable_ratelimit--; 1371da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds unsigned long flags; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 1451da177e4SLinus Torvalds disable_ratelimit++; 1461da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...) 1501da177e4SLinus Torvalds { 1511da177e4SLinus Torvalds char s[256]; 1521da177e4SLinus Torvalds va_list args; 1531da177e4SLinus Torvalds unsigned int len; 1541da177e4SLinus Torvalds unsigned long flags; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds WARN_ON(!prefix); 1571da177e4SLinus Torvalds if (type & debug) { 1581da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 1591da177e4SLinus Torvalds if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) { 1601da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 1611da177e4SLinus Torvalds return; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds va_start(args, fmt); 1681da177e4SLinus Torvalds len += vsnprintf(&s[len], (256 - len), fmt, args); 1691da177e4SLinus Torvalds va_end(args); 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds printk(s); 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds WARN_ON(len < 5); 1741da177e4SLinus Torvalds } 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds module_param(debug, uint, 0644); 1801da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors."); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644); 1831da177e4SLinus Torvalds MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting."); 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */ 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; } 1881da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; } 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */ 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds /********************************************************************* 1941da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 1951da177e4SLinus Torvalds *********************************************************************/ 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /** 1981da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 1991da177e4SLinus Torvalds * 2001da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2011da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2021da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2031da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2041da177e4SLinus Torvalds */ 2051da177e4SLinus Torvalds #ifndef CONFIG_SMP 2061da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2071da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2121da177e4SLinus Torvalds return; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2151da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2161da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 2171da177e4SLinus Torvalds dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || 2201da177e4SLinus Torvalds (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || 22142d4dc3fSBenjamin Herrenschmidt (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 2221da177e4SLinus Torvalds loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new); 2231da177e4SLinus Torvalds dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new); 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds } 2261da177e4SLinus Torvalds #else 2271da177e4SLinus Torvalds static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; } 2281da177e4SLinus Torvalds #endif 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /** 2321da177e4SLinus Torvalds * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition 2331da177e4SLinus Torvalds * 2341da177e4SLinus Torvalds * This function calls the transition notifiers and the "adjust_jiffies" function. It is called 2351da177e4SLinus Torvalds * twice on all CPU frequency changes that have external effects. 2361da177e4SLinus Torvalds */ 2371da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds freqs->flags = cpufreq_driver->flags; 2421da177e4SLinus Torvalds dprintk("notification %u of frequency transition to %u kHz\n", state, freqs->new); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds down_read(&cpufreq_notifier_rwsem); 2451da177e4SLinus Torvalds switch (state) { 2461da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 2471da177e4SLinus Torvalds /* detect if the driver reported a value as "old frequency" which 2481da177e4SLinus Torvalds * is not equal to what the cpufreq core thinks is "old frequency". 2491da177e4SLinus Torvalds */ 2501da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 2511da177e4SLinus Torvalds if ((likely(cpufreq_cpu_data[freqs->cpu])) && 2521da177e4SLinus Torvalds (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)) && 2531da177e4SLinus Torvalds (likely(cpufreq_cpu_data[freqs->cpu]->cur)) && 2541da177e4SLinus Torvalds (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur))) 2551da177e4SLinus Torvalds { 25678ee998fSDave Jones dprintk(KERN_WARNING "Warning: CPU frequency is %u, " 2571da177e4SLinus Torvalds "cpufreq assumed %u kHz.\n", freqs->old, cpufreq_cpu_data[freqs->cpu]->cur); 2581da177e4SLinus Torvalds freqs->old = cpufreq_cpu_data[freqs->cpu]->cur; 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs); 2621da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 2631da177e4SLinus Torvalds break; 2641da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 2651da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 2661da177e4SLinus Torvalds notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs); 2671da177e4SLinus Torvalds if ((likely(cpufreq_cpu_data[freqs->cpu])) && 2681da177e4SLinus Torvalds (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu))) 2691da177e4SLinus Torvalds cpufreq_cpu_data[freqs->cpu]->cur = freqs->new; 2701da177e4SLinus Torvalds break; 2711da177e4SLinus Torvalds } 2721da177e4SLinus Torvalds up_read(&cpufreq_notifier_rwsem); 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds 2781da177e4SLinus Torvalds /********************************************************************* 2791da177e4SLinus Torvalds * SYSFS INTERFACE * 2801da177e4SLinus Torvalds *********************************************************************/ 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /** 2831da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 2841da177e4SLinus Torvalds */ 2851da177e4SLinus Torvalds static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, 2861da177e4SLinus Torvalds struct cpufreq_governor **governor) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds if (!cpufreq_driver) 2891da177e4SLinus Torvalds return -EINVAL; 2901da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 2911da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 2921da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 2931da177e4SLinus Torvalds return 0; 2941da177e4SLinus Torvalds } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { 2951da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 2961da177e4SLinus Torvalds return 0; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds return -EINVAL; 2991da177e4SLinus Torvalds } else { 3001da177e4SLinus Torvalds struct cpufreq_governor *t; 3011da177e4SLinus Torvalds down(&cpufreq_governor_sem); 3021da177e4SLinus Torvalds if (!cpufreq_driver || !cpufreq_driver->target) 3031da177e4SLinus Torvalds goto out; 3041da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 3051da177e4SLinus Torvalds if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) { 3061da177e4SLinus Torvalds *governor = t; 3071da177e4SLinus Torvalds up(&cpufreq_governor_sem); 3081da177e4SLinus Torvalds return 0; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds out: 3121da177e4SLinus Torvalds up(&cpufreq_governor_sem); 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds return -EINVAL; 3151da177e4SLinus Torvalds } 3161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_parse_governor); 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds /* drivers/base/cpu.c */ 3201da177e4SLinus Torvalds extern struct sysdev_class cpu_sysdev_class; 3211da177e4SLinus Torvalds 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds /** 3241da177e4SLinus Torvalds * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information 3251da177e4SLinus Torvalds * 3261da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 3271da177e4SLinus Torvalds * "unsigned int". 3281da177e4SLinus Torvalds */ 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds #define show_one(file_name, object) \ 3311da177e4SLinus Torvalds static ssize_t show_##file_name \ 3321da177e4SLinus Torvalds (struct cpufreq_policy * policy, char *buf) \ 3331da177e4SLinus Torvalds { \ 3341da177e4SLinus Torvalds return sprintf (buf, "%u\n", policy->object); \ 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 3381da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 3391da177e4SLinus Torvalds show_one(scaling_min_freq, min); 3401da177e4SLinus Torvalds show_one(scaling_max_freq, max); 3411da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /** 3441da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 3451da177e4SLinus Torvalds */ 3461da177e4SLinus Torvalds #define store_one(file_name, object) \ 3471da177e4SLinus Torvalds static ssize_t store_##file_name \ 3481da177e4SLinus Torvalds (struct cpufreq_policy * policy, const char *buf, size_t count) \ 3491da177e4SLinus Torvalds { \ 3501da177e4SLinus Torvalds unsigned int ret = -EINVAL; \ 3511da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 3521da177e4SLinus Torvalds \ 3531da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 3541da177e4SLinus Torvalds if (ret) \ 3551da177e4SLinus Torvalds return -EINVAL; \ 3561da177e4SLinus Torvalds \ 3571da177e4SLinus Torvalds ret = sscanf (buf, "%u", &new_policy.object); \ 3581da177e4SLinus Torvalds if (ret != 1) \ 3591da177e4SLinus Torvalds return -EINVAL; \ 3601da177e4SLinus Torvalds \ 3611da177e4SLinus Torvalds ret = cpufreq_set_policy(&new_policy); \ 3621da177e4SLinus Torvalds \ 3631da177e4SLinus Torvalds return ret ? ret : count; \ 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds store_one(scaling_min_freq,min); 3671da177e4SLinus Torvalds store_one(scaling_max_freq,max); 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds /** 3701da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 3711da177e4SLinus Torvalds */ 3721da177e4SLinus Torvalds static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf) 3731da177e4SLinus Torvalds { 3741da177e4SLinus Torvalds unsigned int cur_freq = cpufreq_get(policy->cpu); 3751da177e4SLinus Torvalds if (!cur_freq) 3761da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 3771da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /** 3821da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf) 3851da177e4SLinus Torvalds { 3861da177e4SLinus Torvalds if(policy->policy == CPUFREQ_POLICY_POWERSAVE) 3871da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 3881da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 3891da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 3901da177e4SLinus Torvalds else if (policy->governor) 3911da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name); 3921da177e4SLinus Torvalds return -EINVAL; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds /** 3971da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 3981da177e4SLinus Torvalds */ 3991da177e4SLinus Torvalds static ssize_t store_scaling_governor (struct cpufreq_policy * policy, 4001da177e4SLinus Torvalds const char *buf, size_t count) 4011da177e4SLinus Torvalds { 4021da177e4SLinus Torvalds unsigned int ret = -EINVAL; 4031da177e4SLinus Torvalds char str_governor[16]; 4041da177e4SLinus Torvalds struct cpufreq_policy new_policy; 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 4071da177e4SLinus Torvalds if (ret) 4081da177e4SLinus Torvalds return ret; 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds ret = sscanf (buf, "%15s", str_governor); 4111da177e4SLinus Torvalds if (ret != 1) 4121da177e4SLinus Torvalds return -EINVAL; 4131da177e4SLinus Torvalds 4141da177e4SLinus Torvalds if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor)) 4151da177e4SLinus Torvalds return -EINVAL; 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds ret = cpufreq_set_policy(&new_policy); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds return ret ? ret : count; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds /** 4231da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 4241da177e4SLinus Torvalds */ 4251da177e4SLinus Torvalds static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf) 4261da177e4SLinus Torvalds { 4271da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds /** 4311da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 4321da177e4SLinus Torvalds */ 4331da177e4SLinus Torvalds static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy, 4341da177e4SLinus Torvalds char *buf) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds ssize_t i = 0; 4371da177e4SLinus Torvalds struct cpufreq_governor *t; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (!cpufreq_driver->target) { 4401da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 4411da177e4SLinus Torvalds goto out; 4421da177e4SLinus Torvalds } 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 4451da177e4SLinus Torvalds if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) 4461da177e4SLinus Torvalds goto out; 4471da177e4SLinus Torvalds i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds out: 4501da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 4511da177e4SLinus Torvalds return i; 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds /** 4541da177e4SLinus Torvalds * show_affected_cpus - show the CPUs affected by each transition 4551da177e4SLinus Torvalds */ 4561da177e4SLinus Torvalds static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf) 4571da177e4SLinus Torvalds { 4581da177e4SLinus Torvalds ssize_t i = 0; 4591da177e4SLinus Torvalds unsigned int cpu; 4601da177e4SLinus Torvalds 4611da177e4SLinus Torvalds for_each_cpu_mask(cpu, policy->cpus) { 4621da177e4SLinus Torvalds if (i) 4631da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 4641da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 4651da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 4661da177e4SLinus Torvalds break; 4671da177e4SLinus Torvalds } 4681da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 4691da177e4SLinus Torvalds return i; 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds #define define_one_ro(_name) \ 4741da177e4SLinus Torvalds static struct freq_attr _name = \ 4751da177e4SLinus Torvalds __ATTR(_name, 0444, show_##_name, NULL) 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds #define define_one_ro0400(_name) \ 4781da177e4SLinus Torvalds static struct freq_attr _name = \ 4791da177e4SLinus Torvalds __ATTR(_name, 0400, show_##_name, NULL) 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds #define define_one_rw(_name) \ 4821da177e4SLinus Torvalds static struct freq_attr _name = \ 4831da177e4SLinus Torvalds __ATTR(_name, 0644, show_##_name, store_##_name) 4841da177e4SLinus Torvalds 4851da177e4SLinus Torvalds define_one_ro0400(cpuinfo_cur_freq); 4861da177e4SLinus Torvalds define_one_ro(cpuinfo_min_freq); 4871da177e4SLinus Torvalds define_one_ro(cpuinfo_max_freq); 4881da177e4SLinus Torvalds define_one_ro(scaling_available_governors); 4891da177e4SLinus Torvalds define_one_ro(scaling_driver); 4901da177e4SLinus Torvalds define_one_ro(scaling_cur_freq); 4911da177e4SLinus Torvalds define_one_ro(affected_cpus); 4921da177e4SLinus Torvalds define_one_rw(scaling_min_freq); 4931da177e4SLinus Torvalds define_one_rw(scaling_max_freq); 4941da177e4SLinus Torvalds define_one_rw(scaling_governor); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds static struct attribute * default_attrs[] = { 4971da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 4981da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 4991da177e4SLinus Torvalds &scaling_min_freq.attr, 5001da177e4SLinus Torvalds &scaling_max_freq.attr, 5011da177e4SLinus Torvalds &affected_cpus.attr, 5021da177e4SLinus Torvalds &scaling_governor.attr, 5031da177e4SLinus Torvalds &scaling_driver.attr, 5041da177e4SLinus Torvalds &scaling_available_governors.attr, 5051da177e4SLinus Torvalds NULL 5061da177e4SLinus Torvalds }; 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds #define to_policy(k) container_of(k,struct cpufreq_policy,kobj) 5091da177e4SLinus Torvalds #define to_attr(a) container_of(a,struct freq_attr,attr) 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf) 5121da177e4SLinus Torvalds { 5131da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 5141da177e4SLinus Torvalds struct freq_attr * fattr = to_attr(attr); 5151da177e4SLinus Torvalds ssize_t ret; 5161da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 5171da177e4SLinus Torvalds if (!policy) 5181da177e4SLinus Torvalds return -EINVAL; 51970f2817aSDmitry Torokhov ret = fattr->show ? fattr->show(policy,buf) : -EIO; 5201da177e4SLinus Torvalds cpufreq_cpu_put(policy); 5211da177e4SLinus Torvalds return ret; 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds static ssize_t store(struct kobject * kobj, struct attribute * attr, 5251da177e4SLinus Torvalds const char * buf, size_t count) 5261da177e4SLinus Torvalds { 5271da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 5281da177e4SLinus Torvalds struct freq_attr * fattr = to_attr(attr); 5291da177e4SLinus Torvalds ssize_t ret; 5301da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 5311da177e4SLinus Torvalds if (!policy) 5321da177e4SLinus Torvalds return -EINVAL; 53370f2817aSDmitry Torokhov ret = fattr->store ? fattr->store(policy,buf,count) : -EIO; 5341da177e4SLinus Torvalds cpufreq_cpu_put(policy); 5351da177e4SLinus Torvalds return ret; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject * kobj) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 5411da177e4SLinus Torvalds dprintk("last reference is dropped\n"); 5421da177e4SLinus Torvalds complete(&policy->kobj_unregister); 5431da177e4SLinus Torvalds } 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = { 5461da177e4SLinus Torvalds .show = show, 5471da177e4SLinus Torvalds .store = store, 5481da177e4SLinus Torvalds }; 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 5511da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 5521da177e4SLinus Torvalds .default_attrs = default_attrs, 5531da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 5541da177e4SLinus Torvalds }; 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds /** 5581da177e4SLinus Torvalds * cpufreq_add_dev - add a CPU device 5591da177e4SLinus Torvalds * 5601da177e4SLinus Torvalds * Adds the cpufreq interface for a CPU device. 5611da177e4SLinus Torvalds */ 5621da177e4SLinus Torvalds static int cpufreq_add_dev (struct sys_device * sys_dev) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 5651da177e4SLinus Torvalds int ret = 0; 5661da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5671da177e4SLinus Torvalds struct cpufreq_policy *policy; 5681da177e4SLinus Torvalds struct freq_attr **drv_attr; 5691da177e4SLinus Torvalds unsigned long flags; 5701da177e4SLinus Torvalds unsigned int j; 5711da177e4SLinus Torvalds 572c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 573c32b6b8eSAshok Raj return 0; 574c32b6b8eSAshok Raj 5751da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 5761da177e4SLinus Torvalds dprintk("adding CPU %u\n", cpu); 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds #ifdef CONFIG_SMP 5791da177e4SLinus Torvalds /* check whether a different CPU already registered this 5801da177e4SLinus Torvalds * CPU because it is in the same boat. */ 5811da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 5821da177e4SLinus Torvalds if (unlikely(policy)) { 5831da177e4SLinus Torvalds dprintk("CPU already managed, adding link\n"); 5841da177e4SLinus Torvalds sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq"); 5851da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 5861da177e4SLinus Torvalds return 0; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds #endif 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) { 5911da177e4SLinus Torvalds ret = -EINVAL; 5921da177e4SLinus Torvalds goto module_out; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 595e98df50cSDave Jones policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 5961da177e4SLinus Torvalds if (!policy) { 5971da177e4SLinus Torvalds ret = -ENOMEM; 5981da177e4SLinus Torvalds goto nomem_out; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds policy->cpu = cpu; 6021da177e4SLinus Torvalds policy->cpus = cpumask_of_cpu(cpu); 6031da177e4SLinus Torvalds 6041da177e4SLinus Torvalds init_MUTEX_LOCKED(&policy->lock); 6051da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 6061da177e4SLinus Torvalds INIT_WORK(&policy->update, handle_update, (void *)(long)cpu); 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 6091da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds ret = cpufreq_driver->init(policy); 6121da177e4SLinus Torvalds if (ret) { 6131da177e4SLinus Torvalds dprintk("initialization failed\n"); 6141da177e4SLinus Torvalds goto err_out; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds /* prepare interface data */ 6201da177e4SLinus Torvalds policy->kobj.parent = &sys_dev->kobj; 6211da177e4SLinus Torvalds policy->kobj.ktype = &ktype_cpufreq; 6221da177e4SLinus Torvalds strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN); 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds ret = kobject_register(&policy->kobj); 6251da177e4SLinus Torvalds if (ret) 6268085e1f1SVenkatesh Pallipadi goto err_out_driver_exit; 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds /* set up files for this cpu device */ 6291da177e4SLinus Torvalds drv_attr = cpufreq_driver->attr; 6301da177e4SLinus Torvalds while ((drv_attr) && (*drv_attr)) { 6311da177e4SLinus Torvalds sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 6321da177e4SLinus Torvalds drv_attr++; 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds if (cpufreq_driver->get) 6351da177e4SLinus Torvalds sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 6361da177e4SLinus Torvalds if (cpufreq_driver->target) 6371da177e4SLinus Torvalds sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 6401da177e4SLinus Torvalds for_each_cpu_mask(j, policy->cpus) 6411da177e4SLinus Torvalds cpufreq_cpu_data[j] = policy; 6421da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 6431da177e4SLinus Torvalds policy->governor = NULL; /* to assure that the starting sequence is 6441da177e4SLinus Torvalds * run in cpufreq_set_policy */ 6451da177e4SLinus Torvalds up(&policy->lock); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds /* set default policy */ 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds ret = cpufreq_set_policy(&new_policy); 6501da177e4SLinus Torvalds if (ret) { 6511da177e4SLinus Torvalds dprintk("setting policy failed\n"); 6521da177e4SLinus Torvalds goto err_out_unregister; 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 6561da177e4SLinus Torvalds dprintk("initialization complete\n"); 6571da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds return 0; 6601da177e4SLinus Torvalds 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds err_out_unregister: 6631da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 6641da177e4SLinus Torvalds for_each_cpu_mask(j, policy->cpus) 6651da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 6661da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds kobject_unregister(&policy->kobj); 6691da177e4SLinus Torvalds wait_for_completion(&policy->kobj_unregister); 6701da177e4SLinus Torvalds 6718085e1f1SVenkatesh Pallipadi err_out_driver_exit: 6728085e1f1SVenkatesh Pallipadi if (cpufreq_driver->exit) 6738085e1f1SVenkatesh Pallipadi cpufreq_driver->exit(policy); 6748085e1f1SVenkatesh Pallipadi 6751da177e4SLinus Torvalds err_out: 6761da177e4SLinus Torvalds kfree(policy); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds nomem_out: 6791da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 6801da177e4SLinus Torvalds module_out: 6811da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 6821da177e4SLinus Torvalds return ret; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds /** 6871da177e4SLinus Torvalds * cpufreq_remove_dev - remove a CPU device 6881da177e4SLinus Torvalds * 6891da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 6901da177e4SLinus Torvalds */ 6911da177e4SLinus Torvalds static int cpufreq_remove_dev (struct sys_device * sys_dev) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 6941da177e4SLinus Torvalds unsigned long flags; 6951da177e4SLinus Torvalds struct cpufreq_policy *data; 696d434fca7SAshok Raj struct sys_device *cpu_sys_dev; 6971da177e4SLinus Torvalds #ifdef CONFIG_SMP 6981da177e4SLinus Torvalds unsigned int j; 6991da177e4SLinus Torvalds #endif 7001da177e4SLinus Torvalds 7011da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 7021da177e4SLinus Torvalds dprintk("unregistering CPU %u\n", cpu); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 7051da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds if (!data) { 7081da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 7091da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7101da177e4SLinus Torvalds return -EINVAL; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds cpufreq_cpu_data[cpu] = NULL; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds #ifdef CONFIG_SMP 7161da177e4SLinus Torvalds /* if this isn't the CPU which is the parent of the kobj, we 7171da177e4SLinus Torvalds * only need to unlink, put and exit 7181da177e4SLinus Torvalds */ 7191da177e4SLinus Torvalds if (unlikely(cpu != data->cpu)) { 7201da177e4SLinus Torvalds dprintk("removing link\n"); 7211da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 7221da177e4SLinus Torvalds sysfs_remove_link(&sys_dev->kobj, "cpufreq"); 7231da177e4SLinus Torvalds cpufreq_cpu_put(data); 7241da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7251da177e4SLinus Torvalds return 0; 7261da177e4SLinus Torvalds } 7271da177e4SLinus Torvalds #endif 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) { 7311da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 7321da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7331da177e4SLinus Torvalds return -EFAULT; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds #ifdef CONFIG_SMP 7371da177e4SLinus Torvalds /* if we have other CPUs still registered, we need to unlink them, 7381da177e4SLinus Torvalds * or else wait_for_completion below will lock up. Clean the 7391da177e4SLinus Torvalds * cpufreq_cpu_data[] while holding the lock, and remove the sysfs 7401da177e4SLinus Torvalds * links afterwards. 7411da177e4SLinus Torvalds */ 7421da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 7431da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 7441da177e4SLinus Torvalds if (j == cpu) 7451da177e4SLinus Torvalds continue; 7461da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 7471da177e4SLinus Torvalds } 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 7531da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 7541da177e4SLinus Torvalds if (j == cpu) 7551da177e4SLinus Torvalds continue; 7561da177e4SLinus Torvalds dprintk("removing link for cpu %u\n", j); 757d434fca7SAshok Raj cpu_sys_dev = get_cpu_sysdev(j); 758d434fca7SAshok Raj sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); 7591da177e4SLinus Torvalds cpufreq_cpu_put(data); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds #else 7631da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 7641da177e4SLinus Torvalds #endif 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds down(&data->lock); 7671da177e4SLinus Torvalds if (cpufreq_driver->target) 7681da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 7691da177e4SLinus Torvalds up(&data->lock); 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds kobject_unregister(&data->kobj); 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds kobject_put(&data->kobj); 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /* we need to make sure that the underlying kobj is actually 7761da177e4SLinus Torvalds * not referenced anymore by anybody before we proceed with 7771da177e4SLinus Torvalds * unloading. 7781da177e4SLinus Torvalds */ 7791da177e4SLinus Torvalds dprintk("waiting for dropping of refcount\n"); 7801da177e4SLinus Torvalds wait_for_completion(&data->kobj_unregister); 7811da177e4SLinus Torvalds dprintk("wait complete\n"); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds if (cpufreq_driver->exit) 7841da177e4SLinus Torvalds cpufreq_driver->exit(data); 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds kfree(data); 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7891da177e4SLinus Torvalds 7901da177e4SLinus Torvalds return 0; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds static void handle_update(void *data) 7951da177e4SLinus Torvalds { 7961da177e4SLinus Torvalds unsigned int cpu = (unsigned int)(long)data; 7971da177e4SLinus Torvalds dprintk("handle_update for cpu %u called\n", cpu); 7981da177e4SLinus Torvalds cpufreq_update_policy(cpu); 7991da177e4SLinus Torvalds } 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds /** 8021da177e4SLinus Torvalds * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 8031da177e4SLinus Torvalds * @cpu: cpu number 8041da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 8051da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 8061da177e4SLinus Torvalds * 8071da177e4SLinus Torvalds * We adjust to current frequency first, and need to clean up later. So either call 8081da177e4SLinus Torvalds * to cpufreq_update_policy() or schedule handle_update()). 8091da177e4SLinus Torvalds */ 8101da177e4SLinus Torvalds static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq) 8111da177e4SLinus Torvalds { 8121da177e4SLinus Torvalds struct cpufreq_freqs freqs; 8131da177e4SLinus Torvalds 81478ee998fSDave Jones dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing " 8151da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds freqs.cpu = cpu; 8181da177e4SLinus Torvalds freqs.old = old_freq; 8191da177e4SLinus Torvalds freqs.new = new_freq; 8201da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 8211da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 8221da177e4SLinus Torvalds } 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds 8251da177e4SLinus Torvalds /** 8261da177e4SLinus Torvalds * cpufreq_get - get the current CPU frequency (in kHz) 8271da177e4SLinus Torvalds * @cpu: CPU number 8281da177e4SLinus Torvalds * 8291da177e4SLinus Torvalds * Get the CPU current (static) CPU frequency 8301da177e4SLinus Torvalds */ 8311da177e4SLinus Torvalds unsigned int cpufreq_get(unsigned int cpu) 8321da177e4SLinus Torvalds { 8331da177e4SLinus Torvalds struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 8341da177e4SLinus Torvalds unsigned int ret = 0; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds if (!policy) 8371da177e4SLinus Torvalds return 0; 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds if (!cpufreq_driver->get) 8401da177e4SLinus Torvalds goto out; 8411da177e4SLinus Torvalds 8421da177e4SLinus Torvalds down(&policy->lock); 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds ret = cpufreq_driver->get(cpu); 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds /* verify no discrepancy between actual and saved value exists */ 8491da177e4SLinus Torvalds if (unlikely(ret != policy->cur)) { 8501da177e4SLinus Torvalds cpufreq_out_of_sync(cpu, policy->cur, ret); 8511da177e4SLinus Torvalds schedule_work(&policy->update); 8521da177e4SLinus Torvalds } 8531da177e4SLinus Torvalds } 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds up(&policy->lock); 8561da177e4SLinus Torvalds 8571da177e4SLinus Torvalds out: 8581da177e4SLinus Torvalds cpufreq_cpu_put(policy); 8591da177e4SLinus Torvalds 8601da177e4SLinus Torvalds return (ret); 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds /** 86642d4dc3fSBenjamin Herrenschmidt * cpufreq_suspend - let the low level driver prepare for suspend 86742d4dc3fSBenjamin Herrenschmidt */ 86842d4dc3fSBenjamin Herrenschmidt 869e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg) 87042d4dc3fSBenjamin Herrenschmidt { 87142d4dc3fSBenjamin Herrenschmidt int cpu = sysdev->id; 87242d4dc3fSBenjamin Herrenschmidt unsigned int ret = 0; 87342d4dc3fSBenjamin Herrenschmidt unsigned int cur_freq = 0; 87442d4dc3fSBenjamin Herrenschmidt struct cpufreq_policy *cpu_policy; 87542d4dc3fSBenjamin Herrenschmidt 87642d4dc3fSBenjamin Herrenschmidt dprintk("resuming cpu %u\n", cpu); 87742d4dc3fSBenjamin Herrenschmidt 87842d4dc3fSBenjamin Herrenschmidt if (!cpu_online(cpu)) 87942d4dc3fSBenjamin Herrenschmidt return 0; 88042d4dc3fSBenjamin Herrenschmidt 88142d4dc3fSBenjamin Herrenschmidt /* we may be lax here as interrupts are off. Nonetheless 88242d4dc3fSBenjamin Herrenschmidt * we need to grab the correct cpu policy, as to check 88342d4dc3fSBenjamin Herrenschmidt * whether we really run on this CPU. 88442d4dc3fSBenjamin Herrenschmidt */ 88542d4dc3fSBenjamin Herrenschmidt 88642d4dc3fSBenjamin Herrenschmidt cpu_policy = cpufreq_cpu_get(cpu); 88742d4dc3fSBenjamin Herrenschmidt if (!cpu_policy) 88842d4dc3fSBenjamin Herrenschmidt return -EINVAL; 88942d4dc3fSBenjamin Herrenschmidt 89042d4dc3fSBenjamin Herrenschmidt /* only handle each CPU group once */ 89142d4dc3fSBenjamin Herrenschmidt if (unlikely(cpu_policy->cpu != cpu)) { 89242d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 89342d4dc3fSBenjamin Herrenschmidt return 0; 89442d4dc3fSBenjamin Herrenschmidt } 89542d4dc3fSBenjamin Herrenschmidt 89642d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->suspend) { 897e00d9967SBernard Blackham ret = cpufreq_driver->suspend(cpu_policy, pmsg); 89842d4dc3fSBenjamin Herrenschmidt if (ret) { 89942d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 90042d4dc3fSBenjamin Herrenschmidt "step on CPU %u\n", cpu_policy->cpu); 90142d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 90242d4dc3fSBenjamin Herrenschmidt return ret; 90342d4dc3fSBenjamin Herrenschmidt } 90442d4dc3fSBenjamin Herrenschmidt } 90542d4dc3fSBenjamin Herrenschmidt 90642d4dc3fSBenjamin Herrenschmidt 90742d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) 90842d4dc3fSBenjamin Herrenschmidt goto out; 90942d4dc3fSBenjamin Herrenschmidt 91042d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->get) 91142d4dc3fSBenjamin Herrenschmidt cur_freq = cpufreq_driver->get(cpu_policy->cpu); 91242d4dc3fSBenjamin Herrenschmidt 91342d4dc3fSBenjamin Herrenschmidt if (!cur_freq || !cpu_policy->cur) { 91442d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed to assert current " 91542d4dc3fSBenjamin Herrenschmidt "frequency is what timing core thinks it is.\n"); 91642d4dc3fSBenjamin Herrenschmidt goto out; 91742d4dc3fSBenjamin Herrenschmidt } 91842d4dc3fSBenjamin Herrenschmidt 91942d4dc3fSBenjamin Herrenschmidt if (unlikely(cur_freq != cpu_policy->cur)) { 92042d4dc3fSBenjamin Herrenschmidt struct cpufreq_freqs freqs; 92142d4dc3fSBenjamin Herrenschmidt 92242d4dc3fSBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 92378ee998fSDave Jones dprintk(KERN_DEBUG "Warning: CPU frequency is %u, " 92442d4dc3fSBenjamin Herrenschmidt "cpufreq assumed %u kHz.\n", 92542d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 92642d4dc3fSBenjamin Herrenschmidt 92742d4dc3fSBenjamin Herrenschmidt freqs.cpu = cpu; 92842d4dc3fSBenjamin Herrenschmidt freqs.old = cpu_policy->cur; 92942d4dc3fSBenjamin Herrenschmidt freqs.new = cur_freq; 93042d4dc3fSBenjamin Herrenschmidt 93142d4dc3fSBenjamin Herrenschmidt notifier_call_chain(&cpufreq_transition_notifier_list, 93242d4dc3fSBenjamin Herrenschmidt CPUFREQ_SUSPENDCHANGE, &freqs); 93342d4dc3fSBenjamin Herrenschmidt adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); 93442d4dc3fSBenjamin Herrenschmidt 93542d4dc3fSBenjamin Herrenschmidt cpu_policy->cur = cur_freq; 93642d4dc3fSBenjamin Herrenschmidt } 93742d4dc3fSBenjamin Herrenschmidt 93842d4dc3fSBenjamin Herrenschmidt out: 93942d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 94042d4dc3fSBenjamin Herrenschmidt return 0; 94142d4dc3fSBenjamin Herrenschmidt } 94242d4dc3fSBenjamin Herrenschmidt 94342d4dc3fSBenjamin Herrenschmidt /** 9441da177e4SLinus Torvalds * cpufreq_resume - restore proper CPU frequency handling after resume 9451da177e4SLinus Torvalds * 9461da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 9471da177e4SLinus Torvalds * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync 94842d4dc3fSBenjamin Herrenschmidt * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are 94942d4dc3fSBenjamin Herrenschmidt * restored. 9501da177e4SLinus Torvalds */ 9511da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device * sysdev) 9521da177e4SLinus Torvalds { 9531da177e4SLinus Torvalds int cpu = sysdev->id; 9541da177e4SLinus Torvalds unsigned int ret = 0; 9551da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds dprintk("resuming cpu %u\n", cpu); 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds if (!cpu_online(cpu)) 9601da177e4SLinus Torvalds return 0; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds /* we may be lax here as interrupts are off. Nonetheless 9631da177e4SLinus Torvalds * we need to grab the correct cpu policy, as to check 9641da177e4SLinus Torvalds * whether we really run on this CPU. 9651da177e4SLinus Torvalds */ 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 9681da177e4SLinus Torvalds if (!cpu_policy) 9691da177e4SLinus Torvalds return -EINVAL; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds /* only handle each CPU group once */ 9721da177e4SLinus Torvalds if (unlikely(cpu_policy->cpu != cpu)) { 9731da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 9741da177e4SLinus Torvalds return 0; 9751da177e4SLinus Torvalds } 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (cpufreq_driver->resume) { 9781da177e4SLinus Torvalds ret = cpufreq_driver->resume(cpu_policy); 9791da177e4SLinus Torvalds if (ret) { 9801da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 9811da177e4SLinus Torvalds "step on CPU %u\n", cpu_policy->cpu); 9821da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 9831da177e4SLinus Torvalds return ret; 9841da177e4SLinus Torvalds } 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 9881da177e4SLinus Torvalds unsigned int cur_freq = 0; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds if (cpufreq_driver->get) 9911da177e4SLinus Torvalds cur_freq = cpufreq_driver->get(cpu_policy->cpu); 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds if (!cur_freq || !cpu_policy->cur) { 99442d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: resume failed to assert " 99542d4dc3fSBenjamin Herrenschmidt "current frequency is what timing core " 99642d4dc3fSBenjamin Herrenschmidt "thinks it is.\n"); 9971da177e4SLinus Torvalds goto out; 9981da177e4SLinus Torvalds } 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds if (unlikely(cur_freq != cpu_policy->cur)) { 10011da177e4SLinus Torvalds struct cpufreq_freqs freqs; 10021da177e4SLinus Torvalds 1003ac09f698SBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 100478ee998fSDave Jones dprintk(KERN_WARNING "Warning: CPU frequency" 1005ac09f698SBenjamin Herrenschmidt "is %u, cpufreq assumed %u kHz.\n", 100642d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds freqs.cpu = cpu; 10091da177e4SLinus Torvalds freqs.old = cpu_policy->cur; 10101da177e4SLinus Torvalds freqs.new = cur_freq; 10111da177e4SLinus Torvalds 101242d4dc3fSBenjamin Herrenschmidt notifier_call_chain(&cpufreq_transition_notifier_list, 101342d4dc3fSBenjamin Herrenschmidt CPUFREQ_RESUMECHANGE, &freqs); 10141da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); 10151da177e4SLinus Torvalds 10161da177e4SLinus Torvalds cpu_policy->cur = cur_freq; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds out: 10211da177e4SLinus Torvalds schedule_work(&cpu_policy->update); 10221da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 10231da177e4SLinus Torvalds return ret; 10241da177e4SLinus Torvalds } 10251da177e4SLinus Torvalds 10261da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = { 10271da177e4SLinus Torvalds .add = cpufreq_add_dev, 10281da177e4SLinus Torvalds .remove = cpufreq_remove_dev, 102942d4dc3fSBenjamin Herrenschmidt .suspend = cpufreq_suspend, 10301da177e4SLinus Torvalds .resume = cpufreq_resume, 10311da177e4SLinus Torvalds }; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /********************************************************************* 10351da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 10361da177e4SLinus Torvalds *********************************************************************/ 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds /** 10391da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 10401da177e4SLinus Torvalds * @nb: notifier function to register 10411da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 10421da177e4SLinus Torvalds * 10431da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 10441da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 10451da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 10461da177e4SLinus Torvalds * changes in cpufreq policy. 10471da177e4SLinus Torvalds * 10481da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 10491da177e4SLinus Torvalds * notifier_chain_register. 10501da177e4SLinus Torvalds */ 10511da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 10521da177e4SLinus Torvalds { 10531da177e4SLinus Torvalds int ret; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds down_write(&cpufreq_notifier_rwsem); 10561da177e4SLinus Torvalds switch (list) { 10571da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 10581da177e4SLinus Torvalds ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb); 10591da177e4SLinus Torvalds break; 10601da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 10611da177e4SLinus Torvalds ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb); 10621da177e4SLinus Torvalds break; 10631da177e4SLinus Torvalds default: 10641da177e4SLinus Torvalds ret = -EINVAL; 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds up_write(&cpufreq_notifier_rwsem); 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds return ret; 10691da177e4SLinus Torvalds } 10701da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds /** 10741da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 10751da177e4SLinus Torvalds * @nb: notifier block to be unregistered 10761da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 10771da177e4SLinus Torvalds * 10781da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 10791da177e4SLinus Torvalds * 10801da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 10811da177e4SLinus Torvalds * notifier_chain_unregister. 10821da177e4SLinus Torvalds */ 10831da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 10841da177e4SLinus Torvalds { 10851da177e4SLinus Torvalds int ret; 10861da177e4SLinus Torvalds 10871da177e4SLinus Torvalds down_write(&cpufreq_notifier_rwsem); 10881da177e4SLinus Torvalds switch (list) { 10891da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 10901da177e4SLinus Torvalds ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb); 10911da177e4SLinus Torvalds break; 10921da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 10931da177e4SLinus Torvalds ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb); 10941da177e4SLinus Torvalds break; 10951da177e4SLinus Torvalds default: 10961da177e4SLinus Torvalds ret = -EINVAL; 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds up_write(&cpufreq_notifier_rwsem); 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds return ret; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds /********************************************************************* 11061da177e4SLinus Torvalds * GOVERNORS * 11071da177e4SLinus Torvalds *********************************************************************/ 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 11111da177e4SLinus Torvalds unsigned int target_freq, 11121da177e4SLinus Torvalds unsigned int relation) 11131da177e4SLinus Torvalds { 11141da177e4SLinus Torvalds int retval = -EINVAL; 1115c32b6b8eSAshok Raj 1116c32b6b8eSAshok Raj /* 1117*90d45d17SAshok Raj * If we are already in context of hotplug thread, we dont need to 1118*90d45d17SAshok Raj * acquire the hotplug lock. Otherwise acquire cpucontrol to prevent 1119*90d45d17SAshok Raj * hotplug from removing this cpu that we are working on. 1120c32b6b8eSAshok Raj */ 1121*90d45d17SAshok Raj if (!current_in_cpu_hotplug()) 1122*90d45d17SAshok Raj lock_cpu_hotplug(); 1123*90d45d17SAshok Raj 11241da177e4SLinus Torvalds dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 11251da177e4SLinus Torvalds target_freq, relation); 11261da177e4SLinus Torvalds if (cpu_online(policy->cpu) && cpufreq_driver->target) 11271da177e4SLinus Torvalds retval = cpufreq_driver->target(policy, target_freq, relation); 1128*90d45d17SAshok Raj 1129*90d45d17SAshok Raj if (!current_in_cpu_hotplug()) 1130*90d45d17SAshok Raj unlock_cpu_hotplug(); 1131*90d45d17SAshok Raj 11321da177e4SLinus Torvalds return retval; 11331da177e4SLinus Torvalds } 11341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 11371da177e4SLinus Torvalds unsigned int target_freq, 11381da177e4SLinus Torvalds unsigned int relation) 11391da177e4SLinus Torvalds { 1140cc993cabSDave Jones int ret; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 11431da177e4SLinus Torvalds if (!policy) 11441da177e4SLinus Torvalds return -EINVAL; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds down(&policy->lock); 11471da177e4SLinus Torvalds 11481da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 11491da177e4SLinus Torvalds 11501da177e4SLinus Torvalds up(&policy->lock); 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds cpufreq_cpu_put(policy); 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds return ret; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event) 11601da177e4SLinus Torvalds { 1161cc993cabSDave Jones int ret; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 11641da177e4SLinus Torvalds return -EINVAL; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event); 11671da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /* we keep one module reference alive for each CPU governed by this CPU */ 11701da177e4SLinus Torvalds if ((event != CPUFREQ_GOV_START) || ret) 11711da177e4SLinus Torvalds module_put(policy->governor->owner); 11721da177e4SLinus Torvalds if ((event == CPUFREQ_GOV_STOP) && !ret) 11731da177e4SLinus Torvalds module_put(policy->governor->owner); 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds return ret; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds int cpufreq_governor(unsigned int cpu, unsigned int event) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds int ret = 0; 11821da177e4SLinus Torvalds struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds if (!policy) 11851da177e4SLinus Torvalds return -EINVAL; 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds down(&policy->lock); 11881da177e4SLinus Torvalds ret = __cpufreq_governor(policy, event); 11891da177e4SLinus Torvalds up(&policy->lock); 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds cpufreq_cpu_put(policy); 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds return ret; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_governor); 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 11991da177e4SLinus Torvalds { 12001da177e4SLinus Torvalds struct cpufreq_governor *t; 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds if (!governor) 12031da177e4SLinus Torvalds return -EINVAL; 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds down(&cpufreq_governor_sem); 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 12081da177e4SLinus Torvalds if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) { 12091da177e4SLinus Torvalds up(&cpufreq_governor_sem); 12101da177e4SLinus Torvalds return -EBUSY; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds up(&cpufreq_governor_sem); 12161da177e4SLinus Torvalds 12171da177e4SLinus Torvalds return 0; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 12231da177e4SLinus Torvalds { 12241da177e4SLinus Torvalds if (!governor) 12251da177e4SLinus Torvalds return; 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds down(&cpufreq_governor_sem); 12281da177e4SLinus Torvalds list_del(&governor->governor_list); 12291da177e4SLinus Torvalds up(&cpufreq_governor_sem); 12301da177e4SLinus Torvalds return; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /********************************************************************* 12371da177e4SLinus Torvalds * POLICY INTERFACE * 12381da177e4SLinus Torvalds *********************************************************************/ 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /** 12411da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 12421da177e4SLinus Torvalds * @policy: struct cpufreq_policy into which the current cpufreq_policy is written 12431da177e4SLinus Torvalds * 12441da177e4SLinus Torvalds * Reads the current cpufreq policy. 12451da177e4SLinus Torvalds */ 12461da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 12471da177e4SLinus Torvalds { 12481da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 12491da177e4SLinus Torvalds if (!policy) 12501da177e4SLinus Torvalds return -EINVAL; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 12531da177e4SLinus Torvalds if (!cpu_policy) 12541da177e4SLinus Torvalds return -EINVAL; 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds down(&cpu_policy->lock); 12571da177e4SLinus Torvalds memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 12581da177e4SLinus Torvalds up(&cpu_policy->lock); 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds return 0; 12631da177e4SLinus Torvalds } 12641da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy) 12681da177e4SLinus Torvalds { 12691da177e4SLinus Torvalds int ret = 0; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 12721da177e4SLinus Torvalds dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 12731da177e4SLinus Torvalds policy->min, policy->max); 12741da177e4SLinus Torvalds 12751da177e4SLinus Torvalds memcpy(&policy->cpuinfo, 12761da177e4SLinus Torvalds &data->cpuinfo, 12771da177e4SLinus Torvalds sizeof(struct cpufreq_cpuinfo)); 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 12801da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 12811da177e4SLinus Torvalds if (ret) 12821da177e4SLinus Torvalds goto error_out; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds down_read(&cpufreq_notifier_rwsem); 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 12871da177e4SLinus Torvalds notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST, 12881da177e4SLinus Torvalds policy); 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 12911da177e4SLinus Torvalds notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE, 12921da177e4SLinus Torvalds policy); 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit, 12951da177e4SLinus Torvalds which might be different to the first one */ 12961da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 12971da177e4SLinus Torvalds if (ret) { 12981da177e4SLinus Torvalds up_read(&cpufreq_notifier_rwsem); 12991da177e4SLinus Torvalds goto error_out; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds /* notification of the new policy */ 13031da177e4SLinus Torvalds notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY, 13041da177e4SLinus Torvalds policy); 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds up_read(&cpufreq_notifier_rwsem); 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds data->min = policy->min; 13091da177e4SLinus Torvalds data->max = policy->max; 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max); 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 13141da177e4SLinus Torvalds data->policy = policy->policy; 13151da177e4SLinus Torvalds dprintk("setting range\n"); 13161da177e4SLinus Torvalds ret = cpufreq_driver->setpolicy(policy); 13171da177e4SLinus Torvalds } else { 13181da177e4SLinus Torvalds if (policy->governor != data->governor) { 13191da177e4SLinus Torvalds /* save old, working values */ 13201da177e4SLinus Torvalds struct cpufreq_governor *old_gov = data->governor; 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds dprintk("governor switch\n"); 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* end old governor */ 13251da177e4SLinus Torvalds if (data->governor) 13261da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* start new governor */ 13291da177e4SLinus Torvalds data->governor = policy->governor; 13301da177e4SLinus Torvalds if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 13311da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 13321da177e4SLinus Torvalds dprintk("starting governor %s failed\n", data->governor->name); 13331da177e4SLinus Torvalds if (old_gov) { 13341da177e4SLinus Torvalds data->governor = old_gov; 13351da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_START); 13361da177e4SLinus Torvalds } 13371da177e4SLinus Torvalds ret = -EINVAL; 13381da177e4SLinus Torvalds goto error_out; 13391da177e4SLinus Torvalds } 13401da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds dprintk("governor: change or update limits\n"); 13431da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds error_out: 13471da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 13481da177e4SLinus Torvalds return ret; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds /** 13521da177e4SLinus Torvalds * cpufreq_set_policy - set a new CPUFreq policy 13531da177e4SLinus Torvalds * @policy: policy to be set. 13541da177e4SLinus Torvalds * 13551da177e4SLinus Torvalds * Sets a new CPU frequency and voltage scaling policy. 13561da177e4SLinus Torvalds */ 13571da177e4SLinus Torvalds int cpufreq_set_policy(struct cpufreq_policy *policy) 13581da177e4SLinus Torvalds { 13591da177e4SLinus Torvalds int ret = 0; 13601da177e4SLinus Torvalds struct cpufreq_policy *data; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds if (!policy) 13631da177e4SLinus Torvalds return -EINVAL; 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds data = cpufreq_cpu_get(policy->cpu); 13661da177e4SLinus Torvalds if (!data) 13671da177e4SLinus Torvalds return -EINVAL; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds /* lock this CPU */ 13701da177e4SLinus Torvalds down(&data->lock); 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, policy); 13731da177e4SLinus Torvalds data->user_policy.min = data->min; 13741da177e4SLinus Torvalds data->user_policy.max = data->max; 13751da177e4SLinus Torvalds data->user_policy.policy = data->policy; 13761da177e4SLinus Torvalds data->user_policy.governor = data->governor; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds up(&data->lock); 13791da177e4SLinus Torvalds cpufreq_cpu_put(data); 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds return ret; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_set_policy); 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds /** 13871da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 13881da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 13891da177e4SLinus Torvalds * 13901da177e4SLinus Torvalds * Usefull for policy notifiers which have different necessities 13911da177e4SLinus Torvalds * at different times. 13921da177e4SLinus Torvalds */ 13931da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 13941da177e4SLinus Torvalds { 13951da177e4SLinus Torvalds struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 13961da177e4SLinus Torvalds struct cpufreq_policy policy; 13971da177e4SLinus Torvalds int ret = 0; 13981da177e4SLinus Torvalds 13991da177e4SLinus Torvalds if (!data) 14001da177e4SLinus Torvalds return -ENODEV; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds down(&data->lock); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds dprintk("updating policy for CPU %u\n", cpu); 14051da177e4SLinus Torvalds memcpy(&policy, 14061da177e4SLinus Torvalds data, 14071da177e4SLinus Torvalds sizeof(struct cpufreq_policy)); 14081da177e4SLinus Torvalds policy.min = data->user_policy.min; 14091da177e4SLinus Torvalds policy.max = data->user_policy.max; 14101da177e4SLinus Torvalds policy.policy = data->user_policy.policy; 14111da177e4SLinus Torvalds policy.governor = data->user_policy.governor; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, &policy); 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds up(&data->lock); 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds cpufreq_cpu_put(data); 14181da177e4SLinus Torvalds return ret; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 14211da177e4SLinus Torvalds 1422c32b6b8eSAshok Raj static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, 1423c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1424c32b6b8eSAshok Raj { 1425c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 1426c32b6b8eSAshok Raj struct cpufreq_policy *policy; 1427c32b6b8eSAshok Raj struct sys_device *sys_dev; 1428c32b6b8eSAshok Raj 1429c32b6b8eSAshok Raj sys_dev = get_cpu_sysdev(cpu); 1430c32b6b8eSAshok Raj 1431c32b6b8eSAshok Raj if (sys_dev) { 1432c32b6b8eSAshok Raj switch (action) { 1433c32b6b8eSAshok Raj case CPU_ONLINE: 1434c32b6b8eSAshok Raj cpufreq_add_dev(sys_dev); 1435c32b6b8eSAshok Raj break; 1436c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 1437c32b6b8eSAshok Raj /* 1438c32b6b8eSAshok Raj * We attempt to put this cpu in lowest frequency 1439c32b6b8eSAshok Raj * possible before going down. This will permit 1440c32b6b8eSAshok Raj * hardware-managed P-State to switch other related 1441c32b6b8eSAshok Raj * threads to min or higher speeds if possible. 1442c32b6b8eSAshok Raj */ 1443c32b6b8eSAshok Raj policy = cpufreq_cpu_data[cpu]; 1444c32b6b8eSAshok Raj if (policy) { 1445c32b6b8eSAshok Raj cpufreq_driver_target(policy, policy->min, 1446c32b6b8eSAshok Raj CPUFREQ_RELATION_H); 1447c32b6b8eSAshok Raj } 1448c32b6b8eSAshok Raj break; 1449c32b6b8eSAshok Raj case CPU_DEAD: 1450c32b6b8eSAshok Raj cpufreq_remove_dev(sys_dev); 1451c32b6b8eSAshok Raj break; 1452c32b6b8eSAshok Raj } 1453c32b6b8eSAshok Raj } 1454c32b6b8eSAshok Raj return NOTIFY_OK; 1455c32b6b8eSAshok Raj } 1456c32b6b8eSAshok Raj 1457c32b6b8eSAshok Raj static struct notifier_block cpufreq_cpu_notifier = 1458c32b6b8eSAshok Raj { 1459c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 1460c32b6b8eSAshok Raj }; 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds /********************************************************************* 14631da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 14641da177e4SLinus Torvalds *********************************************************************/ 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds /** 14671da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 14681da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 14691da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 14701da177e4SLinus Torvalds * 14711da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 14721da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 14731da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 14741da177e4SLinus Torvalds * 14751da177e4SLinus Torvalds */ 14761da177e4SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 14771da177e4SLinus Torvalds { 14781da177e4SLinus Torvalds unsigned long flags; 14791da177e4SLinus Torvalds int ret; 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 14821da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 14831da177e4SLinus Torvalds return -EINVAL; 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds dprintk("trying to register driver %s\n", driver_data->name); 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds if (driver_data->setpolicy) 14881da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 14891da177e4SLinus Torvalds 14901da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 14911da177e4SLinus Torvalds if (cpufreq_driver) { 14921da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 14931da177e4SLinus Torvalds return -EBUSY; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds cpufreq_driver = driver_data; 14961da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 15011da177e4SLinus Torvalds int i; 15021da177e4SLinus Torvalds ret = -ENODEV; 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /* check for at least one working CPU */ 15051da177e4SLinus Torvalds for (i=0; i<NR_CPUS; i++) 15061da177e4SLinus Torvalds if (cpufreq_cpu_data[i]) 15071da177e4SLinus Torvalds ret = 0; 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 15101da177e4SLinus Torvalds if (ret) { 15111da177e4SLinus Torvalds dprintk("no CPU initialized for driver %s\n", driver_data->name); 15121da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 15151da177e4SLinus Torvalds cpufreq_driver = NULL; 15161da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 15171da177e4SLinus Torvalds } 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds if (!ret) { 1521c32b6b8eSAshok Raj register_cpu_notifier(&cpufreq_cpu_notifier); 15221da177e4SLinus Torvalds dprintk("driver %s up and running\n", driver_data->name); 15231da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds return (ret); 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /** 15321da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 15331da177e4SLinus Torvalds * 15341da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 15351da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 15361da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 15371da177e4SLinus Torvalds * currently not initialised. 15381da177e4SLinus Torvalds */ 15391da177e4SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 15401da177e4SLinus Torvalds { 15411da177e4SLinus Torvalds unsigned long flags; 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds if (!cpufreq_driver || (driver != cpufreq_driver)) { 15461da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 15471da177e4SLinus Torvalds return -EINVAL; 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds dprintk("unregistering driver %s\n", driver->name); 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 1553c32b6b8eSAshok Raj unregister_cpu_notifier(&cpufreq_cpu_notifier); 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 15561da177e4SLinus Torvalds cpufreq_driver = NULL; 15571da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 15581da177e4SLinus Torvalds 15591da177e4SLinus Torvalds return 0; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 1562