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]; 42084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 43084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */ 44084f3493SThomas Renninger static struct cpufreq_governor *cpufreq_cpu_governor[NR_CPUS]; 45084f3493SThomas Renninger #endif 461da177e4SLinus Torvalds static DEFINE_SPINLOCK(cpufreq_driver_lock); 471da177e4SLinus Torvalds 485a01f2e8SVenkatesh Pallipadi /* 495a01f2e8SVenkatesh Pallipadi * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure 505a01f2e8SVenkatesh Pallipadi * all cpufreq/hotplug/workqueue/etc related lock issues. 515a01f2e8SVenkatesh Pallipadi * 525a01f2e8SVenkatesh Pallipadi * The rules for this semaphore: 535a01f2e8SVenkatesh Pallipadi * - Any routine that wants to read from the policy structure will 545a01f2e8SVenkatesh Pallipadi * do a down_read on this semaphore. 555a01f2e8SVenkatesh Pallipadi * - Any routine that will write to the policy structure and/or may take away 565a01f2e8SVenkatesh Pallipadi * the policy altogether (eg. CPU hotplug), will hold this lock in write 575a01f2e8SVenkatesh Pallipadi * mode before doing so. 585a01f2e8SVenkatesh Pallipadi * 595a01f2e8SVenkatesh Pallipadi * Additional rules: 605a01f2e8SVenkatesh Pallipadi * - All holders of the lock should check to make sure that the CPU they 615a01f2e8SVenkatesh Pallipadi * are concerned with are online after they get the lock. 625a01f2e8SVenkatesh Pallipadi * - Governor routines that can be called in cpufreq hotplug path should not 635a01f2e8SVenkatesh Pallipadi * take this sem as top level hotplug notifier handler takes this. 645a01f2e8SVenkatesh Pallipadi */ 655a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(int, policy_cpu); 665a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem); 675a01f2e8SVenkatesh Pallipadi 685a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu) \ 695a01f2e8SVenkatesh Pallipadi int lock_policy_rwsem_##mode \ 705a01f2e8SVenkatesh Pallipadi (int cpu) \ 715a01f2e8SVenkatesh Pallipadi { \ 725a01f2e8SVenkatesh Pallipadi int policy_cpu = per_cpu(policy_cpu, cpu); \ 735a01f2e8SVenkatesh Pallipadi BUG_ON(policy_cpu == -1); \ 745a01f2e8SVenkatesh Pallipadi down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \ 755a01f2e8SVenkatesh Pallipadi if (unlikely(!cpu_online(cpu))) { \ 765a01f2e8SVenkatesh Pallipadi up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \ 775a01f2e8SVenkatesh Pallipadi return -1; \ 785a01f2e8SVenkatesh Pallipadi } \ 795a01f2e8SVenkatesh Pallipadi \ 805a01f2e8SVenkatesh Pallipadi return 0; \ 815a01f2e8SVenkatesh Pallipadi } 825a01f2e8SVenkatesh Pallipadi 835a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu); 845a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(lock_policy_rwsem_read); 855a01f2e8SVenkatesh Pallipadi 865a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu); 875a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(lock_policy_rwsem_write); 885a01f2e8SVenkatesh Pallipadi 895a01f2e8SVenkatesh Pallipadi void unlock_policy_rwsem_read(int cpu) 905a01f2e8SVenkatesh Pallipadi { 915a01f2e8SVenkatesh Pallipadi int policy_cpu = per_cpu(policy_cpu, cpu); 925a01f2e8SVenkatesh Pallipadi BUG_ON(policy_cpu == -1); 935a01f2e8SVenkatesh Pallipadi up_read(&per_cpu(cpu_policy_rwsem, policy_cpu)); 945a01f2e8SVenkatesh Pallipadi } 955a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read); 965a01f2e8SVenkatesh Pallipadi 975a01f2e8SVenkatesh Pallipadi void unlock_policy_rwsem_write(int cpu) 985a01f2e8SVenkatesh Pallipadi { 995a01f2e8SVenkatesh Pallipadi int policy_cpu = per_cpu(policy_cpu, cpu); 1005a01f2e8SVenkatesh Pallipadi BUG_ON(policy_cpu == -1); 1015a01f2e8SVenkatesh Pallipadi up_write(&per_cpu(cpu_policy_rwsem, policy_cpu)); 1025a01f2e8SVenkatesh Pallipadi } 1035a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write); 1045a01f2e8SVenkatesh Pallipadi 1055a01f2e8SVenkatesh Pallipadi 1061da177e4SLinus Torvalds /* internal prototypes */ 1071da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); 1085a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu); 10965f27f38SDavid Howells static void handle_update(struct work_struct *work); 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds /** 1121da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 1131da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 1141da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 1151da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 1161da177e4SLinus Torvalds * The mutex locks both lists. 1171da177e4SLinus Torvalds */ 118e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 119b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 1201da177e4SLinus Torvalds 121b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 122b4dfdbb3SAlan Stern { 123b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 124b4dfdbb3SAlan Stern return 0; 125b4dfdbb3SAlan Stern } 126b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 1293fc54d37Sakpm@osdl.org static DEFINE_MUTEX (cpufreq_governor_mutex); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 1321da177e4SLinus Torvalds { 1331da177e4SLinus Torvalds struct cpufreq_policy *data; 1341da177e4SLinus Torvalds unsigned long flags; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds if (cpu >= NR_CPUS) 1371da177e4SLinus Torvalds goto err_out; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds /* get the cpufreq driver */ 1401da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds if (!cpufreq_driver) 1431da177e4SLinus Torvalds goto err_out_unlock; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) 1461da177e4SLinus Torvalds goto err_out_unlock; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* get the CPU */ 1501da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds if (!data) 1531da177e4SLinus Torvalds goto err_out_put_module; 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) 1561da177e4SLinus Torvalds goto err_out_put_module; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1591da177e4SLinus Torvalds return data; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds err_out_put_module: 1621da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1631da177e4SLinus Torvalds err_out_unlock: 1641da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1651da177e4SLinus Torvalds err_out: 1661da177e4SLinus Torvalds return NULL; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 1691da177e4SLinus Torvalds 1707d5e350fSDave Jones 1711da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds kobject_put(&data->kobj); 1741da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /********************************************************************* 1801da177e4SLinus Torvalds * UNIFIED DEBUG HELPERS * 1811da177e4SLinus Torvalds *********************************************************************/ 1821da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */ 1851da177e4SLinus Torvalds static unsigned int debug; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can 1881da177e4SLinus Torvalds * set or modify this value. 1891da177e4SLinus Torvalds */ 1901da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful 1931da177e4SLinus Torvalds * loading of a cpufreq driver, temporarily disabled when a new policy 1941da177e4SLinus Torvalds * is set, and disabled upon cpufreq driver removal 1951da177e4SLinus Torvalds */ 1961da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1; 1971da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock); 1981da177e4SLinus Torvalds 199858119e1SArjan van de Ven static void cpufreq_debug_enable_ratelimit(void) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds unsigned long flags; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2041da177e4SLinus Torvalds if (disable_ratelimit) 2051da177e4SLinus Torvalds disable_ratelimit--; 2061da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 209858119e1SArjan van de Ven static void cpufreq_debug_disable_ratelimit(void) 2101da177e4SLinus Torvalds { 2111da177e4SLinus Torvalds unsigned long flags; 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2141da177e4SLinus Torvalds disable_ratelimit++; 2151da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 218e08f5f5bSGautham R Shenoy void cpufreq_debug_printk(unsigned int type, const char *prefix, 219e08f5f5bSGautham R Shenoy const char *fmt, ...) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds char s[256]; 2221da177e4SLinus Torvalds va_list args; 2231da177e4SLinus Torvalds unsigned int len; 2241da177e4SLinus Torvalds unsigned long flags; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds WARN_ON(!prefix); 2271da177e4SLinus Torvalds if (type & debug) { 2281da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 229e08f5f5bSGautham R Shenoy if (!disable_ratelimit && debug_ratelimit 230e08f5f5bSGautham R Shenoy && !printk_ratelimit()) { 2311da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2321da177e4SLinus Torvalds return; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds va_start(args, fmt); 2391da177e4SLinus Torvalds len += vsnprintf(&s[len], (256 - len), fmt, args); 2401da177e4SLinus Torvalds va_end(args); 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds printk(s); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds WARN_ON(len < 5); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds } 2471da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk); 2481da177e4SLinus Torvalds 2491da177e4SLinus Torvalds 2501da177e4SLinus Torvalds module_param(debug, uint, 0644); 251e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core," 252e08f5f5bSGautham R Shenoy " 2 to debug drivers, and 4 to debug governors."); 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644); 255e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:" 256e08f5f5bSGautham R Shenoy " set to 0 to disable ratelimiting."); 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */ 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; } 2611da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */ 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /********************************************************************* 2671da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2681da177e4SLinus Torvalds *********************************************************************/ 2691da177e4SLinus Torvalds 2701da177e4SLinus Torvalds /** 2711da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2721da177e4SLinus Torvalds * 2731da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2741da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2751da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2761da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2771da177e4SLinus Torvalds */ 2781da177e4SLinus Torvalds #ifndef CONFIG_SMP 2791da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2801da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2811da177e4SLinus Torvalds 282858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2831da177e4SLinus Torvalds { 2841da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2851da177e4SLinus Torvalds return; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2881da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2891da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 290e08f5f5bSGautham R Shenoy dprintk("saving %lu as reference value for loops_per_jiffy; " 291e08f5f5bSGautham R Shenoy "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 2921da177e4SLinus Torvalds } 2931da177e4SLinus Torvalds if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || 2941da177e4SLinus Torvalds (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || 29542d4dc3fSBenjamin Herrenschmidt (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 296e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 297e08f5f5bSGautham R Shenoy ci->new); 298e08f5f5bSGautham R Shenoy dprintk("scaling loops_per_jiffy to %lu " 299e08f5f5bSGautham R Shenoy "for frequency %u kHz\n", loops_per_jiffy, ci->new); 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds #else 303e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 304e08f5f5bSGautham R Shenoy { 305e08f5f5bSGautham R Shenoy return; 306e08f5f5bSGautham R Shenoy } 3071da177e4SLinus Torvalds #endif 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds 3101da177e4SLinus Torvalds /** 311e4472cb3SDave Jones * cpufreq_notify_transition - call notifier chain and adjust_jiffies 312e4472cb3SDave Jones * on frequency transition. 3131da177e4SLinus Torvalds * 314e4472cb3SDave Jones * This function calls the transition notifiers and the "adjust_jiffies" 315e4472cb3SDave Jones * function. It is called twice on all CPU frequency changes that have 316e4472cb3SDave Jones * external effects. 3171da177e4SLinus Torvalds */ 3181da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) 3191da177e4SLinus Torvalds { 320e4472cb3SDave Jones struct cpufreq_policy *policy; 321e4472cb3SDave Jones 3221da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds freqs->flags = cpufreq_driver->flags; 325e4472cb3SDave Jones dprintk("notification %u of frequency transition to %u kHz\n", 326e4472cb3SDave Jones state, freqs->new); 3271da177e4SLinus Torvalds 328e4472cb3SDave Jones policy = cpufreq_cpu_data[freqs->cpu]; 3291da177e4SLinus Torvalds switch (state) { 330e4472cb3SDave Jones 3311da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 332e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 333e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 334e4472cb3SDave Jones * "old frequency". 3351da177e4SLinus Torvalds */ 3361da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 337e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 338e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 339b10eec22SJan Beulich dprintk("Warning: CPU frequency is" 340e4472cb3SDave Jones " %u, cpufreq assumed %u kHz.\n", 341e4472cb3SDave Jones freqs->old, policy->cur); 342e4472cb3SDave Jones freqs->old = policy->cur; 3431da177e4SLinus Torvalds } 3441da177e4SLinus Torvalds } 345b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 346e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3471da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3481da177e4SLinus Torvalds break; 349e4472cb3SDave Jones 3501da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3511da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 352b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 353e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 354e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 355e4472cb3SDave Jones policy->cur = freqs->new; 3561da177e4SLinus Torvalds break; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds /********************************************************************* 3641da177e4SLinus Torvalds * SYSFS INTERFACE * 3651da177e4SLinus Torvalds *********************************************************************/ 3661da177e4SLinus Torvalds 3673bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 3683bcb09a3SJeremy Fitzhardinge { 3693bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 3703bcb09a3SJeremy Fitzhardinge 3713bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 3723bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) 3733bcb09a3SJeremy Fitzhardinge return t; 3743bcb09a3SJeremy Fitzhardinge 3753bcb09a3SJeremy Fitzhardinge return NULL; 3763bcb09a3SJeremy Fitzhardinge } 3773bcb09a3SJeremy Fitzhardinge 3781da177e4SLinus Torvalds /** 3791da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 3801da177e4SLinus Torvalds */ 3811da177e4SLinus Torvalds static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, 3821da177e4SLinus Torvalds struct cpufreq_governor **governor) 3831da177e4SLinus Torvalds { 3843bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 3853bcb09a3SJeremy Fitzhardinge 3861da177e4SLinus Torvalds if (!cpufreq_driver) 3873bcb09a3SJeremy Fitzhardinge goto out; 3883bcb09a3SJeremy Fitzhardinge 3891da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 3901da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 3911da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 3923bcb09a3SJeremy Fitzhardinge err = 0; 393e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 394e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 3951da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 3963bcb09a3SJeremy Fitzhardinge err = 0; 3971da177e4SLinus Torvalds } 3983bcb09a3SJeremy Fitzhardinge } else if (cpufreq_driver->target) { 3991da177e4SLinus Torvalds struct cpufreq_governor *t; 4003bcb09a3SJeremy Fitzhardinge 4013fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4023bcb09a3SJeremy Fitzhardinge 4033bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 4043bcb09a3SJeremy Fitzhardinge 405ea714970SJeremy Fitzhardinge if (t == NULL) { 406e08f5f5bSGautham R Shenoy char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", 407e08f5f5bSGautham R Shenoy str_governor); 408ea714970SJeremy Fitzhardinge 409ea714970SJeremy Fitzhardinge if (name) { 410ea714970SJeremy Fitzhardinge int ret; 411ea714970SJeremy Fitzhardinge 412ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 413ea714970SJeremy Fitzhardinge ret = request_module(name); 414ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 415ea714970SJeremy Fitzhardinge 416ea714970SJeremy Fitzhardinge if (ret == 0) 417ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 418ea714970SJeremy Fitzhardinge } 419ea714970SJeremy Fitzhardinge 420ea714970SJeremy Fitzhardinge kfree(name); 421ea714970SJeremy Fitzhardinge } 422ea714970SJeremy Fitzhardinge 4233bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4241da177e4SLinus Torvalds *governor = t; 4253bcb09a3SJeremy Fitzhardinge err = 0; 4261da177e4SLinus Torvalds } 4273bcb09a3SJeremy Fitzhardinge 4283bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds out: 4313bcb09a3SJeremy Fitzhardinge return err; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds /* drivers/base/cpu.c */ 4361da177e4SLinus Torvalds extern struct sysdev_class cpu_sysdev_class; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /** 440e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 441e08f5f5bSGautham R Shenoy * print out cpufreq information 4421da177e4SLinus Torvalds * 4431da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 4441da177e4SLinus Torvalds * "unsigned int". 4451da177e4SLinus Torvalds */ 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds #define show_one(file_name, object) \ 4481da177e4SLinus Torvalds static ssize_t show_##file_name \ 4491da177e4SLinus Torvalds (struct cpufreq_policy * policy, char *buf) \ 4501da177e4SLinus Torvalds { \ 4511da177e4SLinus Torvalds return sprintf (buf, "%u\n", policy->object); \ 4521da177e4SLinus Torvalds } 4531da177e4SLinus Torvalds 4541da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 4551da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 4561da177e4SLinus Torvalds show_one(scaling_min_freq, min); 4571da177e4SLinus Torvalds show_one(scaling_max_freq, max); 4581da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 4591da177e4SLinus Torvalds 460e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 461e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy); 4627970e08bSThomas Renninger 4631da177e4SLinus Torvalds /** 4641da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds #define store_one(file_name, object) \ 4671da177e4SLinus Torvalds static ssize_t store_##file_name \ 4681da177e4SLinus Torvalds (struct cpufreq_policy * policy, const char *buf, size_t count) \ 4691da177e4SLinus Torvalds { \ 4701da177e4SLinus Torvalds unsigned int ret = -EINVAL; \ 4711da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 4721da177e4SLinus Torvalds \ 4731da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 4741da177e4SLinus Torvalds if (ret) \ 4751da177e4SLinus Torvalds return -EINVAL; \ 4761da177e4SLinus Torvalds \ 4771da177e4SLinus Torvalds ret = sscanf (buf, "%u", &new_policy.object); \ 4781da177e4SLinus Torvalds if (ret != 1) \ 4791da177e4SLinus Torvalds return -EINVAL; \ 4801da177e4SLinus Torvalds \ 4817970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); \ 4827970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 4831da177e4SLinus Torvalds \ 4841da177e4SLinus Torvalds return ret ? ret : count; \ 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds store_one(scaling_min_freq,min); 4881da177e4SLinus Torvalds store_one(scaling_max_freq,max); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /** 4911da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 4921da177e4SLinus Torvalds */ 493e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, 494e08f5f5bSGautham R Shenoy char *buf) 4951da177e4SLinus Torvalds { 4965a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 4971da177e4SLinus Torvalds if (!cur_freq) 4981da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 4991da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /** 5041da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5051da177e4SLinus Torvalds */ 506e08f5f5bSGautham R Shenoy static ssize_t show_scaling_governor (struct cpufreq_policy * policy, 507e08f5f5bSGautham R Shenoy char *buf) 5081da177e4SLinus Torvalds { 5091da177e4SLinus Torvalds if(policy->policy == CPUFREQ_POLICY_POWERSAVE) 5101da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5111da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5121da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5131da177e4SLinus Torvalds else if (policy->governor) 5141da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name); 5151da177e4SLinus Torvalds return -EINVAL; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /** 5201da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5211da177e4SLinus Torvalds */ 5221da177e4SLinus Torvalds static ssize_t store_scaling_governor (struct cpufreq_policy * policy, 5231da177e4SLinus Torvalds const char *buf, size_t count) 5241da177e4SLinus Torvalds { 5251da177e4SLinus Torvalds unsigned int ret = -EINVAL; 5261da177e4SLinus Torvalds char str_governor[16]; 5271da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5301da177e4SLinus Torvalds if (ret) 5311da177e4SLinus Torvalds return ret; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds ret = sscanf (buf, "%15s", str_governor); 5341da177e4SLinus Torvalds if (ret != 1) 5351da177e4SLinus Torvalds return -EINVAL; 5361da177e4SLinus Torvalds 537e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 538e08f5f5bSGautham R Shenoy &new_policy.governor)) 5391da177e4SLinus Torvalds return -EINVAL; 5401da177e4SLinus Torvalds 5417970e08bSThomas Renninger /* Do not use cpufreq_set_policy here or the user_policy.max 5427970e08bSThomas Renninger will be wrongly overridden */ 5437970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 5447970e08bSThomas Renninger 5457970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 5467970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 5477970e08bSThomas Renninger 548e08f5f5bSGautham R Shenoy if (ret) 549e08f5f5bSGautham R Shenoy return ret; 550e08f5f5bSGautham R Shenoy else 551e08f5f5bSGautham R Shenoy return count; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /** 5551da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 5561da177e4SLinus Torvalds */ 5571da177e4SLinus Torvalds static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /** 5631da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy, 5661da177e4SLinus Torvalds char *buf) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds ssize_t i = 0; 5691da177e4SLinus Torvalds struct cpufreq_governor *t; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds if (!cpufreq_driver->target) { 5721da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 5731da177e4SLinus Torvalds goto out; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 5771da177e4SLinus Torvalds if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) 5781da177e4SLinus Torvalds goto out; 5791da177e4SLinus Torvalds i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds out: 5821da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 5831da177e4SLinus Torvalds return i; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds /** 5861da177e4SLinus Torvalds * show_affected_cpus - show the CPUs affected by each transition 5871da177e4SLinus Torvalds */ 5881da177e4SLinus Torvalds static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf) 5891da177e4SLinus Torvalds { 5901da177e4SLinus Torvalds ssize_t i = 0; 5911da177e4SLinus Torvalds unsigned int cpu; 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds for_each_cpu_mask(cpu, policy->cpus) { 5941da177e4SLinus Torvalds if (i) 5951da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 5961da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 5971da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 5981da177e4SLinus Torvalds break; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6011da177e4SLinus Torvalds return i; 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds 6049e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 6059e76988eSVenki Pallipadi const char *buf, size_t count) 6069e76988eSVenki Pallipadi { 6079e76988eSVenki Pallipadi unsigned int freq = 0; 6089e76988eSVenki Pallipadi unsigned int ret; 6099e76988eSVenki Pallipadi 6109e76988eSVenki Pallipadi if (!policy->governor->store_setspeed) 6119e76988eSVenki Pallipadi return -EINVAL; 6129e76988eSVenki Pallipadi 6139e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 6149e76988eSVenki Pallipadi if (ret != 1) 6159e76988eSVenki Pallipadi return -EINVAL; 6169e76988eSVenki Pallipadi 6179e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 6189e76988eSVenki Pallipadi 6199e76988eSVenki Pallipadi return count; 6209e76988eSVenki Pallipadi } 6219e76988eSVenki Pallipadi 6229e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 6239e76988eSVenki Pallipadi { 6249e76988eSVenki Pallipadi if (!policy->governor->show_setspeed) 6259e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 6269e76988eSVenki Pallipadi 6279e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 6289e76988eSVenki Pallipadi } 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds #define define_one_ro(_name) \ 6311da177e4SLinus Torvalds static struct freq_attr _name = \ 6321da177e4SLinus Torvalds __ATTR(_name, 0444, show_##_name, NULL) 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds #define define_one_ro0400(_name) \ 6351da177e4SLinus Torvalds static struct freq_attr _name = \ 6361da177e4SLinus Torvalds __ATTR(_name, 0400, show_##_name, NULL) 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds #define define_one_rw(_name) \ 6391da177e4SLinus Torvalds static struct freq_attr _name = \ 6401da177e4SLinus Torvalds __ATTR(_name, 0644, show_##_name, store_##_name) 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds define_one_ro0400(cpuinfo_cur_freq); 6431da177e4SLinus Torvalds define_one_ro(cpuinfo_min_freq); 6441da177e4SLinus Torvalds define_one_ro(cpuinfo_max_freq); 6451da177e4SLinus Torvalds define_one_ro(scaling_available_governors); 6461da177e4SLinus Torvalds define_one_ro(scaling_driver); 6471da177e4SLinus Torvalds define_one_ro(scaling_cur_freq); 6481da177e4SLinus Torvalds define_one_ro(affected_cpus); 6491da177e4SLinus Torvalds define_one_rw(scaling_min_freq); 6501da177e4SLinus Torvalds define_one_rw(scaling_max_freq); 6511da177e4SLinus Torvalds define_one_rw(scaling_governor); 6529e76988eSVenki Pallipadi define_one_rw(scaling_setspeed); 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds static struct attribute * default_attrs[] = { 6551da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 6561da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 6571da177e4SLinus Torvalds &scaling_min_freq.attr, 6581da177e4SLinus Torvalds &scaling_max_freq.attr, 6591da177e4SLinus Torvalds &affected_cpus.attr, 6601da177e4SLinus Torvalds &scaling_governor.attr, 6611da177e4SLinus Torvalds &scaling_driver.attr, 6621da177e4SLinus Torvalds &scaling_available_governors.attr, 6639e76988eSVenki Pallipadi &scaling_setspeed.attr, 6641da177e4SLinus Torvalds NULL 6651da177e4SLinus Torvalds }; 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds #define to_policy(k) container_of(k,struct cpufreq_policy,kobj) 6681da177e4SLinus Torvalds #define to_attr(a) container_of(a,struct freq_attr,attr) 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf) 6711da177e4SLinus Torvalds { 6721da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 6731da177e4SLinus Torvalds struct freq_attr * fattr = to_attr(attr); 6740db4a8a9SDave Jones ssize_t ret = -EINVAL; 6751da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 6761da177e4SLinus Torvalds if (!policy) 6770db4a8a9SDave Jones goto no_policy; 6785a01f2e8SVenkatesh Pallipadi 6795a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_read(policy->cpu) < 0) 6800db4a8a9SDave Jones goto fail; 6815a01f2e8SVenkatesh Pallipadi 682e08f5f5bSGautham R Shenoy if (fattr->show) 683e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 684e08f5f5bSGautham R Shenoy else 685e08f5f5bSGautham R Shenoy ret = -EIO; 686e08f5f5bSGautham R Shenoy 6875a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(policy->cpu); 6880db4a8a9SDave Jones fail: 6891da177e4SLinus Torvalds cpufreq_cpu_put(policy); 6900db4a8a9SDave Jones no_policy: 6911da177e4SLinus Torvalds return ret; 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds static ssize_t store(struct kobject * kobj, struct attribute * attr, 6951da177e4SLinus Torvalds const char * buf, size_t count) 6961da177e4SLinus Torvalds { 6971da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 6981da177e4SLinus Torvalds struct freq_attr * fattr = to_attr(attr); 699a07530b4SDave Jones ssize_t ret = -EINVAL; 7001da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 7011da177e4SLinus Torvalds if (!policy) 702a07530b4SDave Jones goto no_policy; 7035a01f2e8SVenkatesh Pallipadi 7045a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(policy->cpu) < 0) 705a07530b4SDave Jones goto fail; 7065a01f2e8SVenkatesh Pallipadi 707e08f5f5bSGautham R Shenoy if (fattr->store) 708e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 709e08f5f5bSGautham R Shenoy else 710e08f5f5bSGautham R Shenoy ret = -EIO; 711e08f5f5bSGautham R Shenoy 7125a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 713a07530b4SDave Jones fail: 7141da177e4SLinus Torvalds cpufreq_cpu_put(policy); 715a07530b4SDave Jones no_policy: 7161da177e4SLinus Torvalds return ret; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds 7191da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject * kobj) 7201da177e4SLinus Torvalds { 7211da177e4SLinus Torvalds struct cpufreq_policy * policy = to_policy(kobj); 7221da177e4SLinus Torvalds dprintk("last reference is dropped\n"); 7231da177e4SLinus Torvalds complete(&policy->kobj_unregister); 7241da177e4SLinus Torvalds } 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = { 7271da177e4SLinus Torvalds .show = show, 7281da177e4SLinus Torvalds .store = store, 7291da177e4SLinus Torvalds }; 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 7321da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 7331da177e4SLinus Torvalds .default_attrs = default_attrs, 7341da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 7351da177e4SLinus Torvalds }; 7361da177e4SLinus Torvalds 7371da177e4SLinus Torvalds 7381da177e4SLinus Torvalds /** 7391da177e4SLinus Torvalds * cpufreq_add_dev - add a CPU device 7401da177e4SLinus Torvalds * 7411da177e4SLinus Torvalds * Adds the cpufreq interface for a CPU device. 7421da177e4SLinus Torvalds */ 7431da177e4SLinus Torvalds static int cpufreq_add_dev (struct sys_device * sys_dev) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 7461da177e4SLinus Torvalds int ret = 0; 7471da177e4SLinus Torvalds struct cpufreq_policy new_policy; 7481da177e4SLinus Torvalds struct cpufreq_policy *policy; 7491da177e4SLinus Torvalds struct freq_attr **drv_attr; 7508ff69732SDave Jones struct sys_device *cpu_sys_dev; 7511da177e4SLinus Torvalds unsigned long flags; 7521da177e4SLinus Torvalds unsigned int j; 7538ff69732SDave Jones #ifdef CONFIG_SMP 7548ff69732SDave Jones struct cpufreq_policy *managed_policy; 7558ff69732SDave Jones #endif 7561da177e4SLinus Torvalds 757c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 758c32b6b8eSAshok Raj return 0; 759c32b6b8eSAshok Raj 7601da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 7611da177e4SLinus Torvalds dprintk("adding CPU %u\n", cpu); 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds #ifdef CONFIG_SMP 7641da177e4SLinus Torvalds /* check whether a different CPU already registered this 7651da177e4SLinus Torvalds * CPU because it is in the same boat. */ 7661da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 7671da177e4SLinus Torvalds if (unlikely(policy)) { 7688ff69732SDave Jones cpufreq_cpu_put(policy); 7691da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7701da177e4SLinus Torvalds return 0; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds #endif 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) { 7751da177e4SLinus Torvalds ret = -EINVAL; 7761da177e4SLinus Torvalds goto module_out; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 779e98df50cSDave Jones policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 7801da177e4SLinus Torvalds if (!policy) { 7811da177e4SLinus Torvalds ret = -ENOMEM; 7821da177e4SLinus Torvalds goto nomem_out; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds policy->cpu = cpu; 7861da177e4SLinus Torvalds policy->cpus = cpumask_of_cpu(cpu); 7871da177e4SLinus Torvalds 7885a01f2e8SVenkatesh Pallipadi /* Initially set CPU itself as the policy_cpu */ 7895a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = cpu; 7905a01f2e8SVenkatesh Pallipadi lock_policy_rwsem_write(cpu); 7915a01f2e8SVenkatesh Pallipadi 7921da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 79365f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 7941da177e4SLinus Torvalds 7958122c6ceSThomas Renninger /* Set governor before ->init, so that driver could check it */ 7968122c6ceSThomas Renninger policy->governor = CPUFREQ_DEFAULT_GOVERNOR; 7971da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 7981da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 7991da177e4SLinus Torvalds */ 8001da177e4SLinus Torvalds ret = cpufreq_driver->init(policy); 8011da177e4SLinus Torvalds if (ret) { 8021da177e4SLinus Torvalds dprintk("initialization failed\n"); 8035a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8041da177e4SLinus Torvalds goto err_out; 8051da177e4SLinus Torvalds } 80622c970f3SThomas Renninger policy->user_policy.min = policy->cpuinfo.min_freq; 80722c970f3SThomas Renninger policy->user_policy.max = policy->cpuinfo.max_freq; 8081da177e4SLinus Torvalds 8098ff69732SDave Jones #ifdef CONFIG_SMP 810084f3493SThomas Renninger 811084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 812084f3493SThomas Renninger if (cpufreq_cpu_governor[cpu]){ 813084f3493SThomas Renninger policy->governor = cpufreq_cpu_governor[cpu]; 814084f3493SThomas Renninger dprintk("Restoring governor %s for cpu %d\n", 815084f3493SThomas Renninger policy->governor->name, cpu); 816084f3493SThomas Renninger } 817084f3493SThomas Renninger #endif 818084f3493SThomas Renninger 8198ff69732SDave Jones for_each_cpu_mask(j, policy->cpus) { 8208ff69732SDave Jones if (cpu == j) 8218ff69732SDave Jones continue; 8228ff69732SDave Jones 8238ff69732SDave Jones /* check for existing affected CPUs. They may not be aware 8248ff69732SDave Jones * of it due to CPU Hotplug. 8258ff69732SDave Jones */ 8268ff69732SDave Jones managed_policy = cpufreq_cpu_get(j); 8278ff69732SDave Jones if (unlikely(managed_policy)) { 8285a01f2e8SVenkatesh Pallipadi 8295a01f2e8SVenkatesh Pallipadi /* Set proper policy_cpu */ 8305a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8315a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = managed_policy->cpu; 8325a01f2e8SVenkatesh Pallipadi 8335a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(cpu) < 0) 8345a01f2e8SVenkatesh Pallipadi goto err_out_driver_exit; 8355a01f2e8SVenkatesh Pallipadi 8368ff69732SDave Jones spin_lock_irqsave(&cpufreq_driver_lock, flags); 8378ff69732SDave Jones managed_policy->cpus = policy->cpus; 8388ff69732SDave Jones cpufreq_cpu_data[cpu] = managed_policy; 8398ff69732SDave Jones spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 8408ff69732SDave Jones 8418ff69732SDave Jones dprintk("CPU already managed, adding link\n"); 8420142f9dcSAhmed S. Darwish ret = sysfs_create_link(&sys_dev->kobj, 8430142f9dcSAhmed S. Darwish &managed_policy->kobj, 8440142f9dcSAhmed S. Darwish "cpufreq"); 8450142f9dcSAhmed S. Darwish if (ret) { 8465a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8470142f9dcSAhmed S. Darwish goto err_out_driver_exit; 8480142f9dcSAhmed S. Darwish } 8498ff69732SDave Jones 8508ff69732SDave Jones cpufreq_debug_enable_ratelimit(); 8518ff69732SDave Jones ret = 0; 8525a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8538ff69732SDave Jones goto err_out_driver_exit; /* call driver->exit() */ 8548ff69732SDave Jones } 8558ff69732SDave Jones } 8568ff69732SDave Jones #endif 8571da177e4SLinus Torvalds memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* prepare interface data */ 860038c5b3eSGreg Kroah-Hartman ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj, 861038c5b3eSGreg Kroah-Hartman "cpufreq"); 862f3876c1bSAndrew Morton if (ret) { 8635a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8648085e1f1SVenkatesh Pallipadi goto err_out_driver_exit; 865f3876c1bSAndrew Morton } 8661da177e4SLinus Torvalds /* set up files for this cpu device */ 8671da177e4SLinus Torvalds drv_attr = cpufreq_driver->attr; 8681da177e4SLinus Torvalds while ((drv_attr) && (*drv_attr)) { 86958a7295bSTobias Klauser ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 870bd6cba53SDave Jones if (ret) { 871bd6cba53SDave Jones unlock_policy_rwsem_write(cpu); 8720a4b2cccSThomas Renninger goto err_out_driver_exit; 873bd6cba53SDave Jones } 8741da177e4SLinus Torvalds drv_attr++; 8751da177e4SLinus Torvalds } 8760a4b2cccSThomas Renninger if (cpufreq_driver->get){ 87758a7295bSTobias Klauser ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 878bd6cba53SDave Jones if (ret) { 879bd6cba53SDave Jones unlock_policy_rwsem_write(cpu); 8800a4b2cccSThomas Renninger goto err_out_driver_exit; 8810a4b2cccSThomas Renninger } 882bd6cba53SDave Jones } 8830a4b2cccSThomas Renninger if (cpufreq_driver->target){ 88458a7295bSTobias Klauser ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 885bd6cba53SDave Jones if (ret) { 886bd6cba53SDave Jones unlock_policy_rwsem_write(cpu); 8870a4b2cccSThomas Renninger goto err_out_driver_exit; 8880a4b2cccSThomas Renninger } 889bd6cba53SDave Jones } 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 8925a01f2e8SVenkatesh Pallipadi for_each_cpu_mask(j, policy->cpus) { 8931da177e4SLinus Torvalds cpufreq_cpu_data[j] = policy; 8945a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, j) = policy->cpu; 8955a01f2e8SVenkatesh Pallipadi } 8961da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 8978ff69732SDave Jones 8988ff69732SDave Jones /* symlink affected CPUs */ 8998ff69732SDave Jones for_each_cpu_mask(j, policy->cpus) { 9008ff69732SDave Jones if (j == cpu) 9018ff69732SDave Jones continue; 9028ff69732SDave Jones if (!cpu_online(j)) 9038ff69732SDave Jones continue; 9048ff69732SDave Jones 9051f8b2c9dSDave Jones dprintk("CPU %u already managed, adding link\n", j); 9068ff69732SDave Jones cpufreq_cpu_get(cpu); 9078ff69732SDave Jones cpu_sys_dev = get_cpu_sysdev(j); 9080142f9dcSAhmed S. Darwish ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 9098ff69732SDave Jones "cpufreq"); 9100142f9dcSAhmed S. Darwish if (ret) { 9115a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9120142f9dcSAhmed S. Darwish goto err_out_unregister; 9130142f9dcSAhmed S. Darwish } 9148ff69732SDave Jones } 9158ff69732SDave Jones 9161da177e4SLinus Torvalds policy->governor = NULL; /* to assure that the starting sequence is 9171da177e4SLinus Torvalds * run in cpufreq_set_policy */ 9181da177e4SLinus Torvalds 9191da177e4SLinus Torvalds /* set default policy */ 92022c970f3SThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 92122c970f3SThomas Renninger policy->user_policy.policy = policy->policy; 922084f3493SThomas Renninger policy->user_policy.governor = policy->governor; 92322c970f3SThomas Renninger 92422c970f3SThomas Renninger unlock_policy_rwsem_write(cpu); 92522c970f3SThomas Renninger 9261da177e4SLinus Torvalds if (ret) { 9271da177e4SLinus Torvalds dprintk("setting policy failed\n"); 9281da177e4SLinus Torvalds goto err_out_unregister; 9291da177e4SLinus Torvalds } 9301da177e4SLinus Torvalds 931038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 9321da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9331da177e4SLinus Torvalds dprintk("initialization complete\n"); 9341da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9351da177e4SLinus Torvalds 9361da177e4SLinus Torvalds return 0; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds err_out_unregister: 9401da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 9411da177e4SLinus Torvalds for_each_cpu_mask(j, policy->cpus) 9421da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 9431da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9441da177e4SLinus Torvalds 945c10997f6SGreg Kroah-Hartman kobject_put(&policy->kobj); 9461da177e4SLinus Torvalds wait_for_completion(&policy->kobj_unregister); 9471da177e4SLinus Torvalds 9488085e1f1SVenkatesh Pallipadi err_out_driver_exit: 9498085e1f1SVenkatesh Pallipadi if (cpufreq_driver->exit) 9508085e1f1SVenkatesh Pallipadi cpufreq_driver->exit(policy); 9518085e1f1SVenkatesh Pallipadi 9521da177e4SLinus Torvalds err_out: 9531da177e4SLinus Torvalds kfree(policy); 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds nomem_out: 9561da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9571da177e4SLinus Torvalds module_out: 9581da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9591da177e4SLinus Torvalds return ret; 9601da177e4SLinus Torvalds } 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds /** 9645a01f2e8SVenkatesh Pallipadi * __cpufreq_remove_dev - remove a CPU device 9651da177e4SLinus Torvalds * 9661da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 9675a01f2e8SVenkatesh Pallipadi * Caller should already have policy_rwsem in write mode for this CPU. 9685a01f2e8SVenkatesh Pallipadi * This routine frees the rwsem before returning. 9691da177e4SLinus Torvalds */ 9705a01f2e8SVenkatesh Pallipadi static int __cpufreq_remove_dev (struct sys_device * sys_dev) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 9731da177e4SLinus Torvalds unsigned long flags; 9741da177e4SLinus Torvalds struct cpufreq_policy *data; 9751da177e4SLinus Torvalds #ifdef CONFIG_SMP 976e738cf6dSGrant Coady struct sys_device *cpu_sys_dev; 9771da177e4SLinus Torvalds unsigned int j; 9781da177e4SLinus Torvalds #endif 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 9811da177e4SLinus Torvalds dprintk("unregistering CPU %u\n", cpu); 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 9841da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds if (!data) { 9871da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9881da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9895a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9901da177e4SLinus Torvalds return -EINVAL; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds cpufreq_cpu_data[cpu] = NULL; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds #ifdef CONFIG_SMP 9961da177e4SLinus Torvalds /* if this isn't the CPU which is the parent of the kobj, we 9971da177e4SLinus Torvalds * only need to unlink, put and exit 9981da177e4SLinus Torvalds */ 9991da177e4SLinus Torvalds if (unlikely(cpu != data->cpu)) { 10001da177e4SLinus Torvalds dprintk("removing link\n"); 10018ff69732SDave Jones cpu_clear(cpu, data->cpus); 10021da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10031da177e4SLinus Torvalds sysfs_remove_link(&sys_dev->kobj, "cpufreq"); 10041da177e4SLinus Torvalds cpufreq_cpu_put(data); 10051da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10065a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10071da177e4SLinus Torvalds return 0; 10081da177e4SLinus Torvalds } 10091da177e4SLinus Torvalds #endif 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds #ifdef CONFIG_SMP 1012084f3493SThomas Renninger 1013084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 1014084f3493SThomas Renninger cpufreq_cpu_governor[cpu] = data->governor; 1015084f3493SThomas Renninger #endif 1016084f3493SThomas Renninger 10171da177e4SLinus Torvalds /* if we have other CPUs still registered, we need to unlink them, 10181da177e4SLinus Torvalds * or else wait_for_completion below will lock up. Clean the 10191da177e4SLinus Torvalds * cpufreq_cpu_data[] while holding the lock, and remove the sysfs 10201da177e4SLinus Torvalds * links afterwards. 10211da177e4SLinus Torvalds */ 10221da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 10231da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 10241da177e4SLinus Torvalds if (j == cpu) 10251da177e4SLinus Torvalds continue; 10261da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 10331da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 10341da177e4SLinus Torvalds if (j == cpu) 10351da177e4SLinus Torvalds continue; 10361da177e4SLinus Torvalds dprintk("removing link for cpu %u\n", j); 1037084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 1038084f3493SThomas Renninger cpufreq_cpu_governor[j] = data->governor; 1039084f3493SThomas Renninger #endif 1040d434fca7SAshok Raj cpu_sys_dev = get_cpu_sysdev(j); 1041d434fca7SAshok Raj sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); 10421da177e4SLinus Torvalds cpufreq_cpu_put(data); 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds #else 10461da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10471da177e4SLinus Torvalds #endif 10481da177e4SLinus Torvalds 10491da177e4SLinus Torvalds if (cpufreq_driver->target) 10501da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 10515a01f2e8SVenkatesh Pallipadi 10525a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds kobject_put(&data->kobj); 10551da177e4SLinus Torvalds 10561da177e4SLinus Torvalds /* we need to make sure that the underlying kobj is actually 10571da177e4SLinus Torvalds * not referenced anymore by anybody before we proceed with 10581da177e4SLinus Torvalds * unloading. 10591da177e4SLinus Torvalds */ 10601da177e4SLinus Torvalds dprintk("waiting for dropping of refcount\n"); 10611da177e4SLinus Torvalds wait_for_completion(&data->kobj_unregister); 10621da177e4SLinus Torvalds dprintk("wait complete\n"); 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds if (cpufreq_driver->exit) 10651da177e4SLinus Torvalds cpufreq_driver->exit(data); 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds kfree(data); 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10701da177e4SLinus Torvalds return 0; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds 10745a01f2e8SVenkatesh Pallipadi static int cpufreq_remove_dev (struct sys_device * sys_dev) 10755a01f2e8SVenkatesh Pallipadi { 10765a01f2e8SVenkatesh Pallipadi unsigned int cpu = sys_dev->id; 10775a01f2e8SVenkatesh Pallipadi int retval; 1078ec28297aSVenki Pallipadi 1079ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1080ec28297aSVenki Pallipadi return 0; 1081ec28297aSVenki Pallipadi 10825a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 10835a01f2e8SVenkatesh Pallipadi BUG(); 10845a01f2e8SVenkatesh Pallipadi 10855a01f2e8SVenkatesh Pallipadi retval = __cpufreq_remove_dev(sys_dev); 10865a01f2e8SVenkatesh Pallipadi return retval; 10875a01f2e8SVenkatesh Pallipadi } 10885a01f2e8SVenkatesh Pallipadi 10895a01f2e8SVenkatesh Pallipadi 109065f27f38SDavid Howells static void handle_update(struct work_struct *work) 10911da177e4SLinus Torvalds { 109265f27f38SDavid Howells struct cpufreq_policy *policy = 109365f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 109465f27f38SDavid Howells unsigned int cpu = policy->cpu; 10951da177e4SLinus Torvalds dprintk("handle_update for cpu %u called\n", cpu); 10961da177e4SLinus Torvalds cpufreq_update_policy(cpu); 10971da177e4SLinus Torvalds } 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds /** 11001da177e4SLinus Torvalds * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 11011da177e4SLinus Torvalds * @cpu: cpu number 11021da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 11031da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 11041da177e4SLinus Torvalds * 11051da177e4SLinus Torvalds * We adjust to current frequency first, and need to clean up later. So either call 11061da177e4SLinus Torvalds * to cpufreq_update_policy() or schedule handle_update()). 11071da177e4SLinus Torvalds */ 1108e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1109e08f5f5bSGautham R Shenoy unsigned int new_freq) 11101da177e4SLinus Torvalds { 11111da177e4SLinus Torvalds struct cpufreq_freqs freqs; 11121da177e4SLinus Torvalds 1113b10eec22SJan Beulich dprintk("Warning: CPU frequency out of sync: cpufreq and timing " 11141da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds freqs.cpu = cpu; 11171da177e4SLinus Torvalds freqs.old = old_freq; 11181da177e4SLinus Torvalds freqs.new = new_freq; 11191da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 11201da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds /** 11254ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 112695235ca2SVenkatesh Pallipadi * @cpu: CPU number 112795235ca2SVenkatesh Pallipadi * 112895235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 112995235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 113095235ca2SVenkatesh Pallipadi */ 113195235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 113295235ca2SVenkatesh Pallipadi { 113395235ca2SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1134e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 113595235ca2SVenkatesh Pallipadi 113695235ca2SVenkatesh Pallipadi if (policy) { 1137e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 113895235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 113995235ca2SVenkatesh Pallipadi } 114095235ca2SVenkatesh Pallipadi 1141*4d34a67dSDave Jones return ret_freq; 114295235ca2SVenkatesh Pallipadi } 114395235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 114495235ca2SVenkatesh Pallipadi 114595235ca2SVenkatesh Pallipadi 11465a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 11471da177e4SLinus Torvalds { 11485a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_data[cpu]; 1149e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds if (!cpufreq_driver->get) 1152*4d34a67dSDave Jones return ret_freq; 11531da177e4SLinus Torvalds 1154e08f5f5bSGautham R Shenoy ret_freq = cpufreq_driver->get(cpu); 11551da177e4SLinus Torvalds 1156e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 1157e08f5f5bSGautham R Shenoy !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1158e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1159e08f5f5bSGautham R Shenoy saved value exists */ 1160e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1161e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 11621da177e4SLinus Torvalds schedule_work(&policy->update); 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds } 11651da177e4SLinus Torvalds 1166*4d34a67dSDave Jones return ret_freq; 11675a01f2e8SVenkatesh Pallipadi } 11681da177e4SLinus Torvalds 11695a01f2e8SVenkatesh Pallipadi /** 11705a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 11715a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 11725a01f2e8SVenkatesh Pallipadi * 11735a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 11745a01f2e8SVenkatesh Pallipadi */ 11755a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 11765a01f2e8SVenkatesh Pallipadi { 11775a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 11785a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 11795a01f2e8SVenkatesh Pallipadi 11805a01f2e8SVenkatesh Pallipadi if (!policy) 11815a01f2e8SVenkatesh Pallipadi goto out; 11825a01f2e8SVenkatesh Pallipadi 11835a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 11845a01f2e8SVenkatesh Pallipadi goto out_policy; 11855a01f2e8SVenkatesh Pallipadi 11865a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 11875a01f2e8SVenkatesh Pallipadi 11885a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 11895a01f2e8SVenkatesh Pallipadi 11905a01f2e8SVenkatesh Pallipadi out_policy: 11911da177e4SLinus Torvalds cpufreq_cpu_put(policy); 11925a01f2e8SVenkatesh Pallipadi out: 1193*4d34a67dSDave Jones return ret_freq; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds 11981da177e4SLinus Torvalds /** 119942d4dc3fSBenjamin Herrenschmidt * cpufreq_suspend - let the low level driver prepare for suspend 120042d4dc3fSBenjamin Herrenschmidt */ 120142d4dc3fSBenjamin Herrenschmidt 1202e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg) 120342d4dc3fSBenjamin Herrenschmidt { 120442d4dc3fSBenjamin Herrenschmidt int cpu = sysdev->id; 1205e08f5f5bSGautham R Shenoy int ret = 0; 120642d4dc3fSBenjamin Herrenschmidt unsigned int cur_freq = 0; 120742d4dc3fSBenjamin Herrenschmidt struct cpufreq_policy *cpu_policy; 120842d4dc3fSBenjamin Herrenschmidt 12090e37b159SDave Jones dprintk("suspending cpu %u\n", cpu); 121042d4dc3fSBenjamin Herrenschmidt 121142d4dc3fSBenjamin Herrenschmidt if (!cpu_online(cpu)) 121242d4dc3fSBenjamin Herrenschmidt return 0; 121342d4dc3fSBenjamin Herrenschmidt 121442d4dc3fSBenjamin Herrenschmidt /* we may be lax here as interrupts are off. Nonetheless 121542d4dc3fSBenjamin Herrenschmidt * we need to grab the correct cpu policy, as to check 121642d4dc3fSBenjamin Herrenschmidt * whether we really run on this CPU. 121742d4dc3fSBenjamin Herrenschmidt */ 121842d4dc3fSBenjamin Herrenschmidt 121942d4dc3fSBenjamin Herrenschmidt cpu_policy = cpufreq_cpu_get(cpu); 122042d4dc3fSBenjamin Herrenschmidt if (!cpu_policy) 122142d4dc3fSBenjamin Herrenschmidt return -EINVAL; 122242d4dc3fSBenjamin Herrenschmidt 122342d4dc3fSBenjamin Herrenschmidt /* only handle each CPU group once */ 1224c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1225c9060494SDave Jones goto out; 122642d4dc3fSBenjamin Herrenschmidt 122742d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->suspend) { 1228e00d9967SBernard Blackham ret = cpufreq_driver->suspend(cpu_policy, pmsg); 122942d4dc3fSBenjamin Herrenschmidt if (ret) { 123042d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 123142d4dc3fSBenjamin Herrenschmidt "step on CPU %u\n", cpu_policy->cpu); 1232c9060494SDave Jones goto out; 123342d4dc3fSBenjamin Herrenschmidt } 123442d4dc3fSBenjamin Herrenschmidt } 123542d4dc3fSBenjamin Herrenschmidt 123642d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) 123742d4dc3fSBenjamin Herrenschmidt goto out; 123842d4dc3fSBenjamin Herrenschmidt 123942d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->get) 124042d4dc3fSBenjamin Herrenschmidt cur_freq = cpufreq_driver->get(cpu_policy->cpu); 124142d4dc3fSBenjamin Herrenschmidt 124242d4dc3fSBenjamin Herrenschmidt if (!cur_freq || !cpu_policy->cur) { 124342d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed to assert current " 124442d4dc3fSBenjamin Herrenschmidt "frequency is what timing core thinks it is.\n"); 124542d4dc3fSBenjamin Herrenschmidt goto out; 124642d4dc3fSBenjamin Herrenschmidt } 124742d4dc3fSBenjamin Herrenschmidt 124842d4dc3fSBenjamin Herrenschmidt if (unlikely(cur_freq != cpu_policy->cur)) { 124942d4dc3fSBenjamin Herrenschmidt struct cpufreq_freqs freqs; 125042d4dc3fSBenjamin Herrenschmidt 125142d4dc3fSBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1252b10eec22SJan Beulich dprintk("Warning: CPU frequency is %u, " 125342d4dc3fSBenjamin Herrenschmidt "cpufreq assumed %u kHz.\n", 125442d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 125542d4dc3fSBenjamin Herrenschmidt 125642d4dc3fSBenjamin Herrenschmidt freqs.cpu = cpu; 125742d4dc3fSBenjamin Herrenschmidt freqs.old = cpu_policy->cur; 125842d4dc3fSBenjamin Herrenschmidt freqs.new = cur_freq; 125942d4dc3fSBenjamin Herrenschmidt 1260b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 126142d4dc3fSBenjamin Herrenschmidt CPUFREQ_SUSPENDCHANGE, &freqs); 126242d4dc3fSBenjamin Herrenschmidt adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); 126342d4dc3fSBenjamin Herrenschmidt 126442d4dc3fSBenjamin Herrenschmidt cpu_policy->cur = cur_freq; 126542d4dc3fSBenjamin Herrenschmidt } 126642d4dc3fSBenjamin Herrenschmidt 126742d4dc3fSBenjamin Herrenschmidt out: 126842d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 1269c9060494SDave Jones return ret; 127042d4dc3fSBenjamin Herrenschmidt } 127142d4dc3fSBenjamin Herrenschmidt 127242d4dc3fSBenjamin Herrenschmidt /** 12731da177e4SLinus Torvalds * cpufreq_resume - restore proper CPU frequency handling after resume 12741da177e4SLinus Torvalds * 12751da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 12761da177e4SLinus Torvalds * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync 127742d4dc3fSBenjamin Herrenschmidt * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are 127842d4dc3fSBenjamin Herrenschmidt * restored. 12791da177e4SLinus Torvalds */ 12801da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device * sysdev) 12811da177e4SLinus Torvalds { 12821da177e4SLinus Torvalds int cpu = sysdev->id; 1283e08f5f5bSGautham R Shenoy int ret = 0; 12841da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds dprintk("resuming cpu %u\n", cpu); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds if (!cpu_online(cpu)) 12891da177e4SLinus Torvalds return 0; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds /* we may be lax here as interrupts are off. Nonetheless 12921da177e4SLinus Torvalds * we need to grab the correct cpu policy, as to check 12931da177e4SLinus Torvalds * whether we really run on this CPU. 12941da177e4SLinus Torvalds */ 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 12971da177e4SLinus Torvalds if (!cpu_policy) 12981da177e4SLinus Torvalds return -EINVAL; 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds /* only handle each CPU group once */ 1301c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1302c9060494SDave Jones goto fail; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds if (cpufreq_driver->resume) { 13051da177e4SLinus Torvalds ret = cpufreq_driver->resume(cpu_policy); 13061da177e4SLinus Torvalds if (ret) { 13071da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 13081da177e4SLinus Torvalds "step on CPU %u\n", cpu_policy->cpu); 1309c9060494SDave Jones goto fail; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 13141da177e4SLinus Torvalds unsigned int cur_freq = 0; 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds if (cpufreq_driver->get) 13171da177e4SLinus Torvalds cur_freq = cpufreq_driver->get(cpu_policy->cpu); 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds if (!cur_freq || !cpu_policy->cur) { 132042d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: resume failed to assert " 132142d4dc3fSBenjamin Herrenschmidt "current frequency is what timing core " 132242d4dc3fSBenjamin Herrenschmidt "thinks it is.\n"); 13231da177e4SLinus Torvalds goto out; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds if (unlikely(cur_freq != cpu_policy->cur)) { 13271da177e4SLinus Torvalds struct cpufreq_freqs freqs; 13281da177e4SLinus Torvalds 1329ac09f698SBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1330b10eec22SJan Beulich dprintk("Warning: CPU frequency " 1331ac09f698SBenjamin Herrenschmidt "is %u, cpufreq assumed %u kHz.\n", 133242d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds freqs.cpu = cpu; 13351da177e4SLinus Torvalds freqs.old = cpu_policy->cur; 13361da177e4SLinus Torvalds freqs.new = cur_freq; 13371da177e4SLinus Torvalds 1338b4dfdbb3SAlan Stern srcu_notifier_call_chain( 1339e041c683SAlan Stern &cpufreq_transition_notifier_list, 134042d4dc3fSBenjamin Herrenschmidt CPUFREQ_RESUMECHANGE, &freqs); 13411da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds cpu_policy->cur = cur_freq; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds } 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds out: 13481da177e4SLinus Torvalds schedule_work(&cpu_policy->update); 1349c9060494SDave Jones fail: 13501da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 13511da177e4SLinus Torvalds return ret; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = { 13551da177e4SLinus Torvalds .add = cpufreq_add_dev, 13561da177e4SLinus Torvalds .remove = cpufreq_remove_dev, 135742d4dc3fSBenjamin Herrenschmidt .suspend = cpufreq_suspend, 13581da177e4SLinus Torvalds .resume = cpufreq_resume, 13591da177e4SLinus Torvalds }; 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds /********************************************************************* 13631da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 13641da177e4SLinus Torvalds *********************************************************************/ 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds /** 13671da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 13681da177e4SLinus Torvalds * @nb: notifier function to register 13691da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 13701da177e4SLinus Torvalds * 13711da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 13721da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 13731da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 13741da177e4SLinus Torvalds * changes in cpufreq policy. 13751da177e4SLinus Torvalds * 13761da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1377e041c683SAlan Stern * blocking_notifier_chain_register. 13781da177e4SLinus Torvalds */ 13791da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 13801da177e4SLinus Torvalds { 13811da177e4SLinus Torvalds int ret; 13821da177e4SLinus Torvalds 13831da177e4SLinus Torvalds switch (list) { 13841da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1385b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1386e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 13871da177e4SLinus Torvalds break; 13881da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1389e041c683SAlan Stern ret = blocking_notifier_chain_register( 1390e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 13911da177e4SLinus Torvalds break; 13921da177e4SLinus Torvalds default: 13931da177e4SLinus Torvalds ret = -EINVAL; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds return ret; 13971da177e4SLinus Torvalds } 13981da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds /** 14021da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 14031da177e4SLinus Torvalds * @nb: notifier block to be unregistered 14041da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 14051da177e4SLinus Torvalds * 14061da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 14071da177e4SLinus Torvalds * 14081da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1409e041c683SAlan Stern * blocking_notifier_chain_unregister. 14101da177e4SLinus Torvalds */ 14111da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 14121da177e4SLinus Torvalds { 14131da177e4SLinus Torvalds int ret; 14141da177e4SLinus Torvalds 14151da177e4SLinus Torvalds switch (list) { 14161da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1417b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1418e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 14191da177e4SLinus Torvalds break; 14201da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1421e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1422e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 14231da177e4SLinus Torvalds break; 14241da177e4SLinus Torvalds default: 14251da177e4SLinus Torvalds ret = -EINVAL; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds return ret; 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 14311da177e4SLinus Torvalds 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds /********************************************************************* 14341da177e4SLinus Torvalds * GOVERNORS * 14351da177e4SLinus Torvalds *********************************************************************/ 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 14391da177e4SLinus Torvalds unsigned int target_freq, 14401da177e4SLinus Torvalds unsigned int relation) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds int retval = -EINVAL; 1443c32b6b8eSAshok Raj 14441da177e4SLinus Torvalds dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 14451da177e4SLinus Torvalds target_freq, relation); 14461da177e4SLinus Torvalds if (cpu_online(policy->cpu) && cpufreq_driver->target) 14471da177e4SLinus Torvalds retval = cpufreq_driver->target(policy, target_freq, relation); 144890d45d17SAshok Raj 14491da177e4SLinus Torvalds return retval; 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 14541da177e4SLinus Torvalds unsigned int target_freq, 14551da177e4SLinus Torvalds unsigned int relation) 14561da177e4SLinus Torvalds { 1457cc993cabSDave Jones int ret; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 14601da177e4SLinus Torvalds if (!policy) 14611da177e4SLinus Torvalds return -EINVAL; 14621da177e4SLinus Torvalds 14635a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 14645a01f2e8SVenkatesh Pallipadi return -EINVAL; 14651da177e4SLinus Torvalds 14661da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 14671da177e4SLinus Torvalds 14685a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds cpufreq_cpu_put(policy); 14711da177e4SLinus Torvalds return ret; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 14741da177e4SLinus Torvalds 14755a01f2e8SVenkatesh Pallipadi int __cpufreq_driver_getavg(struct cpufreq_policy *policy) 1476dfde5d62SVenkatesh Pallipadi { 1477dfde5d62SVenkatesh Pallipadi int ret = 0; 1478dfde5d62SVenkatesh Pallipadi 1479dfde5d62SVenkatesh Pallipadi policy = cpufreq_cpu_get(policy->cpu); 1480dfde5d62SVenkatesh Pallipadi if (!policy) 1481dfde5d62SVenkatesh Pallipadi return -EINVAL; 1482dfde5d62SVenkatesh Pallipadi 1483dfde5d62SVenkatesh Pallipadi if (cpu_online(policy->cpu) && cpufreq_driver->getavg) 1484dfde5d62SVenkatesh Pallipadi ret = cpufreq_driver->getavg(policy->cpu); 1485dfde5d62SVenkatesh Pallipadi 1486dfde5d62SVenkatesh Pallipadi cpufreq_cpu_put(policy); 1487dfde5d62SVenkatesh Pallipadi return ret; 1488dfde5d62SVenkatesh Pallipadi } 14895a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg); 1490dfde5d62SVenkatesh Pallipadi 1491153d7f3fSArjan van de Ven /* 1492153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1493153d7f3fSArjan van de Ven */ 14941da177e4SLinus Torvalds 1495e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1496e08f5f5bSGautham R Shenoy unsigned int event) 14971da177e4SLinus Torvalds { 1498cc993cabSDave Jones int ret; 14996afde10cSThomas Renninger 15006afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 15016afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 15026afde10cSThomas Renninger That this is the case is already ensured in Kconfig 15036afde10cSThomas Renninger */ 15046afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 15056afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 15066afde10cSThomas Renninger #else 15076afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 15086afde10cSThomas Renninger #endif 15091c256245SThomas Renninger 15101c256245SThomas Renninger if (policy->governor->max_transition_latency && 15111c256245SThomas Renninger policy->cpuinfo.transition_latency > 15121c256245SThomas Renninger policy->governor->max_transition_latency) { 15136afde10cSThomas Renninger if (!gov) 15146afde10cSThomas Renninger return -EINVAL; 15156afde10cSThomas Renninger else { 15161c256245SThomas Renninger printk(KERN_WARNING "%s governor failed, too long" 15171c256245SThomas Renninger " transition latency of HW, fallback" 15181c256245SThomas Renninger " to %s governor\n", 15191c256245SThomas Renninger policy->governor->name, 15201c256245SThomas Renninger gov->name); 15211c256245SThomas Renninger policy->governor = gov; 15221c256245SThomas Renninger } 15236afde10cSThomas Renninger } 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 15261da177e4SLinus Torvalds return -EINVAL; 15271da177e4SLinus Torvalds 1528e08f5f5bSGautham R Shenoy dprintk("__cpufreq_governor for CPU %u, event %u\n", 1529e08f5f5bSGautham R Shenoy policy->cpu, event); 15301da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 15311da177e4SLinus Torvalds 1532e08f5f5bSGautham R Shenoy /* we keep one module reference alive for 1533e08f5f5bSGautham R Shenoy each CPU governed by this CPU */ 15341da177e4SLinus Torvalds if ((event != CPUFREQ_GOV_START) || ret) 15351da177e4SLinus Torvalds module_put(policy->governor->owner); 15361da177e4SLinus Torvalds if ((event == CPUFREQ_GOV_STOP) && !ret) 15371da177e4SLinus Torvalds module_put(policy->governor->owner); 15381da177e4SLinus Torvalds 15391da177e4SLinus Torvalds return ret; 15401da177e4SLinus Torvalds } 15411da177e4SLinus Torvalds 15421da177e4SLinus Torvalds 15431da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 15441da177e4SLinus Torvalds { 15453bcb09a3SJeremy Fitzhardinge int err; 15461da177e4SLinus Torvalds 15471da177e4SLinus Torvalds if (!governor) 15481da177e4SLinus Torvalds return -EINVAL; 15491da177e4SLinus Torvalds 15503fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 15511da177e4SLinus Torvalds 15523bcb09a3SJeremy Fitzhardinge err = -EBUSY; 15533bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 15543bcb09a3SJeremy Fitzhardinge err = 0; 15551da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 15563bcb09a3SJeremy Fitzhardinge } 15571da177e4SLinus Torvalds 15583fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 15593bcb09a3SJeremy Fitzhardinge return err; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds 15641da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 15651da177e4SLinus Torvalds { 15661da177e4SLinus Torvalds if (!governor) 15671da177e4SLinus Torvalds return; 15681da177e4SLinus Torvalds 15693fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 15701da177e4SLinus Torvalds list_del(&governor->governor_list); 15713fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 15721da177e4SLinus Torvalds return; 15731da177e4SLinus Torvalds } 15741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 15751da177e4SLinus Torvalds 15761da177e4SLinus Torvalds 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds /********************************************************************* 15791da177e4SLinus Torvalds * POLICY INTERFACE * 15801da177e4SLinus Torvalds *********************************************************************/ 15811da177e4SLinus Torvalds 15821da177e4SLinus Torvalds /** 15831da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 15841da177e4SLinus Torvalds * @policy: struct cpufreq_policy into which the current cpufreq_policy is written 15851da177e4SLinus Torvalds * 15861da177e4SLinus Torvalds * Reads the current cpufreq policy. 15871da177e4SLinus Torvalds */ 15881da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 15891da177e4SLinus Torvalds { 15901da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 15911da177e4SLinus Torvalds if (!policy) 15921da177e4SLinus Torvalds return -EINVAL; 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 15951da177e4SLinus Torvalds if (!cpu_policy) 15961da177e4SLinus Torvalds return -EINVAL; 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 16011da177e4SLinus Torvalds return 0; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds 1606153d7f3fSArjan van de Ven /* 1607e08f5f5bSGautham R Shenoy * data : current policy. 1608e08f5f5bSGautham R Shenoy * policy : policy to be set. 1609153d7f3fSArjan van de Ven */ 1610e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 1611e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy) 16121da177e4SLinus Torvalds { 16131da177e4SLinus Torvalds int ret = 0; 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 16161da177e4SLinus Torvalds dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 16171da177e4SLinus Torvalds policy->min, policy->max); 16181da177e4SLinus Torvalds 1619e08f5f5bSGautham R Shenoy memcpy(&policy->cpuinfo, &data->cpuinfo, 1620e08f5f5bSGautham R Shenoy sizeof(struct cpufreq_cpuinfo)); 16211da177e4SLinus Torvalds 162253391fa2SYi Yang if (policy->min > data->max || policy->max < data->min) { 16239c9a43edSMattia Dongili ret = -EINVAL; 16249c9a43edSMattia Dongili goto error_out; 16259c9a43edSMattia Dongili } 16269c9a43edSMattia Dongili 16271da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 16281da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 16291da177e4SLinus Torvalds if (ret) 16301da177e4SLinus Torvalds goto error_out; 16311da177e4SLinus Torvalds 16321da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 1633e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1634e041c683SAlan Stern CPUFREQ_ADJUST, policy); 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 1637e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1638e041c683SAlan Stern CPUFREQ_INCOMPATIBLE, policy); 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit, 16411da177e4SLinus Torvalds which might be different to the first one */ 16421da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 1643e041c683SAlan Stern if (ret) 16441da177e4SLinus Torvalds goto error_out; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds /* notification of the new policy */ 1647e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1648e041c683SAlan Stern CPUFREQ_NOTIFY, policy); 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds data->min = policy->min; 16511da177e4SLinus Torvalds data->max = policy->max; 16521da177e4SLinus Torvalds 1653e08f5f5bSGautham R Shenoy dprintk("new min and max freqs are %u - %u kHz\n", 1654e08f5f5bSGautham R Shenoy data->min, data->max); 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 16571da177e4SLinus Torvalds data->policy = policy->policy; 16581da177e4SLinus Torvalds dprintk("setting range\n"); 16591da177e4SLinus Torvalds ret = cpufreq_driver->setpolicy(policy); 16601da177e4SLinus Torvalds } else { 16611da177e4SLinus Torvalds if (policy->governor != data->governor) { 16621da177e4SLinus Torvalds /* save old, working values */ 16631da177e4SLinus Torvalds struct cpufreq_governor *old_gov = data->governor; 16641da177e4SLinus Torvalds 16651da177e4SLinus Torvalds dprintk("governor switch\n"); 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds /* end old governor */ 16681da177e4SLinus Torvalds if (data->governor) 16691da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* start new governor */ 16721da177e4SLinus Torvalds data->governor = policy->governor; 16731da177e4SLinus Torvalds if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 16741da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 1675e08f5f5bSGautham R Shenoy dprintk("starting governor %s failed\n", 1676e08f5f5bSGautham R Shenoy data->governor->name); 16771da177e4SLinus Torvalds if (old_gov) { 16781da177e4SLinus Torvalds data->governor = old_gov; 1679e08f5f5bSGautham R Shenoy __cpufreq_governor(data, 1680e08f5f5bSGautham R Shenoy CPUFREQ_GOV_START); 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds ret = -EINVAL; 16831da177e4SLinus Torvalds goto error_out; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 16861da177e4SLinus Torvalds } 16871da177e4SLinus Torvalds dprintk("governor: change or update limits\n"); 16881da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds 16911da177e4SLinus Torvalds error_out: 16921da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 16931da177e4SLinus Torvalds return ret; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds 16961da177e4SLinus Torvalds /** 16971da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 16981da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 16991da177e4SLinus Torvalds * 17001da177e4SLinus Torvalds * Usefull for policy notifiers which have different necessities 17011da177e4SLinus Torvalds * at different times. 17021da177e4SLinus Torvalds */ 17031da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 17041da177e4SLinus Torvalds { 17051da177e4SLinus Torvalds struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 17061da177e4SLinus Torvalds struct cpufreq_policy policy; 17071da177e4SLinus Torvalds int ret = 0; 17081da177e4SLinus Torvalds 17091da177e4SLinus Torvalds if (!data) 17101da177e4SLinus Torvalds return -ENODEV; 17111da177e4SLinus Torvalds 17125a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 17135a01f2e8SVenkatesh Pallipadi return -EINVAL; 17141da177e4SLinus Torvalds 17151da177e4SLinus Torvalds dprintk("updating policy for CPU %u\n", cpu); 17167d5e350fSDave Jones memcpy(&policy, data, sizeof(struct cpufreq_policy)); 17171da177e4SLinus Torvalds policy.min = data->user_policy.min; 17181da177e4SLinus Torvalds policy.max = data->user_policy.max; 17191da177e4SLinus Torvalds policy.policy = data->user_policy.policy; 17201da177e4SLinus Torvalds policy.governor = data->user_policy.governor; 17211da177e4SLinus Torvalds 17220961dd0dSThomas Renninger /* BIOS might change freq behind our back 17230961dd0dSThomas Renninger -> ask driver for current freq and notify governors about a change */ 17240961dd0dSThomas Renninger if (cpufreq_driver->get) { 17250961dd0dSThomas Renninger policy.cur = cpufreq_driver->get(cpu); 1726a85f7bd3SThomas Renninger if (!data->cur) { 1727a85f7bd3SThomas Renninger dprintk("Driver did not initialize current freq"); 1728a85f7bd3SThomas Renninger data->cur = policy.cur; 1729a85f7bd3SThomas Renninger } else { 17300961dd0dSThomas Renninger if (data->cur != policy.cur) 1731e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, data->cur, 1732e08f5f5bSGautham R Shenoy policy.cur); 17330961dd0dSThomas Renninger } 1734a85f7bd3SThomas Renninger } 17350961dd0dSThomas Renninger 17361da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, &policy); 17371da177e4SLinus Torvalds 17385a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 17395a01f2e8SVenkatesh Pallipadi 17401da177e4SLinus Torvalds cpufreq_cpu_put(data); 17411da177e4SLinus Torvalds return ret; 17421da177e4SLinus Torvalds } 17431da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 17441da177e4SLinus Torvalds 1745dd184a01SSatyam Sharma static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, 1746c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1747c32b6b8eSAshok Raj { 1748c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 1749c32b6b8eSAshok Raj struct sys_device *sys_dev; 1750c32b6b8eSAshok Raj 1751c32b6b8eSAshok Raj sys_dev = get_cpu_sysdev(cpu); 1752c32b6b8eSAshok Raj if (sys_dev) { 1753c32b6b8eSAshok Raj switch (action) { 1754c32b6b8eSAshok Raj case CPU_ONLINE: 17558bb78442SRafael J. Wysocki case CPU_ONLINE_FROZEN: 1756c32b6b8eSAshok Raj cpufreq_add_dev(sys_dev); 1757c32b6b8eSAshok Raj break; 1758c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 17598bb78442SRafael J. Wysocki case CPU_DOWN_PREPARE_FROZEN: 17605a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 17615a01f2e8SVenkatesh Pallipadi BUG(); 17625a01f2e8SVenkatesh Pallipadi 17635a01f2e8SVenkatesh Pallipadi __cpufreq_remove_dev(sys_dev); 1764c32b6b8eSAshok Raj break; 17655a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 17668bb78442SRafael J. Wysocki case CPU_DOWN_FAILED_FROZEN: 17675a01f2e8SVenkatesh Pallipadi cpufreq_add_dev(sys_dev); 1768c32b6b8eSAshok Raj break; 1769c32b6b8eSAshok Raj } 1770c32b6b8eSAshok Raj } 1771c32b6b8eSAshok Raj return NOTIFY_OK; 1772c32b6b8eSAshok Raj } 1773c32b6b8eSAshok Raj 1774f6ebef30SSam Ravnborg static struct notifier_block __refdata cpufreq_cpu_notifier = 1775c32b6b8eSAshok Raj { 1776c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 1777c32b6b8eSAshok Raj }; 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds /********************************************************************* 17801da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 17811da177e4SLinus Torvalds *********************************************************************/ 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /** 17841da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 17851da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 17861da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 17871da177e4SLinus Torvalds * 17881da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 17891da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 17901da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 17911da177e4SLinus Torvalds * 17921da177e4SLinus Torvalds */ 1793221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 17941da177e4SLinus Torvalds { 17951da177e4SLinus Torvalds unsigned long flags; 17961da177e4SLinus Torvalds int ret; 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 17991da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 18001da177e4SLinus Torvalds return -EINVAL; 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds dprintk("trying to register driver %s\n", driver_data->name); 18031da177e4SLinus Torvalds 18041da177e4SLinus Torvalds if (driver_data->setpolicy) 18051da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18081da177e4SLinus Torvalds if (cpufreq_driver) { 18091da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18101da177e4SLinus Torvalds return -EBUSY; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds cpufreq_driver = driver_data; 18131da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 18181da177e4SLinus Torvalds int i; 18191da177e4SLinus Torvalds ret = -ENODEV; 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds /* check for at least one working CPU */ 18221da177e4SLinus Torvalds for (i=0; i<NR_CPUS; i++) 18231da177e4SLinus Torvalds if (cpufreq_cpu_data[i]) 18241da177e4SLinus Torvalds ret = 0; 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 18271da177e4SLinus Torvalds if (ret) { 1828e08f5f5bSGautham R Shenoy dprintk("no CPU initialized for driver %s\n", 1829e08f5f5bSGautham R Shenoy driver_data->name); 1830e08f5f5bSGautham R Shenoy sysdev_driver_unregister(&cpu_sysdev_class, 1831e08f5f5bSGautham R Shenoy &cpufreq_sysdev_driver); 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18341da177e4SLinus Torvalds cpufreq_driver = NULL; 18351da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds if (!ret) { 184065edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 18411da177e4SLinus Torvalds dprintk("driver %s up and running\n", driver_data->name); 18421da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 1845*4d34a67dSDave Jones return ret; 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds /** 18511da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 18521da177e4SLinus Torvalds * 18531da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 18541da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 18551da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 18561da177e4SLinus Torvalds * currently not initialised. 18571da177e4SLinus Torvalds */ 1858221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 18591da177e4SLinus Torvalds { 18601da177e4SLinus Torvalds unsigned long flags; 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds if (!cpufreq_driver || (driver != cpufreq_driver)) { 18651da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18661da177e4SLinus Torvalds return -EINVAL; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds dprintk("unregistering driver %s\n", driver->name); 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 187265edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18751da177e4SLinus Torvalds cpufreq_driver = NULL; 18761da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds return 0; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 18815a01f2e8SVenkatesh Pallipadi 18825a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 18835a01f2e8SVenkatesh Pallipadi { 18845a01f2e8SVenkatesh Pallipadi int cpu; 18855a01f2e8SVenkatesh Pallipadi 18865a01f2e8SVenkatesh Pallipadi for_each_possible_cpu(cpu) { 18875a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = -1; 18885a01f2e8SVenkatesh Pallipadi init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 18895a01f2e8SVenkatesh Pallipadi } 18905a01f2e8SVenkatesh Pallipadi return 0; 18915a01f2e8SVenkatesh Pallipadi } 18925a01f2e8SVenkatesh Pallipadi 18935a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 1894