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 98ff69732SDave Jones * Feb 2006 - Jacob Shin <jacob.shin@amd.com> 108ff69732SDave Jones * Fix handling for CPU hotplug -- affected CPUs 11c32b6b8eSAshok Raj * 121da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 131da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 141da177e4SLinus Torvalds * published by the Free Software Foundation. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #include <linux/kernel.h> 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/init.h> 211da177e4SLinus Torvalds #include <linux/notifier.h> 221da177e4SLinus Torvalds #include <linux/cpufreq.h> 231da177e4SLinus Torvalds #include <linux/delay.h> 241da177e4SLinus Torvalds #include <linux/interrupt.h> 251da177e4SLinus Torvalds #include <linux/spinlock.h> 261da177e4SLinus Torvalds #include <linux/device.h> 271da177e4SLinus Torvalds #include <linux/slab.h> 281da177e4SLinus Torvalds #include <linux/cpu.h> 291da177e4SLinus Torvalds #include <linux/completion.h> 303fc54d37Sakpm@osdl.org #include <linux/mutex.h> 311da177e4SLinus Torvalds 32e08f5f5bSGautham R Shenoy #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \ 33e08f5f5bSGautham R Shenoy "cpufreq-core", msg) 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds /** 36cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 371da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 381da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 391da177e4SLinus Torvalds */ 401da177e4SLinus Torvalds static struct cpufreq_driver *cpufreq_driver; 411da177e4SLinus Torvalds static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS]; 421da177e4SLinus Torvalds static DEFINE_SPINLOCK(cpufreq_driver_lock); 431da177e4SLinus Torvalds 44*5a01f2e8SVenkatesh Pallipadi /* 45*5a01f2e8SVenkatesh Pallipadi * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure 46*5a01f2e8SVenkatesh Pallipadi * all cpufreq/hotplug/workqueue/etc related lock issues. 47*5a01f2e8SVenkatesh Pallipadi * 48*5a01f2e8SVenkatesh Pallipadi * The rules for this semaphore: 49*5a01f2e8SVenkatesh Pallipadi * - Any routine that wants to read from the policy structure will 50*5a01f2e8SVenkatesh Pallipadi * do a down_read on this semaphore. 51*5a01f2e8SVenkatesh Pallipadi * - Any routine that will write to the policy structure and/or may take away 52*5a01f2e8SVenkatesh Pallipadi * the policy altogether (eg. CPU hotplug), will hold this lock in write 53*5a01f2e8SVenkatesh Pallipadi * mode before doing so. 54*5a01f2e8SVenkatesh Pallipadi * 55*5a01f2e8SVenkatesh Pallipadi * Additional rules: 56*5a01f2e8SVenkatesh Pallipadi * - All holders of the lock should check to make sure that the CPU they 57*5a01f2e8SVenkatesh Pallipadi * are concerned with are online after they get the lock. 58*5a01f2e8SVenkatesh Pallipadi * - Governor routines that can be called in cpufreq hotplug path should not 59*5a01f2e8SVenkatesh Pallipadi * take this sem as top level hotplug notifier handler takes this. 60*5a01f2e8SVenkatesh Pallipadi */ 61*5a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(int, policy_cpu); 62*5a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem); 63*5a01f2e8SVenkatesh Pallipadi 64*5a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu) \ 65*5a01f2e8SVenkatesh Pallipadi int lock_policy_rwsem_##mode \ 66*5a01f2e8SVenkatesh Pallipadi (int cpu) \ 67*5a01f2e8SVenkatesh Pallipadi { \ 68*5a01f2e8SVenkatesh Pallipadi int policy_cpu = per_cpu(policy_cpu, cpu); \ 69*5a01f2e8SVenkatesh Pallipadi BUG_ON(policy_cpu == -1); \ 70*5a01f2e8SVenkatesh Pallipadi down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \ 71*5a01f2e8SVenkatesh Pallipadi if (unlikely(!cpu_online(cpu))) { \ 72*5a01f2e8SVenkatesh Pallipadi up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \ 73*5a01f2e8SVenkatesh Pallipadi return -1; \ 74*5a01f2e8SVenkatesh Pallipadi } \ 75*5a01f2e8SVenkatesh Pallipadi \ 76*5a01f2e8SVenkatesh Pallipadi return 0; \ 77*5a01f2e8SVenkatesh Pallipadi } 78*5a01f2e8SVenkatesh Pallipadi 79*5a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu); 80*5a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(lock_policy_rwsem_read); 81*5a01f2e8SVenkatesh Pallipadi 82*5a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu); 83*5a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(lock_policy_rwsem_write); 84*5a01f2e8SVenkatesh Pallipadi 85*5a01f2e8SVenkatesh Pallipadi void unlock_policy_rwsem_read(int cpu) 86*5a01f2e8SVenkatesh Pallipadi { 87*5a01f2e8SVenkatesh Pallipadi int policy_cpu = per_cpu(policy_cpu, cpu); 88*5a01f2e8SVenkatesh Pallipadi BUG_ON(policy_cpu == -1); 89*5a01f2e8SVenkatesh Pallipadi up_read(&per_cpu(cpu_policy_rwsem, policy_cpu)); 90*5a01f2e8SVenkatesh Pallipadi } 91*5a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read); 92*5a01f2e8SVenkatesh Pallipadi 93*5a01f2e8SVenkatesh Pallipadi void unlock_policy_rwsem_write(int cpu) 94*5a01f2e8SVenkatesh Pallipadi { 95*5a01f2e8SVenkatesh Pallipadi int policy_cpu = per_cpu(policy_cpu, cpu); 96*5a01f2e8SVenkatesh Pallipadi BUG_ON(policy_cpu == -1); 97*5a01f2e8SVenkatesh Pallipadi up_write(&per_cpu(cpu_policy_rwsem, policy_cpu)); 98*5a01f2e8SVenkatesh Pallipadi } 99*5a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write); 100*5a01f2e8SVenkatesh Pallipadi 101*5a01f2e8SVenkatesh Pallipadi 1021da177e4SLinus Torvalds /* internal prototypes */ 1031da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); 104*5a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu); 10565f27f38SDavid Howells static void handle_update(struct work_struct *work); 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds /** 1081da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 1091da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 1101da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 1111da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 1121da177e4SLinus Torvalds * The mutex locks both lists. 1131da177e4SLinus Torvalds */ 114e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 115b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 1161da177e4SLinus Torvalds 117b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 118b4dfdbb3SAlan Stern { 119b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 120b4dfdbb3SAlan Stern return 0; 121b4dfdbb3SAlan Stern } 122b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 1253fc54d37Sakpm@osdl.org static DEFINE_MUTEX (cpufreq_governor_mutex); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds struct cpufreq_policy *data; 1301da177e4SLinus Torvalds unsigned long flags; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds if (cpu >= NR_CPUS) 1331da177e4SLinus Torvalds goto err_out; 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* get the cpufreq driver */ 1361da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds if (!cpufreq_driver) 1391da177e4SLinus Torvalds goto err_out_unlock; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) 1421da177e4SLinus Torvalds goto err_out_unlock; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* get the CPU */ 1461da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds if (!data) 1491da177e4SLinus Torvalds goto err_out_put_module; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) 1521da177e4SLinus Torvalds goto err_out_put_module; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1551da177e4SLinus Torvalds return data; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds err_out_put_module: 1581da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1591da177e4SLinus Torvalds err_out_unlock: 1601da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1611da177e4SLinus Torvalds err_out: 1621da177e4SLinus Torvalds return NULL; 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 1651da177e4SLinus Torvalds 1667d5e350fSDave Jones 1671da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data) 1681da177e4SLinus Torvalds { 1691da177e4SLinus Torvalds kobject_put(&data->kobj); 1701da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds /********************************************************************* 1761da177e4SLinus Torvalds * UNIFIED DEBUG HELPERS * 1771da177e4SLinus Torvalds *********************************************************************/ 1781da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */ 1811da177e4SLinus Torvalds static unsigned int debug; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can 1841da177e4SLinus Torvalds * set or modify this value. 1851da177e4SLinus Torvalds */ 1861da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1; 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful 1891da177e4SLinus Torvalds * loading of a cpufreq driver, temporarily disabled when a new policy 1901da177e4SLinus Torvalds * is set, and disabled upon cpufreq driver removal 1911da177e4SLinus Torvalds */ 1921da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1; 1931da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock); 1941da177e4SLinus Torvalds 195858119e1SArjan van de Ven static void cpufreq_debug_enable_ratelimit(void) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds unsigned long flags; 1981da177e4SLinus Torvalds 1991da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2001da177e4SLinus Torvalds if (disable_ratelimit) 2011da177e4SLinus Torvalds disable_ratelimit--; 2021da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205858119e1SArjan van de Ven static void cpufreq_debug_disable_ratelimit(void) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds unsigned long flags; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2101da177e4SLinus Torvalds disable_ratelimit++; 2111da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 214e08f5f5bSGautham R Shenoy void cpufreq_debug_printk(unsigned int type, const char *prefix, 215e08f5f5bSGautham R Shenoy const char *fmt, ...) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds char s[256]; 2181da177e4SLinus Torvalds va_list args; 2191da177e4SLinus Torvalds unsigned int len; 2201da177e4SLinus Torvalds unsigned long flags; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds WARN_ON(!prefix); 2231da177e4SLinus Torvalds if (type & debug) { 2241da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 225e08f5f5bSGautham R Shenoy if (!disable_ratelimit && debug_ratelimit 226e08f5f5bSGautham R Shenoy && !printk_ratelimit()) { 2271da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2281da177e4SLinus Torvalds return; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds va_start(args, fmt); 2351da177e4SLinus Torvalds len += vsnprintf(&s[len], (256 - len), fmt, args); 2361da177e4SLinus Torvalds va_end(args); 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds printk(s); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds WARN_ON(len < 5); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds module_param(debug, uint, 0644); 247e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core," 248e08f5f5bSGautham R Shenoy " 2 to debug drivers, and 4 to debug governors."); 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644); 251e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:" 252e08f5f5bSGautham R Shenoy " set to 0 to disable ratelimiting."); 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */ 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; } 2571da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */ 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /********************************************************************* 2631da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2641da177e4SLinus Torvalds *********************************************************************/ 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /** 2671da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2681da177e4SLinus Torvalds * 2691da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2701da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2711da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2721da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds #ifndef CONFIG_SMP 2751da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2761da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2771da177e4SLinus Torvalds 278858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2811da177e4SLinus Torvalds return; 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2841da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2851da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 286e08f5f5bSGautham R Shenoy dprintk("saving %lu as reference value for loops_per_jiffy;" 287e08f5f5bSGautham R Shenoy "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || 2901da177e4SLinus Torvalds (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || 29142d4dc3fSBenjamin Herrenschmidt (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 292e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 293e08f5f5bSGautham R Shenoy ci->new); 294e08f5f5bSGautham R Shenoy dprintk("scaling loops_per_jiffy to %lu" 295e08f5f5bSGautham R Shenoy "for frequency %u kHz\n", loops_per_jiffy, ci->new); 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds #else 299e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 300e08f5f5bSGautham R Shenoy { 301e08f5f5bSGautham R Shenoy return; 302e08f5f5bSGautham R Shenoy } 3031da177e4SLinus Torvalds #endif 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds /** 307e4472cb3SDave Jones * cpufreq_notify_transition - call notifier chain and adjust_jiffies 308e4472cb3SDave Jones * on frequency transition. 3091da177e4SLinus Torvalds * 310e4472cb3SDave Jones * This function calls the transition notifiers and the "adjust_jiffies" 311e4472cb3SDave Jones * function. It is called twice on all CPU frequency changes that have 312e4472cb3SDave Jones * external effects. 3131da177e4SLinus Torvalds */ 3141da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) 3151da177e4SLinus Torvalds { 316e4472cb3SDave Jones struct cpufreq_policy *policy; 317e4472cb3SDave Jones 3181da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds freqs->flags = cpufreq_driver->flags; 321e4472cb3SDave Jones dprintk("notification %u of frequency transition to %u kHz\n", 322e4472cb3SDave Jones state, freqs->new); 3231da177e4SLinus Torvalds 324e4472cb3SDave Jones policy = cpufreq_cpu_data[freqs->cpu]; 3251da177e4SLinus Torvalds switch (state) { 326e4472cb3SDave Jones 3271da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 328e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 329e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 330e4472cb3SDave Jones * "old frequency". 3311da177e4SLinus Torvalds */ 3321da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 333e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 334e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 335b10eec22SJan Beulich dprintk("Warning: CPU frequency is" 336e4472cb3SDave Jones " %u, cpufreq assumed %u kHz.\n", 337e4472cb3SDave Jones freqs->old, policy->cur); 338e4472cb3SDave Jones freqs->old = policy->cur; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds } 341b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 342e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3431da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3441da177e4SLinus Torvalds break; 345e4472cb3SDave Jones 3461da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3471da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 348b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 349e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 350e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 351e4472cb3SDave Jones policy->cur = freqs->new; 3521da177e4SLinus Torvalds break; 3531da177e4SLinus Torvalds } 3541da177e4SLinus Torvalds } 3551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /********************************************************************* 3601da177e4SLinus Torvalds * SYSFS INTERFACE * 3611da177e4SLinus Torvalds *********************************************************************/ 3621da177e4SLinus Torvalds 3633bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 3643bcb09a3SJeremy Fitzhardinge { 3653bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 3663bcb09a3SJeremy Fitzhardinge 3673bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 3683bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) 3693bcb09a3SJeremy Fitzhardinge return t; 3703bcb09a3SJeremy Fitzhardinge 3713bcb09a3SJeremy Fitzhardinge return NULL; 3723bcb09a3SJeremy Fitzhardinge } 3733bcb09a3SJeremy Fitzhardinge 3741da177e4SLinus Torvalds /** 3751da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, 3781da177e4SLinus Torvalds struct cpufreq_governor **governor) 3791da177e4SLinus Torvalds { 3803bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 3813bcb09a3SJeremy Fitzhardinge 3821da177e4SLinus Torvalds if (!cpufreq_driver) 3833bcb09a3SJeremy Fitzhardinge goto out; 3843bcb09a3SJeremy Fitzhardinge 3851da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 3861da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 3871da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 3883bcb09a3SJeremy Fitzhardinge err = 0; 389e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 390e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 3911da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 3923bcb09a3SJeremy Fitzhardinge err = 0; 3931da177e4SLinus Torvalds } 3943bcb09a3SJeremy Fitzhardinge } else if (cpufreq_driver->target) { 3951da177e4SLinus Torvalds struct cpufreq_governor *t; 3963bcb09a3SJeremy Fitzhardinge 3973fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 3983bcb09a3SJeremy Fitzhardinge 3993bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 4003bcb09a3SJeremy Fitzhardinge 401ea714970SJeremy Fitzhardinge if (t == NULL) { 402e08f5f5bSGautham R Shenoy char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", 403e08f5f5bSGautham R Shenoy str_governor); 404ea714970SJeremy Fitzhardinge 405ea714970SJeremy Fitzhardinge if (name) { 406ea714970SJeremy Fitzhardinge int ret; 407ea714970SJeremy Fitzhardinge 408ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 409ea714970SJeremy Fitzhardinge ret = request_module(name); 410ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 411ea714970SJeremy Fitzhardinge 412ea714970SJeremy Fitzhardinge if (ret == 0) 413ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 414ea714970SJeremy Fitzhardinge } 415ea714970SJeremy Fitzhardinge 416ea714970SJeremy Fitzhardinge kfree(name); 417ea714970SJeremy Fitzhardinge } 418ea714970SJeremy Fitzhardinge 4193bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4201da177e4SLinus Torvalds *governor = t; 4213bcb09a3SJeremy Fitzhardinge err = 0; 4221da177e4SLinus Torvalds } 4233bcb09a3SJeremy Fitzhardinge 4243bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4251da177e4SLinus Torvalds } 4261da177e4SLinus Torvalds out: 4273bcb09a3SJeremy Fitzhardinge return err; 4281da177e4SLinus Torvalds } 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds /* drivers/base/cpu.c */ 4321da177e4SLinus Torvalds extern struct sysdev_class cpu_sysdev_class; 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds /** 436e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 437e08f5f5bSGautham R Shenoy * print out cpufreq information 4381da177e4SLinus Torvalds * 4391da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 4401da177e4SLinus Torvalds * "unsigned int". 4411da177e4SLinus Torvalds */ 4421da177e4SLinus Torvalds 4431da177e4SLinus Torvalds #define show_one(file_name, object) \ 4441da177e4SLinus Torvalds static ssize_t show_##file_name \ 4451da177e4SLinus Torvalds (struct cpufreq_policy * policy, char *buf) \ 4461da177e4SLinus Torvalds { \ 4471da177e4SLinus Torvalds return sprintf (buf, "%u\n", policy->object); \ 4481da177e4SLinus Torvalds } 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 4511da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 4521da177e4SLinus Torvalds show_one(scaling_min_freq, min); 4531da177e4SLinus Torvalds show_one(scaling_max_freq, max); 4541da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 4551da177e4SLinus Torvalds 456e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 457e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy); 4587970e08bSThomas Renninger 4591da177e4SLinus Torvalds /** 4601da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 4611da177e4SLinus Torvalds */ 4621da177e4SLinus Torvalds #define store_one(file_name, object) \ 4631da177e4SLinus Torvalds static ssize_t store_##file_name \ 4641da177e4SLinus Torvalds (struct cpufreq_policy * policy, const char *buf, size_t count) \ 4651da177e4SLinus Torvalds { \ 4661da177e4SLinus Torvalds unsigned int ret = -EINVAL; \ 4671da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 4681da177e4SLinus Torvalds \ 4691da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 4701da177e4SLinus Torvalds if (ret) \ 4711da177e4SLinus Torvalds return -EINVAL; \ 4721da177e4SLinus Torvalds \ 4731da177e4SLinus Torvalds ret = sscanf (buf, "%u", &new_policy.object); \ 4741da177e4SLinus Torvalds if (ret != 1) \ 4751da177e4SLinus Torvalds return -EINVAL; \ 4761da177e4SLinus Torvalds \ 4777970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); \ 4787970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 4791da177e4SLinus Torvalds \ 4801da177e4SLinus Torvalds return ret ? ret : count; \ 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds store_one(scaling_min_freq,min); 4841da177e4SLinus Torvalds store_one(scaling_max_freq,max); 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds /** 4871da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 4881da177e4SLinus Torvalds */ 489e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, 490e08f5f5bSGautham R Shenoy char *buf) 4911da177e4SLinus Torvalds { 492*5a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 4931da177e4SLinus Torvalds if (!cur_freq) 4941da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 4951da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds /** 5001da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5011da177e4SLinus Torvalds */ 502e08f5f5bSGautham R Shenoy static ssize_t show_scaling_governor (struct cpufreq_policy * policy, 503e08f5f5bSGautham R Shenoy char *buf) 5041da177e4SLinus Torvalds { 5051da177e4SLinus Torvalds if(policy->policy == CPUFREQ_POLICY_POWERSAVE) 5061da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5071da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5081da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5091da177e4SLinus Torvalds else if (policy->governor) 5101da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name); 5111da177e4SLinus Torvalds return -EINVAL; 5121da177e4SLinus Torvalds } 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /** 5161da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5171da177e4SLinus Torvalds */ 5181da177e4SLinus Torvalds static ssize_t store_scaling_governor (struct cpufreq_policy * policy, 5191da177e4SLinus Torvalds const char *buf, size_t count) 5201da177e4SLinus Torvalds { 5211da177e4SLinus Torvalds unsigned int ret = -EINVAL; 5221da177e4SLinus Torvalds char str_governor[16]; 5231da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5261da177e4SLinus Torvalds if (ret) 5271da177e4SLinus Torvalds return ret; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds ret = sscanf (buf, "%15s", str_governor); 5301da177e4SLinus Torvalds if (ret != 1) 5311da177e4SLinus Torvalds return -EINVAL; 5321da177e4SLinus Torvalds 533e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 534e08f5f5bSGautham R Shenoy &new_policy.governor)) 5351da177e4SLinus Torvalds return -EINVAL; 5361da177e4SLinus Torvalds 5377970e08bSThomas Renninger /* Do not use cpufreq_set_policy here or the user_policy.max 5387970e08bSThomas Renninger will be wrongly overridden */ 5397970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 5407970e08bSThomas Renninger 5417970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 5427970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 5437970e08bSThomas Renninger 544e08f5f5bSGautham R Shenoy if (ret) 545e08f5f5bSGautham R Shenoy return ret; 546e08f5f5bSGautham R Shenoy else 547e08f5f5bSGautham R Shenoy return count; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 5501da177e4SLinus Torvalds /** 5511da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 5521da177e4SLinus Torvalds */ 5531da177e4SLinus Torvalds static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf) 5541da177e4SLinus Torvalds { 5551da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /** 5591da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 5601da177e4SLinus Torvalds */ 5611da177e4SLinus Torvalds static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy, 5621da177e4SLinus Torvalds char *buf) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds ssize_t i = 0; 5651da177e4SLinus Torvalds struct cpufreq_governor *t; 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds if (!cpufreq_driver->target) { 5681da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 5691da177e4SLinus Torvalds goto out; 5701da177e4SLinus Torvalds } 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 5731da177e4SLinus Torvalds if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) 5741da177e4SLinus Torvalds goto out; 5751da177e4SLinus Torvalds i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds out: 5781da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 5791da177e4SLinus Torvalds return i; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds /** 5821da177e4SLinus Torvalds * show_affected_cpus - show the CPUs affected by each transition 5831da177e4SLinus Torvalds */ 5841da177e4SLinus Torvalds static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds ssize_t i = 0; 5871da177e4SLinus Torvalds unsigned int cpu; 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds for_each_cpu_mask(cpu, policy->cpus) { 5901da177e4SLinus Torvalds if (i) 5911da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 5921da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 5931da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 5941da177e4SLinus Torvalds break; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 5971da177e4SLinus Torvalds return i; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds #define define_one_ro(_name) \ 6021da177e4SLinus Torvalds static struct freq_attr _name = \ 6031da177e4SLinus Torvalds __ATTR(_name, 0444, show_##_name, NULL) 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds #define define_one_ro0400(_name) \ 6061da177e4SLinus Torvalds static struct freq_attr _name = \ 6071da177e4SLinus Torvalds __ATTR(_name, 0400, show_##_name, NULL) 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds #define define_one_rw(_name) \ 6101da177e4SLinus Torvalds static struct freq_attr _name = \ 6111da177e4SLinus Torvalds __ATTR(_name, 0644, show_##_name, store_##_name) 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds define_one_ro0400(cpuinfo_cur_freq); 6141da177e4SLinus Torvalds define_one_ro(cpuinfo_min_freq); 6151da177e4SLinus Torvalds define_one_ro(cpuinfo_max_freq); 6161da177e4SLinus Torvalds define_one_ro(scaling_available_governors); 6171da177e4SLinus Torvalds define_one_ro(scaling_driver); 6181da177e4SLinus Torvalds define_one_ro(scaling_cur_freq); 6191da177e4SLinus Torvalds define_one_ro(affected_cpus); 6201da177e4SLinus Torvalds define_one_rw(scaling_min_freq); 6211da177e4SLinus Torvalds define_one_rw(scaling_max_freq); 6221da177e4SLinus Torvalds define_one_rw(scaling_governor); 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds static struct attribute * default_attrs[] = { 6251da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 6261da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 6271da177e4SLinus Torvalds &scaling_min_freq.attr, 6281da177e4SLinus Torvalds &scaling_max_freq.attr, 6291da177e4SLinus Torvalds &affected_cpus.attr, 6301da177e4SLinus Torvalds &scaling_governor.attr, 6311da177e4SLinus Torvalds &scaling_driver.attr, 6321da177e4SLinus Torvalds &scaling_available_governors.attr, 6331da177e4SLinus Torvalds NULL 6341da177e4SLinus Torvalds }; 6351da177e4SLinus Torvalds 6361da177e4SLinus Torvalds #define to_policy(k) container_of(k,struct cpufreq_policy,kobj) 6371da177e4SLinus Torvalds #define to_attr(a) container_of(a,struct freq_attr,attr) 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf) 6401da177e4SLinus Torvalds { 6411da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 6421da177e4SLinus Torvalds struct freq_attr * fattr = to_attr(attr); 6431da177e4SLinus Torvalds ssize_t ret; 6441da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 6451da177e4SLinus Torvalds if (!policy) 6461da177e4SLinus Torvalds return -EINVAL; 647*5a01f2e8SVenkatesh Pallipadi 648*5a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_read(policy->cpu) < 0) 649*5a01f2e8SVenkatesh Pallipadi return -EINVAL; 650*5a01f2e8SVenkatesh Pallipadi 651e08f5f5bSGautham R Shenoy if (fattr->show) 652e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 653e08f5f5bSGautham R Shenoy else 654e08f5f5bSGautham R Shenoy ret = -EIO; 655e08f5f5bSGautham R Shenoy 656*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(policy->cpu); 657*5a01f2e8SVenkatesh Pallipadi 6581da177e4SLinus Torvalds cpufreq_cpu_put(policy); 6591da177e4SLinus Torvalds return ret; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds static ssize_t store(struct kobject * kobj, struct attribute * attr, 6631da177e4SLinus Torvalds const char * buf, size_t count) 6641da177e4SLinus Torvalds { 6651da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 6661da177e4SLinus Torvalds struct freq_attr * fattr = to_attr(attr); 6671da177e4SLinus Torvalds ssize_t ret; 6681da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 6691da177e4SLinus Torvalds if (!policy) 6701da177e4SLinus Torvalds return -EINVAL; 671*5a01f2e8SVenkatesh Pallipadi 672*5a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(policy->cpu) < 0) 673*5a01f2e8SVenkatesh Pallipadi return -EINVAL; 674*5a01f2e8SVenkatesh Pallipadi 675e08f5f5bSGautham R Shenoy if (fattr->store) 676e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 677e08f5f5bSGautham R Shenoy else 678e08f5f5bSGautham R Shenoy ret = -EIO; 679e08f5f5bSGautham R Shenoy 680*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 681*5a01f2e8SVenkatesh Pallipadi 6821da177e4SLinus Torvalds cpufreq_cpu_put(policy); 6831da177e4SLinus Torvalds return ret; 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject * kobj) 6871da177e4SLinus Torvalds { 6881da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 6891da177e4SLinus Torvalds dprintk("last reference is dropped\n"); 6901da177e4SLinus Torvalds complete(&policy->kobj_unregister); 6911da177e4SLinus Torvalds } 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = { 6941da177e4SLinus Torvalds .show = show, 6951da177e4SLinus Torvalds .store = store, 6961da177e4SLinus Torvalds }; 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 6991da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 7001da177e4SLinus Torvalds .default_attrs = default_attrs, 7011da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 7021da177e4SLinus Torvalds }; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds /** 7061da177e4SLinus Torvalds * cpufreq_add_dev - add a CPU device 7071da177e4SLinus Torvalds * 7081da177e4SLinus Torvalds * Adds the cpufreq interface for a CPU device. 7091da177e4SLinus Torvalds */ 7101da177e4SLinus Torvalds static int cpufreq_add_dev (struct sys_device * sys_dev) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 7131da177e4SLinus Torvalds int ret = 0; 7141da177e4SLinus Torvalds struct cpufreq_policy new_policy; 7151da177e4SLinus Torvalds struct cpufreq_policy *policy; 7161da177e4SLinus Torvalds struct freq_attr **drv_attr; 7178ff69732SDave Jones struct sys_device *cpu_sys_dev; 7181da177e4SLinus Torvalds unsigned long flags; 7191da177e4SLinus Torvalds unsigned int j; 7208ff69732SDave Jones #ifdef CONFIG_SMP 7218ff69732SDave Jones struct cpufreq_policy *managed_policy; 7228ff69732SDave Jones #endif 7231da177e4SLinus Torvalds 724c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 725c32b6b8eSAshok Raj return 0; 726c32b6b8eSAshok Raj 7271da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 7281da177e4SLinus Torvalds dprintk("adding CPU %u\n", cpu); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds #ifdef CONFIG_SMP 7311da177e4SLinus Torvalds /* check whether a different CPU already registered this 7321da177e4SLinus Torvalds * CPU because it is in the same boat. */ 7331da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 7341da177e4SLinus Torvalds if (unlikely(policy)) { 7358ff69732SDave Jones cpufreq_cpu_put(policy); 7361da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7371da177e4SLinus Torvalds return 0; 7381da177e4SLinus Torvalds } 7391da177e4SLinus Torvalds #endif 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) { 7421da177e4SLinus Torvalds ret = -EINVAL; 7431da177e4SLinus Torvalds goto module_out; 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 746e98df50cSDave Jones policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 7471da177e4SLinus Torvalds if (!policy) { 7481da177e4SLinus Torvalds ret = -ENOMEM; 7491da177e4SLinus Torvalds goto nomem_out; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds policy->cpu = cpu; 7531da177e4SLinus Torvalds policy->cpus = cpumask_of_cpu(cpu); 7541da177e4SLinus Torvalds 755*5a01f2e8SVenkatesh Pallipadi /* Initially set CPU itself as the policy_cpu */ 756*5a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = cpu; 757*5a01f2e8SVenkatesh Pallipadi lock_policy_rwsem_write(cpu); 758*5a01f2e8SVenkatesh Pallipadi 7591da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 76065f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 7631da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 7641da177e4SLinus Torvalds */ 7651da177e4SLinus Torvalds ret = cpufreq_driver->init(policy); 7661da177e4SLinus Torvalds if (ret) { 7671da177e4SLinus Torvalds dprintk("initialization failed\n"); 768*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 7691da177e4SLinus Torvalds goto err_out; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7728ff69732SDave Jones #ifdef CONFIG_SMP 7738ff69732SDave Jones for_each_cpu_mask(j, policy->cpus) { 7748ff69732SDave Jones if (cpu == j) 7758ff69732SDave Jones continue; 7768ff69732SDave Jones 7778ff69732SDave Jones /* check for existing affected CPUs. They may not be aware 7788ff69732SDave Jones * of it due to CPU Hotplug. 7798ff69732SDave Jones */ 7808ff69732SDave Jones managed_policy = cpufreq_cpu_get(j); 7818ff69732SDave Jones if (unlikely(managed_policy)) { 782*5a01f2e8SVenkatesh Pallipadi 783*5a01f2e8SVenkatesh Pallipadi /* Set proper policy_cpu */ 784*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 785*5a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = managed_policy->cpu; 786*5a01f2e8SVenkatesh Pallipadi 787*5a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(cpu) < 0) 788*5a01f2e8SVenkatesh Pallipadi goto err_out_driver_exit; 789*5a01f2e8SVenkatesh Pallipadi 7908ff69732SDave Jones spin_lock_irqsave(&cpufreq_driver_lock, flags); 7918ff69732SDave Jones managed_policy->cpus = policy->cpus; 7928ff69732SDave Jones cpufreq_cpu_data[cpu] = managed_policy; 7938ff69732SDave Jones spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 7948ff69732SDave Jones 7958ff69732SDave Jones dprintk("CPU already managed, adding link\n"); 7960142f9dcSAhmed S. Darwish ret = sysfs_create_link(&sys_dev->kobj, 7970142f9dcSAhmed S. Darwish &managed_policy->kobj, 7980142f9dcSAhmed S. Darwish "cpufreq"); 7990142f9dcSAhmed S. Darwish if (ret) { 800*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8010142f9dcSAhmed S. Darwish goto err_out_driver_exit; 8020142f9dcSAhmed S. Darwish } 8038ff69732SDave Jones 8048ff69732SDave Jones cpufreq_debug_enable_ratelimit(); 8058ff69732SDave Jones ret = 0; 806*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8078ff69732SDave Jones goto err_out_driver_exit; /* call driver->exit() */ 8088ff69732SDave Jones } 8098ff69732SDave Jones } 8108ff69732SDave Jones #endif 8111da177e4SLinus Torvalds memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /* prepare interface data */ 8141da177e4SLinus Torvalds policy->kobj.parent = &sys_dev->kobj; 8151da177e4SLinus Torvalds policy->kobj.ktype = &ktype_cpufreq; 8161da177e4SLinus Torvalds strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds ret = kobject_register(&policy->kobj); 819f3876c1bSAndrew Morton if (ret) { 820*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8218085e1f1SVenkatesh Pallipadi goto err_out_driver_exit; 822f3876c1bSAndrew Morton } 8231da177e4SLinus Torvalds /* set up files for this cpu device */ 8241da177e4SLinus Torvalds drv_attr = cpufreq_driver->attr; 8251da177e4SLinus Torvalds while ((drv_attr) && (*drv_attr)) { 8261da177e4SLinus Torvalds sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 8271da177e4SLinus Torvalds drv_attr++; 8281da177e4SLinus Torvalds } 8291da177e4SLinus Torvalds if (cpufreq_driver->get) 8301da177e4SLinus Torvalds sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 8311da177e4SLinus Torvalds if (cpufreq_driver->target) 8321da177e4SLinus Torvalds sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 835*5a01f2e8SVenkatesh Pallipadi for_each_cpu_mask(j, policy->cpus) { 8361da177e4SLinus Torvalds cpufreq_cpu_data[j] = policy; 837*5a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, j) = policy->cpu; 838*5a01f2e8SVenkatesh Pallipadi } 8391da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 8408ff69732SDave Jones 8418ff69732SDave Jones /* symlink affected CPUs */ 8428ff69732SDave Jones for_each_cpu_mask(j, policy->cpus) { 8438ff69732SDave Jones if (j == cpu) 8448ff69732SDave Jones continue; 8458ff69732SDave Jones if (!cpu_online(j)) 8468ff69732SDave Jones continue; 8478ff69732SDave Jones 8481f8b2c9dSDave Jones dprintk("CPU %u already managed, adding link\n", j); 8498ff69732SDave Jones cpufreq_cpu_get(cpu); 8508ff69732SDave Jones cpu_sys_dev = get_cpu_sysdev(j); 8510142f9dcSAhmed S. Darwish ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 8528ff69732SDave Jones "cpufreq"); 8530142f9dcSAhmed S. Darwish if (ret) { 854*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8550142f9dcSAhmed S. Darwish goto err_out_unregister; 8560142f9dcSAhmed S. Darwish } 8578ff69732SDave Jones } 8588ff69732SDave Jones 8591da177e4SLinus Torvalds policy->governor = NULL; /* to assure that the starting sequence is 8601da177e4SLinus Torvalds * run in cpufreq_set_policy */ 861*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* set default policy */ 8641da177e4SLinus Torvalds ret = cpufreq_set_policy(&new_policy); 8651da177e4SLinus Torvalds if (ret) { 8661da177e4SLinus Torvalds dprintk("setting policy failed\n"); 8671da177e4SLinus Torvalds goto err_out_unregister; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 8711da177e4SLinus Torvalds dprintk("initialization complete\n"); 8721da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds return 0; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds 8771da177e4SLinus Torvalds err_out_unregister: 8781da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 8791da177e4SLinus Torvalds for_each_cpu_mask(j, policy->cpus) 8801da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 8811da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds kobject_unregister(&policy->kobj); 8841da177e4SLinus Torvalds wait_for_completion(&policy->kobj_unregister); 8851da177e4SLinus Torvalds 8868085e1f1SVenkatesh Pallipadi err_out_driver_exit: 8878085e1f1SVenkatesh Pallipadi if (cpufreq_driver->exit) 8888085e1f1SVenkatesh Pallipadi cpufreq_driver->exit(policy); 8898085e1f1SVenkatesh Pallipadi 8901da177e4SLinus Torvalds err_out: 8911da177e4SLinus Torvalds kfree(policy); 8921da177e4SLinus Torvalds 8931da177e4SLinus Torvalds nomem_out: 8941da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 8951da177e4SLinus Torvalds module_out: 8961da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 8971da177e4SLinus Torvalds return ret; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /** 902*5a01f2e8SVenkatesh Pallipadi * __cpufreq_remove_dev - remove a CPU device 9031da177e4SLinus Torvalds * 9041da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 905*5a01f2e8SVenkatesh Pallipadi * Caller should already have policy_rwsem in write mode for this CPU. 906*5a01f2e8SVenkatesh Pallipadi * This routine frees the rwsem before returning. 9071da177e4SLinus Torvalds */ 908*5a01f2e8SVenkatesh Pallipadi static int __cpufreq_remove_dev (struct sys_device * sys_dev) 9091da177e4SLinus Torvalds { 9101da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 9111da177e4SLinus Torvalds unsigned long flags; 9121da177e4SLinus Torvalds struct cpufreq_policy *data; 9131da177e4SLinus Torvalds #ifdef CONFIG_SMP 914e738cf6dSGrant Coady struct sys_device *cpu_sys_dev; 9151da177e4SLinus Torvalds unsigned int j; 9161da177e4SLinus Torvalds #endif 9171da177e4SLinus Torvalds 9181da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 9191da177e4SLinus Torvalds dprintk("unregistering CPU %u\n", cpu); 9201da177e4SLinus Torvalds 9211da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 9221da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds if (!data) { 9251da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9261da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 927*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9281da177e4SLinus Torvalds return -EINVAL; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds cpufreq_cpu_data[cpu] = NULL; 9311da177e4SLinus Torvalds 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds #ifdef CONFIG_SMP 9341da177e4SLinus Torvalds /* if this isn't the CPU which is the parent of the kobj, we 9351da177e4SLinus Torvalds * only need to unlink, put and exit 9361da177e4SLinus Torvalds */ 9371da177e4SLinus Torvalds if (unlikely(cpu != data->cpu)) { 9381da177e4SLinus Torvalds dprintk("removing link\n"); 9398ff69732SDave Jones cpu_clear(cpu, data->cpus); 9401da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9411da177e4SLinus Torvalds sysfs_remove_link(&sys_dev->kobj, "cpufreq"); 9421da177e4SLinus Torvalds cpufreq_cpu_put(data); 9431da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 944*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9451da177e4SLinus Torvalds return 0; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds #endif 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) { 9511da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9521da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 953*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9541da177e4SLinus Torvalds return -EFAULT; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds #ifdef CONFIG_SMP 9581da177e4SLinus Torvalds /* if we have other CPUs still registered, we need to unlink them, 9591da177e4SLinus Torvalds * or else wait_for_completion below will lock up. Clean the 9601da177e4SLinus Torvalds * cpufreq_cpu_data[] while holding the lock, and remove the sysfs 9611da177e4SLinus Torvalds * links afterwards. 9621da177e4SLinus Torvalds */ 9631da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 9641da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 9651da177e4SLinus Torvalds if (j == cpu) 9661da177e4SLinus Torvalds continue; 9671da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 9741da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 9751da177e4SLinus Torvalds if (j == cpu) 9761da177e4SLinus Torvalds continue; 9771da177e4SLinus Torvalds dprintk("removing link for cpu %u\n", j); 978d434fca7SAshok Raj cpu_sys_dev = get_cpu_sysdev(j); 979d434fca7SAshok Raj sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); 9801da177e4SLinus Torvalds cpufreq_cpu_put(data); 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds } 9831da177e4SLinus Torvalds #else 9841da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9851da177e4SLinus Torvalds #endif 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (cpufreq_driver->target) 9881da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 989*5a01f2e8SVenkatesh Pallipadi 990*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds kobject_unregister(&data->kobj); 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds kobject_put(&data->kobj); 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds /* we need to make sure that the underlying kobj is actually 9971da177e4SLinus Torvalds * not referenced anymore by anybody before we proceed with 9981da177e4SLinus Torvalds * unloading. 9991da177e4SLinus Torvalds */ 10001da177e4SLinus Torvalds dprintk("waiting for dropping of refcount\n"); 10011da177e4SLinus Torvalds wait_for_completion(&data->kobj_unregister); 10021da177e4SLinus Torvalds dprintk("wait complete\n"); 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds if (cpufreq_driver->exit) 10051da177e4SLinus Torvalds cpufreq_driver->exit(data); 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds kfree(data); 10081da177e4SLinus Torvalds 10091da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10101da177e4SLinus Torvalds return 0; 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds 1014*5a01f2e8SVenkatesh Pallipadi static int cpufreq_remove_dev (struct sys_device * sys_dev) 1015*5a01f2e8SVenkatesh Pallipadi { 1016*5a01f2e8SVenkatesh Pallipadi unsigned int cpu = sys_dev->id; 1017*5a01f2e8SVenkatesh Pallipadi int retval; 1018*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 1019*5a01f2e8SVenkatesh Pallipadi BUG(); 1020*5a01f2e8SVenkatesh Pallipadi 1021*5a01f2e8SVenkatesh Pallipadi retval = __cpufreq_remove_dev(sys_dev); 1022*5a01f2e8SVenkatesh Pallipadi return retval; 1023*5a01f2e8SVenkatesh Pallipadi } 1024*5a01f2e8SVenkatesh Pallipadi 1025*5a01f2e8SVenkatesh Pallipadi 102665f27f38SDavid Howells static void handle_update(struct work_struct *work) 10271da177e4SLinus Torvalds { 102865f27f38SDavid Howells struct cpufreq_policy *policy = 102965f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 103065f27f38SDavid Howells unsigned int cpu = policy->cpu; 10311da177e4SLinus Torvalds dprintk("handle_update for cpu %u called\n", cpu); 10321da177e4SLinus Torvalds cpufreq_update_policy(cpu); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds /** 10361da177e4SLinus Torvalds * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 10371da177e4SLinus Torvalds * @cpu: cpu number 10381da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 10391da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 10401da177e4SLinus Torvalds * 10411da177e4SLinus Torvalds * We adjust to current frequency first, and need to clean up later. So either call 10421da177e4SLinus Torvalds * to cpufreq_update_policy() or schedule handle_update()). 10431da177e4SLinus Torvalds */ 1044e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1045e08f5f5bSGautham R Shenoy unsigned int new_freq) 10461da177e4SLinus Torvalds { 10471da177e4SLinus Torvalds struct cpufreq_freqs freqs; 10481da177e4SLinus Torvalds 1049b10eec22SJan Beulich dprintk("Warning: CPU frequency out of sync: cpufreq and timing " 10501da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 10511da177e4SLinus Torvalds 10521da177e4SLinus Torvalds freqs.cpu = cpu; 10531da177e4SLinus Torvalds freqs.old = old_freq; 10541da177e4SLinus Torvalds freqs.new = new_freq; 10551da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 10561da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /** 10614ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 106295235ca2SVenkatesh Pallipadi * @cpu: CPU number 106395235ca2SVenkatesh Pallipadi * 106495235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 106595235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 106695235ca2SVenkatesh Pallipadi */ 106795235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 106895235ca2SVenkatesh Pallipadi { 106995235ca2SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1070e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 107195235ca2SVenkatesh Pallipadi 107295235ca2SVenkatesh Pallipadi if (policy) { 1073*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 1074*5a01f2e8SVenkatesh Pallipadi return ret_freq; 1075*5a01f2e8SVenkatesh Pallipadi 1076e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 1077*5a01f2e8SVenkatesh Pallipadi 1078*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 107995235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 108095235ca2SVenkatesh Pallipadi } 108195235ca2SVenkatesh Pallipadi 1082e08f5f5bSGautham R Shenoy return (ret_freq); 108395235ca2SVenkatesh Pallipadi } 108495235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 108595235ca2SVenkatesh Pallipadi 108695235ca2SVenkatesh Pallipadi 1087*5a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 10881da177e4SLinus Torvalds { 1089*5a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_data[cpu]; 1090e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds if (!cpufreq_driver->get) 1093*5a01f2e8SVenkatesh Pallipadi return (ret_freq); 10941da177e4SLinus Torvalds 1095e08f5f5bSGautham R Shenoy ret_freq = cpufreq_driver->get(cpu); 10961da177e4SLinus Torvalds 1097e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 1098e08f5f5bSGautham R Shenoy !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1099e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1100e08f5f5bSGautham R Shenoy saved value exists */ 1101e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1102e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 11031da177e4SLinus Torvalds schedule_work(&policy->update); 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 1107*5a01f2e8SVenkatesh Pallipadi return (ret_freq); 1108*5a01f2e8SVenkatesh Pallipadi } 11091da177e4SLinus Torvalds 1110*5a01f2e8SVenkatesh Pallipadi /** 1111*5a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 1112*5a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 1113*5a01f2e8SVenkatesh Pallipadi * 1114*5a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 1115*5a01f2e8SVenkatesh Pallipadi */ 1116*5a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 1117*5a01f2e8SVenkatesh Pallipadi { 1118*5a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 1119*5a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1120*5a01f2e8SVenkatesh Pallipadi 1121*5a01f2e8SVenkatesh Pallipadi if (!policy) 1122*5a01f2e8SVenkatesh Pallipadi goto out; 1123*5a01f2e8SVenkatesh Pallipadi 1124*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 1125*5a01f2e8SVenkatesh Pallipadi goto out_policy; 1126*5a01f2e8SVenkatesh Pallipadi 1127*5a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 1128*5a01f2e8SVenkatesh Pallipadi 1129*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 1130*5a01f2e8SVenkatesh Pallipadi 1131*5a01f2e8SVenkatesh Pallipadi out_policy: 11321da177e4SLinus Torvalds cpufreq_cpu_put(policy); 1133*5a01f2e8SVenkatesh Pallipadi out: 1134e08f5f5bSGautham R Shenoy return (ret_freq); 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 11371da177e4SLinus Torvalds 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds /** 114042d4dc3fSBenjamin Herrenschmidt * cpufreq_suspend - let the low level driver prepare for suspend 114142d4dc3fSBenjamin Herrenschmidt */ 114242d4dc3fSBenjamin Herrenschmidt 1143e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg) 114442d4dc3fSBenjamin Herrenschmidt { 114542d4dc3fSBenjamin Herrenschmidt int cpu = sysdev->id; 1146e08f5f5bSGautham R Shenoy int ret = 0; 114742d4dc3fSBenjamin Herrenschmidt unsigned int cur_freq = 0; 114842d4dc3fSBenjamin Herrenschmidt struct cpufreq_policy *cpu_policy; 114942d4dc3fSBenjamin Herrenschmidt 11500e37b159SDave Jones dprintk("suspending cpu %u\n", cpu); 115142d4dc3fSBenjamin Herrenschmidt 115242d4dc3fSBenjamin Herrenschmidt if (!cpu_online(cpu)) 115342d4dc3fSBenjamin Herrenschmidt return 0; 115442d4dc3fSBenjamin Herrenschmidt 115542d4dc3fSBenjamin Herrenschmidt /* we may be lax here as interrupts are off. Nonetheless 115642d4dc3fSBenjamin Herrenschmidt * we need to grab the correct cpu policy, as to check 115742d4dc3fSBenjamin Herrenschmidt * whether we really run on this CPU. 115842d4dc3fSBenjamin Herrenschmidt */ 115942d4dc3fSBenjamin Herrenschmidt 116042d4dc3fSBenjamin Herrenschmidt cpu_policy = cpufreq_cpu_get(cpu); 116142d4dc3fSBenjamin Herrenschmidt if (!cpu_policy) 116242d4dc3fSBenjamin Herrenschmidt return -EINVAL; 116342d4dc3fSBenjamin Herrenschmidt 116442d4dc3fSBenjamin Herrenschmidt /* only handle each CPU group once */ 116542d4dc3fSBenjamin Herrenschmidt if (unlikely(cpu_policy->cpu != cpu)) { 116642d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 116742d4dc3fSBenjamin Herrenschmidt return 0; 116842d4dc3fSBenjamin Herrenschmidt } 116942d4dc3fSBenjamin Herrenschmidt 117042d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->suspend) { 1171e00d9967SBernard Blackham ret = cpufreq_driver->suspend(cpu_policy, pmsg); 117242d4dc3fSBenjamin Herrenschmidt if (ret) { 117342d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 117442d4dc3fSBenjamin Herrenschmidt "step on CPU %u\n", cpu_policy->cpu); 117542d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 117642d4dc3fSBenjamin Herrenschmidt return ret; 117742d4dc3fSBenjamin Herrenschmidt } 117842d4dc3fSBenjamin Herrenschmidt } 117942d4dc3fSBenjamin Herrenschmidt 118042d4dc3fSBenjamin Herrenschmidt 118142d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) 118242d4dc3fSBenjamin Herrenschmidt goto out; 118342d4dc3fSBenjamin Herrenschmidt 118442d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->get) 118542d4dc3fSBenjamin Herrenschmidt cur_freq = cpufreq_driver->get(cpu_policy->cpu); 118642d4dc3fSBenjamin Herrenschmidt 118742d4dc3fSBenjamin Herrenschmidt if (!cur_freq || !cpu_policy->cur) { 118842d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed to assert current " 118942d4dc3fSBenjamin Herrenschmidt "frequency is what timing core thinks it is.\n"); 119042d4dc3fSBenjamin Herrenschmidt goto out; 119142d4dc3fSBenjamin Herrenschmidt } 119242d4dc3fSBenjamin Herrenschmidt 119342d4dc3fSBenjamin Herrenschmidt if (unlikely(cur_freq != cpu_policy->cur)) { 119442d4dc3fSBenjamin Herrenschmidt struct cpufreq_freqs freqs; 119542d4dc3fSBenjamin Herrenschmidt 119642d4dc3fSBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1197b10eec22SJan Beulich dprintk("Warning: CPU frequency is %u, " 119842d4dc3fSBenjamin Herrenschmidt "cpufreq assumed %u kHz.\n", 119942d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 120042d4dc3fSBenjamin Herrenschmidt 120142d4dc3fSBenjamin Herrenschmidt freqs.cpu = cpu; 120242d4dc3fSBenjamin Herrenschmidt freqs.old = cpu_policy->cur; 120342d4dc3fSBenjamin Herrenschmidt freqs.new = cur_freq; 120442d4dc3fSBenjamin Herrenschmidt 1205b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 120642d4dc3fSBenjamin Herrenschmidt CPUFREQ_SUSPENDCHANGE, &freqs); 120742d4dc3fSBenjamin Herrenschmidt adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); 120842d4dc3fSBenjamin Herrenschmidt 120942d4dc3fSBenjamin Herrenschmidt cpu_policy->cur = cur_freq; 121042d4dc3fSBenjamin Herrenschmidt } 121142d4dc3fSBenjamin Herrenschmidt 121242d4dc3fSBenjamin Herrenschmidt out: 121342d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 121442d4dc3fSBenjamin Herrenschmidt return 0; 121542d4dc3fSBenjamin Herrenschmidt } 121642d4dc3fSBenjamin Herrenschmidt 121742d4dc3fSBenjamin Herrenschmidt /** 12181da177e4SLinus Torvalds * cpufreq_resume - restore proper CPU frequency handling after resume 12191da177e4SLinus Torvalds * 12201da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 12211da177e4SLinus Torvalds * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync 122242d4dc3fSBenjamin Herrenschmidt * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are 122342d4dc3fSBenjamin Herrenschmidt * restored. 12241da177e4SLinus Torvalds */ 12251da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device * sysdev) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds int cpu = sysdev->id; 1228e08f5f5bSGautham R Shenoy int ret = 0; 12291da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds dprintk("resuming cpu %u\n", cpu); 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds if (!cpu_online(cpu)) 12341da177e4SLinus Torvalds return 0; 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds /* we may be lax here as interrupts are off. Nonetheless 12371da177e4SLinus Torvalds * we need to grab the correct cpu policy, as to check 12381da177e4SLinus Torvalds * whether we really run on this CPU. 12391da177e4SLinus Torvalds */ 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 12421da177e4SLinus Torvalds if (!cpu_policy) 12431da177e4SLinus Torvalds return -EINVAL; 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds /* only handle each CPU group once */ 12461da177e4SLinus Torvalds if (unlikely(cpu_policy->cpu != cpu)) { 12471da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 12481da177e4SLinus Torvalds return 0; 12491da177e4SLinus Torvalds } 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds if (cpufreq_driver->resume) { 12521da177e4SLinus Torvalds ret = cpufreq_driver->resume(cpu_policy); 12531da177e4SLinus Torvalds if (ret) { 12541da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 12551da177e4SLinus Torvalds "step on CPU %u\n", cpu_policy->cpu); 12561da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 12571da177e4SLinus Torvalds return ret; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 12621da177e4SLinus Torvalds unsigned int cur_freq = 0; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds if (cpufreq_driver->get) 12651da177e4SLinus Torvalds cur_freq = cpufreq_driver->get(cpu_policy->cpu); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds if (!cur_freq || !cpu_policy->cur) { 126842d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: resume failed to assert " 126942d4dc3fSBenjamin Herrenschmidt "current frequency is what timing core " 127042d4dc3fSBenjamin Herrenschmidt "thinks it is.\n"); 12711da177e4SLinus Torvalds goto out; 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds if (unlikely(cur_freq != cpu_policy->cur)) { 12751da177e4SLinus Torvalds struct cpufreq_freqs freqs; 12761da177e4SLinus Torvalds 1277ac09f698SBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1278b10eec22SJan Beulich dprintk("Warning: CPU frequency" 1279ac09f698SBenjamin Herrenschmidt "is %u, cpufreq assumed %u kHz.\n", 128042d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds freqs.cpu = cpu; 12831da177e4SLinus Torvalds freqs.old = cpu_policy->cur; 12841da177e4SLinus Torvalds freqs.new = cur_freq; 12851da177e4SLinus Torvalds 1286b4dfdbb3SAlan Stern srcu_notifier_call_chain( 1287e041c683SAlan Stern &cpufreq_transition_notifier_list, 128842d4dc3fSBenjamin Herrenschmidt CPUFREQ_RESUMECHANGE, &freqs); 12891da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds cpu_policy->cur = cur_freq; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds 12951da177e4SLinus Torvalds out: 12961da177e4SLinus Torvalds schedule_work(&cpu_policy->update); 12971da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 12981da177e4SLinus Torvalds return ret; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = { 13021da177e4SLinus Torvalds .add = cpufreq_add_dev, 13031da177e4SLinus Torvalds .remove = cpufreq_remove_dev, 130442d4dc3fSBenjamin Herrenschmidt .suspend = cpufreq_suspend, 13051da177e4SLinus Torvalds .resume = cpufreq_resume, 13061da177e4SLinus Torvalds }; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds /********************************************************************* 13101da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 13111da177e4SLinus Torvalds *********************************************************************/ 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /** 13141da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 13151da177e4SLinus Torvalds * @nb: notifier function to register 13161da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 13171da177e4SLinus Torvalds * 13181da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 13191da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 13201da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 13211da177e4SLinus Torvalds * changes in cpufreq policy. 13221da177e4SLinus Torvalds * 13231da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1324e041c683SAlan Stern * blocking_notifier_chain_register. 13251da177e4SLinus Torvalds */ 13261da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 13271da177e4SLinus Torvalds { 13281da177e4SLinus Torvalds int ret; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds switch (list) { 13311da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1332b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1333e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 13341da177e4SLinus Torvalds break; 13351da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1336e041c683SAlan Stern ret = blocking_notifier_chain_register( 1337e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 13381da177e4SLinus Torvalds break; 13391da177e4SLinus Torvalds default: 13401da177e4SLinus Torvalds ret = -EINVAL; 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds return ret; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds /** 13491da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 13501da177e4SLinus Torvalds * @nb: notifier block to be unregistered 13511da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 13521da177e4SLinus Torvalds * 13531da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 13541da177e4SLinus Torvalds * 13551da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1356e041c683SAlan Stern * blocking_notifier_chain_unregister. 13571da177e4SLinus Torvalds */ 13581da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 13591da177e4SLinus Torvalds { 13601da177e4SLinus Torvalds int ret; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds switch (list) { 13631da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1364b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1365e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 13661da177e4SLinus Torvalds break; 13671da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1368e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1369e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 13701da177e4SLinus Torvalds break; 13711da177e4SLinus Torvalds default: 13721da177e4SLinus Torvalds ret = -EINVAL; 13731da177e4SLinus Torvalds } 13741da177e4SLinus Torvalds 13751da177e4SLinus Torvalds return ret; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds /********************************************************************* 13811da177e4SLinus Torvalds * GOVERNORS * 13821da177e4SLinus Torvalds *********************************************************************/ 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 13861da177e4SLinus Torvalds unsigned int target_freq, 13871da177e4SLinus Torvalds unsigned int relation) 13881da177e4SLinus Torvalds { 13891da177e4SLinus Torvalds int retval = -EINVAL; 1390c32b6b8eSAshok Raj 13911da177e4SLinus Torvalds dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 13921da177e4SLinus Torvalds target_freq, relation); 13931da177e4SLinus Torvalds if (cpu_online(policy->cpu) && cpufreq_driver->target) 13941da177e4SLinus Torvalds retval = cpufreq_driver->target(policy, target_freq, relation); 139590d45d17SAshok Raj 13961da177e4SLinus Torvalds return retval; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 14011da177e4SLinus Torvalds unsigned int target_freq, 14021da177e4SLinus Torvalds unsigned int relation) 14031da177e4SLinus Torvalds { 1404cc993cabSDave Jones int ret; 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 14071da177e4SLinus Torvalds if (!policy) 14081da177e4SLinus Torvalds return -EINVAL; 14091da177e4SLinus Torvalds 1410*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 1411*5a01f2e8SVenkatesh Pallipadi return -EINVAL; 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 14141da177e4SLinus Torvalds 1415*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds cpufreq_cpu_put(policy); 14181da177e4SLinus Torvalds return ret; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 14211da177e4SLinus Torvalds 1422*5a01f2e8SVenkatesh Pallipadi int __cpufreq_driver_getavg(struct cpufreq_policy *policy) 1423dfde5d62SVenkatesh Pallipadi { 1424dfde5d62SVenkatesh Pallipadi int ret = 0; 1425dfde5d62SVenkatesh Pallipadi 1426dfde5d62SVenkatesh Pallipadi policy = cpufreq_cpu_get(policy->cpu); 1427dfde5d62SVenkatesh Pallipadi if (!policy) 1428dfde5d62SVenkatesh Pallipadi return -EINVAL; 1429dfde5d62SVenkatesh Pallipadi 1430dfde5d62SVenkatesh Pallipadi if (cpu_online(policy->cpu) && cpufreq_driver->getavg) 1431dfde5d62SVenkatesh Pallipadi ret = cpufreq_driver->getavg(policy->cpu); 1432dfde5d62SVenkatesh Pallipadi 1433dfde5d62SVenkatesh Pallipadi cpufreq_cpu_put(policy); 1434dfde5d62SVenkatesh Pallipadi return ret; 1435dfde5d62SVenkatesh Pallipadi } 1436*5a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg); 1437dfde5d62SVenkatesh Pallipadi 1438153d7f3fSArjan van de Ven /* 1439153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1440153d7f3fSArjan van de Ven */ 14411da177e4SLinus Torvalds 1442e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1443e08f5f5bSGautham R Shenoy unsigned int event) 14441da177e4SLinus Torvalds { 1445cc993cabSDave Jones int ret; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 14481da177e4SLinus Torvalds return -EINVAL; 14491da177e4SLinus Torvalds 1450e08f5f5bSGautham R Shenoy dprintk("__cpufreq_governor for CPU %u, event %u\n", 1451e08f5f5bSGautham R Shenoy policy->cpu, event); 14521da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 14531da177e4SLinus Torvalds 1454e08f5f5bSGautham R Shenoy /* we keep one module reference alive for 1455e08f5f5bSGautham R Shenoy each CPU governed by this CPU */ 14561da177e4SLinus Torvalds if ((event != CPUFREQ_GOV_START) || ret) 14571da177e4SLinus Torvalds module_put(policy->governor->owner); 14581da177e4SLinus Torvalds if ((event == CPUFREQ_GOV_STOP) && !ret) 14591da177e4SLinus Torvalds module_put(policy->governor->owner); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds return ret; 14621da177e4SLinus Torvalds } 14631da177e4SLinus Torvalds 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 14661da177e4SLinus Torvalds { 14673bcb09a3SJeremy Fitzhardinge int err; 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds if (!governor) 14701da177e4SLinus Torvalds return -EINVAL; 14711da177e4SLinus Torvalds 14723fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 14731da177e4SLinus Torvalds 14743bcb09a3SJeremy Fitzhardinge err = -EBUSY; 14753bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 14763bcb09a3SJeremy Fitzhardinge err = 0; 14771da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 14783bcb09a3SJeremy Fitzhardinge } 14791da177e4SLinus Torvalds 14803fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 14813bcb09a3SJeremy Fitzhardinge return err; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 14871da177e4SLinus Torvalds { 14881da177e4SLinus Torvalds if (!governor) 14891da177e4SLinus Torvalds return; 14901da177e4SLinus Torvalds 14913fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 14921da177e4SLinus Torvalds list_del(&governor->governor_list); 14933fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 14941da177e4SLinus Torvalds return; 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 14971da177e4SLinus Torvalds 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds /********************************************************************* 15011da177e4SLinus Torvalds * POLICY INTERFACE * 15021da177e4SLinus Torvalds *********************************************************************/ 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds /** 15051da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 15061da177e4SLinus Torvalds * @policy: struct cpufreq_policy into which the current cpufreq_policy is written 15071da177e4SLinus Torvalds * 15081da177e4SLinus Torvalds * Reads the current cpufreq policy. 15091da177e4SLinus Torvalds */ 15101da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 15111da177e4SLinus Torvalds { 15121da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 15131da177e4SLinus Torvalds if (!policy) 15141da177e4SLinus Torvalds return -EINVAL; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 15171da177e4SLinus Torvalds if (!cpu_policy) 15181da177e4SLinus Torvalds return -EINVAL; 15191da177e4SLinus Torvalds 15201da177e4SLinus Torvalds memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 15211da177e4SLinus Torvalds 15221da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 15231da177e4SLinus Torvalds return 0; 15241da177e4SLinus Torvalds } 15251da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds 1528153d7f3fSArjan van de Ven /* 1529e08f5f5bSGautham R Shenoy * data : current policy. 1530e08f5f5bSGautham R Shenoy * policy : policy to be set. 1531153d7f3fSArjan van de Ven */ 1532e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 1533e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy) 15341da177e4SLinus Torvalds { 15351da177e4SLinus Torvalds int ret = 0; 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 15381da177e4SLinus Torvalds dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 15391da177e4SLinus Torvalds policy->min, policy->max); 15401da177e4SLinus Torvalds 1541e08f5f5bSGautham R Shenoy memcpy(&policy->cpuinfo, &data->cpuinfo, 1542e08f5f5bSGautham R Shenoy sizeof(struct cpufreq_cpuinfo)); 15431da177e4SLinus Torvalds 15449c9a43edSMattia Dongili if (policy->min > data->min && policy->min > policy->max) { 15459c9a43edSMattia Dongili ret = -EINVAL; 15469c9a43edSMattia Dongili goto error_out; 15479c9a43edSMattia Dongili } 15489c9a43edSMattia Dongili 15491da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 15501da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 15511da177e4SLinus Torvalds if (ret) 15521da177e4SLinus Torvalds goto error_out; 15531da177e4SLinus Torvalds 15541da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 1555e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1556e041c683SAlan Stern CPUFREQ_ADJUST, policy); 15571da177e4SLinus Torvalds 15581da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 1559e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1560e041c683SAlan Stern CPUFREQ_INCOMPATIBLE, policy); 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit, 15631da177e4SLinus Torvalds which might be different to the first one */ 15641da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 1565e041c683SAlan Stern if (ret) 15661da177e4SLinus Torvalds goto error_out; 15671da177e4SLinus Torvalds 15681da177e4SLinus Torvalds /* notification of the new policy */ 1569e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1570e041c683SAlan Stern CPUFREQ_NOTIFY, policy); 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds data->min = policy->min; 15731da177e4SLinus Torvalds data->max = policy->max; 15741da177e4SLinus Torvalds 1575e08f5f5bSGautham R Shenoy dprintk("new min and max freqs are %u - %u kHz\n", 1576e08f5f5bSGautham R Shenoy data->min, data->max); 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 15791da177e4SLinus Torvalds data->policy = policy->policy; 15801da177e4SLinus Torvalds dprintk("setting range\n"); 15811da177e4SLinus Torvalds ret = cpufreq_driver->setpolicy(policy); 15821da177e4SLinus Torvalds } else { 15831da177e4SLinus Torvalds if (policy->governor != data->governor) { 15841da177e4SLinus Torvalds /* save old, working values */ 15851da177e4SLinus Torvalds struct cpufreq_governor *old_gov = data->governor; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds dprintk("governor switch\n"); 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds /* end old governor */ 15901da177e4SLinus Torvalds if (data->governor) 15911da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds /* start new governor */ 15941da177e4SLinus Torvalds data->governor = policy->governor; 15951da177e4SLinus Torvalds if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 15961da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 1597e08f5f5bSGautham R Shenoy dprintk("starting governor %s failed\n", 1598e08f5f5bSGautham R Shenoy data->governor->name); 15991da177e4SLinus Torvalds if (old_gov) { 16001da177e4SLinus Torvalds data->governor = old_gov; 1601e08f5f5bSGautham R Shenoy __cpufreq_governor(data, 1602e08f5f5bSGautham R Shenoy CPUFREQ_GOV_START); 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds ret = -EINVAL; 16051da177e4SLinus Torvalds goto error_out; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 16081da177e4SLinus Torvalds } 16091da177e4SLinus Torvalds dprintk("governor: change or update limits\n"); 16101da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 16111da177e4SLinus Torvalds } 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds error_out: 16141da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 16151da177e4SLinus Torvalds return ret; 16161da177e4SLinus Torvalds } 16171da177e4SLinus Torvalds 16181da177e4SLinus Torvalds /** 16191da177e4SLinus Torvalds * cpufreq_set_policy - set a new CPUFreq policy 16201da177e4SLinus Torvalds * @policy: policy to be set. 16211da177e4SLinus Torvalds * 16221da177e4SLinus Torvalds * Sets a new CPU frequency and voltage scaling policy. 16231da177e4SLinus Torvalds */ 16241da177e4SLinus Torvalds int cpufreq_set_policy(struct cpufreq_policy *policy) 16251da177e4SLinus Torvalds { 16261da177e4SLinus Torvalds int ret = 0; 16271da177e4SLinus Torvalds struct cpufreq_policy *data; 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds if (!policy) 16301da177e4SLinus Torvalds return -EINVAL; 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds data = cpufreq_cpu_get(policy->cpu); 16331da177e4SLinus Torvalds if (!data) 16341da177e4SLinus Torvalds return -EINVAL; 16351da177e4SLinus Torvalds 1636*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 1637*5a01f2e8SVenkatesh Pallipadi return -EINVAL; 1638*5a01f2e8SVenkatesh Pallipadi 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, policy); 16411da177e4SLinus Torvalds data->user_policy.min = data->min; 16421da177e4SLinus Torvalds data->user_policy.max = data->max; 16431da177e4SLinus Torvalds data->user_policy.policy = data->policy; 16441da177e4SLinus Torvalds data->user_policy.governor = data->governor; 16451da177e4SLinus Torvalds 1646*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 1647153d7f3fSArjan van de Ven 16481da177e4SLinus Torvalds cpufreq_cpu_put(data); 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds return ret; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_set_policy); 16531da177e4SLinus Torvalds 16541da177e4SLinus Torvalds 16551da177e4SLinus Torvalds /** 16561da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 16571da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 16581da177e4SLinus Torvalds * 16591da177e4SLinus Torvalds * Usefull for policy notifiers which have different necessities 16601da177e4SLinus Torvalds * at different times. 16611da177e4SLinus Torvalds */ 16621da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 16631da177e4SLinus Torvalds { 16641da177e4SLinus Torvalds struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 16651da177e4SLinus Torvalds struct cpufreq_policy policy; 16661da177e4SLinus Torvalds int ret = 0; 16671da177e4SLinus Torvalds 16681da177e4SLinus Torvalds if (!data) 16691da177e4SLinus Torvalds return -ENODEV; 16701da177e4SLinus Torvalds 1671*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 1672*5a01f2e8SVenkatesh Pallipadi return -EINVAL; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds dprintk("updating policy for CPU %u\n", cpu); 16757d5e350fSDave Jones memcpy(&policy, data, sizeof(struct cpufreq_policy)); 16761da177e4SLinus Torvalds policy.min = data->user_policy.min; 16771da177e4SLinus Torvalds policy.max = data->user_policy.max; 16781da177e4SLinus Torvalds policy.policy = data->user_policy.policy; 16791da177e4SLinus Torvalds policy.governor = data->user_policy.governor; 16801da177e4SLinus Torvalds 16810961dd0dSThomas Renninger /* BIOS might change freq behind our back 16820961dd0dSThomas Renninger -> ask driver for current freq and notify governors about a change */ 16830961dd0dSThomas Renninger if (cpufreq_driver->get) { 16840961dd0dSThomas Renninger policy.cur = cpufreq_driver->get(cpu); 1685a85f7bd3SThomas Renninger if (!data->cur) { 1686a85f7bd3SThomas Renninger dprintk("Driver did not initialize current freq"); 1687a85f7bd3SThomas Renninger data->cur = policy.cur; 1688a85f7bd3SThomas Renninger } else { 16890961dd0dSThomas Renninger if (data->cur != policy.cur) 1690e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, data->cur, 1691e08f5f5bSGautham R Shenoy policy.cur); 16920961dd0dSThomas Renninger } 1693a85f7bd3SThomas Renninger } 16940961dd0dSThomas Renninger 16951da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, &policy); 16961da177e4SLinus Torvalds 1697*5a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 1698*5a01f2e8SVenkatesh Pallipadi 16991da177e4SLinus Torvalds cpufreq_cpu_put(data); 17001da177e4SLinus Torvalds return ret; 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 17031da177e4SLinus Torvalds 170465edc68cSChandra Seetharaman static int cpufreq_cpu_callback(struct notifier_block *nfb, 1705c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1706c32b6b8eSAshok Raj { 1707c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 1708c32b6b8eSAshok Raj struct sys_device *sys_dev; 1709*5a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy; 1710c32b6b8eSAshok Raj 1711c32b6b8eSAshok Raj sys_dev = get_cpu_sysdev(cpu); 1712c32b6b8eSAshok Raj if (sys_dev) { 1713c32b6b8eSAshok Raj switch (action) { 1714c32b6b8eSAshok Raj case CPU_ONLINE: 1715c32b6b8eSAshok Raj cpufreq_add_dev(sys_dev); 1716c32b6b8eSAshok Raj break; 1717c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 1718*5a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 1719*5a01f2e8SVenkatesh Pallipadi BUG(); 1720*5a01f2e8SVenkatesh Pallipadi 1721c32b6b8eSAshok Raj policy = cpufreq_cpu_data[cpu]; 1722c32b6b8eSAshok Raj if (policy) { 1723*5a01f2e8SVenkatesh Pallipadi __cpufreq_driver_target(policy, policy->min, 1724c32b6b8eSAshok Raj CPUFREQ_RELATION_H); 1725c32b6b8eSAshok Raj } 1726*5a01f2e8SVenkatesh Pallipadi __cpufreq_remove_dev(sys_dev); 1727c32b6b8eSAshok Raj break; 1728*5a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 1729*5a01f2e8SVenkatesh Pallipadi cpufreq_add_dev(sys_dev); 1730c32b6b8eSAshok Raj break; 1731c32b6b8eSAshok Raj } 1732c32b6b8eSAshok Raj } 1733c32b6b8eSAshok Raj return NOTIFY_OK; 1734c32b6b8eSAshok Raj } 1735c32b6b8eSAshok Raj 173674b85f37SChandra Seetharaman static struct notifier_block __cpuinitdata cpufreq_cpu_notifier = 1737c32b6b8eSAshok Raj { 1738c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 1739c32b6b8eSAshok Raj }; 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds /********************************************************************* 17421da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 17431da177e4SLinus Torvalds *********************************************************************/ 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /** 17461da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 17471da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 17481da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 17491da177e4SLinus Torvalds * 17501da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 17511da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 17521da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 17531da177e4SLinus Torvalds * 17541da177e4SLinus Torvalds */ 17551da177e4SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 17561da177e4SLinus Torvalds { 17571da177e4SLinus Torvalds unsigned long flags; 17581da177e4SLinus Torvalds int ret; 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 17611da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 17621da177e4SLinus Torvalds return -EINVAL; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds dprintk("trying to register driver %s\n", driver_data->name); 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds if (driver_data->setpolicy) 17671da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 17701da177e4SLinus Torvalds if (cpufreq_driver) { 17711da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 17721da177e4SLinus Torvalds return -EBUSY; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds cpufreq_driver = driver_data; 17751da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 17801da177e4SLinus Torvalds int i; 17811da177e4SLinus Torvalds ret = -ENODEV; 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /* check for at least one working CPU */ 17841da177e4SLinus Torvalds for (i=0; i<NR_CPUS; i++) 17851da177e4SLinus Torvalds if (cpufreq_cpu_data[i]) 17861da177e4SLinus Torvalds ret = 0; 17871da177e4SLinus Torvalds 17881da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 17891da177e4SLinus Torvalds if (ret) { 1790e08f5f5bSGautham R Shenoy dprintk("no CPU initialized for driver %s\n", 1791e08f5f5bSGautham R Shenoy driver_data->name); 1792e08f5f5bSGautham R Shenoy sysdev_driver_unregister(&cpu_sysdev_class, 1793e08f5f5bSGautham R Shenoy &cpufreq_sysdev_driver); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 17961da177e4SLinus Torvalds cpufreq_driver = NULL; 17971da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds if (!ret) { 180265edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 18031da177e4SLinus Torvalds dprintk("driver %s up and running\n", driver_data->name); 18041da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18051da177e4SLinus Torvalds } 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds return (ret); 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds /** 18131da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 18141da177e4SLinus Torvalds * 18151da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 18161da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 18171da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 18181da177e4SLinus Torvalds * currently not initialised. 18191da177e4SLinus Torvalds */ 18201da177e4SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 18211da177e4SLinus Torvalds { 18221da177e4SLinus Torvalds unsigned long flags; 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds if (!cpufreq_driver || (driver != cpufreq_driver)) { 18271da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18281da177e4SLinus Torvalds return -EINVAL; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds dprintk("unregistering driver %s\n", driver->name); 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 183465edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18371da177e4SLinus Torvalds cpufreq_driver = NULL; 18381da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds return 0; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 1843*5a01f2e8SVenkatesh Pallipadi 1844*5a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 1845*5a01f2e8SVenkatesh Pallipadi { 1846*5a01f2e8SVenkatesh Pallipadi int cpu; 1847*5a01f2e8SVenkatesh Pallipadi 1848*5a01f2e8SVenkatesh Pallipadi for_each_possible_cpu(cpu) { 1849*5a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = -1; 1850*5a01f2e8SVenkatesh Pallipadi init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 1851*5a01f2e8SVenkatesh Pallipadi } 1852*5a01f2e8SVenkatesh Pallipadi return 0; 1853*5a01f2e8SVenkatesh Pallipadi } 1854*5a01f2e8SVenkatesh Pallipadi 1855*5a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 1856