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 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds /** 7611da177e4SLinus Torvalds * cpufreq_add_dev - add a CPU device 7621da177e4SLinus Torvalds * 7631da177e4SLinus Torvalds * Adds the cpufreq interface for a CPU device. 764*3f4a782bSMathieu Desnoyers * 765*3f4a782bSMathieu Desnoyers * The Oracle says: try running cpufreq registration/unregistration concurrently 766*3f4a782bSMathieu Desnoyers * with with cpu hotplugging and all hell will break loose. Tried to clean this 767*3f4a782bSMathieu Desnoyers * mess up, but more thorough testing is needed. - Mathieu 7681da177e4SLinus Torvalds */ 7691da177e4SLinus Torvalds static int cpufreq_add_dev(struct sys_device *sys_dev) 7701da177e4SLinus Torvalds { 7711da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 7721da177e4SLinus Torvalds int ret = 0; 7731da177e4SLinus Torvalds struct cpufreq_policy new_policy; 7741da177e4SLinus Torvalds struct cpufreq_policy *policy; 7751da177e4SLinus Torvalds struct freq_attr **drv_attr; 7768ff69732SDave Jones struct sys_device *cpu_sys_dev; 7771da177e4SLinus Torvalds unsigned long flags; 7781da177e4SLinus Torvalds unsigned int j; 7798ff69732SDave Jones #ifdef CONFIG_SMP 7808ff69732SDave Jones struct cpufreq_policy *managed_policy; 7818ff69732SDave Jones #endif 7821da177e4SLinus Torvalds 783c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 784c32b6b8eSAshok Raj return 0; 785c32b6b8eSAshok Raj 7861da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 7871da177e4SLinus Torvalds dprintk("adding CPU %u\n", cpu); 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds #ifdef CONFIG_SMP 7901da177e4SLinus Torvalds /* check whether a different CPU already registered this 7911da177e4SLinus Torvalds * CPU because it is in the same boat. */ 7921da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 7931da177e4SLinus Torvalds if (unlikely(policy)) { 7948ff69732SDave Jones cpufreq_cpu_put(policy); 7951da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 7961da177e4SLinus Torvalds return 0; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds #endif 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (!try_module_get(cpufreq_driver->owner)) { 8011da177e4SLinus Torvalds ret = -EINVAL; 8021da177e4SLinus Torvalds goto module_out; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 805e98df50cSDave Jones policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 8061da177e4SLinus Torvalds if (!policy) { 8071da177e4SLinus Torvalds ret = -ENOMEM; 8081da177e4SLinus Torvalds goto nomem_out; 8091da177e4SLinus Torvalds } 810835481d9SRusty Russell if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) { 811835481d9SRusty Russell ret = -ENOMEM; 812*3f4a782bSMathieu Desnoyers goto err_free_policy; 813835481d9SRusty Russell } 814eaa95840SYinghai Lu if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) { 815835481d9SRusty Russell ret = -ENOMEM; 816*3f4a782bSMathieu Desnoyers goto err_free_cpumask; 817835481d9SRusty Russell } 8181da177e4SLinus Torvalds 8191da177e4SLinus Torvalds policy->cpu = cpu; 820835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 8211da177e4SLinus Torvalds 8225a01f2e8SVenkatesh Pallipadi /* Initially set CPU itself as the policy_cpu */ 8235a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = cpu; 824*3f4a782bSMathieu Desnoyers ret = (lock_policy_rwsem_write(cpu) < 0); 825*3f4a782bSMathieu Desnoyers WARN_ON(ret); 8265a01f2e8SVenkatesh Pallipadi 8271da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 82865f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 8291da177e4SLinus Torvalds 8308122c6ceSThomas Renninger /* Set governor before ->init, so that driver could check it */ 8318122c6ceSThomas Renninger policy->governor = CPUFREQ_DEFAULT_GOVERNOR; 8321da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 8331da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 8341da177e4SLinus Torvalds */ 8351da177e4SLinus Torvalds ret = cpufreq_driver->init(policy); 8361da177e4SLinus Torvalds if (ret) { 8371da177e4SLinus Torvalds dprintk("initialization failed\n"); 838*3f4a782bSMathieu Desnoyers goto err_unlock_policy; 8391da177e4SLinus Torvalds } 840187d9f4eSMike Chan policy->user_policy.min = policy->min; 841187d9f4eSMike Chan policy->user_policy.max = policy->max; 8421da177e4SLinus Torvalds 843a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 844a1531acdSThomas Renninger CPUFREQ_START, policy); 845a1531acdSThomas Renninger 8468ff69732SDave Jones #ifdef CONFIG_SMP 847084f3493SThomas Renninger 848084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 8497a6aedfaSMike Travis if (per_cpu(cpufreq_cpu_governor, cpu)) { 8507a6aedfaSMike Travis policy->governor = per_cpu(cpufreq_cpu_governor, cpu); 851084f3493SThomas Renninger dprintk("Restoring governor %s for cpu %d\n", 852084f3493SThomas Renninger policy->governor->name, cpu); 853084f3493SThomas Renninger } 854084f3493SThomas Renninger #endif 855084f3493SThomas Renninger 856835481d9SRusty Russell for_each_cpu(j, policy->cpus) { 8578ff69732SDave Jones if (cpu == j) 8588ff69732SDave Jones continue; 8598ff69732SDave Jones 86029464f28SDave Jones /* Check for existing affected CPUs. 86129464f28SDave Jones * They may not be aware of it due to CPU Hotplug. 8628ff69732SDave Jones */ 863*3f4a782bSMathieu Desnoyers managed_policy = cpufreq_cpu_get(j); 8648ff69732SDave Jones if (unlikely(managed_policy)) { 8655a01f2e8SVenkatesh Pallipadi 8665a01f2e8SVenkatesh Pallipadi /* Set proper policy_cpu */ 8675a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 8685a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = managed_policy->cpu; 8695a01f2e8SVenkatesh Pallipadi 870*3f4a782bSMathieu Desnoyers if (lock_policy_rwsem_write(cpu) < 0) { 871*3f4a782bSMathieu Desnoyers /* Should not go through policy unlock path */ 872*3f4a782bSMathieu Desnoyers if (cpufreq_driver->exit) 873*3f4a782bSMathieu Desnoyers cpufreq_driver->exit(policy); 874*3f4a782bSMathieu Desnoyers ret = -EBUSY; 875*3f4a782bSMathieu Desnoyers cpufreq_cpu_put(managed_policy); 876*3f4a782bSMathieu Desnoyers goto err_free_cpumask; 877*3f4a782bSMathieu Desnoyers } 8785a01f2e8SVenkatesh Pallipadi 8798ff69732SDave Jones spin_lock_irqsave(&cpufreq_driver_lock, flags); 880835481d9SRusty Russell cpumask_copy(managed_policy->cpus, policy->cpus); 8817a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, cpu) = managed_policy; 8828ff69732SDave Jones spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 8838ff69732SDave Jones 8848ff69732SDave Jones dprintk("CPU already managed, adding link\n"); 8850142f9dcSAhmed S. Darwish ret = sysfs_create_link(&sys_dev->kobj, 8860142f9dcSAhmed S. Darwish &managed_policy->kobj, 8870142f9dcSAhmed S. Darwish "cpufreq"); 888*3f4a782bSMathieu Desnoyers if (!ret) 889*3f4a782bSMathieu Desnoyers cpufreq_cpu_put(managed_policy); 890*3f4a782bSMathieu Desnoyers /* 891*3f4a782bSMathieu Desnoyers * Success. We only needed to be added to the mask. 892*3f4a782bSMathieu Desnoyers * Call driver->exit() because only the cpu parent of 893*3f4a782bSMathieu Desnoyers * the kobj needed to call init(). 894*3f4a782bSMathieu Desnoyers */ 895*3f4a782bSMathieu Desnoyers goto out_driver_exit; /* call driver->exit() */ 8968ff69732SDave Jones } 8978ff69732SDave Jones } 8988ff69732SDave Jones #endif 8991da177e4SLinus Torvalds memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds /* prepare interface data */ 902129f8ae9SDave Jones ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj, 903129f8ae9SDave Jones "cpufreq"); 90445709118SDave Jones if (ret) 905*3f4a782bSMathieu Desnoyers goto out_driver_exit; 90645709118SDave Jones 9071da177e4SLinus Torvalds /* set up files for this cpu device */ 9081da177e4SLinus Torvalds drv_attr = cpufreq_driver->attr; 9091da177e4SLinus Torvalds while ((drv_attr) && (*drv_attr)) { 910129f8ae9SDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 91145709118SDave Jones if (ret) 912*3f4a782bSMathieu Desnoyers goto err_out_kobj_put; 9131da177e4SLinus Torvalds drv_attr++; 9141da177e4SLinus Torvalds } 9150a4b2cccSThomas Renninger if (cpufreq_driver->get) { 916129f8ae9SDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 91745709118SDave Jones if (ret) 918*3f4a782bSMathieu Desnoyers goto err_out_kobj_put; 9190a4b2cccSThomas Renninger } 9200a4b2cccSThomas Renninger if (cpufreq_driver->target) { 921129f8ae9SDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 92245709118SDave Jones if (ret) 923*3f4a782bSMathieu Desnoyers goto err_out_kobj_put; 9240a4b2cccSThomas Renninger } 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 927835481d9SRusty Russell for_each_cpu(j, policy->cpus) { 9287a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = policy; 9295a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, j) = policy->cpu; 9305a01f2e8SVenkatesh Pallipadi } 9311da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9328ff69732SDave Jones 9338ff69732SDave Jones /* symlink affected CPUs */ 934835481d9SRusty Russell for_each_cpu(j, policy->cpus) { 9358ff69732SDave Jones if (j == cpu) 9368ff69732SDave Jones continue; 9378ff69732SDave Jones if (!cpu_online(j)) 9388ff69732SDave Jones continue; 9398ff69732SDave Jones 9401f8b2c9dSDave Jones dprintk("CPU %u already managed, adding link\n", j); 941*3f4a782bSMathieu Desnoyers managed_policy = cpufreq_cpu_get(cpu); 9428ff69732SDave Jones cpu_sys_dev = get_cpu_sysdev(j); 9430142f9dcSAhmed S. Darwish ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 9448ff69732SDave Jones "cpufreq"); 945*3f4a782bSMathieu Desnoyers if (ret) { 946*3f4a782bSMathieu Desnoyers cpufreq_cpu_put(managed_policy); 9470142f9dcSAhmed S. Darwish goto err_out_unregister; 9480142f9dcSAhmed S. Darwish } 949*3f4a782bSMathieu Desnoyers } 9508ff69732SDave Jones 9511da177e4SLinus Torvalds policy->governor = NULL; /* to assure that the starting sequence is 9521da177e4SLinus Torvalds * run in cpufreq_set_policy */ 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds /* set default policy */ 95522c970f3SThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 95622c970f3SThomas Renninger policy->user_policy.policy = policy->policy; 957084f3493SThomas Renninger policy->user_policy.governor = policy->governor; 95822c970f3SThomas Renninger 9591da177e4SLinus Torvalds if (ret) { 9601da177e4SLinus Torvalds dprintk("setting policy failed\n"); 9611da177e4SLinus Torvalds goto err_out_unregister; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 964dca02613SLothar Waßmann unlock_policy_rwsem_write(cpu); 965dca02613SLothar Waßmann 966038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 9671da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9681da177e4SLinus Torvalds dprintk("initialization complete\n"); 9691da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds return 0; 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds 9741da177e4SLinus Torvalds err_out_unregister: 9751da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 976835481d9SRusty Russell for_each_cpu(j, policy->cpus) 9777a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 9781da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 9791da177e4SLinus Torvalds 980*3f4a782bSMathieu Desnoyers err_out_kobj_put: 981c10997f6SGreg Kroah-Hartman kobject_put(&policy->kobj); 9821da177e4SLinus Torvalds wait_for_completion(&policy->kobj_unregister); 9831da177e4SLinus Torvalds 984*3f4a782bSMathieu Desnoyers out_driver_exit: 9858085e1f1SVenkatesh Pallipadi if (cpufreq_driver->exit) 9868085e1f1SVenkatesh Pallipadi cpufreq_driver->exit(policy); 9878085e1f1SVenkatesh Pallipadi 988*3f4a782bSMathieu Desnoyers err_unlock_policy: 98945709118SDave Jones unlock_policy_rwsem_write(cpu); 990*3f4a782bSMathieu Desnoyers err_free_cpumask: 991*3f4a782bSMathieu Desnoyers free_cpumask_var(policy->cpus); 992*3f4a782bSMathieu Desnoyers err_free_policy: 9931da177e4SLinus Torvalds kfree(policy); 9941da177e4SLinus Torvalds nomem_out: 9951da177e4SLinus Torvalds module_put(cpufreq_driver->owner); 9961da177e4SLinus Torvalds module_out: 9971da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 9981da177e4SLinus Torvalds return ret; 9991da177e4SLinus Torvalds } 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /** 10035a01f2e8SVenkatesh Pallipadi * __cpufreq_remove_dev - remove a CPU device 10041da177e4SLinus Torvalds * 10051da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 10065a01f2e8SVenkatesh Pallipadi * Caller should already have policy_rwsem in write mode for this CPU. 10075a01f2e8SVenkatesh Pallipadi * This routine frees the rwsem before returning. 10081da177e4SLinus Torvalds */ 10095a01f2e8SVenkatesh Pallipadi static int __cpufreq_remove_dev(struct sys_device *sys_dev) 10101da177e4SLinus Torvalds { 10111da177e4SLinus Torvalds unsigned int cpu = sys_dev->id; 10121da177e4SLinus Torvalds unsigned long flags; 10131da177e4SLinus Torvalds struct cpufreq_policy *data; 10141da177e4SLinus Torvalds #ifdef CONFIG_SMP 1015e738cf6dSGrant Coady struct sys_device *cpu_sys_dev; 10161da177e4SLinus Torvalds unsigned int j; 10171da177e4SLinus Torvalds #endif 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 10201da177e4SLinus Torvalds dprintk("unregistering CPU %u\n", cpu); 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 10237a6aedfaSMike Travis data = per_cpu(cpufreq_cpu_data, cpu); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (!data) { 10261da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10271da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10285a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10291da177e4SLinus Torvalds return -EINVAL; 10301da177e4SLinus Torvalds } 10317a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, cpu) = NULL; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds #ifdef CONFIG_SMP 10351da177e4SLinus Torvalds /* if this isn't the CPU which is the parent of the kobj, we 10361da177e4SLinus Torvalds * only need to unlink, put and exit 10371da177e4SLinus Torvalds */ 10381da177e4SLinus Torvalds if (unlikely(cpu != data->cpu)) { 10391da177e4SLinus Torvalds dprintk("removing link\n"); 1040835481d9SRusty Russell cpumask_clear_cpu(cpu, data->cpus); 10411da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10421da177e4SLinus Torvalds sysfs_remove_link(&sys_dev->kobj, "cpufreq"); 10431da177e4SLinus Torvalds cpufreq_cpu_put(data); 10441da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 10455a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 10461da177e4SLinus Torvalds return 0; 10471da177e4SLinus Torvalds } 10481da177e4SLinus Torvalds #endif 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds #ifdef CONFIG_SMP 1051084f3493SThomas Renninger 1052084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 10537a6aedfaSMike Travis per_cpu(cpufreq_cpu_governor, cpu) = data->governor; 1054084f3493SThomas Renninger #endif 1055084f3493SThomas Renninger 10561da177e4SLinus Torvalds /* if we have other CPUs still registered, we need to unlink them, 10571da177e4SLinus Torvalds * or else wait_for_completion below will lock up. Clean the 10587a6aedfaSMike Travis * per_cpu(cpufreq_cpu_data) while holding the lock, and remove 10597a6aedfaSMike Travis * the sysfs links afterwards. 10601da177e4SLinus Torvalds */ 1061835481d9SRusty Russell if (unlikely(cpumask_weight(data->cpus) > 1)) { 1062835481d9SRusty Russell for_each_cpu(j, data->cpus) { 10631da177e4SLinus Torvalds if (j == cpu) 10641da177e4SLinus Torvalds continue; 10657a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10701da177e4SLinus Torvalds 1071835481d9SRusty Russell if (unlikely(cpumask_weight(data->cpus) > 1)) { 1072835481d9SRusty Russell for_each_cpu(j, data->cpus) { 10731da177e4SLinus Torvalds if (j == cpu) 10741da177e4SLinus Torvalds continue; 10751da177e4SLinus Torvalds dprintk("removing link for cpu %u\n", j); 1076084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 10777a6aedfaSMike Travis per_cpu(cpufreq_cpu_governor, j) = data->governor; 1078084f3493SThomas Renninger #endif 1079d434fca7SAshok Raj cpu_sys_dev = get_cpu_sysdev(j); 1080d434fca7SAshok Raj sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq"); 10811da177e4SLinus Torvalds cpufreq_cpu_put(data); 10821da177e4SLinus Torvalds } 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds #else 10851da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 10861da177e4SLinus Torvalds #endif 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds if (cpufreq_driver->target) 10891da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 10905a01f2e8SVenkatesh Pallipadi 10911da177e4SLinus Torvalds kobject_put(&data->kobj); 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds /* we need to make sure that the underlying kobj is actually 10941da177e4SLinus Torvalds * not referenced anymore by anybody before we proceed with 10951da177e4SLinus Torvalds * unloading. 10961da177e4SLinus Torvalds */ 10971da177e4SLinus Torvalds dprintk("waiting for dropping of refcount\n"); 10981da177e4SLinus Torvalds wait_for_completion(&data->kobj_unregister); 10991da177e4SLinus Torvalds dprintk("wait complete\n"); 11001da177e4SLinus Torvalds 11011da177e4SLinus Torvalds if (cpufreq_driver->exit) 11021da177e4SLinus Torvalds cpufreq_driver->exit(data); 11031da177e4SLinus Torvalds 11047d26e2d5Svenkatesh.pallipadi@intel.com unlock_policy_rwsem_write(cpu); 11057d26e2d5Svenkatesh.pallipadi@intel.com 1106835481d9SRusty Russell free_cpumask_var(data->related_cpus); 1107835481d9SRusty Russell free_cpumask_var(data->cpus); 11081da177e4SLinus Torvalds kfree(data); 1109835481d9SRusty Russell per_cpu(cpufreq_cpu_data, cpu) = NULL; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 11121da177e4SLinus Torvalds return 0; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds 11165a01f2e8SVenkatesh Pallipadi static int cpufreq_remove_dev(struct sys_device *sys_dev) 11175a01f2e8SVenkatesh Pallipadi { 11185a01f2e8SVenkatesh Pallipadi unsigned int cpu = sys_dev->id; 11195a01f2e8SVenkatesh Pallipadi int retval; 1120ec28297aSVenki Pallipadi 1121ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1122ec28297aSVenki Pallipadi return 0; 1123ec28297aSVenki Pallipadi 11245a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 11255a01f2e8SVenkatesh Pallipadi BUG(); 11265a01f2e8SVenkatesh Pallipadi 11275a01f2e8SVenkatesh Pallipadi retval = __cpufreq_remove_dev(sys_dev); 11285a01f2e8SVenkatesh Pallipadi return retval; 11295a01f2e8SVenkatesh Pallipadi } 11305a01f2e8SVenkatesh Pallipadi 11315a01f2e8SVenkatesh Pallipadi 113265f27f38SDavid Howells static void handle_update(struct work_struct *work) 11331da177e4SLinus Torvalds { 113465f27f38SDavid Howells struct cpufreq_policy *policy = 113565f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 113665f27f38SDavid Howells unsigned int cpu = policy->cpu; 11371da177e4SLinus Torvalds dprintk("handle_update for cpu %u called\n", cpu); 11381da177e4SLinus Torvalds cpufreq_update_policy(cpu); 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 11411da177e4SLinus Torvalds /** 11421da177e4SLinus Torvalds * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 11431da177e4SLinus Torvalds * @cpu: cpu number 11441da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 11451da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 11461da177e4SLinus Torvalds * 114729464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 114829464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 11491da177e4SLinus Torvalds */ 1150e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1151e08f5f5bSGautham R Shenoy unsigned int new_freq) 11521da177e4SLinus Torvalds { 11531da177e4SLinus Torvalds struct cpufreq_freqs freqs; 11541da177e4SLinus Torvalds 1155b10eec22SJan Beulich dprintk("Warning: CPU frequency out of sync: cpufreq and timing " 11561da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 11571da177e4SLinus Torvalds 11581da177e4SLinus Torvalds freqs.cpu = cpu; 11591da177e4SLinus Torvalds freqs.old = old_freq; 11601da177e4SLinus Torvalds freqs.new = new_freq; 11611da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 11621da177e4SLinus Torvalds cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds /** 11674ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 116895235ca2SVenkatesh Pallipadi * @cpu: CPU number 116995235ca2SVenkatesh Pallipadi * 117095235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 117195235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 117295235ca2SVenkatesh Pallipadi */ 117395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 117495235ca2SVenkatesh Pallipadi { 117595235ca2SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1176e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 117795235ca2SVenkatesh Pallipadi 117895235ca2SVenkatesh Pallipadi if (policy) { 1179e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 118095235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 118195235ca2SVenkatesh Pallipadi } 118295235ca2SVenkatesh Pallipadi 11834d34a67dSDave Jones return ret_freq; 118495235ca2SVenkatesh Pallipadi } 118595235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 118695235ca2SVenkatesh Pallipadi 118795235ca2SVenkatesh Pallipadi 11885a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 11891da177e4SLinus Torvalds { 11907a6aedfaSMike Travis struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1191e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds if (!cpufreq_driver->get) 11944d34a67dSDave Jones return ret_freq; 11951da177e4SLinus Torvalds 1196e08f5f5bSGautham R Shenoy ret_freq = cpufreq_driver->get(cpu); 11971da177e4SLinus Torvalds 1198e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 1199e08f5f5bSGautham R Shenoy !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1200e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1201e08f5f5bSGautham R Shenoy saved value exists */ 1202e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1203e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 12041da177e4SLinus Torvalds schedule_work(&policy->update); 12051da177e4SLinus Torvalds } 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12084d34a67dSDave Jones return ret_freq; 12095a01f2e8SVenkatesh Pallipadi } 12101da177e4SLinus Torvalds 12115a01f2e8SVenkatesh Pallipadi /** 12125a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 12135a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 12145a01f2e8SVenkatesh Pallipadi * 12155a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 12165a01f2e8SVenkatesh Pallipadi */ 12175a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 12185a01f2e8SVenkatesh Pallipadi { 12195a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 12205a01f2e8SVenkatesh Pallipadi struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 12215a01f2e8SVenkatesh Pallipadi 12225a01f2e8SVenkatesh Pallipadi if (!policy) 12235a01f2e8SVenkatesh Pallipadi goto out; 12245a01f2e8SVenkatesh Pallipadi 12255a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 12265a01f2e8SVenkatesh Pallipadi goto out_policy; 12275a01f2e8SVenkatesh Pallipadi 12285a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 12295a01f2e8SVenkatesh Pallipadi 12305a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 12315a01f2e8SVenkatesh Pallipadi 12325a01f2e8SVenkatesh Pallipadi out_policy: 12331da177e4SLinus Torvalds cpufreq_cpu_put(policy); 12345a01f2e8SVenkatesh Pallipadi out: 12354d34a67dSDave Jones return ret_freq; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds 12401da177e4SLinus Torvalds /** 124142d4dc3fSBenjamin Herrenschmidt * cpufreq_suspend - let the low level driver prepare for suspend 124242d4dc3fSBenjamin Herrenschmidt */ 124342d4dc3fSBenjamin Herrenschmidt 1244e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg) 124542d4dc3fSBenjamin Herrenschmidt { 124642d4dc3fSBenjamin Herrenschmidt int cpu = sysdev->id; 1247e08f5f5bSGautham R Shenoy int ret = 0; 124842d4dc3fSBenjamin Herrenschmidt unsigned int cur_freq = 0; 124942d4dc3fSBenjamin Herrenschmidt struct cpufreq_policy *cpu_policy; 125042d4dc3fSBenjamin Herrenschmidt 12510e37b159SDave Jones dprintk("suspending cpu %u\n", cpu); 125242d4dc3fSBenjamin Herrenschmidt 125342d4dc3fSBenjamin Herrenschmidt if (!cpu_online(cpu)) 125442d4dc3fSBenjamin Herrenschmidt return 0; 125542d4dc3fSBenjamin Herrenschmidt 125642d4dc3fSBenjamin Herrenschmidt /* we may be lax here as interrupts are off. Nonetheless 125742d4dc3fSBenjamin Herrenschmidt * we need to grab the correct cpu policy, as to check 125842d4dc3fSBenjamin Herrenschmidt * whether we really run on this CPU. 125942d4dc3fSBenjamin Herrenschmidt */ 126042d4dc3fSBenjamin Herrenschmidt 126142d4dc3fSBenjamin Herrenschmidt cpu_policy = cpufreq_cpu_get(cpu); 126242d4dc3fSBenjamin Herrenschmidt if (!cpu_policy) 126342d4dc3fSBenjamin Herrenschmidt return -EINVAL; 126442d4dc3fSBenjamin Herrenschmidt 126542d4dc3fSBenjamin Herrenschmidt /* only handle each CPU group once */ 1266c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1267c9060494SDave Jones goto out; 126842d4dc3fSBenjamin Herrenschmidt 126942d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->suspend) { 1270e00d9967SBernard Blackham ret = cpufreq_driver->suspend(cpu_policy, pmsg); 127142d4dc3fSBenjamin Herrenschmidt if (ret) { 127242d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 127342d4dc3fSBenjamin Herrenschmidt "step on CPU %u\n", cpu_policy->cpu); 1274c9060494SDave Jones goto out; 127542d4dc3fSBenjamin Herrenschmidt } 127642d4dc3fSBenjamin Herrenschmidt } 127742d4dc3fSBenjamin Herrenschmidt 127842d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS) 127942d4dc3fSBenjamin Herrenschmidt goto out; 128042d4dc3fSBenjamin Herrenschmidt 128142d4dc3fSBenjamin Herrenschmidt if (cpufreq_driver->get) 128242d4dc3fSBenjamin Herrenschmidt cur_freq = cpufreq_driver->get(cpu_policy->cpu); 128342d4dc3fSBenjamin Herrenschmidt 128442d4dc3fSBenjamin Herrenschmidt if (!cur_freq || !cpu_policy->cur) { 128542d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed to assert current " 128642d4dc3fSBenjamin Herrenschmidt "frequency is what timing core thinks it is.\n"); 128742d4dc3fSBenjamin Herrenschmidt goto out; 128842d4dc3fSBenjamin Herrenschmidt } 128942d4dc3fSBenjamin Herrenschmidt 129042d4dc3fSBenjamin Herrenschmidt if (unlikely(cur_freq != cpu_policy->cur)) { 129142d4dc3fSBenjamin Herrenschmidt struct cpufreq_freqs freqs; 129242d4dc3fSBenjamin Herrenschmidt 129342d4dc3fSBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1294b10eec22SJan Beulich dprintk("Warning: CPU frequency is %u, " 129542d4dc3fSBenjamin Herrenschmidt "cpufreq assumed %u kHz.\n", 129642d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 129742d4dc3fSBenjamin Herrenschmidt 129842d4dc3fSBenjamin Herrenschmidt freqs.cpu = cpu; 129942d4dc3fSBenjamin Herrenschmidt freqs.old = cpu_policy->cur; 130042d4dc3fSBenjamin Herrenschmidt freqs.new = cur_freq; 130142d4dc3fSBenjamin Herrenschmidt 1302b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 130342d4dc3fSBenjamin Herrenschmidt CPUFREQ_SUSPENDCHANGE, &freqs); 130442d4dc3fSBenjamin Herrenschmidt adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs); 130542d4dc3fSBenjamin Herrenschmidt 130642d4dc3fSBenjamin Herrenschmidt cpu_policy->cur = cur_freq; 130742d4dc3fSBenjamin Herrenschmidt } 130842d4dc3fSBenjamin Herrenschmidt 130942d4dc3fSBenjamin Herrenschmidt out: 131042d4dc3fSBenjamin Herrenschmidt cpufreq_cpu_put(cpu_policy); 1311c9060494SDave Jones return ret; 131242d4dc3fSBenjamin Herrenschmidt } 131342d4dc3fSBenjamin Herrenschmidt 131442d4dc3fSBenjamin Herrenschmidt /** 13151da177e4SLinus Torvalds * cpufreq_resume - restore proper CPU frequency handling after resume 13161da177e4SLinus Torvalds * 13171da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 13181da177e4SLinus Torvalds * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync 131942d4dc3fSBenjamin Herrenschmidt * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are 132042d4dc3fSBenjamin Herrenschmidt * restored. 13211da177e4SLinus Torvalds */ 13221da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device *sysdev) 13231da177e4SLinus Torvalds { 13241da177e4SLinus Torvalds int cpu = sysdev->id; 1325e08f5f5bSGautham R Shenoy int ret = 0; 13261da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds dprintk("resuming cpu %u\n", cpu); 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds if (!cpu_online(cpu)) 13311da177e4SLinus Torvalds return 0; 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds /* we may be lax here as interrupts are off. Nonetheless 13341da177e4SLinus Torvalds * we need to grab the correct cpu policy, as to check 13351da177e4SLinus Torvalds * whether we really run on this CPU. 13361da177e4SLinus Torvalds */ 13371da177e4SLinus Torvalds 13381da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 13391da177e4SLinus Torvalds if (!cpu_policy) 13401da177e4SLinus Torvalds return -EINVAL; 13411da177e4SLinus Torvalds 13421da177e4SLinus Torvalds /* only handle each CPU group once */ 1343c9060494SDave Jones if (unlikely(cpu_policy->cpu != cpu)) 1344c9060494SDave Jones goto fail; 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds if (cpufreq_driver->resume) { 13471da177e4SLinus Torvalds ret = cpufreq_driver->resume(cpu_policy); 13481da177e4SLinus Torvalds if (ret) { 13491da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 13501da177e4SLinus Torvalds "step on CPU %u\n", cpu_policy->cpu); 1351c9060494SDave Jones goto fail; 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds } 13541da177e4SLinus Torvalds 13551da177e4SLinus Torvalds if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 13561da177e4SLinus Torvalds unsigned int cur_freq = 0; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds if (cpufreq_driver->get) 13591da177e4SLinus Torvalds cur_freq = cpufreq_driver->get(cpu_policy->cpu); 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds if (!cur_freq || !cpu_policy->cur) { 136242d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: resume failed to assert " 136342d4dc3fSBenjamin Herrenschmidt "current frequency is what timing core " 136442d4dc3fSBenjamin Herrenschmidt "thinks it is.\n"); 13651da177e4SLinus Torvalds goto out; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds if (unlikely(cur_freq != cpu_policy->cur)) { 13691da177e4SLinus Torvalds struct cpufreq_freqs freqs; 13701da177e4SLinus Torvalds 1371ac09f698SBenjamin Herrenschmidt if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN)) 1372b10eec22SJan Beulich dprintk("Warning: CPU frequency " 1373ac09f698SBenjamin Herrenschmidt "is %u, cpufreq assumed %u kHz.\n", 137442d4dc3fSBenjamin Herrenschmidt cur_freq, cpu_policy->cur); 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds freqs.cpu = cpu; 13771da177e4SLinus Torvalds freqs.old = cpu_policy->cur; 13781da177e4SLinus Torvalds freqs.new = cur_freq; 13791da177e4SLinus Torvalds 1380b4dfdbb3SAlan Stern srcu_notifier_call_chain( 1381e041c683SAlan Stern &cpufreq_transition_notifier_list, 138242d4dc3fSBenjamin Herrenschmidt CPUFREQ_RESUMECHANGE, &freqs); 13831da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs); 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds cpu_policy->cur = cur_freq; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds out: 13901da177e4SLinus Torvalds schedule_work(&cpu_policy->update); 1391c9060494SDave Jones fail: 13921da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 13931da177e4SLinus Torvalds return ret; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = { 13971da177e4SLinus Torvalds .add = cpufreq_add_dev, 13981da177e4SLinus Torvalds .remove = cpufreq_remove_dev, 139942d4dc3fSBenjamin Herrenschmidt .suspend = cpufreq_suspend, 14001da177e4SLinus Torvalds .resume = cpufreq_resume, 14011da177e4SLinus Torvalds }; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /********************************************************************* 14051da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 14061da177e4SLinus Torvalds *********************************************************************/ 14071da177e4SLinus Torvalds 14081da177e4SLinus Torvalds /** 14091da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 14101da177e4SLinus Torvalds * @nb: notifier function to register 14111da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 14121da177e4SLinus Torvalds * 14131da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 14141da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 14151da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 14161da177e4SLinus Torvalds * changes in cpufreq policy. 14171da177e4SLinus Torvalds * 14181da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1419e041c683SAlan Stern * blocking_notifier_chain_register. 14201da177e4SLinus Torvalds */ 14211da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 14221da177e4SLinus Torvalds { 14231da177e4SLinus Torvalds int ret; 14241da177e4SLinus Torvalds 142574212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 142674212ca4SCesar Eduardo Barros 14271da177e4SLinus Torvalds switch (list) { 14281da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1429b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1430e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 14311da177e4SLinus Torvalds break; 14321da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1433e041c683SAlan Stern ret = blocking_notifier_chain_register( 1434e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 14351da177e4SLinus Torvalds break; 14361da177e4SLinus Torvalds default: 14371da177e4SLinus Torvalds ret = -EINVAL; 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds return ret; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 14431da177e4SLinus Torvalds 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds /** 14461da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 14471da177e4SLinus Torvalds * @nb: notifier block to be unregistered 14481da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 14491da177e4SLinus Torvalds * 14501da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 14511da177e4SLinus Torvalds * 14521da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1453e041c683SAlan Stern * blocking_notifier_chain_unregister. 14541da177e4SLinus Torvalds */ 14551da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 14561da177e4SLinus Torvalds { 14571da177e4SLinus Torvalds int ret; 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds switch (list) { 14601da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1461b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1462e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 14631da177e4SLinus Torvalds break; 14641da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1465e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1466e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 14671da177e4SLinus Torvalds break; 14681da177e4SLinus Torvalds default: 14691da177e4SLinus Torvalds ret = -EINVAL; 14701da177e4SLinus Torvalds } 14711da177e4SLinus Torvalds 14721da177e4SLinus Torvalds return ret; 14731da177e4SLinus Torvalds } 14741da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds 14771da177e4SLinus Torvalds /********************************************************************* 14781da177e4SLinus Torvalds * GOVERNORS * 14791da177e4SLinus Torvalds *********************************************************************/ 14801da177e4SLinus Torvalds 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 14831da177e4SLinus Torvalds unsigned int target_freq, 14841da177e4SLinus Torvalds unsigned int relation) 14851da177e4SLinus Torvalds { 14861da177e4SLinus Torvalds int retval = -EINVAL; 1487c32b6b8eSAshok Raj 14881da177e4SLinus Torvalds dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 14891da177e4SLinus Torvalds target_freq, relation); 14901da177e4SLinus Torvalds if (cpu_online(policy->cpu) && cpufreq_driver->target) 14911da177e4SLinus Torvalds retval = cpufreq_driver->target(policy, target_freq, relation); 149290d45d17SAshok Raj 14931da177e4SLinus Torvalds return retval; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 14961da177e4SLinus Torvalds 14971da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 14981da177e4SLinus Torvalds unsigned int target_freq, 14991da177e4SLinus Torvalds unsigned int relation) 15001da177e4SLinus Torvalds { 1501f1829e4aSJulia Lawall int ret = -EINVAL; 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds policy = cpufreq_cpu_get(policy->cpu); 15041da177e4SLinus Torvalds if (!policy) 1505f1829e4aSJulia Lawall goto no_policy; 15061da177e4SLinus Torvalds 15075a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 1508f1829e4aSJulia Lawall goto fail; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 15111da177e4SLinus Torvalds 15125a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 15131da177e4SLinus Torvalds 1514f1829e4aSJulia Lawall fail: 15151da177e4SLinus Torvalds cpufreq_cpu_put(policy); 1516f1829e4aSJulia Lawall no_policy: 15171da177e4SLinus Torvalds return ret; 15181da177e4SLinus Torvalds } 15191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 15201da177e4SLinus Torvalds 1521bf0b90e3Svenkatesh.pallipadi@intel.com int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu) 1522dfde5d62SVenkatesh Pallipadi { 1523dfde5d62SVenkatesh Pallipadi int ret = 0; 1524dfde5d62SVenkatesh Pallipadi 1525dfde5d62SVenkatesh Pallipadi policy = cpufreq_cpu_get(policy->cpu); 1526dfde5d62SVenkatesh Pallipadi if (!policy) 1527dfde5d62SVenkatesh Pallipadi return -EINVAL; 1528dfde5d62SVenkatesh Pallipadi 1529bf0b90e3Svenkatesh.pallipadi@intel.com if (cpu_online(cpu) && cpufreq_driver->getavg) 1530bf0b90e3Svenkatesh.pallipadi@intel.com ret = cpufreq_driver->getavg(policy, cpu); 1531dfde5d62SVenkatesh Pallipadi 1532dfde5d62SVenkatesh Pallipadi cpufreq_cpu_put(policy); 1533dfde5d62SVenkatesh Pallipadi return ret; 1534dfde5d62SVenkatesh Pallipadi } 15355a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg); 1536dfde5d62SVenkatesh Pallipadi 1537153d7f3fSArjan van de Ven /* 1538153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1539153d7f3fSArjan van de Ven */ 15401da177e4SLinus Torvalds 1541e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1542e08f5f5bSGautham R Shenoy unsigned int event) 15431da177e4SLinus Torvalds { 1544cc993cabSDave Jones int ret; 15456afde10cSThomas Renninger 15466afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 15476afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 15486afde10cSThomas Renninger That this is the case is already ensured in Kconfig 15496afde10cSThomas Renninger */ 15506afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 15516afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 15526afde10cSThomas Renninger #else 15536afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 15546afde10cSThomas Renninger #endif 15551c256245SThomas Renninger 15561c256245SThomas Renninger if (policy->governor->max_transition_latency && 15571c256245SThomas Renninger policy->cpuinfo.transition_latency > 15581c256245SThomas Renninger policy->governor->max_transition_latency) { 15596afde10cSThomas Renninger if (!gov) 15606afde10cSThomas Renninger return -EINVAL; 15616afde10cSThomas Renninger else { 15621c256245SThomas Renninger printk(KERN_WARNING "%s governor failed, too long" 15631c256245SThomas Renninger " transition latency of HW, fallback" 15641c256245SThomas Renninger " to %s governor\n", 15651c256245SThomas Renninger policy->governor->name, 15661c256245SThomas Renninger gov->name); 15671c256245SThomas Renninger policy->governor = gov; 15681c256245SThomas Renninger } 15696afde10cSThomas Renninger } 15701da177e4SLinus Torvalds 15711da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 15721da177e4SLinus Torvalds return -EINVAL; 15731da177e4SLinus Torvalds 1574e08f5f5bSGautham R Shenoy dprintk("__cpufreq_governor for CPU %u, event %u\n", 1575e08f5f5bSGautham R Shenoy policy->cpu, event); 15761da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 15771da177e4SLinus Torvalds 1578e08f5f5bSGautham R Shenoy /* we keep one module reference alive for 1579e08f5f5bSGautham R Shenoy each CPU governed by this CPU */ 15801da177e4SLinus Torvalds if ((event != CPUFREQ_GOV_START) || ret) 15811da177e4SLinus Torvalds module_put(policy->governor->owner); 15821da177e4SLinus Torvalds if ((event == CPUFREQ_GOV_STOP) && !ret) 15831da177e4SLinus Torvalds module_put(policy->governor->owner); 15841da177e4SLinus Torvalds 15851da177e4SLinus Torvalds return ret; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 15881da177e4SLinus Torvalds 15891da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 15901da177e4SLinus Torvalds { 15913bcb09a3SJeremy Fitzhardinge int err; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds if (!governor) 15941da177e4SLinus Torvalds return -EINVAL; 15951da177e4SLinus Torvalds 15963fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 15971da177e4SLinus Torvalds 15983bcb09a3SJeremy Fitzhardinge err = -EBUSY; 15993bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 16003bcb09a3SJeremy Fitzhardinge err = 0; 16011da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 16023bcb09a3SJeremy Fitzhardinge } 16031da177e4SLinus Torvalds 16043fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 16053bcb09a3SJeremy Fitzhardinge return err; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 16081da177e4SLinus Torvalds 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 16111da177e4SLinus Torvalds { 16121da177e4SLinus Torvalds if (!governor) 16131da177e4SLinus Torvalds return; 16141da177e4SLinus Torvalds 16153fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 16161da177e4SLinus Torvalds list_del(&governor->governor_list); 16173fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 16181da177e4SLinus Torvalds return; 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 16211da177e4SLinus Torvalds 16221da177e4SLinus Torvalds 16231da177e4SLinus Torvalds 16241da177e4SLinus Torvalds /********************************************************************* 16251da177e4SLinus Torvalds * POLICY INTERFACE * 16261da177e4SLinus Torvalds *********************************************************************/ 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds /** 16291da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 163029464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 163129464f28SDave Jones * is written 16321da177e4SLinus Torvalds * 16331da177e4SLinus Torvalds * Reads the current cpufreq policy. 16341da177e4SLinus Torvalds */ 16351da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 16361da177e4SLinus Torvalds { 16371da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 16381da177e4SLinus Torvalds if (!policy) 16391da177e4SLinus Torvalds return -EINVAL; 16401da177e4SLinus Torvalds 16411da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 16421da177e4SLinus Torvalds if (!cpu_policy) 16431da177e4SLinus Torvalds return -EINVAL; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 16481da177e4SLinus Torvalds return 0; 16491da177e4SLinus Torvalds } 16501da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds 1653153d7f3fSArjan van de Ven /* 1654e08f5f5bSGautham R Shenoy * data : current policy. 1655e08f5f5bSGautham R Shenoy * policy : policy to be set. 1656153d7f3fSArjan van de Ven */ 1657e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data, 1658e08f5f5bSGautham R Shenoy struct cpufreq_policy *policy) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds int ret = 0; 16611da177e4SLinus Torvalds 16621da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 16631da177e4SLinus Torvalds dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 16641da177e4SLinus Torvalds policy->min, policy->max); 16651da177e4SLinus Torvalds 1666e08f5f5bSGautham R Shenoy memcpy(&policy->cpuinfo, &data->cpuinfo, 1667e08f5f5bSGautham R Shenoy sizeof(struct cpufreq_cpuinfo)); 16681da177e4SLinus Torvalds 166953391fa2SYi Yang if (policy->min > data->max || policy->max < data->min) { 16709c9a43edSMattia Dongili ret = -EINVAL; 16719c9a43edSMattia Dongili goto error_out; 16729c9a43edSMattia Dongili } 16739c9a43edSMattia Dongili 16741da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 16751da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 16761da177e4SLinus Torvalds if (ret) 16771da177e4SLinus Torvalds goto error_out; 16781da177e4SLinus Torvalds 16791da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 1680e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1681e041c683SAlan Stern CPUFREQ_ADJUST, policy); 16821da177e4SLinus Torvalds 16831da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 1684e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1685e041c683SAlan Stern CPUFREQ_INCOMPATIBLE, policy); 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit, 16881da177e4SLinus Torvalds which might be different to the first one */ 16891da177e4SLinus Torvalds ret = cpufreq_driver->verify(policy); 1690e041c683SAlan Stern if (ret) 16911da177e4SLinus Torvalds goto error_out; 16921da177e4SLinus Torvalds 16931da177e4SLinus Torvalds /* notification of the new policy */ 1694e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1695e041c683SAlan Stern CPUFREQ_NOTIFY, policy); 16961da177e4SLinus Torvalds 16971da177e4SLinus Torvalds data->min = policy->min; 16981da177e4SLinus Torvalds data->max = policy->max; 16991da177e4SLinus Torvalds 1700e08f5f5bSGautham R Shenoy dprintk("new min and max freqs are %u - %u kHz\n", 1701e08f5f5bSGautham R Shenoy data->min, data->max); 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds if (cpufreq_driver->setpolicy) { 17041da177e4SLinus Torvalds data->policy = policy->policy; 17051da177e4SLinus Torvalds dprintk("setting range\n"); 17061da177e4SLinus Torvalds ret = cpufreq_driver->setpolicy(policy); 17071da177e4SLinus Torvalds } else { 17081da177e4SLinus Torvalds if (policy->governor != data->governor) { 17091da177e4SLinus Torvalds /* save old, working values */ 17101da177e4SLinus Torvalds struct cpufreq_governor *old_gov = data->governor; 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds dprintk("governor switch\n"); 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds /* end old governor */ 17151da177e4SLinus Torvalds if (data->governor) 17161da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_STOP); 17171da177e4SLinus Torvalds 17181da177e4SLinus Torvalds /* start new governor */ 17191da177e4SLinus Torvalds data->governor = policy->governor; 17201da177e4SLinus Torvalds if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 17211da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 1722e08f5f5bSGautham R Shenoy dprintk("starting governor %s failed\n", 1723e08f5f5bSGautham R Shenoy data->governor->name); 17241da177e4SLinus Torvalds if (old_gov) { 17251da177e4SLinus Torvalds data->governor = old_gov; 1726e08f5f5bSGautham R Shenoy __cpufreq_governor(data, 1727e08f5f5bSGautham R Shenoy CPUFREQ_GOV_START); 17281da177e4SLinus Torvalds } 17291da177e4SLinus Torvalds ret = -EINVAL; 17301da177e4SLinus Torvalds goto error_out; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 17331da177e4SLinus Torvalds } 17341da177e4SLinus Torvalds dprintk("governor: change or update limits\n"); 17351da177e4SLinus Torvalds __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 17361da177e4SLinus Torvalds } 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds error_out: 17391da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 17401da177e4SLinus Torvalds return ret; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /** 17441da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 17451da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 17461da177e4SLinus Torvalds * 17471da177e4SLinus Torvalds * Usefull for policy notifiers which have different necessities 17481da177e4SLinus Torvalds * at different times. 17491da177e4SLinus Torvalds */ 17501da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 17511da177e4SLinus Torvalds { 17521da177e4SLinus Torvalds struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 17531da177e4SLinus Torvalds struct cpufreq_policy policy; 1754f1829e4aSJulia Lawall int ret; 17551da177e4SLinus Torvalds 1756f1829e4aSJulia Lawall if (!data) { 1757f1829e4aSJulia Lawall ret = -ENODEV; 1758f1829e4aSJulia Lawall goto no_policy; 1759f1829e4aSJulia Lawall } 17601da177e4SLinus Torvalds 1761f1829e4aSJulia Lawall if (unlikely(lock_policy_rwsem_write(cpu))) { 1762f1829e4aSJulia Lawall ret = -EINVAL; 1763f1829e4aSJulia Lawall goto fail; 1764f1829e4aSJulia Lawall } 17651da177e4SLinus Torvalds 17661da177e4SLinus Torvalds dprintk("updating policy for CPU %u\n", cpu); 17677d5e350fSDave Jones memcpy(&policy, data, sizeof(struct cpufreq_policy)); 17681da177e4SLinus Torvalds policy.min = data->user_policy.min; 17691da177e4SLinus Torvalds policy.max = data->user_policy.max; 17701da177e4SLinus Torvalds policy.policy = data->user_policy.policy; 17711da177e4SLinus Torvalds policy.governor = data->user_policy.governor; 17721da177e4SLinus Torvalds 17730961dd0dSThomas Renninger /* BIOS might change freq behind our back 17740961dd0dSThomas Renninger -> ask driver for current freq and notify governors about a change */ 17750961dd0dSThomas Renninger if (cpufreq_driver->get) { 17760961dd0dSThomas Renninger policy.cur = cpufreq_driver->get(cpu); 1777a85f7bd3SThomas Renninger if (!data->cur) { 1778a85f7bd3SThomas Renninger dprintk("Driver did not initialize current freq"); 1779a85f7bd3SThomas Renninger data->cur = policy.cur; 1780a85f7bd3SThomas Renninger } else { 17810961dd0dSThomas Renninger if (data->cur != policy.cur) 1782e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, data->cur, 1783e08f5f5bSGautham R Shenoy policy.cur); 17840961dd0dSThomas Renninger } 1785a85f7bd3SThomas Renninger } 17860961dd0dSThomas Renninger 17871da177e4SLinus Torvalds ret = __cpufreq_set_policy(data, &policy); 17881da177e4SLinus Torvalds 17895a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 17905a01f2e8SVenkatesh Pallipadi 1791f1829e4aSJulia Lawall fail: 17921da177e4SLinus Torvalds cpufreq_cpu_put(data); 1793f1829e4aSJulia Lawall no_policy: 17941da177e4SLinus Torvalds return ret; 17951da177e4SLinus Torvalds } 17961da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 17971da177e4SLinus Torvalds 1798dd184a01SSatyam Sharma static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, 1799c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1800c32b6b8eSAshok Raj { 1801c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 1802c32b6b8eSAshok Raj struct sys_device *sys_dev; 1803c32b6b8eSAshok Raj 1804c32b6b8eSAshok Raj sys_dev = get_cpu_sysdev(cpu); 1805c32b6b8eSAshok Raj if (sys_dev) { 1806c32b6b8eSAshok Raj switch (action) { 1807c32b6b8eSAshok Raj case CPU_ONLINE: 18088bb78442SRafael J. Wysocki case CPU_ONLINE_FROZEN: 1809c32b6b8eSAshok Raj cpufreq_add_dev(sys_dev); 1810c32b6b8eSAshok Raj break; 1811c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 18128bb78442SRafael J. Wysocki case CPU_DOWN_PREPARE_FROZEN: 18135a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(cpu))) 18145a01f2e8SVenkatesh Pallipadi BUG(); 18155a01f2e8SVenkatesh Pallipadi 18165a01f2e8SVenkatesh Pallipadi __cpufreq_remove_dev(sys_dev); 1817c32b6b8eSAshok Raj break; 18185a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 18198bb78442SRafael J. Wysocki case CPU_DOWN_FAILED_FROZEN: 18205a01f2e8SVenkatesh Pallipadi cpufreq_add_dev(sys_dev); 1821c32b6b8eSAshok Raj break; 1822c32b6b8eSAshok Raj } 1823c32b6b8eSAshok Raj } 1824c32b6b8eSAshok Raj return NOTIFY_OK; 1825c32b6b8eSAshok Raj } 1826c32b6b8eSAshok Raj 1827f6ebef30SSam Ravnborg static struct notifier_block __refdata cpufreq_cpu_notifier = 1828c32b6b8eSAshok Raj { 1829c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 1830c32b6b8eSAshok Raj }; 18311da177e4SLinus Torvalds 18321da177e4SLinus Torvalds /********************************************************************* 18331da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 18341da177e4SLinus Torvalds *********************************************************************/ 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds /** 18371da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 18381da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 18391da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 18401da177e4SLinus Torvalds * 18411da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 18421da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 18431da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 18441da177e4SLinus Torvalds * 18451da177e4SLinus Torvalds */ 1846221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 18471da177e4SLinus Torvalds { 18481da177e4SLinus Torvalds unsigned long flags; 18491da177e4SLinus Torvalds int ret; 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 18521da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 18531da177e4SLinus Torvalds return -EINVAL; 18541da177e4SLinus Torvalds 18551da177e4SLinus Torvalds dprintk("trying to register driver %s\n", driver_data->name); 18561da177e4SLinus Torvalds 18571da177e4SLinus Torvalds if (driver_data->setpolicy) 18581da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18611da177e4SLinus Torvalds if (cpufreq_driver) { 18621da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18631da177e4SLinus Torvalds return -EBUSY; 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds cpufreq_driver = driver_data; 18661da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18671da177e4SLinus Torvalds 18687a6aedfaSMike Travis ret = sysdev_driver_register(&cpu_sysdev_class, 18697a6aedfaSMike Travis &cpufreq_sysdev_driver); 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 18721da177e4SLinus Torvalds int i; 18731da177e4SLinus Torvalds ret = -ENODEV; 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds /* check for at least one working CPU */ 18767a6aedfaSMike Travis for (i = 0; i < nr_cpu_ids; i++) 18777a6aedfaSMike Travis if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) { 18781da177e4SLinus Torvalds ret = 0; 18797a6aedfaSMike Travis break; 18807a6aedfaSMike Travis } 18811da177e4SLinus Torvalds 18821da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 18831da177e4SLinus Torvalds if (ret) { 1884e08f5f5bSGautham R Shenoy dprintk("no CPU initialized for driver %s\n", 1885e08f5f5bSGautham R Shenoy driver_data->name); 1886e08f5f5bSGautham R Shenoy sysdev_driver_unregister(&cpu_sysdev_class, 1887e08f5f5bSGautham R Shenoy &cpufreq_sysdev_driver); 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 18901da177e4SLinus Torvalds cpufreq_driver = NULL; 18911da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds } 18941da177e4SLinus Torvalds 18951da177e4SLinus Torvalds if (!ret) { 189665edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 18971da177e4SLinus Torvalds dprintk("driver %s up and running\n", driver_data->name); 18981da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 18991da177e4SLinus Torvalds } 19001da177e4SLinus Torvalds 19014d34a67dSDave Jones return ret; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds 19061da177e4SLinus Torvalds /** 19071da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 19081da177e4SLinus Torvalds * 19091da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 19101da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 19111da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 19121da177e4SLinus Torvalds * currently not initialised. 19131da177e4SLinus Torvalds */ 1914221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 19151da177e4SLinus Torvalds { 19161da177e4SLinus Torvalds unsigned long flags; 19171da177e4SLinus Torvalds 19181da177e4SLinus Torvalds cpufreq_debug_disable_ratelimit(); 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds if (!cpufreq_driver || (driver != cpufreq_driver)) { 19211da177e4SLinus Torvalds cpufreq_debug_enable_ratelimit(); 19221da177e4SLinus Torvalds return -EINVAL; 19231da177e4SLinus Torvalds } 19241da177e4SLinus Torvalds 19251da177e4SLinus Torvalds dprintk("unregistering driver %s\n", driver->name); 19261da177e4SLinus Torvalds 19271da177e4SLinus Torvalds sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 192865edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 19291da177e4SLinus Torvalds 19301da177e4SLinus Torvalds spin_lock_irqsave(&cpufreq_driver_lock, flags); 19311da177e4SLinus Torvalds cpufreq_driver = NULL; 19321da177e4SLinus Torvalds spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds return 0; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 19375a01f2e8SVenkatesh Pallipadi 19385a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 19395a01f2e8SVenkatesh Pallipadi { 19405a01f2e8SVenkatesh Pallipadi int cpu; 19415a01f2e8SVenkatesh Pallipadi 19425a01f2e8SVenkatesh Pallipadi for_each_possible_cpu(cpu) { 19435a01f2e8SVenkatesh Pallipadi per_cpu(policy_cpu, cpu) = -1; 19445a01f2e8SVenkatesh Pallipadi init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 19455a01f2e8SVenkatesh Pallipadi } 19465a01f2e8SVenkatesh Pallipadi return 0; 19475a01f2e8SVenkatesh Pallipadi } 19485a01f2e8SVenkatesh Pallipadi 19495a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 1950