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; 417a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 42084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 43084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */ 447a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_governor *, cpufreq_cpu_governor); 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 */ 10729464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy, 10829464f28SDave Jones unsigned int event); 1095a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu); 11065f27f38SDavid Howells static void handle_update(struct work_struct *work); 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /** 1131da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 1141da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 1151da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 1161da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 1171da177e4SLinus Torvalds * The mutex locks both lists. 1181da177e4SLinus Torvalds */ 119e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 120b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 1211da177e4SLinus Torvalds 12274212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 123b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 124b4dfdbb3SAlan Stern { 125b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 12674212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 127b4dfdbb3SAlan Stern return 0; 128b4dfdbb3SAlan Stern } 129b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 1323fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 1351da177e4SLinus Torvalds { 1361da177e4SLinus Torvalds struct cpufreq_policy *data; 1371da177e4SLinus Torvalds unsigned long flags; 1381da177e4SLinus Torvalds 1397a6aedfaSMike Travis if (cpu >= nr_cpu_ids) 1401da177e4SLinus Torvalds goto err_out; 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* get the cpufreq driver */ 1431da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds if (!cpufreq_driver) 1461da177e4SLinus Torvalds goto err_out_unlock; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) 1491da177e4SLinus Torvalds goto err_out_unlock; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* get the CPU */ 1537a6aedfaSMike Travis data = per_cpu(cpufreq_cpu_data, cpu); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds if (!data) 1561da177e4SLinus Torvalds goto err_out_put_module; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds if (!kobject_get(&data->kobj)) 1591da177e4SLinus Torvalds goto err_out_put_module; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1621da177e4SLinus Torvalds return data; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds err_out_put_module: 1651da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1661da177e4SLinus Torvalds err_out_unlock: 1671da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1681da177e4SLinus Torvalds err_out: 1691da177e4SLinus Torvalds return NULL; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 1721da177e4SLinus Torvalds 1737d5e350fSDave Jones 1741da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data) 1751da177e4SLinus Torvalds { 1761da177e4SLinus Torvalds kobject_put(&data->kobj); 1771da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /********************************************************************* 1831da177e4SLinus Torvalds * UNIFIED DEBUG HELPERS * 1841da177e4SLinus Torvalds *********************************************************************/ 1851da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */ 1881da177e4SLinus Torvalds static unsigned int debug; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can 1911da177e4SLinus Torvalds * set or modify this value. 1921da177e4SLinus Torvalds */ 1931da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful 1961da177e4SLinus Torvalds * loading of a cpufreq driver, temporarily disabled when a new policy 1971da177e4SLinus Torvalds * is set, and disabled upon cpufreq driver removal 1981da177e4SLinus Torvalds */ 1991da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1; 2001da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock); 2011da177e4SLinus Torvalds 202858119e1SArjan van de Ven static void cpufreq_debug_enable_ratelimit(void) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds unsigned long flags; 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2071da177e4SLinus Torvalds if (disable_ratelimit) 2081da177e4SLinus Torvalds disable_ratelimit--; 2091da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 212858119e1SArjan van de Ven static void cpufreq_debug_disable_ratelimit(void) 2131da177e4SLinus Torvalds { 2141da177e4SLinus Torvalds unsigned long flags; 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 2171da177e4SLinus Torvalds disable_ratelimit++; 2181da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 221e08f5f5bSGautham R Shenoy void cpufreq_debug_printk(unsigned int type, const char *prefix, 222e08f5f5bSGautham R Shenoy const char *fmt, ...) 2231da177e4SLinus Torvalds { 2241da177e4SLinus Torvalds char s[256]; 2251da177e4SLinus Torvalds va_list args; 2261da177e4SLinus Torvalds unsigned int len; 2271da177e4SLinus Torvalds unsigned long flags; 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds WARN_ON(!prefix); 2301da177e4SLinus Torvalds if (type & debug) { 2311da177e4SLinus Torvalds spin_lock_irqsave(&disable_ratelimit_lock, flags); 232e08f5f5bSGautham R Shenoy if (!disable_ratelimit && debug_ratelimit 233e08f5f5bSGautham R Shenoy && !printk_ratelimit()) { 2341da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2351da177e4SLinus Torvalds return; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds va_start(args, fmt); 2421da177e4SLinus Torvalds len += vsnprintf(&s[len], (256 - len), fmt, args); 2431da177e4SLinus Torvalds va_end(args); 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds printk(s); 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds WARN_ON(len < 5); 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk); 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds 2531da177e4SLinus Torvalds module_param(debug, uint, 0644); 254e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core," 255e08f5f5bSGautham R Shenoy " 2 to debug drivers, and 4 to debug governors."); 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644); 258e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:" 259e08f5f5bSGautham R Shenoy " set to 0 to disable ratelimiting."); 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */ 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; } 2641da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; } 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */ 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /********************************************************************* 2701da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2711da177e4SLinus Torvalds *********************************************************************/ 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds /** 2741da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2751da177e4SLinus Torvalds * 2761da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2771da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2781da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2791da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2801da177e4SLinus Torvalds */ 2811da177e4SLinus Torvalds #ifndef CONFIG_SMP 2821da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2831da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2841da177e4SLinus Torvalds 285858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2861da177e4SLinus Torvalds { 2871da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2881da177e4SLinus Torvalds return; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2911da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2921da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 293e08f5f5bSGautham R Shenoy dprintk("saving %lu as reference value for loops_per_jiffy; " 294e08f5f5bSGautham R Shenoy "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || 2971da177e4SLinus Torvalds (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || 29842d4dc3fSBenjamin Herrenschmidt (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 299e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 300e08f5f5bSGautham R Shenoy ci->new); 301e08f5f5bSGautham R Shenoy dprintk("scaling loops_per_jiffy to %lu " 302e08f5f5bSGautham R Shenoy "for frequency %u kHz\n", loops_per_jiffy, ci->new); 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds #else 306e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 307e08f5f5bSGautham R Shenoy { 308e08f5f5bSGautham R Shenoy return; 309e08f5f5bSGautham R Shenoy } 3101da177e4SLinus Torvalds #endif 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /** 314e4472cb3SDave Jones * cpufreq_notify_transition - call notifier chain and adjust_jiffies 315e4472cb3SDave Jones * on frequency transition. 3161da177e4SLinus Torvalds * 317e4472cb3SDave Jones * This function calls the transition notifiers and the "adjust_jiffies" 318e4472cb3SDave Jones * function. It is called twice on all CPU frequency changes that have 319e4472cb3SDave Jones * external effects. 3201da177e4SLinus Torvalds */ 3211da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) 3221da177e4SLinus Torvalds { 323e4472cb3SDave Jones struct cpufreq_policy *policy; 324e4472cb3SDave Jones 3251da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds freqs->flags = cpufreq_driver->flags; 328e4472cb3SDave Jones dprintk("notification %u of frequency transition to %u kHz\n", 329e4472cb3SDave Jones state, freqs->new); 3301da177e4SLinus Torvalds 3317a6aedfaSMike Travis policy = per_cpu(cpufreq_cpu_data, freqs->cpu); 3321da177e4SLinus Torvalds switch (state) { 333e4472cb3SDave Jones 3341da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 335e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 336e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 337e4472cb3SDave Jones * "old frequency". 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 340e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 341e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 342b10eec22SJan Beulich dprintk("Warning: CPU frequency is" 343e4472cb3SDave Jones " %u, cpufreq assumed %u kHz.\n", 344e4472cb3SDave Jones freqs->old, policy->cur); 345e4472cb3SDave Jones freqs->old = policy->cur; 3461da177e4SLinus Torvalds } 3471da177e4SLinus Torvalds } 348b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 349e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3501da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3511da177e4SLinus Torvalds break; 352e4472cb3SDave Jones 3531da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3541da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 355b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 356e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 357e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 358e4472cb3SDave Jones policy->cur = freqs->new; 3591da177e4SLinus Torvalds break; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 3621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds /********************************************************************* 3671da177e4SLinus Torvalds * SYSFS INTERFACE * 3681da177e4SLinus Torvalds *********************************************************************/ 3691da177e4SLinus Torvalds 3703bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 3713bcb09a3SJeremy Fitzhardinge { 3723bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 3733bcb09a3SJeremy Fitzhardinge 3743bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 3753bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 3763bcb09a3SJeremy Fitzhardinge return t; 3773bcb09a3SJeremy Fitzhardinge 3783bcb09a3SJeremy Fitzhardinge return NULL; 3793bcb09a3SJeremy Fitzhardinge } 3803bcb09a3SJeremy Fitzhardinge 3811da177e4SLinus Torvalds /** 3821da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 3831da177e4SLinus Torvalds */ 3841da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 3851da177e4SLinus Torvalds struct cpufreq_governor **governor) 3861da177e4SLinus Torvalds { 3873bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 3883bcb09a3SJeremy Fitzhardinge 3891da177e4SLinus Torvalds if (!cpufreq_driver) 3903bcb09a3SJeremy Fitzhardinge goto out; 3913bcb09a3SJeremy Fitzhardinge 3921da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 3931da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 3941da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 3953bcb09a3SJeremy Fitzhardinge err = 0; 396e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 397e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 3981da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 3993bcb09a3SJeremy Fitzhardinge err = 0; 4001da177e4SLinus Torvalds } 4013bcb09a3SJeremy Fitzhardinge } else if (cpufreq_driver->target) { 4021da177e4SLinus Torvalds struct cpufreq_governor *t; 4033bcb09a3SJeremy Fitzhardinge 4043fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4053bcb09a3SJeremy Fitzhardinge 4063bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 4073bcb09a3SJeremy Fitzhardinge 408ea714970SJeremy Fitzhardinge if (t == NULL) { 409e08f5f5bSGautham R Shenoy char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", 410e08f5f5bSGautham R Shenoy str_governor); 411ea714970SJeremy Fitzhardinge 412ea714970SJeremy Fitzhardinge if (name) { 413ea714970SJeremy Fitzhardinge int ret; 414ea714970SJeremy Fitzhardinge 415ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 416326f6a5cSChris Wright ret = request_module("%s", name); 417ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 418ea714970SJeremy Fitzhardinge 419ea714970SJeremy Fitzhardinge if (ret == 0) 420ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 421ea714970SJeremy Fitzhardinge } 422ea714970SJeremy Fitzhardinge 423ea714970SJeremy Fitzhardinge kfree(name); 424ea714970SJeremy Fitzhardinge } 425ea714970SJeremy Fitzhardinge 4263bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4271da177e4SLinus Torvalds *governor = t; 4283bcb09a3SJeremy Fitzhardinge err = 0; 4291da177e4SLinus Torvalds } 4303bcb09a3SJeremy Fitzhardinge 4313bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds out: 4343bcb09a3SJeremy Fitzhardinge return err; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds /** 439e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 440e08f5f5bSGautham R Shenoy * print out cpufreq information 4411da177e4SLinus Torvalds * 4421da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 4431da177e4SLinus Torvalds * "unsigned int". 4441da177e4SLinus Torvalds */ 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds #define show_one(file_name, object) \ 4471da177e4SLinus Torvalds static ssize_t show_##file_name \ 4481da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 4491da177e4SLinus Torvalds { \ 4501da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 4511da177e4SLinus Torvalds } 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 4541da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 455ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 4561da177e4SLinus Torvalds show_one(scaling_min_freq, min); 4571da177e4SLinus Torvalds show_one(scaling_max_freq, max); 4581da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 4591da177e4SLinus Torvalds 460e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 461e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy); 4627970e08bSThomas Renninger 4631da177e4SLinus Torvalds /** 4641da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 4651da177e4SLinus Torvalds */ 4661da177e4SLinus Torvalds #define store_one(file_name, object) \ 4671da177e4SLinus Torvalds static ssize_t store_##file_name \ 4681da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 4691da177e4SLinus Torvalds { \ 4701da177e4SLinus Torvalds unsigned int ret = -EINVAL; \ 4711da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 4721da177e4SLinus Torvalds \ 4731da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 4741da177e4SLinus Torvalds if (ret) \ 4751da177e4SLinus Torvalds return -EINVAL; \ 4761da177e4SLinus Torvalds \ 4771da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 4781da177e4SLinus Torvalds if (ret != 1) \ 4791da177e4SLinus Torvalds return -EINVAL; \ 4801da177e4SLinus Torvalds \ 4817970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); \ 4827970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 4831da177e4SLinus Torvalds \ 4841da177e4SLinus Torvalds return ret ? ret : count; \ 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds store_one(scaling_min_freq, min); 4881da177e4SLinus Torvalds store_one(scaling_max_freq, max); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds /** 4911da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 4921da177e4SLinus Torvalds */ 493e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 494e08f5f5bSGautham R Shenoy char *buf) 4951da177e4SLinus Torvalds { 4965a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 4971da177e4SLinus Torvalds if (!cur_freq) 4981da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 4991da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds /** 5041da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5051da177e4SLinus Torvalds */ 506905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 5071da177e4SLinus Torvalds { 5081da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 5091da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5101da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5111da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5121da177e4SLinus Torvalds else if (policy->governor) 51329464f28SDave Jones return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", 51429464f28SDave Jones policy->governor->name); 5151da177e4SLinus Torvalds return -EINVAL; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /** 5201da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5211da177e4SLinus Torvalds */ 5221da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 5231da177e4SLinus Torvalds const char *buf, size_t count) 5241da177e4SLinus Torvalds { 5251da177e4SLinus Torvalds unsigned int ret = -EINVAL; 5261da177e4SLinus Torvalds char str_governor[16]; 5271da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5281da177e4SLinus Torvalds 5291da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5301da177e4SLinus Torvalds if (ret) 5311da177e4SLinus Torvalds return ret; 5321da177e4SLinus Torvalds 5331da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 5341da177e4SLinus Torvalds if (ret != 1) 5351da177e4SLinus Torvalds return -EINVAL; 5361da177e4SLinus Torvalds 537e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 538e08f5f5bSGautham R Shenoy &new_policy.governor)) 5391da177e4SLinus Torvalds return -EINVAL; 5401da177e4SLinus Torvalds 5417970e08bSThomas Renninger /* Do not use cpufreq_set_policy here or the user_policy.max 5427970e08bSThomas Renninger will be wrongly overridden */ 5437970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 5447970e08bSThomas Renninger 5457970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 5467970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 5477970e08bSThomas Renninger 548e08f5f5bSGautham R Shenoy if (ret) 549e08f5f5bSGautham R Shenoy return ret; 550e08f5f5bSGautham R Shenoy else 551e08f5f5bSGautham R Shenoy return count; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /** 5551da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 5561da177e4SLinus Torvalds */ 5571da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /** 5631da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 5661da177e4SLinus Torvalds char *buf) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds ssize_t i = 0; 5691da177e4SLinus Torvalds struct cpufreq_governor *t; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds if (!cpufreq_driver->target) { 5721da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 5731da177e4SLinus Torvalds goto out; 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 57729464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 57829464f28SDave Jones - (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 } 586e8628dd0SDarrick J. Wong 587835481d9SRusty Russell static ssize_t show_cpus(const struct cpumask *mask, char *buf) 5881da177e4SLinus Torvalds { 5891da177e4SLinus Torvalds ssize_t i = 0; 5901da177e4SLinus Torvalds unsigned int cpu; 5911da177e4SLinus Torvalds 592835481d9SRusty Russell for_each_cpu(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 603e8628dd0SDarrick J. Wong /** 604e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 605e8628dd0SDarrick J. Wong * hw coordination is in use 606e8628dd0SDarrick J. Wong */ 607e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 608e8628dd0SDarrick J. Wong { 609835481d9SRusty Russell if (cpumask_empty(policy->related_cpus)) 610e8628dd0SDarrick J. Wong return show_cpus(policy->cpus, buf); 611e8628dd0SDarrick J. Wong return show_cpus(policy->related_cpus, buf); 612e8628dd0SDarrick J. Wong } 613e8628dd0SDarrick J. Wong 614e8628dd0SDarrick J. Wong /** 615e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 616e8628dd0SDarrick J. Wong */ 617e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 618e8628dd0SDarrick J. Wong { 619e8628dd0SDarrick J. Wong return show_cpus(policy->cpus, buf); 620e8628dd0SDarrick J. Wong } 621e8628dd0SDarrick 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 628879000f9SCHIKAMA masaki if (!policy->governor || !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 { 642879000f9SCHIKAMA masaki if (!policy->governor || !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); 663ed129784SThomas Renninger define_one_ro(cpuinfo_transition_latency); 6641da177e4SLinus Torvalds define_one_ro(scaling_available_governors); 6651da177e4SLinus Torvalds define_one_ro(scaling_driver); 6661da177e4SLinus Torvalds define_one_ro(scaling_cur_freq); 667e8628dd0SDarrick J. Wong define_one_ro(related_cpus); 6681da177e4SLinus Torvalds define_one_ro(affected_cpus); 6691da177e4SLinus Torvalds define_one_rw(scaling_min_freq); 6701da177e4SLinus Torvalds define_one_rw(scaling_max_freq); 6711da177e4SLinus Torvalds define_one_rw(scaling_governor); 6729e76988eSVenki Pallipadi define_one_rw(scaling_setspeed); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 6751da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 6761da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 677ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 6781da177e4SLinus Torvalds &scaling_min_freq.attr, 6791da177e4SLinus Torvalds &scaling_max_freq.attr, 6801da177e4SLinus Torvalds &affected_cpus.attr, 681e8628dd0SDarrick J. Wong &related_cpus.attr, 6821da177e4SLinus Torvalds &scaling_governor.attr, 6831da177e4SLinus Torvalds &scaling_driver.attr, 6841da177e4SLinus Torvalds &scaling_available_governors.attr, 6859e76988eSVenki Pallipadi &scaling_setspeed.attr, 6861da177e4SLinus Torvalds NULL 6871da177e4SLinus Torvalds }; 6881da177e4SLinus Torvalds 6891da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 6901da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 6931da177e4SLinus Torvalds { 6941da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 6951da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 6960db4a8a9SDave Jones ssize_t ret = -EINVAL; 6971da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 6981da177e4SLinus Torvalds if (!policy) 6990db4a8a9SDave Jones goto no_policy; 7005a01f2e8SVenkatesh Pallipadi 7015a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_read(policy->cpu) < 0) 7020db4a8a9SDave Jones goto fail; 7035a01f2e8SVenkatesh Pallipadi 704e08f5f5bSGautham R Shenoy if (fattr->show) 705e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 706e08f5f5bSGautham R Shenoy else 707e08f5f5bSGautham R Shenoy ret = -EIO; 708e08f5f5bSGautham R Shenoy 7095a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(policy->cpu); 7100db4a8a9SDave Jones fail: 7111da177e4SLinus Torvalds cpufreq_cpu_put(policy); 7120db4a8a9SDave Jones no_policy: 7131da177e4SLinus Torvalds return ret; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 7171da177e4SLinus Torvalds const char *buf, size_t count) 7181da177e4SLinus Torvalds { 7191da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7201da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 721a07530b4SDave Jones ssize_t ret = -EINVAL; 7221da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 7231da177e4SLinus Torvalds if (!policy) 724a07530b4SDave Jones goto no_policy; 7255a01f2e8SVenkatesh Pallipadi 7265a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(policy->cpu) < 0) 727a07530b4SDave Jones goto fail; 7285a01f2e8SVenkatesh Pallipadi 729e08f5f5bSGautham R Shenoy if (fattr->store) 730e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 731e08f5f5bSGautham R Shenoy else 732e08f5f5bSGautham R Shenoy ret = -EIO; 733e08f5f5bSGautham R Shenoy 7345a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 735a07530b4SDave Jones fail: 7361da177e4SLinus Torvalds cpufreq_cpu_put(policy); 737a07530b4SDave Jones no_policy: 7381da177e4SLinus Torvalds return ret; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 7421da177e4SLinus Torvalds { 7431da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7441da177e4SLinus Torvalds dprintk("last reference is dropped\n"); 7451da177e4SLinus Torvalds complete(&policy->kobj_unregister); 7461da177e4SLinus Torvalds } 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = { 7491da177e4SLinus Torvalds .show = show, 7501da177e4SLinus Torvalds .store = store, 7511da177e4SLinus Torvalds }; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 7541da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 7551da177e4SLinus Torvalds .default_attrs = default_attrs, 7561da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 7571da177e4SLinus Torvalds }; 7581da177e4SLinus Torvalds 75919d6f7ecSDave Jones /* symlink affected CPUs */ 76019d6f7ecSDave Jones int cpufreq_add_dev_symlink(unsigned int cpu, struct cpufreq_policy *policy) 76119d6f7ecSDave Jones { 76219d6f7ecSDave Jones unsigned int j; 76319d6f7ecSDave Jones int ret = 0; 76419d6f7ecSDave Jones 76519d6f7ecSDave Jones for_each_cpu(j, policy->cpus) { 76619d6f7ecSDave Jones struct cpufreq_policy *managed_policy; 76719d6f7ecSDave Jones struct sys_device *cpu_sys_dev; 76819d6f7ecSDave Jones 76919d6f7ecSDave Jones if (j == cpu) 77019d6f7ecSDave Jones continue; 77119d6f7ecSDave Jones if (!cpu_online(j)) 77219d6f7ecSDave Jones continue; 77319d6f7ecSDave Jones 77419d6f7ecSDave Jones dprintk("CPU %u already managed, adding link\n", j); 77519d6f7ecSDave Jones managed_policy = cpufreq_cpu_get(cpu); 77619d6f7ecSDave Jones cpu_sys_dev = get_cpu_sysdev(j); 77719d6f7ecSDave Jones ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 77819d6f7ecSDave Jones "cpufreq"); 77919d6f7ecSDave Jones if (ret) { 78019d6f7ecSDave Jones cpufreq_cpu_put(managed_policy); 78119d6f7ecSDave Jones return ret; 78219d6f7ecSDave Jones } 78319d6f7ecSDave Jones } 78419d6f7ecSDave Jones return ret; 78519d6f7ecSDave Jones } 78619d6f7ecSDave Jones 787*909a694eSDave Jones int cpufreq_add_dev_interface(unsigned int cpu, struct cpufreq_policy *policy, 788*909a694eSDave Jones struct sys_device *sys_dev) 789*909a694eSDave Jones { 790*909a694eSDave Jones struct freq_attr **drv_attr; 791*909a694eSDave Jones unsigned long flags; 792*909a694eSDave Jones int ret = 0; 793*909a694eSDave Jones unsigned int j; 794*909a694eSDave Jones 795*909a694eSDave Jones /* prepare interface data */ 796*909a694eSDave Jones ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 797*909a694eSDave Jones &sys_dev->kobj, "cpufreq"); 798*909a694eSDave Jones if (ret) 799*909a694eSDave Jones return ret; 800*909a694eSDave Jones 801*909a694eSDave Jones /* set up files for this cpu device */ 802*909a694eSDave Jones drv_attr = cpufreq_driver->attr; 803*909a694eSDave Jones while ((drv_attr) && (*drv_attr)) { 804*909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 805*909a694eSDave Jones if (ret) 806*909a694eSDave Jones goto err_out_kobj_put; 807*909a694eSDave Jones drv_attr++; 808*909a694eSDave Jones } 809*909a694eSDave Jones if (cpufreq_driver->get) { 810*909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 811*909a694eSDave Jones if (ret) 812*909a694eSDave Jones goto err_out_kobj_put; 813*909a694eSDave Jones } 814*909a694eSDave Jones if (cpufreq_driver->target) { 815*909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 816*909a694eSDave Jones if (ret) 817*909a694eSDave Jones goto err_out_kobj_put; 818*909a694eSDave Jones } 819*909a694eSDave Jones 820*909a694eSDave Jones spin_lock_irqsave(&cpufreq_driver_lock, flags); 821*909a694eSDave Jones for_each_cpu(j, policy->cpus) { 822*909a694eSDave Jones if (!cpu_online(j)) 823*909a694eSDave Jones continue; 824*909a694eSDave Jones per_cpu(cpufreq_cpu_data, j) = policy; 825*909a694eSDave Jones per_cpu(policy_cpu, j) = policy->cpu; 826*909a694eSDave Jones } 827*909a694eSDave Jones spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 828*909a694eSDave Jones 829*909a694eSDave Jones ret = cpufreq_add_dev_symlink(cpu, policy); 830*909a694eSDave Jones return ret; 831*909a694eSDave Jones 832*909a694eSDave Jones err_out_kobj_put: 833*909a694eSDave Jones kobject_put(&policy->kobj); 834*909a694eSDave Jones wait_for_completion(&policy->kobj_unregister); 835*909a694eSDave Jones return ret; 836*909a694eSDave Jones } 837*909a694eSDave Jones 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /** 8401da177e4SLinus Torvalds * cpufreq_add_dev - add a CPU device 8411da177e4SLinus Torvalds * 8421da177e4SLinus Torvalds * Adds the cpufreq interface for a CPU device. 8433f4a782bSMathieu Desnoyers * 8443f4a782bSMathieu Desnoyers * The Oracle says: try running cpufreq registration/unregistration concurrently 8453f4a782bSMathieu Desnoyers * with with cpu hotplugging and all hell will break loose. Tried to clean this 8463f4a782bSMathieu Desnoyers * mess up, but more thorough testing is needed. - Mathieu 8471da177e4SLinus Torvalds */ 8481da177e4SLinus Torvalds static int cpufreq_add_dev(struct sys_device *sys_dev) 8491da177e4SLinus Torvalds { 8501da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 8511da177e4SLinus Torvalds int ret = 0; 8521da177e4SLinus Torvalds struct cpufreq_policy new_policy; 8531da177e4SLinus Torvalds struct cpufreq_policy *policy; 8541da177e4SLinus Torvalds unsigned long flags; 8551da177e4SLinus Torvalds unsigned int j; 8561da177e4SLinus Torvalds 857c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 858c32b6b8eSAshok Raj return 0; 859c32b6b8eSAshok Raj 8601da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 8611da177e4SLinus Torvalds dprintk("adding CPU %u\n", cpu); 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds #ifdef CONFIG_SMP 8641da177e4SLinus Torvalds /* check whether a different CPU already registered this 8651da177e4SLinus Torvalds * CPU because it is in the same boat. */ 8661da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 8671da177e4SLinus Torvalds if (unlikely(policy)) { 8688ff69732SDave Jones cpufreq_cpu_put(policy); 8691da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 8701da177e4SLinus Torvalds return 0; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds #endif 8731da177e4SLinus Torvalds 8741da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) { 8751da177e4SLinus Torvalds ret = -EINVAL; 8761da177e4SLinus Torvalds goto module_out; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 879059019a3SDave Jones ret = -ENOMEM; 880e98df50cSDave Jones policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 881059019a3SDave Jones if (!policy) 8821da177e4SLinus Torvalds goto nomem_out; 883059019a3SDave Jones 884059019a3SDave Jones if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 8853f4a782bSMathieu Desnoyers goto err_free_policy; 886059019a3SDave Jones 887059019a3SDave Jones if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 8883f4a782bSMathieu Desnoyers goto err_free_cpumask; 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds policy->cpu = cpu; 891835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 8921da177e4SLinus Torvalds 8935a01f2e8SVenkatesh Pallipadi /* Initially set CPU itself as the policy_cpu */ 8945a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = cpu; 8953f4a782bSMathieu Desnoyers ret = (lock_policy_rwsem_write(cpu) < 0); 8963f4a782bSMathieu Desnoyers WARN_ON(ret); 8975a01f2e8SVenkatesh Pallipadi 8981da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 89965f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 9001da177e4SLinus Torvalds 9018122c6ceSThomas Renninger /* Set governor before ->init, so that driver could check it */ 9028122c6ceSThomas Renninger policy->governor = CPUFREQ_DEFAULT_GOVERNOR; 9031da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 9041da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 9051da177e4SLinus Torvalds */ 9061da177e4SLinus Torvalds ret = cpufreq_driver->init(policy); 9071da177e4SLinus Torvalds if (ret) { 9081da177e4SLinus Torvalds dprintk("initialization failed\n"); 9093f4a782bSMathieu Desnoyers goto err_unlock_policy; 9101da177e4SLinus Torvalds } 911187d9f4eSMike Chan policy->user_policy.min = policy->min; 912187d9f4eSMike Chan policy->user_policy.max = policy->max; 9131da177e4SLinus Torvalds 914a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 915a1531acdSThomas Renninger CPUFREQ_START, policy); 916a1531acdSThomas Renninger 9178ff69732SDave Jones #ifdef CONFIG_SMP 918084f3493SThomas Renninger 919084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 9207a6aedfaSMike Travis if (per_cpu(cpufreq_cpu_governor, cpu)) { 9217a6aedfaSMike Travis policy->governor = per_cpu(cpufreq_cpu_governor, cpu); 922084f3493SThomas Renninger dprintk("Restoring governor %s for cpu %d\n", 923084f3493SThomas Renninger policy->governor->name, cpu); 924084f3493SThomas Renninger } 925084f3493SThomas Renninger #endif 926084f3493SThomas Renninger 927835481d9SRusty Russell for_each_cpu(j, policy->cpus) { 9285e1596f7SDave Jones struct cpufreq_policy *managed_policy; 9295e1596f7SDave Jones 9308ff69732SDave Jones if (cpu == j) 9318ff69732SDave Jones continue; 9328ff69732SDave Jones 93329464f28SDave Jones /* Check for existing affected CPUs. 93429464f28SDave Jones * They may not be aware of it due to CPU Hotplug. 935d5194decSThomas Renninger * cpufreq_cpu_put is called when the device is removed 936d5194decSThomas Renninger * in __cpufreq_remove_dev() 9378ff69732SDave Jones */ 9383f4a782bSMathieu Desnoyers managed_policy = cpufreq_cpu_get(j); 9398ff69732SDave Jones if (unlikely(managed_policy)) { 9405a01f2e8SVenkatesh Pallipadi 9415a01f2e8SVenkatesh Pallipadi /* Set proper policy_cpu */ 9425a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 9435a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = managed_policy->cpu; 9445a01f2e8SVenkatesh Pallipadi 9453f4a782bSMathieu Desnoyers if (lock_policy_rwsem_write(cpu) < 0) { 9463f4a782bSMathieu Desnoyers /* Should not go through policy unlock path */ 9473f4a782bSMathieu Desnoyers if (cpufreq_driver->exit) 9483f4a782bSMathieu Desnoyers cpufreq_driver->exit(policy); 9493f4a782bSMathieu Desnoyers ret = -EBUSY; 9503f4a782bSMathieu Desnoyers cpufreq_cpu_put(managed_policy); 9513f4a782bSMathieu Desnoyers goto err_free_cpumask; 9523f4a782bSMathieu Desnoyers } 9535a01f2e8SVenkatesh Pallipadi 9548ff69732SDave Jones spin_lock_irqsave(&cpufreq_driver_lock, flags); 955835481d9SRusty Russell cpumask_copy(managed_policy->cpus, policy->cpus); 9567a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, cpu) = managed_policy; 9578ff69732SDave Jones spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9588ff69732SDave Jones 9598ff69732SDave Jones dprintk("CPU already managed, adding link\n"); 9600142f9dcSAhmed S. Darwish ret = sysfs_create_link(&sys_dev->kobj, 9610142f9dcSAhmed S. Darwish &managed_policy->kobj, 9620142f9dcSAhmed S. Darwish "cpufreq"); 963d5194decSThomas Renninger if (ret) 9643f4a782bSMathieu Desnoyers cpufreq_cpu_put(managed_policy); 9653f4a782bSMathieu Desnoyers /* 9663f4a782bSMathieu Desnoyers * Success. We only needed to be added to the mask. 9673f4a782bSMathieu Desnoyers * Call driver->exit() because only the cpu parent of 9683f4a782bSMathieu Desnoyers * the kobj needed to call init(). 9693f4a782bSMathieu Desnoyers */ 9703f4a782bSMathieu Desnoyers goto out_driver_exit; /* call driver->exit() */ 9718ff69732SDave Jones } 9728ff69732SDave Jones } 9738ff69732SDave Jones #endif 9741da177e4SLinus Torvalds memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 9751da177e4SLinus Torvalds 976*909a694eSDave Jones ret = cpufreq_add_dev_interface(cpu, policy, sys_dev); 97719d6f7ecSDave Jones if (ret) 9780142f9dcSAhmed S. Darwish goto err_out_unregister; 9798ff69732SDave Jones 9801da177e4SLinus Torvalds policy->governor = NULL; /* to assure that the starting sequence is 9811da177e4SLinus Torvalds * run in cpufreq_set_policy */ 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds /* set default policy */ 98422c970f3SThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 98522c970f3SThomas Renninger policy->user_policy.policy = policy->policy; 986084f3493SThomas Renninger policy->user_policy.governor = policy->governor; 98722c970f3SThomas Renninger 9881da177e4SLinus Torvalds if (ret) { 9891da177e4SLinus Torvalds dprintk("setting policy failed\n"); 9901da177e4SLinus Torvalds goto err_out_unregister; 9911da177e4SLinus Torvalds } 9921da177e4SLinus Torvalds 993dca02613SLothar Waßmann unlock_policy_rwsem_write(cpu); 994dca02613SLothar Waßmann 995038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 9961da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9971da177e4SLinus Torvalds dprintk("initialization complete\n"); 9981da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds return 0; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds err_out_unregister: 10041da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 1005835481d9SRusty Russell for_each_cpu(j, policy->cpus) 10067a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 10071da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10081da177e4SLinus Torvalds 1009c10997f6SGreg Kroah-Hartman kobject_put(&policy->kobj); 10101da177e4SLinus Torvalds wait_for_completion(&policy->kobj_unregister); 10111da177e4SLinus Torvalds 10123f4a782bSMathieu Desnoyers out_driver_exit: 10138085e1f1SVenkatesh Pallipadi if (cpufreq_driver->exit) 10148085e1f1SVenkatesh Pallipadi cpufreq_driver->exit(policy); 10158085e1f1SVenkatesh Pallipadi 10163f4a782bSMathieu Desnoyers err_unlock_policy: 101745709118SDave Jones unlock_policy_rwsem_write(cpu); 10183f4a782bSMathieu Desnoyers err_free_cpumask: 10193f4a782bSMathieu Desnoyers free_cpumask_var(policy->cpus); 10203f4a782bSMathieu Desnoyers err_free_policy: 10211da177e4SLinus Torvalds kfree(policy); 10221da177e4SLinus Torvalds nomem_out: 10231da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 10241da177e4SLinus Torvalds module_out: 10251da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10261da177e4SLinus Torvalds return ret; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /** 10315a01f2e8SVenkatesh Pallipadi * __cpufreq_remove_dev - remove a CPU device 10321da177e4SLinus Torvalds * 10331da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 10345a01f2e8SVenkatesh Pallipadi * Caller should already have policy_rwsem in write mode for this CPU. 10355a01f2e8SVenkatesh Pallipadi * This routine frees the rwsem before returning. 10361da177e4SLinus Torvalds */ 10375a01f2e8SVenkatesh Pallipadi static int __cpufreq_remove_dev(struct sys_device *sys_dev) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 10401da177e4SLinus Torvalds unsigned long flags; 10411da177e4SLinus Torvalds struct cpufreq_policy *data; 10421da177e4SLinus Torvalds #ifdef CONFIG_SMP 1043e738cf6dSGrant Coady struct sys_device *cpu_sys_dev; 10441da177e4SLinus Torvalds unsigned int j; 10451da177e4SLinus Torvalds #endif 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 10481da177e4SLinus Torvalds dprintk("unregistering CPU %u\n", cpu); 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 10517a6aedfaSMike Travis data = per_cpu(cpufreq_cpu_data, cpu); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds if (!data) { 10541da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10551da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10565a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10571da177e4SLinus Torvalds return -EINVAL; 10581da177e4SLinus Torvalds } 10597a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, cpu) = NULL; 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds #ifdef CONFIG_SMP 10631da177e4SLinus Torvalds /* if this isn't the CPU which is the parent of the kobj, we 10641da177e4SLinus Torvalds * only need to unlink, put and exit 10651da177e4SLinus Torvalds */ 10661da177e4SLinus Torvalds if (unlikely(cpu != data->cpu)) { 10671da177e4SLinus Torvalds dprintk("removing link\n"); 1068835481d9SRusty Russell cpumask_clear_cpu(cpu, data->cpus); 10691da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10701da177e4SLinus Torvalds sysfs_remove_link(&sys_dev->kobj, "cpufreq"); 10711da177e4SLinus Torvalds cpufreq_cpu_put(data); 10721da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10735a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10741da177e4SLinus Torvalds return 0; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds #endif 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds #ifdef CONFIG_SMP 1079084f3493SThomas Renninger 1080084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 10817a6aedfaSMike Travis per_cpu(cpufreq_cpu_governor, cpu) = data->governor; 1082084f3493SThomas Renninger #endif 1083084f3493SThomas Renninger 10841da177e4SLinus Torvalds /* if we have other CPUs still registered, we need to unlink them, 10851da177e4SLinus Torvalds * or else wait_for_completion below will lock up. Clean the 10867a6aedfaSMike Travis * per_cpu(cpufreq_cpu_data) while holding the lock, and remove 10877a6aedfaSMike Travis * the sysfs links afterwards. 10881da177e4SLinus Torvalds */ 1089835481d9SRusty Russell if (unlikely(cpumask_weight(data->cpus) > 1)) { 1090835481d9SRusty Russell for_each_cpu(j, data->cpus) { 10911da177e4SLinus Torvalds if (j == cpu) 10921da177e4SLinus Torvalds continue; 10937a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 10941da177e4SLinus Torvalds } 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10981da177e4SLinus Torvalds 1099835481d9SRusty Russell if (unlikely(cpumask_weight(data->cpus) > 1)) { 1100835481d9SRusty Russell for_each_cpu(j, data->cpus) { 11011da177e4SLinus Torvalds if (j == cpu) 11021da177e4SLinus Torvalds continue; 11031da177e4SLinus Torvalds dprintk("removing link for cpu %u\n", j); 1104084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 11057a6aedfaSMike Travis per_cpu(cpufreq_cpu_governor, j) = data->governor; 1106084f3493SThomas Renninger #endif 1107d434fca7SAshok Raj cpu_sys_dev = get_cpu_sysdev(j); 1108d434fca7SAshok Raj sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); 11091da177e4SLinus Torvalds cpufreq_cpu_put(data); 11101da177e4SLinus Torvalds } 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds #else 11131da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 11141da177e4SLinus Torvalds #endif 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds if (cpufreq_driver->target) 11171da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 11185a01f2e8SVenkatesh Pallipadi 11191da177e4SLinus Torvalds kobject_put(&data->kobj); 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds /* we need to make sure that the underlying kobj is actually 11221da177e4SLinus Torvalds * not referenced anymore by anybody before we proceed with 11231da177e4SLinus Torvalds * unloading. 11241da177e4SLinus Torvalds */ 11251da177e4SLinus Torvalds dprintk("waiting for dropping of refcount\n"); 11261da177e4SLinus Torvalds wait_for_completion(&data->kobj_unregister); 11271da177e4SLinus Torvalds dprintk("wait complete\n"); 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds if (cpufreq_driver->exit) 11301da177e4SLinus Torvalds cpufreq_driver->exit(data); 11311da177e4SLinus Torvalds 11327d26e2d5Svenkatesh.pallipadi@intel.com unlock_policy_rwsem_write(cpu); 11337d26e2d5Svenkatesh.pallipadi@intel.com 1134835481d9SRusty Russell free_cpumask_var(data->related_cpus); 1135835481d9SRusty Russell free_cpumask_var(data->cpus); 11361da177e4SLinus Torvalds kfree(data); 1137835481d9SRusty Russell per_cpu(cpufreq_cpu_data, cpu) = NULL; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 11401da177e4SLinus Torvalds return 0; 11411da177e4SLinus Torvalds } 11421da177e4SLinus Torvalds 11431da177e4SLinus Torvalds 11445a01f2e8SVenkatesh Pallipadi static int cpufreq_remove_dev(struct sys_device *sys_dev) 11455a01f2e8SVenkatesh Pallipadi { 11465a01f2e8SVenkatesh Pallipadi unsigned int cpu = sys_dev->id; 11475a01f2e8SVenkatesh Pallipadi int retval; 1148ec28297aSVenki Pallipadi 1149ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1150ec28297aSVenki Pallipadi return 0; 1151ec28297aSVenki Pallipadi 11525a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 11535a01f2e8SVenkatesh Pallipadi BUG(); 11545a01f2e8SVenkatesh Pallipadi 11555a01f2e8SVenkatesh Pallipadi retval = __cpufreq_remove_dev(sys_dev); 11565a01f2e8SVenkatesh Pallipadi return retval; 11575a01f2e8SVenkatesh Pallipadi } 11585a01f2e8SVenkatesh Pallipadi 11595a01f2e8SVenkatesh Pallipadi 116065f27f38SDavid Howells static void handle_update(struct work_struct *work) 11611da177e4SLinus Torvalds { 116265f27f38SDavid Howells struct cpufreq_policy *policy = 116365f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 116465f27f38SDavid Howells unsigned int cpu = policy->cpu; 11651da177e4SLinus Torvalds dprintk("handle_update for cpu %u called\n", cpu); 11661da177e4SLinus Torvalds cpufreq_update_policy(cpu); 11671da177e4SLinus Torvalds } 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds /** 11701da177e4SLinus Torvalds * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 11711da177e4SLinus Torvalds * @cpu: cpu number 11721da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 11731da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 11741da177e4SLinus Torvalds * 117529464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 117629464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 11771da177e4SLinus Torvalds */ 1178e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1179e08f5f5bSGautham R Shenoy unsigned int new_freq) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds struct cpufreq_freqs freqs; 11821da177e4SLinus Torvalds 1183b10eec22SJan Beulich dprintk("Warning: CPU frequency out of sync: cpufreq and timing " 11841da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds freqs.cpu = cpu; 11871da177e4SLinus Torvalds freqs.old = old_freq; 11881da177e4SLinus Torvalds freqs.new = new_freq; 11891da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 11901da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 11911da177e4SLinus Torvalds } 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds 11941da177e4SLinus Torvalds /** 11954ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 119695235ca2SVenkatesh Pallipadi * @cpu: CPU number 119795235ca2SVenkatesh Pallipadi * 119895235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 119995235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 120095235ca2SVenkatesh Pallipadi */ 120195235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 120295235ca2SVenkatesh Pallipadi { 120395235ca2SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1204e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 120595235ca2SVenkatesh Pallipadi 120695235ca2SVenkatesh Pallipadi if (policy) { 1207e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 120895235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 120995235ca2SVenkatesh Pallipadi } 121095235ca2SVenkatesh Pallipadi 12114d34a67dSDave Jones return ret_freq; 121295235ca2SVenkatesh Pallipadi } 121395235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 121495235ca2SVenkatesh Pallipadi 121595235ca2SVenkatesh Pallipadi 12165a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 12171da177e4SLinus Torvalds { 12187a6aedfaSMike Travis struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1219e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 12201da177e4SLinus Torvalds 12211da177e4SLinus Torvalds if (!cpufreq_driver->get) 12224d34a67dSDave Jones return ret_freq; 12231da177e4SLinus Torvalds 1224e08f5f5bSGautham R Shenoy ret_freq = cpufreq_driver->get(cpu); 12251da177e4SLinus Torvalds 1226e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 1227e08f5f5bSGautham R Shenoy !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1228e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1229e08f5f5bSGautham R Shenoy saved value exists */ 1230e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1231e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 12321da177e4SLinus Torvalds schedule_work(&policy->update); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 12364d34a67dSDave Jones return ret_freq; 12375a01f2e8SVenkatesh Pallipadi } 12381da177e4SLinus Torvalds 12395a01f2e8SVenkatesh Pallipadi /** 12405a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 12415a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 12425a01f2e8SVenkatesh Pallipadi * 12435a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 12445a01f2e8SVenkatesh Pallipadi */ 12455a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 12465a01f2e8SVenkatesh Pallipadi { 12475a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 12485a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 12495a01f2e8SVenkatesh Pallipadi 12505a01f2e8SVenkatesh Pallipadi if (!policy) 12515a01f2e8SVenkatesh Pallipadi goto out; 12525a01f2e8SVenkatesh Pallipadi 12535a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 12545a01f2e8SVenkatesh Pallipadi goto out_policy; 12555a01f2e8SVenkatesh Pallipadi 12565a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 12575a01f2e8SVenkatesh Pallipadi 12585a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 12595a01f2e8SVenkatesh Pallipadi 12605a01f2e8SVenkatesh Pallipadi out_policy: 12611da177e4SLinus Torvalds cpufreq_cpu_put(policy); 12625a01f2e8SVenkatesh Pallipadi out: 12634d34a67dSDave Jones return ret_freq; 12641da177e4SLinus Torvalds } 12651da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /** 126942d4dc3fSBenjamin Herrenschmidt * cpufreq_suspend - let the low level driver prepare for suspend 127042d4dc3fSBenjamin Herrenschmidt */ 127142d4dc3fSBenjamin Herrenschmidt 1272e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg) 127342d4dc3fSBenjamin Herrenschmidt { 1274e08f5f5bSGautham R Shenoy int ret = 0; 12754bc5d341SDave Jones 12764bc5d341SDave Jones int cpu = sysdev->id; 127742d4dc3fSBenjamin Herrenschmidt struct cpufreq_policy *cpu_policy; 127842d4dc3fSBenjamin Herrenschmidt 12790e37b159SDave Jones dprintk("suspending cpu %u\n", cpu); 128042d4dc3fSBenjamin Herrenschmidt 128142d4dc3fSBenjamin Herrenschmidt if (!cpu_online(cpu)) 128242d4dc3fSBenjamin Herrenschmidt return 0; 128342d4dc3fSBenjamin Herrenschmidt 128442d4dc3fSBenjamin Herrenschmidt /* we may be lax here as interrupts are off. Nonetheless 128542d4dc3fSBenjamin Herrenschmidt * we need to grab the correct cpu policy, as to check 128642d4dc3fSBenjamin Herrenschmidt * whether we really run on this CPU. 128742d4dc3fSBenjamin Herrenschmidt */ 128842d4dc3fSBenjamin Herrenschmidt 128942d4dc3fSBenjamin Herrenschmidt cpu_policy = cpufreq_cpu_get(cpu); 129042d4dc3fSBenjamin Herrenschmidt if (!cpu_policy) 129142d4dc3fSBenjamin Herrenschmidt return -EINVAL; 129242d4dc3fSBenjamin Herrenschmidt 129342d4dc3fSBenjamin Herrenschmidt /* only handle each CPU group once */ 1294c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1295c9060494SDave Jones goto out; 129642d4dc3fSBenjamin Herrenschmidt 129742d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->suspend) { 1298e00d9967SBernard Blackham ret = cpufreq_driver->suspend(cpu_policy, pmsg); 1299ce6c3997SDominik Brodowski if (ret) 130042d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 130142d4dc3fSBenjamin Herrenschmidt "step on CPU %u\n", cpu_policy->cpu); 130242d4dc3fSBenjamin Herrenschmidt } 130342d4dc3fSBenjamin Herrenschmidt 130442d4dc3fSBenjamin Herrenschmidt out: 130542d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 1306c9060494SDave Jones return ret; 130742d4dc3fSBenjamin Herrenschmidt } 130842d4dc3fSBenjamin Herrenschmidt 130942d4dc3fSBenjamin Herrenschmidt /** 13101da177e4SLinus Torvalds * cpufreq_resume - restore proper CPU frequency handling after resume 13111da177e4SLinus Torvalds * 13121da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 1313ce6c3997SDominik Brodowski * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are 1314ce6c3997SDominik Brodowski * restored. It will verify that the current freq is in sync with 1315ce6c3997SDominik Brodowski * what we believe it to be. This is a bit later than when it 1316ce6c3997SDominik Brodowski * should be, but nonethteless it's better than calling 1317ce6c3997SDominik Brodowski * cpufreq_driver->get() here which might re-enable interrupts... 13181da177e4SLinus Torvalds */ 13191da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device *sysdev) 13201da177e4SLinus Torvalds { 1321e08f5f5bSGautham R Shenoy int ret = 0; 13224bc5d341SDave Jones 13234bc5d341SDave Jones int cpu = sysdev->id; 13241da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds dprintk("resuming cpu %u\n", cpu); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds if (!cpu_online(cpu)) 13291da177e4SLinus Torvalds return 0; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* we may be lax here as interrupts are off. Nonetheless 13321da177e4SLinus Torvalds * we need to grab the correct cpu policy, as to check 13331da177e4SLinus Torvalds * whether we really run on this CPU. 13341da177e4SLinus Torvalds */ 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 13371da177e4SLinus Torvalds if (!cpu_policy) 13381da177e4SLinus Torvalds return -EINVAL; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /* only handle each CPU group once */ 1341c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1342c9060494SDave Jones goto fail; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds if (cpufreq_driver->resume) { 13451da177e4SLinus Torvalds ret = cpufreq_driver->resume(cpu_policy); 13461da177e4SLinus Torvalds if (ret) { 13471da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 13481da177e4SLinus Torvalds "step on CPU %u\n", cpu_policy->cpu); 1349c9060494SDave Jones goto fail; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds schedule_work(&cpu_policy->update); 1354ce6c3997SDominik Brodowski 1355c9060494SDave Jones fail: 13561da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 13571da177e4SLinus Torvalds return ret; 13581da177e4SLinus Torvalds } 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = { 13611da177e4SLinus Torvalds .add = cpufreq_add_dev, 13621da177e4SLinus Torvalds .remove = cpufreq_remove_dev, 136342d4dc3fSBenjamin Herrenschmidt .suspend = cpufreq_suspend, 13641da177e4SLinus Torvalds .resume = cpufreq_resume, 13651da177e4SLinus Torvalds }; 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /********************************************************************* 13691da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 13701da177e4SLinus Torvalds *********************************************************************/ 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds /** 13731da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 13741da177e4SLinus Torvalds * @nb: notifier function to register 13751da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 13761da177e4SLinus Torvalds * 13771da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 13781da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 13791da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 13801da177e4SLinus Torvalds * changes in cpufreq policy. 13811da177e4SLinus Torvalds * 13821da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1383e041c683SAlan Stern * blocking_notifier_chain_register. 13841da177e4SLinus Torvalds */ 13851da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 13861da177e4SLinus Torvalds { 13871da177e4SLinus Torvalds int ret; 13881da177e4SLinus Torvalds 138974212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 139074212ca4SCesar Eduardo Barros 13911da177e4SLinus Torvalds switch (list) { 13921da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1393b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1394e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 13951da177e4SLinus Torvalds break; 13961da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1397e041c683SAlan Stern ret = blocking_notifier_chain_register( 1398e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 13991da177e4SLinus Torvalds break; 14001da177e4SLinus Torvalds default: 14011da177e4SLinus Torvalds ret = -EINVAL; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds return ret; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds /** 14101da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 14111da177e4SLinus Torvalds * @nb: notifier block to be unregistered 14121da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 14131da177e4SLinus Torvalds * 14141da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 14151da177e4SLinus Torvalds * 14161da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1417e041c683SAlan Stern * blocking_notifier_chain_unregister. 14181da177e4SLinus Torvalds */ 14191da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 14201da177e4SLinus Torvalds { 14211da177e4SLinus Torvalds int ret; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds switch (list) { 14241da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1425b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1426e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 14271da177e4SLinus Torvalds break; 14281da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1429e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1430e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 14311da177e4SLinus Torvalds break; 14321da177e4SLinus Torvalds default: 14331da177e4SLinus Torvalds ret = -EINVAL; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds return ret; 14371da177e4SLinus Torvalds } 14381da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds /********************************************************************* 14421da177e4SLinus Torvalds * GOVERNORS * 14431da177e4SLinus Torvalds *********************************************************************/ 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds 14461da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 14471da177e4SLinus Torvalds unsigned int target_freq, 14481da177e4SLinus Torvalds unsigned int relation) 14491da177e4SLinus Torvalds { 14501da177e4SLinus Torvalds int retval = -EINVAL; 1451c32b6b8eSAshok Raj 14521da177e4SLinus Torvalds dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 14531da177e4SLinus Torvalds target_freq, relation); 14541da177e4SLinus Torvalds if (cpu_online(policy->cpu) && cpufreq_driver->target) 14551da177e4SLinus Torvalds retval = cpufreq_driver->target(policy, target_freq, relation); 145690d45d17SAshok Raj 14571da177e4SLinus Torvalds return retval; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 14601da177e4SLinus Torvalds 14611da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 14621da177e4SLinus Torvalds unsigned int target_freq, 14631da177e4SLinus Torvalds unsigned int relation) 14641da177e4SLinus Torvalds { 1465f1829e4aSJulia Lawall int ret = -EINVAL; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 14681da177e4SLinus Torvalds if (!policy) 1469f1829e4aSJulia Lawall goto no_policy; 14701da177e4SLinus Torvalds 14715a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 1472f1829e4aSJulia Lawall goto fail; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 14751da177e4SLinus Torvalds 14765a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 14771da177e4SLinus Torvalds 1478f1829e4aSJulia Lawall fail: 14791da177e4SLinus Torvalds cpufreq_cpu_put(policy); 1480f1829e4aSJulia Lawall no_policy: 14811da177e4SLinus Torvalds return ret; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 14841da177e4SLinus Torvalds 1485bf0b90e3Svenkatesh.pallipadi@intel.com int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu) 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 1493bf0b90e3Svenkatesh.pallipadi@intel.com if (cpu_online(cpu) && cpufreq_driver->getavg) 1494bf0b90e3Svenkatesh.pallipadi@intel.com 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 159429464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 159529464f28SDave Jones * is written 15961da177e4SLinus Torvalds * 15971da177e4SLinus Torvalds * Reads the current cpufreq policy. 15981da177e4SLinus Torvalds */ 15991da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 16001da177e4SLinus Torvalds { 16011da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 16021da177e4SLinus Torvalds if (!policy) 16031da177e4SLinus Torvalds return -EINVAL; 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 16061da177e4SLinus Torvalds if (!cpu_policy) 16071da177e4SLinus Torvalds return -EINVAL; 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 16121da177e4SLinus Torvalds return 0; 16131da177e4SLinus Torvalds } 16141da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds 1617153d7f3fSArjan van de Ven /* 1618e08f5f5bSGautham R Shenoy * data : current policy. 1619e08f5f5bSGautham R Shenoy * policy : policy to be set. 1620153d7f3fSArjan van de Ven */ 1621e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 1622e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy) 16231da177e4SLinus Torvalds { 16241da177e4SLinus Torvalds int ret = 0; 16251da177e4SLinus Torvalds 16261da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 16271da177e4SLinus Torvalds dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 16281da177e4SLinus Torvalds policy->min, policy->max); 16291da177e4SLinus Torvalds 1630e08f5f5bSGautham R Shenoy memcpy(&policy->cpuinfo, &data->cpuinfo, 1631e08f5f5bSGautham R Shenoy sizeof(struct cpufreq_cpuinfo)); 16321da177e4SLinus Torvalds 163353391fa2SYi Yang if (policy->min > data->max || policy->max < data->min) { 16349c9a43edSMattia Dongili ret = -EINVAL; 16359c9a43edSMattia Dongili goto error_out; 16369c9a43edSMattia Dongili } 16379c9a43edSMattia Dongili 16381da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 16391da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 16401da177e4SLinus Torvalds if (ret) 16411da177e4SLinus Torvalds goto error_out; 16421da177e4SLinus Torvalds 16431da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 1644e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1645e041c683SAlan Stern CPUFREQ_ADJUST, policy); 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 1648e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1649e041c683SAlan Stern CPUFREQ_INCOMPATIBLE, policy); 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit, 16521da177e4SLinus Torvalds which might be different to the first one */ 16531da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 1654e041c683SAlan Stern if (ret) 16551da177e4SLinus Torvalds goto error_out; 16561da177e4SLinus Torvalds 16571da177e4SLinus Torvalds /* notification of the new policy */ 1658e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1659e041c683SAlan Stern CPUFREQ_NOTIFY, policy); 16601da177e4SLinus Torvalds 16611da177e4SLinus Torvalds data->min = policy->min; 16621da177e4SLinus Torvalds data->max = policy->max; 16631da177e4SLinus Torvalds 1664e08f5f5bSGautham R Shenoy dprintk("new min and max freqs are %u - %u kHz\n", 1665e08f5f5bSGautham R Shenoy data->min, data->max); 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 16681da177e4SLinus Torvalds data->policy = policy->policy; 16691da177e4SLinus Torvalds dprintk("setting range\n"); 16701da177e4SLinus Torvalds ret = cpufreq_driver->setpolicy(policy); 16711da177e4SLinus Torvalds } else { 16721da177e4SLinus Torvalds if (policy->governor != data->governor) { 16731da177e4SLinus Torvalds /* save old, working values */ 16741da177e4SLinus Torvalds struct cpufreq_governor *old_gov = data->governor; 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds dprintk("governor switch\n"); 16771da177e4SLinus Torvalds 16781da177e4SLinus Torvalds /* end old governor */ 16791da177e4SLinus Torvalds if (data->governor) 16801da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 16811da177e4SLinus Torvalds 16821da177e4SLinus Torvalds /* start new governor */ 16831da177e4SLinus Torvalds data->governor = policy->governor; 16841da177e4SLinus Torvalds if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 16851da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 1686e08f5f5bSGautham R Shenoy dprintk("starting governor %s failed\n", 1687e08f5f5bSGautham R Shenoy data->governor->name); 16881da177e4SLinus Torvalds if (old_gov) { 16891da177e4SLinus Torvalds data->governor = old_gov; 1690e08f5f5bSGautham R Shenoy __cpufreq_governor(data, 1691e08f5f5bSGautham R Shenoy CPUFREQ_GOV_START); 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds ret = -EINVAL; 16941da177e4SLinus Torvalds goto error_out; 16951da177e4SLinus Torvalds } 16961da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 16971da177e4SLinus Torvalds } 16981da177e4SLinus Torvalds dprintk("governor: change or update limits\n"); 16991da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 17001da177e4SLinus Torvalds } 17011da177e4SLinus Torvalds 17021da177e4SLinus Torvalds error_out: 17031da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 17041da177e4SLinus Torvalds return ret; 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds /** 17081da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 17091da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 17101da177e4SLinus Torvalds * 17111da177e4SLinus Torvalds * Usefull for policy notifiers which have different necessities 17121da177e4SLinus Torvalds * at different times. 17131da177e4SLinus Torvalds */ 17141da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 17151da177e4SLinus Torvalds { 17161da177e4SLinus Torvalds struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 17171da177e4SLinus Torvalds struct cpufreq_policy policy; 1718f1829e4aSJulia Lawall int ret; 17191da177e4SLinus Torvalds 1720f1829e4aSJulia Lawall if (!data) { 1721f1829e4aSJulia Lawall ret = -ENODEV; 1722f1829e4aSJulia Lawall goto no_policy; 1723f1829e4aSJulia Lawall } 17241da177e4SLinus Torvalds 1725f1829e4aSJulia Lawall if (unlikely(lock_policy_rwsem_write(cpu))) { 1726f1829e4aSJulia Lawall ret = -EINVAL; 1727f1829e4aSJulia Lawall goto fail; 1728f1829e4aSJulia Lawall } 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds dprintk("updating policy for CPU %u\n", cpu); 17317d5e350fSDave Jones memcpy(&policy, data, sizeof(struct cpufreq_policy)); 17321da177e4SLinus Torvalds policy.min = data->user_policy.min; 17331da177e4SLinus Torvalds policy.max = data->user_policy.max; 17341da177e4SLinus Torvalds policy.policy = data->user_policy.policy; 17351da177e4SLinus Torvalds policy.governor = data->user_policy.governor; 17361da177e4SLinus Torvalds 17370961dd0dSThomas Renninger /* BIOS might change freq behind our back 17380961dd0dSThomas Renninger -> ask driver for current freq and notify governors about a change */ 17390961dd0dSThomas Renninger if (cpufreq_driver->get) { 17400961dd0dSThomas Renninger policy.cur = cpufreq_driver->get(cpu); 1741a85f7bd3SThomas Renninger if (!data->cur) { 1742a85f7bd3SThomas Renninger dprintk("Driver did not initialize current freq"); 1743a85f7bd3SThomas Renninger data->cur = policy.cur; 1744a85f7bd3SThomas Renninger } else { 17450961dd0dSThomas Renninger if (data->cur != policy.cur) 1746e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, data->cur, 1747e08f5f5bSGautham R Shenoy policy.cur); 17480961dd0dSThomas Renninger } 1749a85f7bd3SThomas Renninger } 17500961dd0dSThomas Renninger 17511da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, &policy); 17521da177e4SLinus Torvalds 17535a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 17545a01f2e8SVenkatesh Pallipadi 1755f1829e4aSJulia Lawall fail: 17561da177e4SLinus Torvalds cpufreq_cpu_put(data); 1757f1829e4aSJulia Lawall no_policy: 17581da177e4SLinus Torvalds return ret; 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 17611da177e4SLinus Torvalds 1762dd184a01SSatyam Sharma static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, 1763c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1764c32b6b8eSAshok Raj { 1765c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 1766c32b6b8eSAshok Raj struct sys_device *sys_dev; 1767c32b6b8eSAshok Raj 1768c32b6b8eSAshok Raj sys_dev = get_cpu_sysdev(cpu); 1769c32b6b8eSAshok Raj if (sys_dev) { 1770c32b6b8eSAshok Raj switch (action) { 1771c32b6b8eSAshok Raj case CPU_ONLINE: 17728bb78442SRafael J. Wysocki case CPU_ONLINE_FROZEN: 1773c32b6b8eSAshok Raj cpufreq_add_dev(sys_dev); 1774c32b6b8eSAshok Raj break; 1775c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 17768bb78442SRafael J. Wysocki case CPU_DOWN_PREPARE_FROZEN: 17775a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 17785a01f2e8SVenkatesh Pallipadi BUG(); 17795a01f2e8SVenkatesh Pallipadi 17805a01f2e8SVenkatesh Pallipadi __cpufreq_remove_dev(sys_dev); 1781c32b6b8eSAshok Raj break; 17825a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 17838bb78442SRafael J. Wysocki case CPU_DOWN_FAILED_FROZEN: 17845a01f2e8SVenkatesh Pallipadi cpufreq_add_dev(sys_dev); 1785c32b6b8eSAshok Raj break; 1786c32b6b8eSAshok Raj } 1787c32b6b8eSAshok Raj } 1788c32b6b8eSAshok Raj return NOTIFY_OK; 1789c32b6b8eSAshok Raj } 1790c32b6b8eSAshok Raj 1791f6ebef30SSam Ravnborg static struct notifier_block __refdata cpufreq_cpu_notifier = 1792c32b6b8eSAshok Raj { 1793c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 1794c32b6b8eSAshok Raj }; 17951da177e4SLinus Torvalds 17961da177e4SLinus Torvalds /********************************************************************* 17971da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 17981da177e4SLinus Torvalds *********************************************************************/ 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /** 18011da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 18021da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 18031da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 18041da177e4SLinus Torvalds * 18051da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 18061da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 18071da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 18081da177e4SLinus Torvalds * 18091da177e4SLinus Torvalds */ 1810221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 18111da177e4SLinus Torvalds { 18121da177e4SLinus Torvalds unsigned long flags; 18131da177e4SLinus Torvalds int ret; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 18161da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 18171da177e4SLinus Torvalds return -EINVAL; 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds dprintk("trying to register driver %s\n", driver_data->name); 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds if (driver_data->setpolicy) 18221da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18251da177e4SLinus Torvalds if (cpufreq_driver) { 18261da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18271da177e4SLinus Torvalds return -EBUSY; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds cpufreq_driver = driver_data; 18301da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18311da177e4SLinus Torvalds 18327a6aedfaSMike Travis ret = sysdev_driver_register(&cpu_sysdev_class, 18337a6aedfaSMike Travis &cpufreq_sysdev_driver); 18341da177e4SLinus Torvalds 18351da177e4SLinus Torvalds if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 18361da177e4SLinus Torvalds int i; 18371da177e4SLinus Torvalds ret = -ENODEV; 18381da177e4SLinus Torvalds 18391da177e4SLinus Torvalds /* check for at least one working CPU */ 18407a6aedfaSMike Travis for (i = 0; i < nr_cpu_ids; i++) 18417a6aedfaSMike Travis if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) { 18421da177e4SLinus Torvalds ret = 0; 18437a6aedfaSMike Travis break; 18447a6aedfaSMike Travis } 18451da177e4SLinus Torvalds 18461da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 18471da177e4SLinus Torvalds if (ret) { 1848e08f5f5bSGautham R Shenoy dprintk("no CPU initialized for driver %s\n", 1849e08f5f5bSGautham R Shenoy driver_data->name); 1850e08f5f5bSGautham R Shenoy sysdev_driver_unregister(&cpu_sysdev_class, 1851e08f5f5bSGautham R Shenoy &cpufreq_sysdev_driver); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18541da177e4SLinus Torvalds cpufreq_driver = NULL; 18551da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds } 18581da177e4SLinus Torvalds 18591da177e4SLinus Torvalds if (!ret) { 186065edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 18611da177e4SLinus Torvalds dprintk("driver %s up and running\n", driver_data->name); 18621da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18631da177e4SLinus Torvalds } 18641da177e4SLinus Torvalds 18654d34a67dSDave Jones return ret; 18661da177e4SLinus Torvalds } 18671da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 18681da177e4SLinus Torvalds 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds /** 18711da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 18721da177e4SLinus Torvalds * 18731da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 18741da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 18751da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 18761da177e4SLinus Torvalds * currently not initialised. 18771da177e4SLinus Torvalds */ 1878221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds unsigned long flags; 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds if (!cpufreq_driver || (driver != cpufreq_driver)) { 18851da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18861da177e4SLinus Torvalds return -EINVAL; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds dprintk("unregistering driver %s\n", driver->name); 18901da177e4SLinus Torvalds 18911da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 189265edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 18931da177e4SLinus Torvalds 18941da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18951da177e4SLinus Torvalds cpufreq_driver = NULL; 18961da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18971da177e4SLinus Torvalds 18981da177e4SLinus Torvalds return 0; 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 19015a01f2e8SVenkatesh Pallipadi 19025a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 19035a01f2e8SVenkatesh Pallipadi { 19045a01f2e8SVenkatesh Pallipadi int cpu; 19055a01f2e8SVenkatesh Pallipadi 19065a01f2e8SVenkatesh Pallipadi for_each_possible_cpu(cpu) { 19075a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = -1; 19085a01f2e8SVenkatesh Pallipadi init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 19095a01f2e8SVenkatesh Pallipadi } 19105a01f2e8SVenkatesh Pallipadi return 0; 19115a01f2e8SVenkatesh Pallipadi } 19125a01f2e8SVenkatesh Pallipadi 19135a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 1914