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 12174212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 122b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 123b4dfdbb3SAlan Stern { 124b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 12574212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 126b4dfdbb3SAlan Stern return 0; 127b4dfdbb3SAlan Stern } 128b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 1313fc54d37Sakpm@osdl.org static DEFINE_MUTEX (cpufreq_governor_mutex); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds struct cpufreq_policy *data; 1361da177e4SLinus Torvalds unsigned long flags; 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds if (cpu >= NR_CPUS) 1391da177e4SLinus Torvalds goto err_out; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* get the cpufreq driver */ 1421da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds if (!cpufreq_driver) 1451da177e4SLinus Torvalds goto err_out_unlock; 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) 1481da177e4SLinus Torvalds goto err_out_unlock; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds /* get the CPU */ 1521da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds if (!data) 1551da177e4SLinus Torvalds goto err_out_put_module; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) 1581da177e4SLinus Torvalds goto err_out_put_module; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1611da177e4SLinus Torvalds return data; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds err_out_put_module: 1641da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1651da177e4SLinus Torvalds err_out_unlock: 1661da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1671da177e4SLinus Torvalds err_out: 1681da177e4SLinus Torvalds return NULL; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 1711da177e4SLinus Torvalds 1727d5e350fSDave Jones 1731da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data) 1741da177e4SLinus Torvalds { 1751da177e4SLinus Torvalds kobject_put(&data->kobj); 1761da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds /********************************************************************* 1821da177e4SLinus Torvalds * UNIFIED DEBUG HELPERS * 1831da177e4SLinus Torvalds *********************************************************************/ 1841da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */ 1871da177e4SLinus Torvalds static unsigned int debug; 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can 1901da177e4SLinus Torvalds * set or modify this value. 1911da177e4SLinus Torvalds */ 1921da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful 1951da177e4SLinus Torvalds * loading of a cpufreq driver, temporarily disabled when a new policy 1961da177e4SLinus Torvalds * is set, and disabled upon cpufreq driver removal 1971da177e4SLinus Torvalds */ 1981da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1; 1991da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock); 2001da177e4SLinus Torvalds 201858119e1SArjan van de Ven static void cpufreq_debug_enable_ratelimit(void) 2021da177e4SLinus Torvalds { 2031da177e4SLinus Torvalds unsigned long flags; 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2061da177e4SLinus Torvalds if (disable_ratelimit) 2071da177e4SLinus Torvalds disable_ratelimit--; 2081da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 211858119e1SArjan van de Ven static void cpufreq_debug_disable_ratelimit(void) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds unsigned long flags; 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2161da177e4SLinus Torvalds disable_ratelimit++; 2171da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 220e08f5f5bSGautham R Shenoy void cpufreq_debug_printk(unsigned int type, const char *prefix, 221e08f5f5bSGautham R Shenoy const char *fmt, ...) 2221da177e4SLinus Torvalds { 2231da177e4SLinus Torvalds char s[256]; 2241da177e4SLinus Torvalds va_list args; 2251da177e4SLinus Torvalds unsigned int len; 2261da177e4SLinus Torvalds unsigned long flags; 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds WARN_ON(!prefix); 2291da177e4SLinus Torvalds if (type & debug) { 2301da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 231e08f5f5bSGautham R Shenoy if (!disable_ratelimit && debug_ratelimit 232e08f5f5bSGautham R Shenoy && !printk_ratelimit()) { 2331da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2341da177e4SLinus Torvalds return; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds va_start(args, fmt); 2411da177e4SLinus Torvalds len += vsnprintf(&s[len], (256 - len), fmt, args); 2421da177e4SLinus Torvalds va_end(args); 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds printk(s); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds WARN_ON(len < 5); 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds module_param(debug, uint, 0644); 253e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core," 254e08f5f5bSGautham R Shenoy " 2 to debug drivers, and 4 to debug governors."); 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644); 257e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:" 258e08f5f5bSGautham R Shenoy " set to 0 to disable ratelimiting."); 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */ 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; } 2631da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; } 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */ 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /********************************************************************* 2691da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2701da177e4SLinus Torvalds *********************************************************************/ 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /** 2731da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2741da177e4SLinus Torvalds * 2751da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2761da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2771da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2781da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2791da177e4SLinus Torvalds */ 2801da177e4SLinus Torvalds #ifndef CONFIG_SMP 2811da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2821da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2831da177e4SLinus Torvalds 284858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2871da177e4SLinus Torvalds return; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2901da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2911da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 292e08f5f5bSGautham R Shenoy dprintk("saving %lu as reference value for loops_per_jiffy; " 293e08f5f5bSGautham R Shenoy "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 2941da177e4SLinus Torvalds } 2951da177e4SLinus Torvalds if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || 2961da177e4SLinus Torvalds (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || 29742d4dc3fSBenjamin Herrenschmidt (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 298e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 299e08f5f5bSGautham R Shenoy ci->new); 300e08f5f5bSGautham R Shenoy dprintk("scaling loops_per_jiffy to %lu " 301e08f5f5bSGautham R Shenoy "for frequency %u kHz\n", loops_per_jiffy, ci->new); 3021da177e4SLinus Torvalds } 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds #else 305e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 306e08f5f5bSGautham R Shenoy { 307e08f5f5bSGautham R Shenoy return; 308e08f5f5bSGautham R Shenoy } 3091da177e4SLinus Torvalds #endif 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds /** 313e4472cb3SDave Jones * cpufreq_notify_transition - call notifier chain and adjust_jiffies 314e4472cb3SDave Jones * on frequency transition. 3151da177e4SLinus Torvalds * 316e4472cb3SDave Jones * This function calls the transition notifiers and the "adjust_jiffies" 317e4472cb3SDave Jones * function. It is called twice on all CPU frequency changes that have 318e4472cb3SDave Jones * external effects. 3191da177e4SLinus Torvalds */ 3201da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) 3211da177e4SLinus Torvalds { 322e4472cb3SDave Jones struct cpufreq_policy *policy; 323e4472cb3SDave Jones 3241da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds freqs->flags = cpufreq_driver->flags; 327e4472cb3SDave Jones dprintk("notification %u of frequency transition to %u kHz\n", 328e4472cb3SDave Jones state, freqs->new); 3291da177e4SLinus Torvalds 330e4472cb3SDave Jones policy = cpufreq_cpu_data[freqs->cpu]; 3311da177e4SLinus Torvalds switch (state) { 332e4472cb3SDave Jones 3331da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 334e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 335e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 336e4472cb3SDave Jones * "old frequency". 3371da177e4SLinus Torvalds */ 3381da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 339e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 340e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 341b10eec22SJan Beulich dprintk("Warning: CPU frequency is" 342e4472cb3SDave Jones " %u, cpufreq assumed %u kHz.\n", 343e4472cb3SDave Jones freqs->old, policy->cur); 344e4472cb3SDave Jones freqs->old = policy->cur; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds } 347b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 348e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3491da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3501da177e4SLinus Torvalds break; 351e4472cb3SDave Jones 3521da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3531da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 354b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 355e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 356e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 357e4472cb3SDave Jones policy->cur = freqs->new; 3581da177e4SLinus Torvalds break; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds /********************************************************************* 3661da177e4SLinus Torvalds * SYSFS INTERFACE * 3671da177e4SLinus Torvalds *********************************************************************/ 3681da177e4SLinus Torvalds 3693bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 3703bcb09a3SJeremy Fitzhardinge { 3713bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 3723bcb09a3SJeremy Fitzhardinge 3733bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 3743bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) 3753bcb09a3SJeremy Fitzhardinge return t; 3763bcb09a3SJeremy Fitzhardinge 3773bcb09a3SJeremy Fitzhardinge return NULL; 3783bcb09a3SJeremy Fitzhardinge } 3793bcb09a3SJeremy Fitzhardinge 3801da177e4SLinus Torvalds /** 3811da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 3821da177e4SLinus Torvalds */ 3831da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 3841da177e4SLinus Torvalds struct cpufreq_governor **governor) 3851da177e4SLinus Torvalds { 3863bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 3873bcb09a3SJeremy Fitzhardinge 3881da177e4SLinus Torvalds if (!cpufreq_driver) 3893bcb09a3SJeremy Fitzhardinge goto out; 3903bcb09a3SJeremy Fitzhardinge 3911da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 3921da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 3931da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 3943bcb09a3SJeremy Fitzhardinge err = 0; 395e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 396e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 3971da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 3983bcb09a3SJeremy Fitzhardinge err = 0; 3991da177e4SLinus Torvalds } 4003bcb09a3SJeremy Fitzhardinge } else if (cpufreq_driver->target) { 4011da177e4SLinus Torvalds struct cpufreq_governor *t; 4023bcb09a3SJeremy Fitzhardinge 4033fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4043bcb09a3SJeremy Fitzhardinge 4053bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 4063bcb09a3SJeremy Fitzhardinge 407ea714970SJeremy Fitzhardinge if (t == NULL) { 408e08f5f5bSGautham R Shenoy char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", 409e08f5f5bSGautham R Shenoy str_governor); 410ea714970SJeremy Fitzhardinge 411ea714970SJeremy Fitzhardinge if (name) { 412ea714970SJeremy Fitzhardinge int ret; 413ea714970SJeremy Fitzhardinge 414ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 415ea714970SJeremy Fitzhardinge ret = request_module(name); 416ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 417ea714970SJeremy Fitzhardinge 418ea714970SJeremy Fitzhardinge if (ret == 0) 419ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 420ea714970SJeremy Fitzhardinge } 421ea714970SJeremy Fitzhardinge 422ea714970SJeremy Fitzhardinge kfree(name); 423ea714970SJeremy Fitzhardinge } 424ea714970SJeremy Fitzhardinge 4253bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4261da177e4SLinus Torvalds *governor = t; 4273bcb09a3SJeremy Fitzhardinge err = 0; 4281da177e4SLinus Torvalds } 4293bcb09a3SJeremy Fitzhardinge 4303bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds out: 4333bcb09a3SJeremy Fitzhardinge return err; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds /* drivers/base/cpu.c */ 4381da177e4SLinus Torvalds extern struct sysdev_class cpu_sysdev_class; 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds /** 442e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 443e08f5f5bSGautham R Shenoy * print out cpufreq information 4441da177e4SLinus Torvalds * 4451da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 4461da177e4SLinus Torvalds * "unsigned int". 4471da177e4SLinus Torvalds */ 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds #define show_one(file_name, object) \ 4501da177e4SLinus Torvalds static ssize_t show_##file_name \ 4511da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 4521da177e4SLinus Torvalds { \ 4531da177e4SLinus Torvalds return sprintf (buf, "%u\n", policy->object); \ 4541da177e4SLinus Torvalds } 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 4571da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 4581da177e4SLinus Torvalds show_one(scaling_min_freq, min); 4591da177e4SLinus Torvalds show_one(scaling_max_freq, max); 4601da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 4611da177e4SLinus Torvalds 462e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 463e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy); 4647970e08bSThomas Renninger 4651da177e4SLinus Torvalds /** 4661da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 4671da177e4SLinus Torvalds */ 4681da177e4SLinus Torvalds #define store_one(file_name, object) \ 4691da177e4SLinus Torvalds static ssize_t store_##file_name \ 4701da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 4711da177e4SLinus Torvalds { \ 4721da177e4SLinus Torvalds unsigned int ret = -EINVAL; \ 4731da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 4741da177e4SLinus Torvalds \ 4751da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 4761da177e4SLinus Torvalds if (ret) \ 4771da177e4SLinus Torvalds return -EINVAL; \ 4781da177e4SLinus Torvalds \ 4791da177e4SLinus Torvalds ret = sscanf (buf, "%u", &new_policy.object); \ 4801da177e4SLinus Torvalds if (ret != 1) \ 4811da177e4SLinus Torvalds return -EINVAL; \ 4821da177e4SLinus Torvalds \ 4837970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); \ 4847970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 4851da177e4SLinus Torvalds \ 4861da177e4SLinus Torvalds return ret ? ret : count; \ 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds 4891da177e4SLinus Torvalds store_one(scaling_min_freq,min); 4901da177e4SLinus Torvalds store_one(scaling_max_freq,max); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /** 4931da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 4941da177e4SLinus Torvalds */ 495e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 496e08f5f5bSGautham R Shenoy char *buf) 4971da177e4SLinus Torvalds { 4985a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 4991da177e4SLinus Torvalds if (!cur_freq) 5001da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 5011da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /** 5061da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5071da177e4SLinus Torvalds */ 508905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 5091da177e4SLinus Torvalds { 5101da177e4SLinus Torvalds if(policy->policy == CPUFREQ_POLICY_POWERSAVE) 5111da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5121da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5131da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5141da177e4SLinus Torvalds else if (policy->governor) 5151da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name); 5161da177e4SLinus Torvalds return -EINVAL; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /** 5211da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5221da177e4SLinus Torvalds */ 5231da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 5241da177e4SLinus Torvalds const char *buf, size_t count) 5251da177e4SLinus Torvalds { 5261da177e4SLinus Torvalds unsigned int ret = -EINVAL; 5271da177e4SLinus Torvalds char str_governor[16]; 5281da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5311da177e4SLinus Torvalds if (ret) 5321da177e4SLinus Torvalds return ret; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds ret = sscanf (buf, "%15s", str_governor); 5351da177e4SLinus Torvalds if (ret != 1) 5361da177e4SLinus Torvalds return -EINVAL; 5371da177e4SLinus Torvalds 538e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 539e08f5f5bSGautham R Shenoy &new_policy.governor)) 5401da177e4SLinus Torvalds return -EINVAL; 5411da177e4SLinus Torvalds 5427970e08bSThomas Renninger /* Do not use cpufreq_set_policy here or the user_policy.max 5437970e08bSThomas Renninger will be wrongly overridden */ 5447970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 5457970e08bSThomas Renninger 5467970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 5477970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 5487970e08bSThomas Renninger 549e08f5f5bSGautham R Shenoy if (ret) 550e08f5f5bSGautham R Shenoy return ret; 551e08f5f5bSGautham R Shenoy else 552e08f5f5bSGautham R Shenoy return count; 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds /** 5561da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 5591da177e4SLinus Torvalds { 5601da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds 5631da177e4SLinus Torvalds /** 5641da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 5651da177e4SLinus Torvalds */ 5661da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 5671da177e4SLinus Torvalds char *buf) 5681da177e4SLinus Torvalds { 5691da177e4SLinus Torvalds ssize_t i = 0; 5701da177e4SLinus Torvalds struct cpufreq_governor *t; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds if (!cpufreq_driver->target) { 5731da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 5741da177e4SLinus Torvalds goto out; 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 5781da177e4SLinus Torvalds if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2))) 5791da177e4SLinus Torvalds goto out; 5801da177e4SLinus Torvalds i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds out: 5831da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 5841da177e4SLinus Torvalds return i; 5851da177e4SLinus Torvalds } 586*e8628dd0SDarrick J. Wong 587*e8628dd0SDarrick J. Wong static ssize_t show_cpus(cpumask_t mask, char *buf) 5881da177e4SLinus Torvalds { 5891da177e4SLinus Torvalds ssize_t i = 0; 5901da177e4SLinus Torvalds unsigned int cpu; 5911da177e4SLinus Torvalds 592*e8628dd0SDarrick J. Wong for_each_cpu_mask(cpu, mask) { 5931da177e4SLinus Torvalds if (i) 5941da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 5951da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 5961da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 5971da177e4SLinus Torvalds break; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6001da177e4SLinus Torvalds return i; 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 603*e8628dd0SDarrick J. Wong /** 604*e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 605*e8628dd0SDarrick J. Wong * hw coordination is in use 606*e8628dd0SDarrick J. Wong */ 607*e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 608*e8628dd0SDarrick J. Wong { 609*e8628dd0SDarrick J. Wong if (cpus_empty(policy->related_cpus)) 610*e8628dd0SDarrick J. Wong return show_cpus(policy->cpus, buf); 611*e8628dd0SDarrick J. Wong return show_cpus(policy->related_cpus, buf); 612*e8628dd0SDarrick J. Wong } 613*e8628dd0SDarrick J. Wong 614*e8628dd0SDarrick J. Wong /** 615*e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 616*e8628dd0SDarrick J. Wong */ 617*e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 618*e8628dd0SDarrick J. Wong { 619*e8628dd0SDarrick J. Wong return show_cpus(policy->cpus, buf); 620*e8628dd0SDarrick J. Wong } 621*e8628dd0SDarrick J. Wong 6229e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 6239e76988eSVenki Pallipadi const char *buf, size_t count) 6249e76988eSVenki Pallipadi { 6259e76988eSVenki Pallipadi unsigned int freq = 0; 6269e76988eSVenki Pallipadi unsigned int ret; 6279e76988eSVenki Pallipadi 6289e76988eSVenki Pallipadi if (!policy->governor->store_setspeed) 6299e76988eSVenki Pallipadi return -EINVAL; 6309e76988eSVenki Pallipadi 6319e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 6329e76988eSVenki Pallipadi if (ret != 1) 6339e76988eSVenki Pallipadi return -EINVAL; 6349e76988eSVenki Pallipadi 6359e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 6369e76988eSVenki Pallipadi 6379e76988eSVenki Pallipadi return count; 6389e76988eSVenki Pallipadi } 6399e76988eSVenki Pallipadi 6409e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 6419e76988eSVenki Pallipadi { 6429e76988eSVenki Pallipadi if (!policy->governor->show_setspeed) 6439e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 6449e76988eSVenki Pallipadi 6459e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 6469e76988eSVenki Pallipadi } 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds #define define_one_ro(_name) \ 6491da177e4SLinus Torvalds static struct freq_attr _name = \ 6501da177e4SLinus Torvalds __ATTR(_name, 0444, show_##_name, NULL) 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds #define define_one_ro0400(_name) \ 6531da177e4SLinus Torvalds static struct freq_attr _name = \ 6541da177e4SLinus Torvalds __ATTR(_name, 0400, show_##_name, NULL) 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds #define define_one_rw(_name) \ 6571da177e4SLinus Torvalds static struct freq_attr _name = \ 6581da177e4SLinus Torvalds __ATTR(_name, 0644, show_##_name, store_##_name) 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds define_one_ro0400(cpuinfo_cur_freq); 6611da177e4SLinus Torvalds define_one_ro(cpuinfo_min_freq); 6621da177e4SLinus Torvalds define_one_ro(cpuinfo_max_freq); 6631da177e4SLinus Torvalds define_one_ro(scaling_available_governors); 6641da177e4SLinus Torvalds define_one_ro(scaling_driver); 6651da177e4SLinus Torvalds define_one_ro(scaling_cur_freq); 666*e8628dd0SDarrick J. Wong define_one_ro(related_cpus); 6671da177e4SLinus Torvalds define_one_ro(affected_cpus); 6681da177e4SLinus Torvalds define_one_rw(scaling_min_freq); 6691da177e4SLinus Torvalds define_one_rw(scaling_max_freq); 6701da177e4SLinus Torvalds define_one_rw(scaling_governor); 6719e76988eSVenki Pallipadi define_one_rw(scaling_setspeed); 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 6741da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 6751da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 6761da177e4SLinus Torvalds &scaling_min_freq.attr, 6771da177e4SLinus Torvalds &scaling_max_freq.attr, 6781da177e4SLinus Torvalds &affected_cpus.attr, 679*e8628dd0SDarrick J. Wong &related_cpus.attr, 6801da177e4SLinus Torvalds &scaling_governor.attr, 6811da177e4SLinus Torvalds &scaling_driver.attr, 6821da177e4SLinus Torvalds &scaling_available_governors.attr, 6839e76988eSVenki Pallipadi &scaling_setspeed.attr, 6841da177e4SLinus Torvalds NULL 6851da177e4SLinus Torvalds }; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds #define to_policy(k) container_of(k,struct cpufreq_policy,kobj) 6881da177e4SLinus Torvalds #define to_attr(a) container_of(a,struct freq_attr,attr) 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr ,char *buf) 6911da177e4SLinus Torvalds { 6921da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 6931da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 6940db4a8a9SDave Jones ssize_t ret = -EINVAL; 6951da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 6961da177e4SLinus Torvalds if (!policy) 6970db4a8a9SDave Jones goto no_policy; 6985a01f2e8SVenkatesh Pallipadi 6995a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_read(policy->cpu) < 0) 7000db4a8a9SDave Jones goto fail; 7015a01f2e8SVenkatesh Pallipadi 702e08f5f5bSGautham R Shenoy if (fattr->show) 703e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 704e08f5f5bSGautham R Shenoy else 705e08f5f5bSGautham R Shenoy ret = -EIO; 706e08f5f5bSGautham R Shenoy 7075a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(policy->cpu); 7080db4a8a9SDave Jones fail: 7091da177e4SLinus Torvalds cpufreq_cpu_put(policy); 7100db4a8a9SDave Jones no_policy: 7111da177e4SLinus Torvalds return ret; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 7151da177e4SLinus Torvalds const char *buf, size_t count) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7181da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 719a07530b4SDave Jones ssize_t ret = -EINVAL; 7201da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 7211da177e4SLinus Torvalds if (!policy) 722a07530b4SDave Jones goto no_policy; 7235a01f2e8SVenkatesh Pallipadi 7245a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(policy->cpu) < 0) 725a07530b4SDave Jones goto fail; 7265a01f2e8SVenkatesh Pallipadi 727e08f5f5bSGautham R Shenoy if (fattr->store) 728e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 729e08f5f5bSGautham R Shenoy else 730e08f5f5bSGautham R Shenoy ret = -EIO; 731e08f5f5bSGautham R Shenoy 7325a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 733a07530b4SDave Jones fail: 7341da177e4SLinus Torvalds cpufreq_cpu_put(policy); 735a07530b4SDave Jones no_policy: 7361da177e4SLinus Torvalds return ret; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7421da177e4SLinus Torvalds dprintk("last reference is dropped\n"); 7431da177e4SLinus Torvalds complete(&policy->kobj_unregister); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = { 7471da177e4SLinus Torvalds .show = show, 7481da177e4SLinus Torvalds .store = store, 7491da177e4SLinus Torvalds }; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 7521da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 7531da177e4SLinus Torvalds .default_attrs = default_attrs, 7541da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 7551da177e4SLinus Torvalds }; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /** 7591da177e4SLinus Torvalds * cpufreq_add_dev - add a CPU device 7601da177e4SLinus Torvalds * 7611da177e4SLinus Torvalds * Adds the cpufreq interface for a CPU device. 7621da177e4SLinus Torvalds */ 7631da177e4SLinus Torvalds static int cpufreq_add_dev(struct sys_device *sys_dev) 7641da177e4SLinus Torvalds { 7651da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 7661da177e4SLinus Torvalds int ret = 0; 7671da177e4SLinus Torvalds struct cpufreq_policy new_policy; 7681da177e4SLinus Torvalds struct cpufreq_policy *policy; 7691da177e4SLinus Torvalds struct freq_attr **drv_attr; 7708ff69732SDave Jones struct sys_device *cpu_sys_dev; 7711da177e4SLinus Torvalds unsigned long flags; 7721da177e4SLinus Torvalds unsigned int j; 7738ff69732SDave Jones #ifdef CONFIG_SMP 7748ff69732SDave Jones struct cpufreq_policy *managed_policy; 7758ff69732SDave Jones #endif 7761da177e4SLinus Torvalds 777c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 778c32b6b8eSAshok Raj return 0; 779c32b6b8eSAshok Raj 7801da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 7811da177e4SLinus Torvalds dprintk("adding CPU %u\n", cpu); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds #ifdef CONFIG_SMP 7841da177e4SLinus Torvalds /* check whether a different CPU already registered this 7851da177e4SLinus Torvalds * CPU because it is in the same boat. */ 7861da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 7871da177e4SLinus Torvalds if (unlikely(policy)) { 7888ff69732SDave Jones cpufreq_cpu_put(policy); 7891da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7901da177e4SLinus Torvalds return 0; 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds #endif 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) { 7951da177e4SLinus Torvalds ret = -EINVAL; 7961da177e4SLinus Torvalds goto module_out; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 799e98df50cSDave Jones policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 8001da177e4SLinus Torvalds if (!policy) { 8011da177e4SLinus Torvalds ret = -ENOMEM; 8021da177e4SLinus Torvalds goto nomem_out; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds policy->cpu = cpu; 8061da177e4SLinus Torvalds policy->cpus = cpumask_of_cpu(cpu); 8071da177e4SLinus Torvalds 8085a01f2e8SVenkatesh Pallipadi /* Initially set CPU itself as the policy_cpu */ 8095a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = cpu; 8105a01f2e8SVenkatesh Pallipadi lock_policy_rwsem_write(cpu); 8115a01f2e8SVenkatesh Pallipadi 8121da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 81365f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 8141da177e4SLinus Torvalds 8158122c6ceSThomas Renninger /* Set governor before ->init, so that driver could check it */ 8168122c6ceSThomas Renninger policy->governor = CPUFREQ_DEFAULT_GOVERNOR; 8171da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 8181da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 8191da177e4SLinus Torvalds */ 8201da177e4SLinus Torvalds ret = cpufreq_driver->init(policy); 8211da177e4SLinus Torvalds if (ret) { 8221da177e4SLinus Torvalds dprintk("initialization failed\n"); 8231da177e4SLinus Torvalds goto err_out; 8241da177e4SLinus Torvalds } 82522c970f3SThomas Renninger policy->user_policy.min = policy->cpuinfo.min_freq; 82622c970f3SThomas Renninger policy->user_policy.max = policy->cpuinfo.max_freq; 8271da177e4SLinus Torvalds 8288ff69732SDave Jones #ifdef CONFIG_SMP 829084f3493SThomas Renninger 830084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 831084f3493SThomas Renninger if (cpufreq_cpu_governor[cpu]){ 832084f3493SThomas Renninger policy->governor = cpufreq_cpu_governor[cpu]; 833084f3493SThomas Renninger dprintk("Restoring governor %s for cpu %d\n", 834084f3493SThomas Renninger policy->governor->name, cpu); 835084f3493SThomas Renninger } 836084f3493SThomas Renninger #endif 837084f3493SThomas Renninger 8388ff69732SDave Jones for_each_cpu_mask(j, policy->cpus) { 8398ff69732SDave Jones if (cpu == j) 8408ff69732SDave Jones continue; 8418ff69732SDave Jones 8428ff69732SDave Jones /* check for existing affected CPUs. They may not be aware 8438ff69732SDave Jones * of it due to CPU Hotplug. 8448ff69732SDave Jones */ 84545709118SDave Jones managed_policy = cpufreq_cpu_get(j); // FIXME: Where is this released? What about error paths? 8468ff69732SDave Jones if (unlikely(managed_policy)) { 8475a01f2e8SVenkatesh Pallipadi 8485a01f2e8SVenkatesh Pallipadi /* Set proper policy_cpu */ 8495a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8505a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = managed_policy->cpu; 8515a01f2e8SVenkatesh Pallipadi 8525a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(cpu) < 0) 8535a01f2e8SVenkatesh Pallipadi goto err_out_driver_exit; 8545a01f2e8SVenkatesh Pallipadi 8558ff69732SDave Jones spin_lock_irqsave(&cpufreq_driver_lock, flags); 8568ff69732SDave Jones managed_policy->cpus = policy->cpus; 8578ff69732SDave Jones cpufreq_cpu_data[cpu] = managed_policy; 8588ff69732SDave Jones spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 8598ff69732SDave Jones 8608ff69732SDave Jones dprintk("CPU already managed, adding link\n"); 8610142f9dcSAhmed S. Darwish ret = sysfs_create_link(&sys_dev->kobj, 8620142f9dcSAhmed S. Darwish &managed_policy->kobj, 8630142f9dcSAhmed S. Darwish "cpufreq"); 86445709118SDave Jones if (ret) 8650142f9dcSAhmed S. Darwish goto err_out_driver_exit; 8668ff69732SDave Jones 8678ff69732SDave Jones cpufreq_debug_enable_ratelimit(); 8688ff69732SDave Jones ret = 0; 8698ff69732SDave Jones goto err_out_driver_exit; /* call driver->exit() */ 8708ff69732SDave Jones } 8718ff69732SDave Jones } 8728ff69732SDave Jones #endif 8731da177e4SLinus Torvalds memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* prepare interface data */ 876038c5b3eSGreg Kroah-Hartman ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj, 877038c5b3eSGreg Kroah-Hartman "cpufreq"); 87845709118SDave Jones if (ret) 8798085e1f1SVenkatesh Pallipadi goto err_out_driver_exit; 88045709118SDave Jones 8811da177e4SLinus Torvalds /* set up files for this cpu device */ 8821da177e4SLinus Torvalds drv_attr = cpufreq_driver->attr; 8831da177e4SLinus Torvalds while ((drv_attr) && (*drv_attr)) { 88458a7295bSTobias Klauser ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 88545709118SDave Jones if (ret) 8860a4b2cccSThomas Renninger goto err_out_driver_exit; 8871da177e4SLinus Torvalds drv_attr++; 8881da177e4SLinus Torvalds } 8890a4b2cccSThomas Renninger if (cpufreq_driver->get) { 89058a7295bSTobias Klauser ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 89145709118SDave Jones if (ret) 8920a4b2cccSThomas Renninger goto err_out_driver_exit; 8930a4b2cccSThomas Renninger } 8940a4b2cccSThomas Renninger if (cpufreq_driver->target) { 89558a7295bSTobias Klauser ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 89645709118SDave Jones if (ret) 8970a4b2cccSThomas Renninger goto err_out_driver_exit; 8980a4b2cccSThomas Renninger } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 9015a01f2e8SVenkatesh Pallipadi for_each_cpu_mask(j, policy->cpus) { 9021da177e4SLinus Torvalds cpufreq_cpu_data[j] = policy; 9035a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, j) = policy->cpu; 9045a01f2e8SVenkatesh Pallipadi } 9051da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9068ff69732SDave Jones 9078ff69732SDave Jones /* symlink affected CPUs */ 9088ff69732SDave Jones for_each_cpu_mask(j, policy->cpus) { 9098ff69732SDave Jones if (j == cpu) 9108ff69732SDave Jones continue; 9118ff69732SDave Jones if (!cpu_online(j)) 9128ff69732SDave Jones continue; 9138ff69732SDave Jones 9141f8b2c9dSDave Jones dprintk("CPU %u already managed, adding link\n", j); 9158ff69732SDave Jones cpufreq_cpu_get(cpu); 9168ff69732SDave Jones cpu_sys_dev = get_cpu_sysdev(j); 9170142f9dcSAhmed S. Darwish ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 9188ff69732SDave Jones "cpufreq"); 91945709118SDave Jones if (ret) 9200142f9dcSAhmed S. Darwish goto err_out_unregister; 9210142f9dcSAhmed S. Darwish } 9228ff69732SDave Jones 9231da177e4SLinus Torvalds policy->governor = NULL; /* to assure that the starting sequence is 9241da177e4SLinus Torvalds * run in cpufreq_set_policy */ 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds /* set default policy */ 92722c970f3SThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 92822c970f3SThomas Renninger policy->user_policy.policy = policy->policy; 929084f3493SThomas Renninger policy->user_policy.governor = policy->governor; 93022c970f3SThomas Renninger 93122c970f3SThomas Renninger unlock_policy_rwsem_write(cpu); 93222c970f3SThomas Renninger 9331da177e4SLinus Torvalds if (ret) { 9341da177e4SLinus Torvalds dprintk("setting policy failed\n"); 9351da177e4SLinus Torvalds goto err_out_unregister; 9361da177e4SLinus Torvalds } 9371da177e4SLinus Torvalds 938038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 9391da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9401da177e4SLinus Torvalds dprintk("initialization complete\n"); 9411da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds return 0; 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds err_out_unregister: 9471da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 9481da177e4SLinus Torvalds for_each_cpu_mask(j, policy->cpus) 9491da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 9501da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9511da177e4SLinus Torvalds 952c10997f6SGreg Kroah-Hartman kobject_put(&policy->kobj); 9531da177e4SLinus Torvalds wait_for_completion(&policy->kobj_unregister); 9541da177e4SLinus Torvalds 9558085e1f1SVenkatesh Pallipadi err_out_driver_exit: 9568085e1f1SVenkatesh Pallipadi if (cpufreq_driver->exit) 9578085e1f1SVenkatesh Pallipadi cpufreq_driver->exit(policy); 9588085e1f1SVenkatesh Pallipadi 9591da177e4SLinus Torvalds err_out: 96045709118SDave Jones unlock_policy_rwsem_write(cpu); 9611da177e4SLinus Torvalds kfree(policy); 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds nomem_out: 9641da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9651da177e4SLinus Torvalds module_out: 9661da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9671da177e4SLinus Torvalds return ret; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds /** 9725a01f2e8SVenkatesh Pallipadi * __cpufreq_remove_dev - remove a CPU device 9731da177e4SLinus Torvalds * 9741da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 9755a01f2e8SVenkatesh Pallipadi * Caller should already have policy_rwsem in write mode for this CPU. 9765a01f2e8SVenkatesh Pallipadi * This routine frees the rwsem before returning. 9771da177e4SLinus Torvalds */ 9785a01f2e8SVenkatesh Pallipadi static int __cpufreq_remove_dev(struct sys_device *sys_dev) 9791da177e4SLinus Torvalds { 9801da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 9811da177e4SLinus Torvalds unsigned long flags; 9821da177e4SLinus Torvalds struct cpufreq_policy *data; 9831da177e4SLinus Torvalds #ifdef CONFIG_SMP 984e738cf6dSGrant Coady struct sys_device *cpu_sys_dev; 9851da177e4SLinus Torvalds unsigned int j; 9861da177e4SLinus Torvalds #endif 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 9891da177e4SLinus Torvalds dprintk("unregistering CPU %u\n", cpu); 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 9921da177e4SLinus Torvalds data = cpufreq_cpu_data[cpu]; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds if (!data) { 9951da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9961da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9975a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9981da177e4SLinus Torvalds return -EINVAL; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds cpufreq_cpu_data[cpu] = NULL; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds #ifdef CONFIG_SMP 10041da177e4SLinus Torvalds /* if this isn't the CPU which is the parent of the kobj, we 10051da177e4SLinus Torvalds * only need to unlink, put and exit 10061da177e4SLinus Torvalds */ 10071da177e4SLinus Torvalds if (unlikely(cpu != data->cpu)) { 10081da177e4SLinus Torvalds dprintk("removing link\n"); 10098ff69732SDave Jones cpu_clear(cpu, data->cpus); 10101da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10111da177e4SLinus Torvalds sysfs_remove_link(&sys_dev->kobj, "cpufreq"); 10121da177e4SLinus Torvalds cpufreq_cpu_put(data); 10131da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10145a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10151da177e4SLinus Torvalds return 0; 10161da177e4SLinus Torvalds } 10171da177e4SLinus Torvalds #endif 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds #ifdef CONFIG_SMP 1020084f3493SThomas Renninger 1021084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 1022084f3493SThomas Renninger cpufreq_cpu_governor[cpu] = data->governor; 1023084f3493SThomas Renninger #endif 1024084f3493SThomas Renninger 10251da177e4SLinus Torvalds /* if we have other CPUs still registered, we need to unlink them, 10261da177e4SLinus Torvalds * or else wait_for_completion below will lock up. Clean the 10271da177e4SLinus Torvalds * cpufreq_cpu_data[] while holding the lock, and remove the sysfs 10281da177e4SLinus Torvalds * links afterwards. 10291da177e4SLinus Torvalds */ 10301da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 10311da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 10321da177e4SLinus Torvalds if (j == cpu) 10331da177e4SLinus Torvalds continue; 10341da177e4SLinus Torvalds cpufreq_cpu_data[j] = NULL; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds if (unlikely(cpus_weight(data->cpus) > 1)) { 10411da177e4SLinus Torvalds for_each_cpu_mask(j, data->cpus) { 10421da177e4SLinus Torvalds if (j == cpu) 10431da177e4SLinus Torvalds continue; 10441da177e4SLinus Torvalds dprintk("removing link for cpu %u\n", j); 1045084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 1046084f3493SThomas Renninger cpufreq_cpu_governor[j] = data->governor; 1047084f3493SThomas Renninger #endif 1048d434fca7SAshok Raj cpu_sys_dev = get_cpu_sysdev(j); 1049d434fca7SAshok Raj sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); 10501da177e4SLinus Torvalds cpufreq_cpu_put(data); 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds #else 10541da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10551da177e4SLinus Torvalds #endif 10561da177e4SLinus Torvalds 10571da177e4SLinus Torvalds if (cpufreq_driver->target) 10581da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 10595a01f2e8SVenkatesh Pallipadi 10605a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds kobject_put(&data->kobj); 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds /* we need to make sure that the underlying kobj is actually 10651da177e4SLinus Torvalds * not referenced anymore by anybody before we proceed with 10661da177e4SLinus Torvalds * unloading. 10671da177e4SLinus Torvalds */ 10681da177e4SLinus Torvalds dprintk("waiting for dropping of refcount\n"); 10691da177e4SLinus Torvalds wait_for_completion(&data->kobj_unregister); 10701da177e4SLinus Torvalds dprintk("wait complete\n"); 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds if (cpufreq_driver->exit) 10731da177e4SLinus Torvalds cpufreq_driver->exit(data); 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds kfree(data); 10761da177e4SLinus Torvalds 10771da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10781da177e4SLinus Torvalds return 0; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds 10825a01f2e8SVenkatesh Pallipadi static int cpufreq_remove_dev(struct sys_device *sys_dev) 10835a01f2e8SVenkatesh Pallipadi { 10845a01f2e8SVenkatesh Pallipadi unsigned int cpu = sys_dev->id; 10855a01f2e8SVenkatesh Pallipadi int retval; 1086ec28297aSVenki Pallipadi 1087ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1088ec28297aSVenki Pallipadi return 0; 1089ec28297aSVenki Pallipadi 10905a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 10915a01f2e8SVenkatesh Pallipadi BUG(); 10925a01f2e8SVenkatesh Pallipadi 10935a01f2e8SVenkatesh Pallipadi retval = __cpufreq_remove_dev(sys_dev); 10945a01f2e8SVenkatesh Pallipadi return retval; 10955a01f2e8SVenkatesh Pallipadi } 10965a01f2e8SVenkatesh Pallipadi 10975a01f2e8SVenkatesh Pallipadi 109865f27f38SDavid Howells static void handle_update(struct work_struct *work) 10991da177e4SLinus Torvalds { 110065f27f38SDavid Howells struct cpufreq_policy *policy = 110165f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 110265f27f38SDavid Howells unsigned int cpu = policy->cpu; 11031da177e4SLinus Torvalds dprintk("handle_update for cpu %u called\n", cpu); 11041da177e4SLinus Torvalds cpufreq_update_policy(cpu); 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds /** 11081da177e4SLinus Torvalds * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 11091da177e4SLinus Torvalds * @cpu: cpu number 11101da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 11111da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 11121da177e4SLinus Torvalds * 11131da177e4SLinus Torvalds * We adjust to current frequency first, and need to clean up later. So either call 11141da177e4SLinus Torvalds * to cpufreq_update_policy() or schedule handle_update()). 11151da177e4SLinus Torvalds */ 1116e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1117e08f5f5bSGautham R Shenoy unsigned int new_freq) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds struct cpufreq_freqs freqs; 11201da177e4SLinus Torvalds 1121b10eec22SJan Beulich dprintk("Warning: CPU frequency out of sync: cpufreq and timing " 11221da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 11231da177e4SLinus Torvalds 11241da177e4SLinus Torvalds freqs.cpu = cpu; 11251da177e4SLinus Torvalds freqs.old = old_freq; 11261da177e4SLinus Torvalds freqs.new = new_freq; 11271da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 11281da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 11291da177e4SLinus Torvalds } 11301da177e4SLinus Torvalds 11311da177e4SLinus Torvalds 11321da177e4SLinus Torvalds /** 11334ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 113495235ca2SVenkatesh Pallipadi * @cpu: CPU number 113595235ca2SVenkatesh Pallipadi * 113695235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 113795235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 113895235ca2SVenkatesh Pallipadi */ 113995235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 114095235ca2SVenkatesh Pallipadi { 114195235ca2SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1142e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 114395235ca2SVenkatesh Pallipadi 114495235ca2SVenkatesh Pallipadi if (policy) { 1145e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 114695235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 114795235ca2SVenkatesh Pallipadi } 114895235ca2SVenkatesh Pallipadi 11494d34a67dSDave Jones return ret_freq; 115095235ca2SVenkatesh Pallipadi } 115195235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 115295235ca2SVenkatesh Pallipadi 115395235ca2SVenkatesh Pallipadi 11545a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 11551da177e4SLinus Torvalds { 11565a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_data[cpu]; 1157e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 11581da177e4SLinus Torvalds 11591da177e4SLinus Torvalds if (!cpufreq_driver->get) 11604d34a67dSDave Jones return ret_freq; 11611da177e4SLinus Torvalds 1162e08f5f5bSGautham R Shenoy ret_freq = cpufreq_driver->get(cpu); 11631da177e4SLinus Torvalds 1164e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 1165e08f5f5bSGautham R Shenoy !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1166e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1167e08f5f5bSGautham R Shenoy saved value exists */ 1168e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1169e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 11701da177e4SLinus Torvalds schedule_work(&policy->update); 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 11744d34a67dSDave Jones return ret_freq; 11755a01f2e8SVenkatesh Pallipadi } 11761da177e4SLinus Torvalds 11775a01f2e8SVenkatesh Pallipadi /** 11785a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 11795a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 11805a01f2e8SVenkatesh Pallipadi * 11815a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 11825a01f2e8SVenkatesh Pallipadi */ 11835a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 11845a01f2e8SVenkatesh Pallipadi { 11855a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 11865a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 11875a01f2e8SVenkatesh Pallipadi 11885a01f2e8SVenkatesh Pallipadi if (!policy) 11895a01f2e8SVenkatesh Pallipadi goto out; 11905a01f2e8SVenkatesh Pallipadi 11915a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 11925a01f2e8SVenkatesh Pallipadi goto out_policy; 11935a01f2e8SVenkatesh Pallipadi 11945a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 11955a01f2e8SVenkatesh Pallipadi 11965a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 11975a01f2e8SVenkatesh Pallipadi 11985a01f2e8SVenkatesh Pallipadi out_policy: 11991da177e4SLinus Torvalds cpufreq_cpu_put(policy); 12005a01f2e8SVenkatesh Pallipadi out: 12014d34a67dSDave Jones return ret_freq; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 12041da177e4SLinus Torvalds 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds /** 120742d4dc3fSBenjamin Herrenschmidt * cpufreq_suspend - let the low level driver prepare for suspend 120842d4dc3fSBenjamin Herrenschmidt */ 120942d4dc3fSBenjamin Herrenschmidt 1210e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg) 121142d4dc3fSBenjamin Herrenschmidt { 121242d4dc3fSBenjamin Herrenschmidt int cpu = sysdev->id; 1213e08f5f5bSGautham R Shenoy int ret = 0; 121442d4dc3fSBenjamin Herrenschmidt unsigned int cur_freq = 0; 121542d4dc3fSBenjamin Herrenschmidt struct cpufreq_policy *cpu_policy; 121642d4dc3fSBenjamin Herrenschmidt 12170e37b159SDave Jones dprintk("suspending cpu %u\n", cpu); 121842d4dc3fSBenjamin Herrenschmidt 121942d4dc3fSBenjamin Herrenschmidt if (!cpu_online(cpu)) 122042d4dc3fSBenjamin Herrenschmidt return 0; 122142d4dc3fSBenjamin Herrenschmidt 122242d4dc3fSBenjamin Herrenschmidt /* we may be lax here as interrupts are off. Nonetheless 122342d4dc3fSBenjamin Herrenschmidt * we need to grab the correct cpu policy, as to check 122442d4dc3fSBenjamin Herrenschmidt * whether we really run on this CPU. 122542d4dc3fSBenjamin Herrenschmidt */ 122642d4dc3fSBenjamin Herrenschmidt 122742d4dc3fSBenjamin Herrenschmidt cpu_policy = cpufreq_cpu_get(cpu); 122842d4dc3fSBenjamin Herrenschmidt if (!cpu_policy) 122942d4dc3fSBenjamin Herrenschmidt return -EINVAL; 123042d4dc3fSBenjamin Herrenschmidt 123142d4dc3fSBenjamin Herrenschmidt /* only handle each CPU group once */ 1232c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1233c9060494SDave Jones goto out; 123442d4dc3fSBenjamin Herrenschmidt 123542d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->suspend) { 1236e00d9967SBernard Blackham ret = cpufreq_driver->suspend(cpu_policy, pmsg); 123742d4dc3fSBenjamin Herrenschmidt if (ret) { 123842d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 123942d4dc3fSBenjamin Herrenschmidt "step on CPU %u\n", cpu_policy->cpu); 1240c9060494SDave Jones goto out; 124142d4dc3fSBenjamin Herrenschmidt } 124242d4dc3fSBenjamin Herrenschmidt } 124342d4dc3fSBenjamin Herrenschmidt 124442d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) 124542d4dc3fSBenjamin Herrenschmidt goto out; 124642d4dc3fSBenjamin Herrenschmidt 124742d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->get) 124842d4dc3fSBenjamin Herrenschmidt cur_freq = cpufreq_driver->get(cpu_policy->cpu); 124942d4dc3fSBenjamin Herrenschmidt 125042d4dc3fSBenjamin Herrenschmidt if (!cur_freq || !cpu_policy->cur) { 125142d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed to assert current " 125242d4dc3fSBenjamin Herrenschmidt "frequency is what timing core thinks it is.\n"); 125342d4dc3fSBenjamin Herrenschmidt goto out; 125442d4dc3fSBenjamin Herrenschmidt } 125542d4dc3fSBenjamin Herrenschmidt 125642d4dc3fSBenjamin Herrenschmidt if (unlikely(cur_freq != cpu_policy->cur)) { 125742d4dc3fSBenjamin Herrenschmidt struct cpufreq_freqs freqs; 125842d4dc3fSBenjamin Herrenschmidt 125942d4dc3fSBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1260b10eec22SJan Beulich dprintk("Warning: CPU frequency is %u, " 126142d4dc3fSBenjamin Herrenschmidt "cpufreq assumed %u kHz.\n", 126242d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 126342d4dc3fSBenjamin Herrenschmidt 126442d4dc3fSBenjamin Herrenschmidt freqs.cpu = cpu; 126542d4dc3fSBenjamin Herrenschmidt freqs.old = cpu_policy->cur; 126642d4dc3fSBenjamin Herrenschmidt freqs.new = cur_freq; 126742d4dc3fSBenjamin Herrenschmidt 1268b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 126942d4dc3fSBenjamin Herrenschmidt CPUFREQ_SUSPENDCHANGE, &freqs); 127042d4dc3fSBenjamin Herrenschmidt adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); 127142d4dc3fSBenjamin Herrenschmidt 127242d4dc3fSBenjamin Herrenschmidt cpu_policy->cur = cur_freq; 127342d4dc3fSBenjamin Herrenschmidt } 127442d4dc3fSBenjamin Herrenschmidt 127542d4dc3fSBenjamin Herrenschmidt out: 127642d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 1277c9060494SDave Jones return ret; 127842d4dc3fSBenjamin Herrenschmidt } 127942d4dc3fSBenjamin Herrenschmidt 128042d4dc3fSBenjamin Herrenschmidt /** 12811da177e4SLinus Torvalds * cpufreq_resume - restore proper CPU frequency handling after resume 12821da177e4SLinus Torvalds * 12831da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 12841da177e4SLinus Torvalds * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync 128542d4dc3fSBenjamin Herrenschmidt * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are 128642d4dc3fSBenjamin Herrenschmidt * restored. 12871da177e4SLinus Torvalds */ 12881da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device *sysdev) 12891da177e4SLinus Torvalds { 12901da177e4SLinus Torvalds int cpu = sysdev->id; 1291e08f5f5bSGautham R Shenoy int ret = 0; 12921da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds dprintk("resuming cpu %u\n", cpu); 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds if (!cpu_online(cpu)) 12971da177e4SLinus Torvalds return 0; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* we may be lax here as interrupts are off. Nonetheless 13001da177e4SLinus Torvalds * we need to grab the correct cpu policy, as to check 13011da177e4SLinus Torvalds * whether we really run on this CPU. 13021da177e4SLinus Torvalds */ 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 13051da177e4SLinus Torvalds if (!cpu_policy) 13061da177e4SLinus Torvalds return -EINVAL; 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds /* only handle each CPU group once */ 1309c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1310c9060494SDave Jones goto fail; 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds if (cpufreq_driver->resume) { 13131da177e4SLinus Torvalds ret = cpufreq_driver->resume(cpu_policy); 13141da177e4SLinus Torvalds if (ret) { 13151da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 13161da177e4SLinus Torvalds "step on CPU %u\n", cpu_policy->cpu); 1317c9060494SDave Jones goto fail; 13181da177e4SLinus Torvalds } 13191da177e4SLinus Torvalds } 13201da177e4SLinus Torvalds 13211da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 13221da177e4SLinus Torvalds unsigned int cur_freq = 0; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds if (cpufreq_driver->get) 13251da177e4SLinus Torvalds cur_freq = cpufreq_driver->get(cpu_policy->cpu); 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds if (!cur_freq || !cpu_policy->cur) { 132842d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: resume failed to assert " 132942d4dc3fSBenjamin Herrenschmidt "current frequency is what timing core " 133042d4dc3fSBenjamin Herrenschmidt "thinks it is.\n"); 13311da177e4SLinus Torvalds goto out; 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds if (unlikely(cur_freq != cpu_policy->cur)) { 13351da177e4SLinus Torvalds struct cpufreq_freqs freqs; 13361da177e4SLinus Torvalds 1337ac09f698SBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1338b10eec22SJan Beulich dprintk("Warning: CPU frequency " 1339ac09f698SBenjamin Herrenschmidt "is %u, cpufreq assumed %u kHz.\n", 134042d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds freqs.cpu = cpu; 13431da177e4SLinus Torvalds freqs.old = cpu_policy->cur; 13441da177e4SLinus Torvalds freqs.new = cur_freq; 13451da177e4SLinus Torvalds 1346b4dfdbb3SAlan Stern srcu_notifier_call_chain( 1347e041c683SAlan Stern &cpufreq_transition_notifier_list, 134842d4dc3fSBenjamin Herrenschmidt CPUFREQ_RESUMECHANGE, &freqs); 13491da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds cpu_policy->cur = cur_freq; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds out: 13561da177e4SLinus Torvalds schedule_work(&cpu_policy->update); 1357c9060494SDave Jones fail: 13581da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 13591da177e4SLinus Torvalds return ret; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = { 13631da177e4SLinus Torvalds .add = cpufreq_add_dev, 13641da177e4SLinus Torvalds .remove = cpufreq_remove_dev, 136542d4dc3fSBenjamin Herrenschmidt .suspend = cpufreq_suspend, 13661da177e4SLinus Torvalds .resume = cpufreq_resume, 13671da177e4SLinus Torvalds }; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds /********************************************************************* 13711da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 13721da177e4SLinus Torvalds *********************************************************************/ 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /** 13751da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 13761da177e4SLinus Torvalds * @nb: notifier function to register 13771da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 13781da177e4SLinus Torvalds * 13791da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 13801da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 13811da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 13821da177e4SLinus Torvalds * changes in cpufreq policy. 13831da177e4SLinus Torvalds * 13841da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1385e041c683SAlan Stern * blocking_notifier_chain_register. 13861da177e4SLinus Torvalds */ 13871da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 13881da177e4SLinus Torvalds { 13891da177e4SLinus Torvalds int ret; 13901da177e4SLinus Torvalds 139174212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 139274212ca4SCesar Eduardo Barros 13931da177e4SLinus Torvalds switch (list) { 13941da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1395b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1396e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 13971da177e4SLinus Torvalds break; 13981da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1399e041c683SAlan Stern ret = blocking_notifier_chain_register( 1400e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 14011da177e4SLinus Torvalds break; 14021da177e4SLinus Torvalds default: 14031da177e4SLinus Torvalds ret = -EINVAL; 14041da177e4SLinus Torvalds } 14051da177e4SLinus Torvalds 14061da177e4SLinus Torvalds return ret; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds /** 14121da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 14131da177e4SLinus Torvalds * @nb: notifier block to be unregistered 14141da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 14151da177e4SLinus Torvalds * 14161da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 14171da177e4SLinus Torvalds * 14181da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1419e041c683SAlan Stern * blocking_notifier_chain_unregister. 14201da177e4SLinus Torvalds */ 14211da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 14221da177e4SLinus Torvalds { 14231da177e4SLinus Torvalds int ret; 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds switch (list) { 14261da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1427b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1428e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 14291da177e4SLinus Torvalds break; 14301da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1431e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1432e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 14331da177e4SLinus Torvalds break; 14341da177e4SLinus Torvalds default: 14351da177e4SLinus Torvalds ret = -EINVAL; 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds return ret; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 14411da177e4SLinus Torvalds 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds /********************************************************************* 14441da177e4SLinus Torvalds * GOVERNORS * 14451da177e4SLinus Torvalds *********************************************************************/ 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds 14481da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 14491da177e4SLinus Torvalds unsigned int target_freq, 14501da177e4SLinus Torvalds unsigned int relation) 14511da177e4SLinus Torvalds { 14521da177e4SLinus Torvalds int retval = -EINVAL; 1453c32b6b8eSAshok Raj 14541da177e4SLinus Torvalds dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 14551da177e4SLinus Torvalds target_freq, relation); 14561da177e4SLinus Torvalds if (cpu_online(policy->cpu) && cpufreq_driver->target) 14571da177e4SLinus Torvalds retval = cpufreq_driver->target(policy, target_freq, relation); 145890d45d17SAshok Raj 14591da177e4SLinus Torvalds return retval; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 14641da177e4SLinus Torvalds unsigned int target_freq, 14651da177e4SLinus Torvalds unsigned int relation) 14661da177e4SLinus Torvalds { 1467cc993cabSDave Jones int ret; 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 14701da177e4SLinus Torvalds if (!policy) 14711da177e4SLinus Torvalds return -EINVAL; 14721da177e4SLinus Torvalds 14735a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 14745a01f2e8SVenkatesh Pallipadi return -EINVAL; 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 14771da177e4SLinus Torvalds 14785a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 14791da177e4SLinus Torvalds 14801da177e4SLinus Torvalds cpufreq_cpu_put(policy); 14811da177e4SLinus Torvalds return ret; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 14841da177e4SLinus Torvalds 14855a01f2e8SVenkatesh Pallipadi int __cpufreq_driver_getavg(struct cpufreq_policy *policy) 1486dfde5d62SVenkatesh Pallipadi { 1487dfde5d62SVenkatesh Pallipadi int ret = 0; 1488dfde5d62SVenkatesh Pallipadi 1489dfde5d62SVenkatesh Pallipadi policy = cpufreq_cpu_get(policy->cpu); 1490dfde5d62SVenkatesh Pallipadi if (!policy) 1491dfde5d62SVenkatesh Pallipadi return -EINVAL; 1492dfde5d62SVenkatesh Pallipadi 1493dfde5d62SVenkatesh Pallipadi if (cpu_online(policy->cpu) && cpufreq_driver->getavg) 1494dfde5d62SVenkatesh Pallipadi ret = cpufreq_driver->getavg(policy->cpu); 1495dfde5d62SVenkatesh Pallipadi 1496dfde5d62SVenkatesh Pallipadi cpufreq_cpu_put(policy); 1497dfde5d62SVenkatesh Pallipadi return ret; 1498dfde5d62SVenkatesh Pallipadi } 14995a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg); 1500dfde5d62SVenkatesh Pallipadi 1501153d7f3fSArjan van de Ven /* 1502153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1503153d7f3fSArjan van de Ven */ 15041da177e4SLinus Torvalds 1505e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1506e08f5f5bSGautham R Shenoy unsigned int event) 15071da177e4SLinus Torvalds { 1508cc993cabSDave Jones int ret; 15096afde10cSThomas Renninger 15106afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 15116afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 15126afde10cSThomas Renninger That this is the case is already ensured in Kconfig 15136afde10cSThomas Renninger */ 15146afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 15156afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 15166afde10cSThomas Renninger #else 15176afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 15186afde10cSThomas Renninger #endif 15191c256245SThomas Renninger 15201c256245SThomas Renninger if (policy->governor->max_transition_latency && 15211c256245SThomas Renninger policy->cpuinfo.transition_latency > 15221c256245SThomas Renninger policy->governor->max_transition_latency) { 15236afde10cSThomas Renninger if (!gov) 15246afde10cSThomas Renninger return -EINVAL; 15256afde10cSThomas Renninger else { 15261c256245SThomas Renninger printk(KERN_WARNING "%s governor failed, too long" 15271c256245SThomas Renninger " transition latency of HW, fallback" 15281c256245SThomas Renninger " to %s governor\n", 15291c256245SThomas Renninger policy->governor->name, 15301c256245SThomas Renninger gov->name); 15311c256245SThomas Renninger policy->governor = gov; 15321c256245SThomas Renninger } 15336afde10cSThomas Renninger } 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 15361da177e4SLinus Torvalds return -EINVAL; 15371da177e4SLinus Torvalds 1538e08f5f5bSGautham R Shenoy dprintk("__cpufreq_governor for CPU %u, event %u\n", 1539e08f5f5bSGautham R Shenoy policy->cpu, event); 15401da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 15411da177e4SLinus Torvalds 1542e08f5f5bSGautham R Shenoy /* we keep one module reference alive for 1543e08f5f5bSGautham R Shenoy each CPU governed by this CPU */ 15441da177e4SLinus Torvalds if ((event != CPUFREQ_GOV_START) || ret) 15451da177e4SLinus Torvalds module_put(policy->governor->owner); 15461da177e4SLinus Torvalds if ((event == CPUFREQ_GOV_STOP) && !ret) 15471da177e4SLinus Torvalds module_put(policy->governor->owner); 15481da177e4SLinus Torvalds 15491da177e4SLinus Torvalds return ret; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 15541da177e4SLinus Torvalds { 15553bcb09a3SJeremy Fitzhardinge int err; 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds if (!governor) 15581da177e4SLinus Torvalds return -EINVAL; 15591da177e4SLinus Torvalds 15603fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 15611da177e4SLinus Torvalds 15623bcb09a3SJeremy Fitzhardinge err = -EBUSY; 15633bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 15643bcb09a3SJeremy Fitzhardinge err = 0; 15651da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 15663bcb09a3SJeremy Fitzhardinge } 15671da177e4SLinus Torvalds 15683fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 15693bcb09a3SJeremy Fitzhardinge return err; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds 15741da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 15751da177e4SLinus Torvalds { 15761da177e4SLinus Torvalds if (!governor) 15771da177e4SLinus Torvalds return; 15781da177e4SLinus Torvalds 15793fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 15801da177e4SLinus Torvalds list_del(&governor->governor_list); 15813fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 15821da177e4SLinus Torvalds return; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds /********************************************************************* 15891da177e4SLinus Torvalds * POLICY INTERFACE * 15901da177e4SLinus Torvalds *********************************************************************/ 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds /** 15931da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 15941da177e4SLinus Torvalds * @policy: struct cpufreq_policy into which the current cpufreq_policy is written 15951da177e4SLinus Torvalds * 15961da177e4SLinus Torvalds * Reads the current cpufreq policy. 15971da177e4SLinus Torvalds */ 15981da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 15991da177e4SLinus Torvalds { 16001da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 16011da177e4SLinus Torvalds if (!policy) 16021da177e4SLinus Torvalds return -EINVAL; 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 16051da177e4SLinus Torvalds if (!cpu_policy) 16061da177e4SLinus Torvalds return -EINVAL; 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 16111da177e4SLinus Torvalds return 0; 16121da177e4SLinus Torvalds } 16131da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds 1616153d7f3fSArjan van de Ven /* 1617e08f5f5bSGautham R Shenoy * data : current policy. 1618e08f5f5bSGautham R Shenoy * policy : policy to be set. 1619153d7f3fSArjan van de Ven */ 1620e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 1621e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy) 16221da177e4SLinus Torvalds { 16231da177e4SLinus Torvalds int ret = 0; 16241da177e4SLinus Torvalds 16251da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 16261da177e4SLinus Torvalds dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 16271da177e4SLinus Torvalds policy->min, policy->max); 16281da177e4SLinus Torvalds 1629e08f5f5bSGautham R Shenoy memcpy(&policy->cpuinfo, &data->cpuinfo, 1630e08f5f5bSGautham R Shenoy sizeof(struct cpufreq_cpuinfo)); 16311da177e4SLinus Torvalds 163253391fa2SYi Yang if (policy->min > data->max || policy->max < data->min) { 16339c9a43edSMattia Dongili ret = -EINVAL; 16349c9a43edSMattia Dongili goto error_out; 16359c9a43edSMattia Dongili } 16369c9a43edSMattia Dongili 16371da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 16381da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 16391da177e4SLinus Torvalds if (ret) 16401da177e4SLinus Torvalds goto error_out; 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 1643e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1644e041c683SAlan Stern CPUFREQ_ADJUST, policy); 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 1647e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1648e041c683SAlan Stern CPUFREQ_INCOMPATIBLE, policy); 16491da177e4SLinus Torvalds 16501da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit, 16511da177e4SLinus Torvalds which might be different to the first one */ 16521da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 1653e041c683SAlan Stern if (ret) 16541da177e4SLinus Torvalds goto error_out; 16551da177e4SLinus Torvalds 16561da177e4SLinus Torvalds /* notification of the new policy */ 1657e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1658e041c683SAlan Stern CPUFREQ_NOTIFY, policy); 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds data->min = policy->min; 16611da177e4SLinus Torvalds data->max = policy->max; 16621da177e4SLinus Torvalds 1663e08f5f5bSGautham R Shenoy dprintk("new min and max freqs are %u - %u kHz\n", 1664e08f5f5bSGautham R Shenoy data->min, data->max); 16651da177e4SLinus Torvalds 16661da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 16671da177e4SLinus Torvalds data->policy = policy->policy; 16681da177e4SLinus Torvalds dprintk("setting range\n"); 16691da177e4SLinus Torvalds ret = cpufreq_driver->setpolicy(policy); 16701da177e4SLinus Torvalds } else { 16711da177e4SLinus Torvalds if (policy->governor != data->governor) { 16721da177e4SLinus Torvalds /* save old, working values */ 16731da177e4SLinus Torvalds struct cpufreq_governor *old_gov = data->governor; 16741da177e4SLinus Torvalds 16751da177e4SLinus Torvalds dprintk("governor switch\n"); 16761da177e4SLinus Torvalds 16771da177e4SLinus Torvalds /* end old governor */ 16781da177e4SLinus Torvalds if (data->governor) 16791da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 16801da177e4SLinus Torvalds 16811da177e4SLinus Torvalds /* start new governor */ 16821da177e4SLinus Torvalds data->governor = policy->governor; 16831da177e4SLinus Torvalds if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 16841da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 1685e08f5f5bSGautham R Shenoy dprintk("starting governor %s failed\n", 1686e08f5f5bSGautham R Shenoy data->governor->name); 16871da177e4SLinus Torvalds if (old_gov) { 16881da177e4SLinus Torvalds data->governor = old_gov; 1689e08f5f5bSGautham R Shenoy __cpufreq_governor(data, 1690e08f5f5bSGautham R Shenoy CPUFREQ_GOV_START); 16911da177e4SLinus Torvalds } 16921da177e4SLinus Torvalds ret = -EINVAL; 16931da177e4SLinus Torvalds goto error_out; 16941da177e4SLinus Torvalds } 16951da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds dprintk("governor: change or update limits\n"); 16981da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 16991da177e4SLinus Torvalds } 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds error_out: 17021da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 17031da177e4SLinus Torvalds return ret; 17041da177e4SLinus Torvalds } 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds /** 17071da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 17081da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 17091da177e4SLinus Torvalds * 17101da177e4SLinus Torvalds * Usefull for policy notifiers which have different necessities 17111da177e4SLinus Torvalds * at different times. 17121da177e4SLinus Torvalds */ 17131da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 17141da177e4SLinus Torvalds { 17151da177e4SLinus Torvalds struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 17161da177e4SLinus Torvalds struct cpufreq_policy policy; 17171da177e4SLinus Torvalds int ret = 0; 17181da177e4SLinus Torvalds 17191da177e4SLinus Torvalds if (!data) 17201da177e4SLinus Torvalds return -ENODEV; 17211da177e4SLinus Torvalds 17225a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 17235a01f2e8SVenkatesh Pallipadi return -EINVAL; 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds dprintk("updating policy for CPU %u\n", cpu); 17267d5e350fSDave Jones memcpy(&policy, data, sizeof(struct cpufreq_policy)); 17271da177e4SLinus Torvalds policy.min = data->user_policy.min; 17281da177e4SLinus Torvalds policy.max = data->user_policy.max; 17291da177e4SLinus Torvalds policy.policy = data->user_policy.policy; 17301da177e4SLinus Torvalds policy.governor = data->user_policy.governor; 17311da177e4SLinus Torvalds 17320961dd0dSThomas Renninger /* BIOS might change freq behind our back 17330961dd0dSThomas Renninger -> ask driver for current freq and notify governors about a change */ 17340961dd0dSThomas Renninger if (cpufreq_driver->get) { 17350961dd0dSThomas Renninger policy.cur = cpufreq_driver->get(cpu); 1736a85f7bd3SThomas Renninger if (!data->cur) { 1737a85f7bd3SThomas Renninger dprintk("Driver did not initialize current freq"); 1738a85f7bd3SThomas Renninger data->cur = policy.cur; 1739a85f7bd3SThomas Renninger } else { 17400961dd0dSThomas Renninger if (data->cur != policy.cur) 1741e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, data->cur, 1742e08f5f5bSGautham R Shenoy policy.cur); 17430961dd0dSThomas Renninger } 1744a85f7bd3SThomas Renninger } 17450961dd0dSThomas Renninger 17461da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, &policy); 17471da177e4SLinus Torvalds 17485a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 17495a01f2e8SVenkatesh Pallipadi 17501da177e4SLinus Torvalds cpufreq_cpu_put(data); 17511da177e4SLinus Torvalds return ret; 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 17541da177e4SLinus Torvalds 1755dd184a01SSatyam Sharma static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, 1756c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1757c32b6b8eSAshok Raj { 1758c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 1759c32b6b8eSAshok Raj struct sys_device *sys_dev; 1760c32b6b8eSAshok Raj 1761c32b6b8eSAshok Raj sys_dev = get_cpu_sysdev(cpu); 1762c32b6b8eSAshok Raj if (sys_dev) { 1763c32b6b8eSAshok Raj switch (action) { 1764c32b6b8eSAshok Raj case CPU_ONLINE: 17658bb78442SRafael J. Wysocki case CPU_ONLINE_FROZEN: 1766c32b6b8eSAshok Raj cpufreq_add_dev(sys_dev); 1767c32b6b8eSAshok Raj break; 1768c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 17698bb78442SRafael J. Wysocki case CPU_DOWN_PREPARE_FROZEN: 17705a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 17715a01f2e8SVenkatesh Pallipadi BUG(); 17725a01f2e8SVenkatesh Pallipadi 17735a01f2e8SVenkatesh Pallipadi __cpufreq_remove_dev(sys_dev); 1774c32b6b8eSAshok Raj break; 17755a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 17768bb78442SRafael J. Wysocki case CPU_DOWN_FAILED_FROZEN: 17775a01f2e8SVenkatesh Pallipadi cpufreq_add_dev(sys_dev); 1778c32b6b8eSAshok Raj break; 1779c32b6b8eSAshok Raj } 1780c32b6b8eSAshok Raj } 1781c32b6b8eSAshok Raj return NOTIFY_OK; 1782c32b6b8eSAshok Raj } 1783c32b6b8eSAshok Raj 1784f6ebef30SSam Ravnborg static struct notifier_block __refdata cpufreq_cpu_notifier = 1785c32b6b8eSAshok Raj { 1786c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 1787c32b6b8eSAshok Raj }; 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /********************************************************************* 17901da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 17911da177e4SLinus Torvalds *********************************************************************/ 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds /** 17941da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 17951da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 17961da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 17971da177e4SLinus Torvalds * 17981da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 17991da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 18001da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 18011da177e4SLinus Torvalds * 18021da177e4SLinus Torvalds */ 1803221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 18041da177e4SLinus Torvalds { 18051da177e4SLinus Torvalds unsigned long flags; 18061da177e4SLinus Torvalds int ret; 18071da177e4SLinus Torvalds 18081da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 18091da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 18101da177e4SLinus Torvalds return -EINVAL; 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds dprintk("trying to register driver %s\n", driver_data->name); 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds if (driver_data->setpolicy) 18151da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18181da177e4SLinus Torvalds if (cpufreq_driver) { 18191da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18201da177e4SLinus Torvalds return -EBUSY; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds cpufreq_driver = driver_data; 18231da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); 18261da177e4SLinus Torvalds 18271da177e4SLinus Torvalds if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 18281da177e4SLinus Torvalds int i; 18291da177e4SLinus Torvalds ret = -ENODEV; 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /* check for at least one working CPU */ 18321da177e4SLinus Torvalds for (i=0; i<NR_CPUS; i++) 18331da177e4SLinus Torvalds if (cpufreq_cpu_data[i]) 18341da177e4SLinus Torvalds ret = 0; 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 18371da177e4SLinus Torvalds if (ret) { 1838e08f5f5bSGautham R Shenoy dprintk("no CPU initialized for driver %s\n", 1839e08f5f5bSGautham R Shenoy driver_data->name); 1840e08f5f5bSGautham R Shenoy sysdev_driver_unregister(&cpu_sysdev_class, 1841e08f5f5bSGautham R Shenoy &cpufreq_sysdev_driver); 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18441da177e4SLinus Torvalds cpufreq_driver = NULL; 18451da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18461da177e4SLinus Torvalds } 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds if (!ret) { 185065edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 18511da177e4SLinus Torvalds dprintk("driver %s up and running\n", driver_data->name); 18521da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18531da177e4SLinus Torvalds } 18541da177e4SLinus Torvalds 18554d34a67dSDave Jones return ret; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds /** 18611da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 18621da177e4SLinus Torvalds * 18631da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 18641da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 18651da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 18661da177e4SLinus Torvalds * currently not initialised. 18671da177e4SLinus Torvalds */ 1868221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 18691da177e4SLinus Torvalds { 18701da177e4SLinus Torvalds unsigned long flags; 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds if (!cpufreq_driver || (driver != cpufreq_driver)) { 18751da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18761da177e4SLinus Torvalds return -EINVAL; 18771da177e4SLinus Torvalds } 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds dprintk("unregistering driver %s\n", driver->name); 18801da177e4SLinus Torvalds 18811da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 188265edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18851da177e4SLinus Torvalds cpufreq_driver = NULL; 18861da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds return 0; 18891da177e4SLinus Torvalds } 18901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 18915a01f2e8SVenkatesh Pallipadi 18925a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 18935a01f2e8SVenkatesh Pallipadi { 18945a01f2e8SVenkatesh Pallipadi int cpu; 18955a01f2e8SVenkatesh Pallipadi 18965a01f2e8SVenkatesh Pallipadi for_each_possible_cpu(cpu) { 18975a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = -1; 18985a01f2e8SVenkatesh Pallipadi init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 18995a01f2e8SVenkatesh Pallipadi } 19005a01f2e8SVenkatesh Pallipadi return 0; 19015a01f2e8SVenkatesh Pallipadi } 19025a01f2e8SVenkatesh Pallipadi 19035a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 1904