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> 6bb176f7dSViresh Kumar * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> 71da177e4SLinus Torvalds * 8c32b6b8eSAshok Raj * Oct 2005 - Ashok Raj <ashok.raj@intel.com> 9c32b6b8eSAshok Raj * Added handling for CPU hotplug 108ff69732SDave Jones * Feb 2006 - Jacob Shin <jacob.shin@amd.com> 118ff69732SDave Jones * Fix handling for CPU hotplug -- affected CPUs 12c32b6b8eSAshok Raj * 131da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 141da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 151da177e4SLinus Torvalds * published by the Free Software Foundation. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19db701151SViresh Kumar 205ff0a268SViresh Kumar #include <linux/cpu.h> 211da177e4SLinus Torvalds #include <linux/cpufreq.h> 221da177e4SLinus Torvalds #include <linux/delay.h> 231da177e4SLinus Torvalds #include <linux/device.h> 245ff0a268SViresh Kumar #include <linux/init.h> 255ff0a268SViresh Kumar #include <linux/kernel_stat.h> 265ff0a268SViresh Kumar #include <linux/module.h> 273fc54d37Sakpm@osdl.org #include <linux/mutex.h> 285ff0a268SViresh Kumar #include <linux/slab.h> 292f0aea93SViresh Kumar #include <linux/suspend.h> 305ff0a268SViresh Kumar #include <linux/tick.h> 316f4f2723SThomas Renninger #include <trace/events/power.h> 326f4f2723SThomas Renninger 331da177e4SLinus Torvalds /** 34cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 351da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 361da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 371da177e4SLinus Torvalds */ 381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback); 41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 426f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock); 43c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list); 44bb176f7dSViresh Kumar 45084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */ 46e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor); 471da177e4SLinus Torvalds 482f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 492f0aea93SViresh Kumar static bool cpufreq_suspended; 501da177e4SLinus Torvalds 519c0ebcf7SViresh Kumar static inline bool has_target(void) 529c0ebcf7SViresh Kumar { 539c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 549c0ebcf7SViresh Kumar } 559c0ebcf7SViresh Kumar 565a01f2e8SVenkatesh Pallipadi /* 576eed9404SViresh Kumar * rwsem to guarantee that cpufreq driver module doesn't unload during critical 586eed9404SViresh Kumar * sections 596eed9404SViresh Kumar */ 606eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem); 616eed9404SViresh Kumar 621da177e4SLinus Torvalds /* internal prototypes */ 6329464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy, 6429464f28SDave Jones unsigned int event); 655a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu); 6665f27f38SDavid Howells static void handle_update(struct work_struct *work); 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds /** 691da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 701da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 711da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 721da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 731da177e4SLinus Torvalds * The mutex locks both lists. 741da177e4SLinus Torvalds */ 75e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 76b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 771da177e4SLinus Torvalds 7874212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 79b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 80b4dfdbb3SAlan Stern { 81b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 8274212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 83b4dfdbb3SAlan Stern return 0; 84b4dfdbb3SAlan Stern } 85b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 861da177e4SLinus Torvalds 87a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 88da584455SViresh Kumar static int cpufreq_disabled(void) 89a7b422cdSKonrad Rzeszutek Wilk { 90a7b422cdSKonrad Rzeszutek Wilk return off; 91a7b422cdSKonrad Rzeszutek Wilk } 92a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 93a7b422cdSKonrad Rzeszutek Wilk { 94a7b422cdSKonrad Rzeszutek Wilk off = 1; 95a7b422cdSKonrad Rzeszutek Wilk } 961da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 973fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 981da177e4SLinus Torvalds 994d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1004d5dcc42SViresh Kumar { 1010b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1024d5dcc42SViresh Kumar } 1033f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1044d5dcc42SViresh Kumar 105944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 106944e9a03SViresh Kumar { 107944e9a03SViresh Kumar if (have_governor_per_policy()) 108944e9a03SViresh Kumar return &policy->kobj; 109944e9a03SViresh Kumar else 110944e9a03SViresh Kumar return cpufreq_global_kobject; 111944e9a03SViresh Kumar } 112944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 113944e9a03SViresh Kumar 11472a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 11572a4ce34SViresh Kumar { 11672a4ce34SViresh Kumar u64 idle_time; 11772a4ce34SViresh Kumar u64 cur_wall_time; 11872a4ce34SViresh Kumar u64 busy_time; 11972a4ce34SViresh Kumar 12072a4ce34SViresh Kumar cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); 12172a4ce34SViresh Kumar 12272a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 12372a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 12472a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 12572a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 12672a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 12772a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 12872a4ce34SViresh Kumar 12972a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 13072a4ce34SViresh Kumar if (wall) 13172a4ce34SViresh Kumar *wall = cputime_to_usecs(cur_wall_time); 13272a4ce34SViresh Kumar 13372a4ce34SViresh Kumar return cputime_to_usecs(idle_time); 13472a4ce34SViresh Kumar } 13572a4ce34SViresh Kumar 13672a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 13772a4ce34SViresh Kumar { 13872a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 13972a4ce34SViresh Kumar 14072a4ce34SViresh Kumar if (idle_time == -1ULL) 14172a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 14272a4ce34SViresh Kumar else if (!io_busy) 14372a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 14472a4ce34SViresh Kumar 14572a4ce34SViresh Kumar return idle_time; 14672a4ce34SViresh Kumar } 14772a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 14872a4ce34SViresh Kumar 14970e9e778SViresh Kumar /* 15070e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 15170e9e778SViresh Kumar * drivers of SMP systems. It will do following: 15270e9e778SViresh Kumar * - validate & show freq table passed 15370e9e778SViresh Kumar * - set policies transition latency 15470e9e778SViresh Kumar * - policy->cpus with all possible CPUs 15570e9e778SViresh Kumar */ 15670e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy, 15770e9e778SViresh Kumar struct cpufreq_frequency_table *table, 15870e9e778SViresh Kumar unsigned int transition_latency) 15970e9e778SViresh Kumar { 16070e9e778SViresh Kumar int ret; 16170e9e778SViresh Kumar 16270e9e778SViresh Kumar ret = cpufreq_table_validate_and_show(policy, table); 16370e9e778SViresh Kumar if (ret) { 16470e9e778SViresh Kumar pr_err("%s: invalid frequency table: %d\n", __func__, ret); 16570e9e778SViresh Kumar return ret; 16670e9e778SViresh Kumar } 16770e9e778SViresh Kumar 16870e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 16970e9e778SViresh Kumar 17070e9e778SViresh Kumar /* 17170e9e778SViresh Kumar * The driver only supports the SMP configuartion where all processors 17270e9e778SViresh Kumar * share the clock and voltage and clock. 17370e9e778SViresh Kumar */ 17470e9e778SViresh Kumar cpumask_setall(policy->cpus); 17570e9e778SViresh Kumar 17670e9e778SViresh Kumar return 0; 17770e9e778SViresh Kumar } 17870e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 17970e9e778SViresh Kumar 180652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 181652ed95dSViresh Kumar { 182652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 183652ed95dSViresh Kumar 184652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 185e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 186e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 187652ed95dSViresh Kumar return 0; 188652ed95dSViresh Kumar } 189652ed95dSViresh Kumar 190652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 191652ed95dSViresh Kumar } 192652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 193652ed95dSViresh Kumar 194e0b3165bSViresh Kumar /* Only for cpufreq core internal use */ 195e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 196e0b3165bSViresh Kumar { 197e0b3165bSViresh Kumar return per_cpu(cpufreq_cpu_data, cpu); 198e0b3165bSViresh Kumar } 199e0b3165bSViresh Kumar 2006eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2011da177e4SLinus Torvalds { 2026eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2031da177e4SLinus Torvalds unsigned long flags; 2041da177e4SLinus Torvalds 2056eed9404SViresh Kumar if (cpufreq_disabled() || (cpu >= nr_cpu_ids)) 2066eed9404SViresh Kumar return NULL; 2076eed9404SViresh Kumar 2086eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 2096eed9404SViresh Kumar return NULL; 2101da177e4SLinus Torvalds 2111da177e4SLinus Torvalds /* get the cpufreq driver */ 2120d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2131da177e4SLinus Torvalds 2146eed9404SViresh Kumar if (cpufreq_driver) { 2151da177e4SLinus Torvalds /* get the CPU */ 2163a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 2176eed9404SViresh Kumar if (policy) 2186eed9404SViresh Kumar kobject_get(&policy->kobj); 2196eed9404SViresh Kumar } 2206eed9404SViresh Kumar 2216eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2221da177e4SLinus Torvalds 2233a3e9e06SViresh Kumar if (!policy) 2246eed9404SViresh Kumar up_read(&cpufreq_rwsem); 2251da177e4SLinus Torvalds 2263a3e9e06SViresh Kumar return policy; 227a9144436SStephen Boyd } 2281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2291da177e4SLinus Torvalds 2303a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 231a9144436SStephen Boyd { 232d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 233d5aaffa9SDirk Brandewie return; 234d5aaffa9SDirk Brandewie 2356eed9404SViresh Kumar kobject_put(&policy->kobj); 2366eed9404SViresh Kumar up_read(&cpufreq_rwsem); 237a9144436SStephen Boyd } 2381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds /********************************************************************* 2411da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2421da177e4SLinus Torvalds *********************************************************************/ 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds /** 2451da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2461da177e4SLinus Torvalds * 2471da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2481da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2491da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2501da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2511da177e4SLinus Torvalds */ 2521da177e4SLinus Torvalds #ifndef CONFIG_SMP 2531da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2541da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2551da177e4SLinus Torvalds 256858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2591da177e4SLinus Torvalds return; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2621da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2631da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 264e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 265e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 2661da177e4SLinus Torvalds } 2670b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 268e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 269e08f5f5bSGautham R Shenoy ci->new); 270e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 271e837f9b5SJoe Perches loops_per_jiffy, ci->new); 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds #else 275e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 276e08f5f5bSGautham R Shenoy { 277e08f5f5bSGautham R Shenoy return; 278e08f5f5bSGautham R Shenoy } 2791da177e4SLinus Torvalds #endif 2801da177e4SLinus Torvalds 2810956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 282b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 2831da177e4SLinus Torvalds { 2841da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 2851da177e4SLinus Torvalds 286d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 287d5aaffa9SDirk Brandewie return; 288d5aaffa9SDirk Brandewie 2891c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 2902d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 291e4472cb3SDave Jones state, freqs->new); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds switch (state) { 294e4472cb3SDave Jones 2951da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 296e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 297e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 298e4472cb3SDave Jones * "old frequency". 2991da177e4SLinus Torvalds */ 3001c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 301e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 302e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 303e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 304e4472cb3SDave Jones freqs->old, policy->cur); 305e4472cb3SDave Jones freqs->old = policy->cur; 3061da177e4SLinus Torvalds } 3071da177e4SLinus Torvalds } 308b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 309e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3101da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3111da177e4SLinus Torvalds break; 312e4472cb3SDave Jones 3131da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3141da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 315e837f9b5SJoe Perches pr_debug("FREQ: %lu - CPU: %lu\n", 316e837f9b5SJoe Perches (unsigned long)freqs->new, (unsigned long)freqs->cpu); 31725e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 318b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 319e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 320e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 321e4472cb3SDave Jones policy->cur = freqs->new; 3221da177e4SLinus Torvalds break; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds } 325bb176f7dSViresh Kumar 326b43a7ffbSViresh Kumar /** 327b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 328b43a7ffbSViresh Kumar * on frequency transition. 329b43a7ffbSViresh Kumar * 330b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 331b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 332b43a7ffbSViresh Kumar * external effects. 333b43a7ffbSViresh Kumar */ 334b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy, 335b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 336b43a7ffbSViresh Kumar { 337b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 338b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 339b43a7ffbSViresh Kumar } 3401da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 3411da177e4SLinus Torvalds 342f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 343f7ba3b41SViresh Kumar void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 344f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 345f7ba3b41SViresh Kumar { 346f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 347f7ba3b41SViresh Kumar if (!transition_failed) 348f7ba3b41SViresh Kumar return; 349f7ba3b41SViresh Kumar 350f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 351f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 352f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 353f7ba3b41SViresh Kumar } 354f7ba3b41SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition); 355f7ba3b41SViresh Kumar 356*12478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 357*12478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 358*12478cf0SSrivatsa S. Bhat { 359*12478cf0SSrivatsa S. Bhat wait: 360*12478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 361*12478cf0SSrivatsa S. Bhat 362*12478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 363*12478cf0SSrivatsa S. Bhat 364*12478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 365*12478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 366*12478cf0SSrivatsa S. Bhat goto wait; 367*12478cf0SSrivatsa S. Bhat } 368*12478cf0SSrivatsa S. Bhat 369*12478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 370*12478cf0SSrivatsa S. Bhat 371*12478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 372*12478cf0SSrivatsa S. Bhat 373*12478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 374*12478cf0SSrivatsa S. Bhat } 375*12478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 376*12478cf0SSrivatsa S. Bhat 377*12478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 378*12478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 379*12478cf0SSrivatsa S. Bhat { 380*12478cf0SSrivatsa S. Bhat if (unlikely(WARN_ON(!policy->transition_ongoing))) 381*12478cf0SSrivatsa S. Bhat return; 382*12478cf0SSrivatsa S. Bhat 383*12478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 384*12478cf0SSrivatsa S. Bhat 385*12478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 386*12478cf0SSrivatsa S. Bhat 387*12478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 388*12478cf0SSrivatsa S. Bhat } 389*12478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 390*12478cf0SSrivatsa S. Bhat 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds /********************************************************************* 3931da177e4SLinus Torvalds * SYSFS INTERFACE * 3941da177e4SLinus Torvalds *********************************************************************/ 3958a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 3966f19efc0SLukasz Majewski struct attribute *attr, char *buf) 3976f19efc0SLukasz Majewski { 3986f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 3996f19efc0SLukasz Majewski } 4006f19efc0SLukasz Majewski 4016f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, 4026f19efc0SLukasz Majewski const char *buf, size_t count) 4036f19efc0SLukasz Majewski { 4046f19efc0SLukasz Majewski int ret, enable; 4056f19efc0SLukasz Majewski 4066f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 4076f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 4086f19efc0SLukasz Majewski return -EINVAL; 4096f19efc0SLukasz Majewski 4106f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 411e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 412e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 4136f19efc0SLukasz Majewski return -EINVAL; 4146f19efc0SLukasz Majewski } 4156f19efc0SLukasz Majewski 416e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 417e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 4186f19efc0SLukasz Majewski 4196f19efc0SLukasz Majewski return count; 4206f19efc0SLukasz Majewski } 4216f19efc0SLukasz Majewski define_one_global_rw(boost); 4221da177e4SLinus Torvalds 4233bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 4243bcb09a3SJeremy Fitzhardinge { 4253bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 4263bcb09a3SJeremy Fitzhardinge 4273bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 4283bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 4293bcb09a3SJeremy Fitzhardinge return t; 4303bcb09a3SJeremy Fitzhardinge 4313bcb09a3SJeremy Fitzhardinge return NULL; 4323bcb09a3SJeremy Fitzhardinge } 4333bcb09a3SJeremy Fitzhardinge 4341da177e4SLinus Torvalds /** 4351da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 4361da177e4SLinus Torvalds */ 4371da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 4381da177e4SLinus Torvalds struct cpufreq_governor **governor) 4391da177e4SLinus Torvalds { 4403bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 4413bcb09a3SJeremy Fitzhardinge 4421c3d85ddSRafael J. Wysocki if (!cpufreq_driver) 4433bcb09a3SJeremy Fitzhardinge goto out; 4443bcb09a3SJeremy Fitzhardinge 4451c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 4461da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 4471da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 4483bcb09a3SJeremy Fitzhardinge err = 0; 449e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 450e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 4511da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 4523bcb09a3SJeremy Fitzhardinge err = 0; 4531da177e4SLinus Torvalds } 4549c0ebcf7SViresh Kumar } else if (has_target()) { 4551da177e4SLinus Torvalds struct cpufreq_governor *t; 4563bcb09a3SJeremy Fitzhardinge 4573fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4583bcb09a3SJeremy Fitzhardinge 4593bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 4603bcb09a3SJeremy Fitzhardinge 461ea714970SJeremy Fitzhardinge if (t == NULL) { 462ea714970SJeremy Fitzhardinge int ret; 463ea714970SJeremy Fitzhardinge 464ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4651a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 466ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 467ea714970SJeremy Fitzhardinge 468ea714970SJeremy Fitzhardinge if (ret == 0) 469ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 470ea714970SJeremy Fitzhardinge } 471ea714970SJeremy Fitzhardinge 4723bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4731da177e4SLinus Torvalds *governor = t; 4743bcb09a3SJeremy Fitzhardinge err = 0; 4751da177e4SLinus Torvalds } 4763bcb09a3SJeremy Fitzhardinge 4773bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds out: 4803bcb09a3SJeremy Fitzhardinge return err; 4811da177e4SLinus Torvalds } 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /** 484e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 485e08f5f5bSGautham R Shenoy * print out cpufreq information 4861da177e4SLinus Torvalds * 4871da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 4881da177e4SLinus Torvalds * "unsigned int". 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds #define show_one(file_name, object) \ 4921da177e4SLinus Torvalds static ssize_t show_##file_name \ 4931da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 4941da177e4SLinus Torvalds { \ 4951da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 4981da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 4991da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 500ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 5011da177e4SLinus Torvalds show_one(scaling_min_freq, min); 5021da177e4SLinus Torvalds show_one(scaling_max_freq, max); 5031da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 5041da177e4SLinus Torvalds 505037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 5063a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 5077970e08bSThomas Renninger 5081da177e4SLinus Torvalds /** 5091da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 5101da177e4SLinus Torvalds */ 5111da177e4SLinus Torvalds #define store_one(file_name, object) \ 5121da177e4SLinus Torvalds static ssize_t store_##file_name \ 5131da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 5141da177e4SLinus Torvalds { \ 5155136fa56SSrivatsa S. Bhat int ret; \ 5161da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 5171da177e4SLinus Torvalds \ 5181da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 5191da177e4SLinus Torvalds if (ret) \ 5201da177e4SLinus Torvalds return -EINVAL; \ 5211da177e4SLinus Torvalds \ 5221da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 5231da177e4SLinus Torvalds if (ret != 1) \ 5241da177e4SLinus Torvalds return -EINVAL; \ 5251da177e4SLinus Torvalds \ 526037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 5277970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 5281da177e4SLinus Torvalds \ 5291da177e4SLinus Torvalds return ret ? ret : count; \ 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds store_one(scaling_min_freq, min); 5331da177e4SLinus Torvalds store_one(scaling_max_freq, max); 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /** 5361da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 5371da177e4SLinus Torvalds */ 538e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 539e08f5f5bSGautham R Shenoy char *buf) 5401da177e4SLinus Torvalds { 5415a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 5421da177e4SLinus Torvalds if (!cur_freq) 5431da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 5441da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /** 5481da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5491da177e4SLinus Torvalds */ 550905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 5511da177e4SLinus Torvalds { 5521da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 5531da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5541da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5551da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5561da177e4SLinus Torvalds else if (policy->governor) 5574b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 55829464f28SDave Jones policy->governor->name); 5591da177e4SLinus Torvalds return -EINVAL; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds /** 5631da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5641da177e4SLinus Torvalds */ 5651da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 5661da177e4SLinus Torvalds const char *buf, size_t count) 5671da177e4SLinus Torvalds { 5685136fa56SSrivatsa S. Bhat int ret; 5691da177e4SLinus Torvalds char str_governor[16]; 5701da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5731da177e4SLinus Torvalds if (ret) 5741da177e4SLinus Torvalds return ret; 5751da177e4SLinus Torvalds 5761da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 5771da177e4SLinus Torvalds if (ret != 1) 5781da177e4SLinus Torvalds return -EINVAL; 5791da177e4SLinus Torvalds 580e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 581e08f5f5bSGautham R Shenoy &new_policy.governor)) 5821da177e4SLinus Torvalds return -EINVAL; 5831da177e4SLinus Torvalds 584037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 5857970e08bSThomas Renninger 5867970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 5877970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 5887970e08bSThomas Renninger 589e08f5f5bSGautham R Shenoy if (ret) 590e08f5f5bSGautham R Shenoy return ret; 591e08f5f5bSGautham R Shenoy else 592e08f5f5bSGautham R Shenoy return count; 5931da177e4SLinus Torvalds } 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds /** 5961da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 5971da177e4SLinus Torvalds */ 5981da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 5991da177e4SLinus Torvalds { 6001c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 6011da177e4SLinus Torvalds } 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds /** 6041da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 6051da177e4SLinus Torvalds */ 6061da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 6071da177e4SLinus Torvalds char *buf) 6081da177e4SLinus Torvalds { 6091da177e4SLinus Torvalds ssize_t i = 0; 6101da177e4SLinus Torvalds struct cpufreq_governor *t; 6111da177e4SLinus Torvalds 6129c0ebcf7SViresh Kumar if (!has_target()) { 6131da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 6141da177e4SLinus Torvalds goto out; 6151da177e4SLinus Torvalds } 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 61829464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 61929464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 6201da177e4SLinus Torvalds goto out; 6214b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds out: 6241da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6251da177e4SLinus Torvalds return i; 6261da177e4SLinus Torvalds } 627e8628dd0SDarrick J. Wong 628f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds ssize_t i = 0; 6311da177e4SLinus Torvalds unsigned int cpu; 6321da177e4SLinus Torvalds 633835481d9SRusty Russell for_each_cpu(cpu, mask) { 6341da177e4SLinus Torvalds if (i) 6351da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 6361da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 6371da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 6381da177e4SLinus Torvalds break; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6411da177e4SLinus Torvalds return i; 6421da177e4SLinus Torvalds } 643f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 6441da177e4SLinus Torvalds 645e8628dd0SDarrick J. Wong /** 646e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 647e8628dd0SDarrick J. Wong * hw coordination is in use 648e8628dd0SDarrick J. Wong */ 649e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 650e8628dd0SDarrick J. Wong { 651f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 652e8628dd0SDarrick J. Wong } 653e8628dd0SDarrick J. Wong 654e8628dd0SDarrick J. Wong /** 655e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 656e8628dd0SDarrick J. Wong */ 657e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 658e8628dd0SDarrick J. Wong { 659f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 660e8628dd0SDarrick J. Wong } 661e8628dd0SDarrick J. Wong 6629e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 6639e76988eSVenki Pallipadi const char *buf, size_t count) 6649e76988eSVenki Pallipadi { 6659e76988eSVenki Pallipadi unsigned int freq = 0; 6669e76988eSVenki Pallipadi unsigned int ret; 6679e76988eSVenki Pallipadi 668879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 6699e76988eSVenki Pallipadi return -EINVAL; 6709e76988eSVenki Pallipadi 6719e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 6729e76988eSVenki Pallipadi if (ret != 1) 6739e76988eSVenki Pallipadi return -EINVAL; 6749e76988eSVenki Pallipadi 6759e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 6769e76988eSVenki Pallipadi 6779e76988eSVenki Pallipadi return count; 6789e76988eSVenki Pallipadi } 6799e76988eSVenki Pallipadi 6809e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 6819e76988eSVenki Pallipadi { 682879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 6839e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 6849e76988eSVenki Pallipadi 6859e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 6869e76988eSVenki Pallipadi } 6871da177e4SLinus Torvalds 688e2f74f35SThomas Renninger /** 6898bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 690e2f74f35SThomas Renninger */ 691e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 692e2f74f35SThomas Renninger { 693e2f74f35SThomas Renninger unsigned int limit; 694e2f74f35SThomas Renninger int ret; 6951c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 6961c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 697e2f74f35SThomas Renninger if (!ret) 698e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 699e2f74f35SThomas Renninger } 700e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 701e2f74f35SThomas Renninger } 702e2f74f35SThomas Renninger 7036dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 7046dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 7056dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 7066dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 7076dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 7086dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 7096dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 7106dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 7116dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 7126dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 7136dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 7146dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 7156dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 7166dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 7191da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 7201da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 721ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 7221da177e4SLinus Torvalds &scaling_min_freq.attr, 7231da177e4SLinus Torvalds &scaling_max_freq.attr, 7241da177e4SLinus Torvalds &affected_cpus.attr, 725e8628dd0SDarrick J. Wong &related_cpus.attr, 7261da177e4SLinus Torvalds &scaling_governor.attr, 7271da177e4SLinus Torvalds &scaling_driver.attr, 7281da177e4SLinus Torvalds &scaling_available_governors.attr, 7299e76988eSVenki Pallipadi &scaling_setspeed.attr, 7301da177e4SLinus Torvalds NULL 7311da177e4SLinus Torvalds }; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 7341da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7391da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 7401b750e3bSViresh Kumar ssize_t ret; 7416eed9404SViresh Kumar 7426eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 7431b750e3bSViresh Kumar return -EINVAL; 7445a01f2e8SVenkatesh Pallipadi 745ad7722daSviresh kumar down_read(&policy->rwsem); 7465a01f2e8SVenkatesh Pallipadi 747e08f5f5bSGautham R Shenoy if (fattr->show) 748e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 749e08f5f5bSGautham R Shenoy else 750e08f5f5bSGautham R Shenoy ret = -EIO; 751e08f5f5bSGautham R Shenoy 752ad7722daSviresh kumar up_read(&policy->rwsem); 7536eed9404SViresh Kumar up_read(&cpufreq_rwsem); 7541b750e3bSViresh Kumar 7551da177e4SLinus Torvalds return ret; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 7591da177e4SLinus Torvalds const char *buf, size_t count) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7621da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 763a07530b4SDave Jones ssize_t ret = -EINVAL; 7646eed9404SViresh Kumar 7654f750c93SSrivatsa S. Bhat get_online_cpus(); 7664f750c93SSrivatsa S. Bhat 7674f750c93SSrivatsa S. Bhat if (!cpu_online(policy->cpu)) 7684f750c93SSrivatsa S. Bhat goto unlock; 7694f750c93SSrivatsa S. Bhat 7706eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 7714f750c93SSrivatsa S. Bhat goto unlock; 7725a01f2e8SVenkatesh Pallipadi 773ad7722daSviresh kumar down_write(&policy->rwsem); 7745a01f2e8SVenkatesh Pallipadi 775e08f5f5bSGautham R Shenoy if (fattr->store) 776e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 777e08f5f5bSGautham R Shenoy else 778e08f5f5bSGautham R Shenoy ret = -EIO; 779e08f5f5bSGautham R Shenoy 780ad7722daSviresh kumar up_write(&policy->rwsem); 7816eed9404SViresh Kumar 7826eed9404SViresh Kumar up_read(&cpufreq_rwsem); 7834f750c93SSrivatsa S. Bhat unlock: 7844f750c93SSrivatsa S. Bhat put_online_cpus(); 7854f750c93SSrivatsa S. Bhat 7861da177e4SLinus Torvalds return ret; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7922d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 7931da177e4SLinus Torvalds complete(&policy->kobj_unregister); 7941da177e4SLinus Torvalds } 7951da177e4SLinus Torvalds 79652cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 7971da177e4SLinus Torvalds .show = show, 7981da177e4SLinus Torvalds .store = store, 7991da177e4SLinus Torvalds }; 8001da177e4SLinus Torvalds 8011da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 8021da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 8031da177e4SLinus Torvalds .default_attrs = default_attrs, 8041da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 8051da177e4SLinus Torvalds }; 8061da177e4SLinus Torvalds 8072361be23SViresh Kumar struct kobject *cpufreq_global_kobject; 8082361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 8092361be23SViresh Kumar 8102361be23SViresh Kumar static int cpufreq_global_kobject_usage; 8112361be23SViresh Kumar 8122361be23SViresh Kumar int cpufreq_get_global_kobject(void) 8132361be23SViresh Kumar { 8142361be23SViresh Kumar if (!cpufreq_global_kobject_usage++) 8152361be23SViresh Kumar return kobject_add(cpufreq_global_kobject, 8162361be23SViresh Kumar &cpu_subsys.dev_root->kobj, "%s", "cpufreq"); 8172361be23SViresh Kumar 8182361be23SViresh Kumar return 0; 8192361be23SViresh Kumar } 8202361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject); 8212361be23SViresh Kumar 8222361be23SViresh Kumar void cpufreq_put_global_kobject(void) 8232361be23SViresh Kumar { 8242361be23SViresh Kumar if (!--cpufreq_global_kobject_usage) 8252361be23SViresh Kumar kobject_del(cpufreq_global_kobject); 8262361be23SViresh Kumar } 8272361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject); 8282361be23SViresh Kumar 8292361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr) 8302361be23SViresh Kumar { 8312361be23SViresh Kumar int ret = cpufreq_get_global_kobject(); 8322361be23SViresh Kumar 8332361be23SViresh Kumar if (!ret) { 8342361be23SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, attr); 8352361be23SViresh Kumar if (ret) 8362361be23SViresh Kumar cpufreq_put_global_kobject(); 8372361be23SViresh Kumar } 8382361be23SViresh Kumar 8392361be23SViresh Kumar return ret; 8402361be23SViresh Kumar } 8412361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file); 8422361be23SViresh Kumar 8432361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr) 8442361be23SViresh Kumar { 8452361be23SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, attr); 8462361be23SViresh Kumar cpufreq_put_global_kobject(); 8472361be23SViresh Kumar } 8482361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file); 8492361be23SViresh Kumar 85019d6f7ecSDave Jones /* symlink affected CPUs */ 851308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 85219d6f7ecSDave Jones { 85319d6f7ecSDave Jones unsigned int j; 85419d6f7ecSDave Jones int ret = 0; 85519d6f7ecSDave Jones 85619d6f7ecSDave Jones for_each_cpu(j, policy->cpus) { 8578a25a2fdSKay Sievers struct device *cpu_dev; 85819d6f7ecSDave Jones 859308b60e7SViresh Kumar if (j == policy->cpu) 86019d6f7ecSDave Jones continue; 86119d6f7ecSDave Jones 862e8fdde10SViresh Kumar pr_debug("Adding link for CPU: %u\n", j); 8638a25a2fdSKay Sievers cpu_dev = get_cpu_device(j); 8648a25a2fdSKay Sievers ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 86519d6f7ecSDave Jones "cpufreq"); 86671c3461eSRafael J. Wysocki if (ret) 86771c3461eSRafael J. Wysocki break; 86819d6f7ecSDave Jones } 86919d6f7ecSDave Jones return ret; 87019d6f7ecSDave Jones } 87119d6f7ecSDave Jones 872308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy, 8738a25a2fdSKay Sievers struct device *dev) 874909a694eSDave Jones { 875909a694eSDave Jones struct freq_attr **drv_attr; 876909a694eSDave Jones int ret = 0; 877909a694eSDave Jones 878909a694eSDave Jones /* prepare interface data */ 879909a694eSDave Jones ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 8808a25a2fdSKay Sievers &dev->kobj, "cpufreq"); 881909a694eSDave Jones if (ret) 882909a694eSDave Jones return ret; 883909a694eSDave Jones 884909a694eSDave Jones /* set up files for this cpu device */ 8851c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 886909a694eSDave Jones while ((drv_attr) && (*drv_attr)) { 887909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 888909a694eSDave Jones if (ret) 8891c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 890909a694eSDave Jones drv_attr++; 891909a694eSDave Jones } 8921c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 893909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 894909a694eSDave Jones if (ret) 8951c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 896909a694eSDave Jones } 8979c0ebcf7SViresh Kumar if (has_target()) { 898909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 899909a694eSDave Jones if (ret) 9001c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 901909a694eSDave Jones } 9021c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 903e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 904e2f74f35SThomas Renninger if (ret) 9051c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 906e2f74f35SThomas Renninger } 907909a694eSDave Jones 908308b60e7SViresh Kumar ret = cpufreq_add_dev_symlink(policy); 909ecf7e461SDave Jones if (ret) 910ecf7e461SDave Jones goto err_out_kobj_put; 911ecf7e461SDave Jones 912e18f1682SSrivatsa S. Bhat return ret; 913e18f1682SSrivatsa S. Bhat 914e18f1682SSrivatsa S. Bhat err_out_kobj_put: 915e18f1682SSrivatsa S. Bhat kobject_put(&policy->kobj); 916e18f1682SSrivatsa S. Bhat wait_for_completion(&policy->kobj_unregister); 917e18f1682SSrivatsa S. Bhat return ret; 918e18f1682SSrivatsa S. Bhat } 919e18f1682SSrivatsa S. Bhat 920e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy) 921e18f1682SSrivatsa S. Bhat { 9226e2c89d1Sviresh kumar struct cpufreq_governor *gov = NULL; 923e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 924e18f1682SSrivatsa S. Bhat int ret = 0; 925e18f1682SSrivatsa S. Bhat 926d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 927a27a9ab7SJason Baron 9286e2c89d1Sviresh kumar /* Update governor of new_policy to the governor used before hotplug */ 9296e2c89d1Sviresh kumar gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu)); 9306e2c89d1Sviresh kumar if (gov) 9316e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 9326e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 9336e2c89d1Sviresh kumar else 9346e2c89d1Sviresh kumar gov = CPUFREQ_DEFAULT_GOVERNOR; 9356e2c89d1Sviresh kumar 9366e2c89d1Sviresh kumar new_policy.governor = gov; 9376e2c89d1Sviresh kumar 938a27a9ab7SJason Baron /* Use the default policy if its valid. */ 939a27a9ab7SJason Baron if (cpufreq_driver->setpolicy) 9406e2c89d1Sviresh kumar cpufreq_parse_governor(gov->name, &new_policy.policy, NULL); 941ecf7e461SDave Jones 942ecf7e461SDave Jones /* set default policy */ 943037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 944ecf7e461SDave Jones if (ret) { 9452d06d8c4SDominik Brodowski pr_debug("setting policy failed\n"); 9461c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 9471c3d85ddSRafael J. Wysocki cpufreq_driver->exit(policy); 948ecf7e461SDave Jones } 949909a694eSDave Jones } 950909a694eSDave Jones 951fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 952d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, 95342f921a6SViresh Kumar unsigned int cpu, struct device *dev) 954fcf80582SViresh Kumar { 9559c0ebcf7SViresh Kumar int ret = 0; 956fcf80582SViresh Kumar unsigned long flags; 957fcf80582SViresh Kumar 9589c0ebcf7SViresh Kumar if (has_target()) { 9593de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 9603de9bdebSViresh Kumar if (ret) { 9613de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 9623de9bdebSViresh Kumar return ret; 9633de9bdebSViresh Kumar } 9643de9bdebSViresh Kumar } 965fcf80582SViresh Kumar 966ad7722daSviresh kumar down_write(&policy->rwsem); 9672eaa3e2dSViresh Kumar 9680d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 9692eaa3e2dSViresh Kumar 970fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 971fcf80582SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = policy; 9720d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 973fcf80582SViresh Kumar 974ad7722daSviresh kumar up_write(&policy->rwsem); 9752eaa3e2dSViresh Kumar 9769c0ebcf7SViresh Kumar if (has_target()) { 977e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 978e5c87b76SStratos Karafotis if (!ret) 979e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 980e5c87b76SStratos Karafotis 981e5c87b76SStratos Karafotis if (ret) { 9823de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 9833de9bdebSViresh Kumar return ret; 9843de9bdebSViresh Kumar } 985820c6ca2SViresh Kumar } 986fcf80582SViresh Kumar 98742f921a6SViresh Kumar return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"); 988fcf80582SViresh Kumar } 989fcf80582SViresh Kumar #endif 9901da177e4SLinus Torvalds 9918414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu) 9928414809cSSrivatsa S. Bhat { 9938414809cSSrivatsa S. Bhat struct cpufreq_policy *policy; 9948414809cSSrivatsa S. Bhat unsigned long flags; 9958414809cSSrivatsa S. Bhat 99644871c9cSLan Tianyu read_lock_irqsave(&cpufreq_driver_lock, flags); 9978414809cSSrivatsa S. Bhat 9988414809cSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data_fallback, cpu); 9998414809cSSrivatsa S. Bhat 100044871c9cSLan Tianyu read_unlock_irqrestore(&cpufreq_driver_lock, flags); 10018414809cSSrivatsa S. Bhat 10026e2c89d1Sviresh kumar policy->governor = NULL; 10036e2c89d1Sviresh kumar 10048414809cSSrivatsa S. Bhat return policy; 10058414809cSSrivatsa S. Bhat } 10068414809cSSrivatsa S. Bhat 1007e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void) 1008e9698cc5SSrivatsa S. Bhat { 1009e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1010e9698cc5SSrivatsa S. Bhat 1011e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1012e9698cc5SSrivatsa S. Bhat if (!policy) 1013e9698cc5SSrivatsa S. Bhat return NULL; 1014e9698cc5SSrivatsa S. Bhat 1015e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1016e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1017e9698cc5SSrivatsa S. Bhat 1018e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1019e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1020e9698cc5SSrivatsa S. Bhat 1021c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1022ad7722daSviresh kumar init_rwsem(&policy->rwsem); 1023*12478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 1024*12478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1025ad7722daSviresh kumar 1026e9698cc5SSrivatsa S. Bhat return policy; 1027e9698cc5SSrivatsa S. Bhat 1028e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1029e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1030e9698cc5SSrivatsa S. Bhat err_free_policy: 1031e9698cc5SSrivatsa S. Bhat kfree(policy); 1032e9698cc5SSrivatsa S. Bhat 1033e9698cc5SSrivatsa S. Bhat return NULL; 1034e9698cc5SSrivatsa S. Bhat } 1035e9698cc5SSrivatsa S. Bhat 103642f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 103742f921a6SViresh Kumar { 103842f921a6SViresh Kumar struct kobject *kobj; 103942f921a6SViresh Kumar struct completion *cmp; 104042f921a6SViresh Kumar 1041fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1042fcd7af91SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 1043fcd7af91SViresh Kumar 104442f921a6SViresh Kumar down_read(&policy->rwsem); 104542f921a6SViresh Kumar kobj = &policy->kobj; 104642f921a6SViresh Kumar cmp = &policy->kobj_unregister; 104742f921a6SViresh Kumar up_read(&policy->rwsem); 104842f921a6SViresh Kumar kobject_put(kobj); 104942f921a6SViresh Kumar 105042f921a6SViresh Kumar /* 105142f921a6SViresh Kumar * We need to make sure that the underlying kobj is 105242f921a6SViresh Kumar * actually not referenced anymore by anybody before we 105342f921a6SViresh Kumar * proceed with unloading. 105442f921a6SViresh Kumar */ 105542f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 105642f921a6SViresh Kumar wait_for_completion(cmp); 105742f921a6SViresh Kumar pr_debug("wait complete\n"); 105842f921a6SViresh Kumar } 105942f921a6SViresh Kumar 1060e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy) 1061e9698cc5SSrivatsa S. Bhat { 1062e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1063e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1064e9698cc5SSrivatsa S. Bhat kfree(policy); 1065e9698cc5SSrivatsa S. Bhat } 1066e9698cc5SSrivatsa S. Bhat 10670d66b91eSSrivatsa S. Bhat static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 10680d66b91eSSrivatsa S. Bhat { 106999ec899eSSrivatsa S. Bhat if (WARN_ON(cpu == policy->cpu)) 1070cb38ed5cSSrivatsa S. Bhat return; 1071cb38ed5cSSrivatsa S. Bhat 1072ad7722daSviresh kumar down_write(&policy->rwsem); 10738efd5765SViresh Kumar 10740d66b91eSSrivatsa S. Bhat policy->last_cpu = policy->cpu; 10750d66b91eSSrivatsa S. Bhat policy->cpu = cpu; 10760d66b91eSSrivatsa S. Bhat 1077ad7722daSviresh kumar up_write(&policy->rwsem); 10788efd5765SViresh Kumar 10790d66b91eSSrivatsa S. Bhat blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 10800d66b91eSSrivatsa S. Bhat CPUFREQ_UPDATE_POLICY_CPU, policy); 10810d66b91eSSrivatsa S. Bhat } 10820d66b91eSSrivatsa S. Bhat 108396bbbe4aSViresh Kumar static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 10841da177e4SLinus Torvalds { 1085fcf80582SViresh Kumar unsigned int j, cpu = dev->id; 108665922465SViresh Kumar int ret = -ENOMEM; 10871da177e4SLinus Torvalds struct cpufreq_policy *policy; 10881da177e4SLinus Torvalds unsigned long flags; 108996bbbe4aSViresh Kumar bool recover_policy = cpufreq_suspended; 109090e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU 10911b274294SViresh Kumar struct cpufreq_policy *tpolicy; 109290e41bacSPrarit Bhargava #endif 10931da177e4SLinus Torvalds 1094c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 1095c32b6b8eSAshok Raj return 0; 1096c32b6b8eSAshok Raj 10972d06d8c4SDominik Brodowski pr_debug("adding CPU %u\n", cpu); 10981da177e4SLinus Torvalds 10991da177e4SLinus Torvalds #ifdef CONFIG_SMP 11001da177e4SLinus Torvalds /* check whether a different CPU already registered this 11011da177e4SLinus Torvalds * CPU because it is in the same boat. */ 11021da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 11031da177e4SLinus Torvalds if (unlikely(policy)) { 11048ff69732SDave Jones cpufreq_cpu_put(policy); 11051da177e4SLinus Torvalds return 0; 11061da177e4SLinus Torvalds } 11075025d628SLi Zhong #endif 1108fcf80582SViresh Kumar 11096eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 11106eed9404SViresh Kumar return 0; 11116eed9404SViresh Kumar 1112fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 1113fcf80582SViresh Kumar /* Check if this cpu was hot-unplugged earlier and has siblings */ 11140d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 11151b274294SViresh Kumar list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) { 11161b274294SViresh Kumar if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) { 11170d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 111842f921a6SViresh Kumar ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev); 11196eed9404SViresh Kumar up_read(&cpufreq_rwsem); 11206eed9404SViresh Kumar return ret; 1121fcf80582SViresh Kumar } 11222eaa3e2dSViresh Kumar } 11230d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1124fcf80582SViresh Kumar #endif 11251da177e4SLinus Torvalds 112672368d12SRafael J. Wysocki /* 112772368d12SRafael J. Wysocki * Restore the saved policy when doing light-weight init and fall back 112872368d12SRafael J. Wysocki * to the full init if that fails. 112972368d12SRafael J. Wysocki */ 113096bbbe4aSViresh Kumar policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL; 113172368d12SRafael J. Wysocki if (!policy) { 113296bbbe4aSViresh Kumar recover_policy = false; 1133e9698cc5SSrivatsa S. Bhat policy = cpufreq_policy_alloc(); 1134059019a3SDave Jones if (!policy) 11351da177e4SLinus Torvalds goto nomem_out; 113672368d12SRafael J. Wysocki } 11370d66b91eSSrivatsa S. Bhat 11380d66b91eSSrivatsa S. Bhat /* 11390d66b91eSSrivatsa S. Bhat * In the resume path, since we restore a saved policy, the assignment 11400d66b91eSSrivatsa S. Bhat * to policy->cpu is like an update of the existing policy, rather than 11410d66b91eSSrivatsa S. Bhat * the creation of a brand new one. So we need to perform this update 11420d66b91eSSrivatsa S. Bhat * by invoking update_policy_cpu(). 11430d66b91eSSrivatsa S. Bhat */ 114496bbbe4aSViresh Kumar if (recover_policy && cpu != policy->cpu) 11450d66b91eSSrivatsa S. Bhat update_policy_cpu(policy, cpu); 11460d66b91eSSrivatsa S. Bhat else 11471da177e4SLinus Torvalds policy->cpu = cpu; 11480d66b91eSSrivatsa S. Bhat 1149835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 115265f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 11551da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 11561da177e4SLinus Torvalds */ 11571c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 11581da177e4SLinus Torvalds if (ret) { 11592d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 11602eaa3e2dSViresh Kumar goto err_set_policy_cpu; 11611da177e4SLinus Torvalds } 1162643ae6e8SViresh Kumar 11635a7e56a5SViresh Kumar /* related cpus should atleast have policy->cpus */ 11645a7e56a5SViresh Kumar cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); 11655a7e56a5SViresh Kumar 11665a7e56a5SViresh Kumar /* 11675a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 11685a7e56a5SViresh Kumar * managing offline cpus here. 11695a7e56a5SViresh Kumar */ 11705a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 11715a7e56a5SViresh Kumar 117296bbbe4aSViresh Kumar if (!recover_policy) { 11735a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 11745a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 11755a7e56a5SViresh Kumar } 11765a7e56a5SViresh Kumar 11774e97b631SViresh Kumar down_write(&policy->rwsem); 1178652ed95dSViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1179652ed95dSViresh Kumar for_each_cpu(j, policy->cpus) 1180652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 1181652ed95dSViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1182652ed95dSViresh Kumar 11832ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 1184da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1185da60ce9fSViresh Kumar if (!policy->cur) { 1186da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1187da60ce9fSViresh Kumar goto err_get_freq; 1188da60ce9fSViresh Kumar } 1189da60ce9fSViresh Kumar } 1190da60ce9fSViresh Kumar 1191d3916691SViresh Kumar /* 1192d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1193d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1194d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1195d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1196d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1197d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1198d3916691SViresh Kumar * isn't found in freq-table. 1199d3916691SViresh Kumar * 1200d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1201d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1202d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1203d3916691SViresh Kumar * is initialized to zero). 1204d3916691SViresh Kumar * 1205d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1206d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1207d3916691SViresh Kumar * equal to target-freq. 1208d3916691SViresh Kumar */ 1209d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1210d3916691SViresh Kumar && has_target()) { 1211d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1212d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1213d3916691SViresh Kumar if (ret == -EINVAL) { 1214d3916691SViresh Kumar /* Warn user and fix it */ 1215d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1216d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1217d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1218d3916691SViresh Kumar CPUFREQ_RELATION_L); 1219d3916691SViresh Kumar 1220d3916691SViresh Kumar /* 1221d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1222d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1223d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1224d3916691SViresh Kumar */ 1225d3916691SViresh Kumar BUG_ON(ret); 1226d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1227d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1228d3916691SViresh Kumar } 1229d3916691SViresh Kumar } 1230d3916691SViresh Kumar 1231a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1232a1531acdSThomas Renninger CPUFREQ_START, policy); 1233a1531acdSThomas Renninger 123496bbbe4aSViresh Kumar if (!recover_policy) { 1235308b60e7SViresh Kumar ret = cpufreq_add_dev_interface(policy, dev); 123619d6f7ecSDave Jones if (ret) 12370142f9dcSAhmed S. Darwish goto err_out_unregister; 1238fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1239fcd7af91SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 12409515f4d6SViresh Kumar } 1241c88a1f8bSLukasz Majewski 1242c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1243c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1244c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12458ff69732SDave Jones 1246e18f1682SSrivatsa S. Bhat cpufreq_init_policy(policy); 1247e18f1682SSrivatsa S. Bhat 124896bbbe4aSViresh Kumar if (!recover_policy) { 124908fd8c1cSViresh Kumar policy->user_policy.policy = policy->policy; 125008fd8c1cSViresh Kumar policy->user_policy.governor = policy->governor; 125108fd8c1cSViresh Kumar } 12524e97b631SViresh Kumar up_write(&policy->rwsem); 125308fd8c1cSViresh Kumar 1254038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 12556eed9404SViresh Kumar up_read(&cpufreq_rwsem); 12566eed9404SViresh Kumar 12572d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds return 0; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds err_out_unregister: 1262652ed95dSViresh Kumar err_get_freq: 12630d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 1264474deff7SViresh Kumar for_each_cpu(j, policy->cpus) 12657a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 12660d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12671da177e4SLinus Torvalds 1268da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1269da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 12702eaa3e2dSViresh Kumar err_set_policy_cpu: 127196bbbe4aSViresh Kumar if (recover_policy) { 127272368d12SRafael J. Wysocki /* Do not leave stale fallback data behind. */ 127372368d12SRafael J. Wysocki per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL; 127442f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 127572368d12SRafael J. Wysocki } 1276e9698cc5SSrivatsa S. Bhat cpufreq_policy_free(policy); 127742f921a6SViresh Kumar 12781da177e4SLinus Torvalds nomem_out: 12796eed9404SViresh Kumar up_read(&cpufreq_rwsem); 12806eed9404SViresh Kumar 12811da177e4SLinus Torvalds return ret; 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds 1284a82fab29SSrivatsa S. Bhat /** 1285a82fab29SSrivatsa S. Bhat * cpufreq_add_dev - add a CPU device 1286a82fab29SSrivatsa S. Bhat * 1287a82fab29SSrivatsa S. Bhat * Adds the cpufreq interface for a CPU device. 1288a82fab29SSrivatsa S. Bhat * 1289a82fab29SSrivatsa S. Bhat * The Oracle says: try running cpufreq registration/unregistration concurrently 1290a82fab29SSrivatsa S. Bhat * with with cpu hotplugging and all hell will break loose. Tried to clean this 1291a82fab29SSrivatsa S. Bhat * mess up, but more thorough testing is needed. - Mathieu 1292a82fab29SSrivatsa S. Bhat */ 1293a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 1294a82fab29SSrivatsa S. Bhat { 129596bbbe4aSViresh Kumar return __cpufreq_add_dev(dev, sif); 1296a82fab29SSrivatsa S. Bhat } 1297a82fab29SSrivatsa S. Bhat 12983a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy, 129942f921a6SViresh Kumar unsigned int old_cpu) 1300f9ba680dSSrivatsa S. Bhat { 1301f9ba680dSSrivatsa S. Bhat struct device *cpu_dev; 1302f9ba680dSSrivatsa S. Bhat int ret; 1303f9ba680dSSrivatsa S. Bhat 1304f9ba680dSSrivatsa S. Bhat /* first sibling now owns the new sysfs dir */ 13059c8f1ee4SViresh Kumar cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu)); 1306a82fab29SSrivatsa S. Bhat 1307f9ba680dSSrivatsa S. Bhat sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 13083a3e9e06SViresh Kumar ret = kobject_move(&policy->kobj, &cpu_dev->kobj); 1309f9ba680dSSrivatsa S. Bhat if (ret) { 1310e837f9b5SJoe Perches pr_err("%s: Failed to move kobj: %d\n", __func__, ret); 1311f9ba680dSSrivatsa S. Bhat 1312ad7722daSviresh kumar down_write(&policy->rwsem); 13133a3e9e06SViresh Kumar cpumask_set_cpu(old_cpu, policy->cpus); 1314ad7722daSviresh kumar up_write(&policy->rwsem); 1315f9ba680dSSrivatsa S. Bhat 13163a3e9e06SViresh Kumar ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 1317f9ba680dSSrivatsa S. Bhat "cpufreq"); 1318f9ba680dSSrivatsa S. Bhat 1319f9ba680dSSrivatsa S. Bhat return -EINVAL; 1320f9ba680dSSrivatsa S. Bhat } 1321f9ba680dSSrivatsa S. Bhat 1322f9ba680dSSrivatsa S. Bhat return cpu_dev->id; 1323f9ba680dSSrivatsa S. Bhat } 1324f9ba680dSSrivatsa S. Bhat 1325cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev, 132696bbbe4aSViresh Kumar struct subsys_interface *sif) 13271da177e4SLinus Torvalds { 1328f9ba680dSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 13293de9bdebSViresh Kumar int new_cpu, ret; 13301da177e4SLinus Torvalds unsigned long flags; 13313a3e9e06SViresh Kumar struct cpufreq_policy *policy; 13321da177e4SLinus Torvalds 1333b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 13341da177e4SLinus Torvalds 13350d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 13361da177e4SLinus Torvalds 13373a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 13381da177e4SLinus Torvalds 13398414809cSSrivatsa S. Bhat /* Save the policy somewhere when doing a light-weight tear-down */ 134096bbbe4aSViresh Kumar if (cpufreq_suspended) 13413a3e9e06SViresh Kumar per_cpu(cpufreq_cpu_data_fallback, cpu) = policy; 13428414809cSSrivatsa S. Bhat 13430d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 13441da177e4SLinus Torvalds 13453a3e9e06SViresh Kumar if (!policy) { 1346b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 13471da177e4SLinus Torvalds return -EINVAL; 13481da177e4SLinus Torvalds } 13491da177e4SLinus Torvalds 13509c0ebcf7SViresh Kumar if (has_target()) { 13513de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 13523de9bdebSViresh Kumar if (ret) { 13533de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 13543de9bdebSViresh Kumar return ret; 13553de9bdebSViresh Kumar } 13563de9bdebSViresh Kumar } 13575a01f2e8SVenkatesh Pallipadi 13581c3d85ddSRafael J. Wysocki if (!cpufreq_driver->setpolicy) 1359fa69e33fSDirk Brandewie strncpy(per_cpu(cpufreq_cpu_governor, cpu), 13603a3e9e06SViresh Kumar policy->governor->name, CPUFREQ_NAME_LEN); 13611da177e4SLinus Torvalds 1362ad7722daSviresh kumar down_read(&policy->rwsem); 13633a3e9e06SViresh Kumar cpus = cpumask_weight(policy->cpus); 1364ad7722daSviresh kumar up_read(&policy->rwsem); 13651da177e4SLinus Torvalds 136661173f25SSrivatsa S. Bhat if (cpu != policy->cpu) { 136773bf0fc2SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 136873bf0fc2SViresh Kumar } else if (cpus > 1) { 136942f921a6SViresh Kumar new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu); 1370f9ba680dSSrivatsa S. Bhat if (new_cpu >= 0) { 13713a3e9e06SViresh Kumar update_policy_cpu(policy, new_cpu); 1372a82fab29SSrivatsa S. Bhat 1373bda9f552SStratos Karafotis if (!cpufreq_suspended) 137475949c9aSViresh Kumar pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n", 137575949c9aSViresh Kumar __func__, new_cpu, cpu); 13761da177e4SLinus Torvalds } 1377367dc4aaSDirk Brandewie } else if (cpufreq_driver->stop_cpu && cpufreq_driver->setpolicy) { 1378367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 13791da177e4SLinus Torvalds } 1380b8eed8afSViresh Kumar 1381cedb70afSSrivatsa S. Bhat return 0; 1382cedb70afSSrivatsa S. Bhat } 1383cedb70afSSrivatsa S. Bhat 1384cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev, 138596bbbe4aSViresh Kumar struct subsys_interface *sif) 1386cedb70afSSrivatsa S. Bhat { 1387cedb70afSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 1388cedb70afSSrivatsa S. Bhat int ret; 1389cedb70afSSrivatsa S. Bhat unsigned long flags; 1390cedb70afSSrivatsa S. Bhat struct cpufreq_policy *policy; 1391cedb70afSSrivatsa S. Bhat 1392cedb70afSSrivatsa S. Bhat read_lock_irqsave(&cpufreq_driver_lock, flags); 1393cedb70afSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data, cpu); 1394cedb70afSSrivatsa S. Bhat read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1395cedb70afSSrivatsa S. Bhat 1396cedb70afSSrivatsa S. Bhat if (!policy) { 1397cedb70afSSrivatsa S. Bhat pr_debug("%s: No cpu_data found\n", __func__); 1398cedb70afSSrivatsa S. Bhat return -EINVAL; 1399cedb70afSSrivatsa S. Bhat } 1400cedb70afSSrivatsa S. Bhat 1401ad7722daSviresh kumar down_write(&policy->rwsem); 1402cedb70afSSrivatsa S. Bhat cpus = cpumask_weight(policy->cpus); 14039c8f1ee4SViresh Kumar 14049c8f1ee4SViresh Kumar if (cpus > 1) 14059c8f1ee4SViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 1406ad7722daSviresh kumar up_write(&policy->rwsem); 1407cedb70afSSrivatsa S. Bhat 1408b8eed8afSViresh Kumar /* If cpu is last user of policy, free policy */ 1409b8eed8afSViresh Kumar if (cpus == 1) { 14109c0ebcf7SViresh Kumar if (has_target()) { 14113de9bdebSViresh Kumar ret = __cpufreq_governor(policy, 14123de9bdebSViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 14133de9bdebSViresh Kumar if (ret) { 14143de9bdebSViresh Kumar pr_err("%s: Failed to exit governor\n", 14153de9bdebSViresh Kumar __func__); 14163de9bdebSViresh Kumar return ret; 14173de9bdebSViresh Kumar } 14183de9bdebSViresh Kumar } 14192a998599SRafael J. Wysocki 142096bbbe4aSViresh Kumar if (!cpufreq_suspended) 142142f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 14221da177e4SLinus Torvalds 14238414809cSSrivatsa S. Bhat /* 14248414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 14258414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 14268414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 14278414809cSSrivatsa S. Bhat */ 14281c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 14293a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 143027ecddc2SJacob Shin 14319515f4d6SViresh Kumar /* Remove policy from list of active policies */ 14329515f4d6SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 14339515f4d6SViresh Kumar list_del(&policy->policy_list); 14349515f4d6SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 14359515f4d6SViresh Kumar 143696bbbe4aSViresh Kumar if (!cpufreq_suspended) 14373a3e9e06SViresh Kumar cpufreq_policy_free(policy); 1438e5c87b76SStratos Karafotis } else if (has_target()) { 1439e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 1440e5c87b76SStratos Karafotis if (!ret) 1441e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1442e5c87b76SStratos Karafotis 1443e5c87b76SStratos Karafotis if (ret) { 1444e5c87b76SStratos Karafotis pr_err("%s: Failed to start governor\n", __func__); 14453de9bdebSViresh Kumar return ret; 14463de9bdebSViresh Kumar } 1447b8eed8afSViresh Kumar } 14481da177e4SLinus Torvalds 1449474deff7SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 14501da177e4SLinus Torvalds return 0; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 1453cedb70afSSrivatsa S. Bhat /** 145427a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1455cedb70afSSrivatsa S. Bhat * 1456cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1457cedb70afSSrivatsa S. Bhat */ 14588a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 14595a01f2e8SVenkatesh Pallipadi { 14608a25a2fdSKay Sievers unsigned int cpu = dev->id; 146127a862e9SViresh Kumar int ret; 1462ec28297aSVenki Pallipadi 1463ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1464ec28297aSVenki Pallipadi return 0; 1465ec28297aSVenki Pallipadi 146696bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_prepare(dev, sif); 146727a862e9SViresh Kumar 146827a862e9SViresh Kumar if (!ret) 146996bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_finish(dev, sif); 147027a862e9SViresh Kumar 147127a862e9SViresh Kumar return ret; 14725a01f2e8SVenkatesh Pallipadi } 14735a01f2e8SVenkatesh Pallipadi 147465f27f38SDavid Howells static void handle_update(struct work_struct *work) 14751da177e4SLinus Torvalds { 147665f27f38SDavid Howells struct cpufreq_policy *policy = 147765f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 147865f27f38SDavid Howells unsigned int cpu = policy->cpu; 14792d06d8c4SDominik Brodowski pr_debug("handle_update for cpu %u called\n", cpu); 14801da177e4SLinus Torvalds cpufreq_update_policy(cpu); 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds /** 1484bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1485bb176f7dSViresh Kumar * in deep trouble. 14861da177e4SLinus Torvalds * @cpu: cpu number 14871da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 14881da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 14891da177e4SLinus Torvalds * 149029464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 149129464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 14921da177e4SLinus Torvalds */ 1493e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1494e08f5f5bSGautham R Shenoy unsigned int new_freq) 14951da177e4SLinus Torvalds { 1496b43a7ffbSViresh Kumar struct cpufreq_policy *policy; 14971da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1498b43a7ffbSViresh Kumar unsigned long flags; 1499b43a7ffbSViresh Kumar 1500e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1501e837f9b5SJoe Perches old_freq, new_freq); 15021da177e4SLinus Torvalds 15031da177e4SLinus Torvalds freqs.old = old_freq; 15041da177e4SLinus Torvalds freqs.new = new_freq; 1505b43a7ffbSViresh Kumar 1506b43a7ffbSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 1507b43a7ffbSViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 1508b43a7ffbSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1509b43a7ffbSViresh Kumar 1510b43a7ffbSViresh Kumar cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); 1511b43a7ffbSViresh Kumar cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds /** 15154ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 151695235ca2SVenkatesh Pallipadi * @cpu: CPU number 151795235ca2SVenkatesh Pallipadi * 151895235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 151995235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 152095235ca2SVenkatesh Pallipadi */ 152195235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 152295235ca2SVenkatesh Pallipadi { 15239e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1524e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 152595235ca2SVenkatesh Pallipadi 15261c3d85ddSRafael J. Wysocki if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 15271c3d85ddSRafael J. Wysocki return cpufreq_driver->get(cpu); 15289e21ba8bSDirk Brandewie 15299e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 153095235ca2SVenkatesh Pallipadi if (policy) { 1531e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 153295235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 153395235ca2SVenkatesh Pallipadi } 153495235ca2SVenkatesh Pallipadi 15354d34a67dSDave Jones return ret_freq; 153695235ca2SVenkatesh Pallipadi } 153795235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 153895235ca2SVenkatesh Pallipadi 15393d737108SJesse Barnes /** 15403d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 15413d737108SJesse Barnes * @cpu: CPU number 15423d737108SJesse Barnes * 15433d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 15443d737108SJesse Barnes */ 15453d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 15463d737108SJesse Barnes { 15473d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15483d737108SJesse Barnes unsigned int ret_freq = 0; 15493d737108SJesse Barnes 15503d737108SJesse Barnes if (policy) { 15513d737108SJesse Barnes ret_freq = policy->max; 15523d737108SJesse Barnes cpufreq_cpu_put(policy); 15533d737108SJesse Barnes } 15543d737108SJesse Barnes 15553d737108SJesse Barnes return ret_freq; 15563d737108SJesse Barnes } 15573d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 15583d737108SJesse Barnes 15595a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 15601da177e4SLinus Torvalds { 15617a6aedfaSMike Travis struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1562e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 15631da177e4SLinus Torvalds 15641c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 15654d34a67dSDave Jones return ret_freq; 15661da177e4SLinus Torvalds 15671c3d85ddSRafael J. Wysocki ret_freq = cpufreq_driver->get(cpu); 15681da177e4SLinus Torvalds 1569e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 15701c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1571e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1572e08f5f5bSGautham R Shenoy saved value exists */ 1573e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1574e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 15751da177e4SLinus Torvalds schedule_work(&policy->update); 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 15794d34a67dSDave Jones return ret_freq; 15805a01f2e8SVenkatesh Pallipadi } 15811da177e4SLinus Torvalds 15825a01f2e8SVenkatesh Pallipadi /** 15835a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 15845a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 15855a01f2e8SVenkatesh Pallipadi * 15865a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 15875a01f2e8SVenkatesh Pallipadi */ 15885a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 15895a01f2e8SVenkatesh Pallipadi { 1590999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15915a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 15925a01f2e8SVenkatesh Pallipadi 1593999976e0SAaron Plattner if (policy) { 1594ad7722daSviresh kumar down_read(&policy->rwsem); 15955a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 1596ad7722daSviresh kumar up_read(&policy->rwsem); 1597999976e0SAaron Plattner 1598999976e0SAaron Plattner cpufreq_cpu_put(policy); 1599999976e0SAaron Plattner } 16006eed9404SViresh Kumar 16014d34a67dSDave Jones return ret_freq; 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 16041da177e4SLinus Torvalds 16058a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 16068a25a2fdSKay Sievers .name = "cpufreq", 16078a25a2fdSKay Sievers .subsys = &cpu_subsys, 16088a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 16098a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1610e00e56dfSRafael J. Wysocki }; 1611e00e56dfSRafael J. Wysocki 1612e28867eaSViresh Kumar /* 1613e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1614e28867eaSViresh Kumar * during suspend.. 161542d4dc3fSBenjamin Herrenschmidt */ 1616e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 161742d4dc3fSBenjamin Herrenschmidt { 1618e28867eaSViresh Kumar int ret; 16194bc5d341SDave Jones 1620e28867eaSViresh Kumar if (!policy->suspend_freq) { 1621e28867eaSViresh Kumar pr_err("%s: suspend_freq can't be zero\n", __func__); 1622e28867eaSViresh Kumar return -EINVAL; 162342d4dc3fSBenjamin Herrenschmidt } 162442d4dc3fSBenjamin Herrenschmidt 1625e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1626e28867eaSViresh Kumar policy->suspend_freq); 1627e28867eaSViresh Kumar 1628e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1629e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1630e28867eaSViresh Kumar if (ret) 1631e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1632e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1633e28867eaSViresh Kumar 1634c9060494SDave Jones return ret; 163542d4dc3fSBenjamin Herrenschmidt } 1636e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 163742d4dc3fSBenjamin Herrenschmidt 163842d4dc3fSBenjamin Herrenschmidt /** 16392f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 16401da177e4SLinus Torvalds * 16412f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 16422f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 16432f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 16442f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 16451da177e4SLinus Torvalds */ 16462f0aea93SViresh Kumar void cpufreq_suspend(void) 16471da177e4SLinus Torvalds { 16483a3e9e06SViresh Kumar struct cpufreq_policy *policy; 16491da177e4SLinus Torvalds 16502f0aea93SViresh Kumar if (!cpufreq_driver) 1651e00e56dfSRafael J. Wysocki return; 16521da177e4SLinus Torvalds 16532f0aea93SViresh Kumar if (!has_target()) 16542f0aea93SViresh Kumar return; 16551da177e4SLinus Torvalds 16562f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 16572f0aea93SViresh Kumar 16582f0aea93SViresh Kumar list_for_each_entry(policy, &cpufreq_policy_list, policy_list) { 16592f0aea93SViresh Kumar if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP)) 16602f0aea93SViresh Kumar pr_err("%s: Failed to stop governor for policy: %p\n", 16612f0aea93SViresh Kumar __func__, policy); 16622f0aea93SViresh Kumar else if (cpufreq_driver->suspend 16632f0aea93SViresh Kumar && cpufreq_driver->suspend(policy)) 16642f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 16652f0aea93SViresh Kumar policy); 16661da177e4SLinus Torvalds } 16671da177e4SLinus Torvalds 16682f0aea93SViresh Kumar cpufreq_suspended = true; 16691da177e4SLinus Torvalds } 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /** 16722f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 16731da177e4SLinus Torvalds * 16742f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 16752f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 16761da177e4SLinus Torvalds */ 16772f0aea93SViresh Kumar void cpufreq_resume(void) 16781da177e4SLinus Torvalds { 16791da177e4SLinus Torvalds struct cpufreq_policy *policy; 16801da177e4SLinus Torvalds 16812f0aea93SViresh Kumar if (!cpufreq_driver) 16821da177e4SLinus Torvalds return; 16831da177e4SLinus Torvalds 16842f0aea93SViresh Kumar if (!has_target()) 16852f0aea93SViresh Kumar return; 16861da177e4SLinus Torvalds 16872f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 16882f0aea93SViresh Kumar 16892f0aea93SViresh Kumar cpufreq_suspended = false; 16902f0aea93SViresh Kumar 16912f0aea93SViresh Kumar list_for_each_entry(policy, &cpufreq_policy_list, policy_list) { 16920c5aa405SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) 16930c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 16940c5aa405SViresh Kumar policy); 16950c5aa405SViresh Kumar else if (__cpufreq_governor(policy, CPUFREQ_GOV_START) 16962f0aea93SViresh Kumar || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS)) 16972f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 16982f0aea93SViresh Kumar __func__, policy); 16992f0aea93SViresh Kumar 17002f0aea93SViresh Kumar /* 17012f0aea93SViresh Kumar * schedule call cpufreq_update_policy() for boot CPU, i.e. last 17022f0aea93SViresh Kumar * policy in list. It will verify that the current freq is in 17032f0aea93SViresh Kumar * sync with what we believe it to be. 17042f0aea93SViresh Kumar */ 17052f0aea93SViresh Kumar if (list_is_last(&policy->policy_list, &cpufreq_policy_list)) 17063a3e9e06SViresh Kumar schedule_work(&policy->update); 17071da177e4SLinus Torvalds } 17082f0aea93SViresh Kumar } 17091da177e4SLinus Torvalds 17109d95046eSBorislav Petkov /** 17119d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17129d95046eSBorislav Petkov * 17139d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17149d95046eSBorislav Petkov * or NULL, if none. 17159d95046eSBorislav Petkov */ 17169d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17179d95046eSBorislav Petkov { 17181c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17191c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17201c3d85ddSRafael J. Wysocki 17211c3d85ddSRafael J. Wysocki return NULL; 17229d95046eSBorislav Petkov } 17239d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds /********************************************************************* 17261da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 17271da177e4SLinus Torvalds *********************************************************************/ 17281da177e4SLinus Torvalds 17291da177e4SLinus Torvalds /** 17301da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 17311da177e4SLinus Torvalds * @nb: notifier function to register 17321da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17331da177e4SLinus Torvalds * 17341da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 17351da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 17361da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 17371da177e4SLinus Torvalds * changes in cpufreq policy. 17381da177e4SLinus Torvalds * 17391da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1740e041c683SAlan Stern * blocking_notifier_chain_register. 17411da177e4SLinus Torvalds */ 17421da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 17431da177e4SLinus Torvalds { 17441da177e4SLinus Torvalds int ret; 17451da177e4SLinus Torvalds 1746d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1747d5aaffa9SDirk Brandewie return -EINVAL; 1748d5aaffa9SDirk Brandewie 174974212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 175074212ca4SCesar Eduardo Barros 17511da177e4SLinus Torvalds switch (list) { 17521da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1753b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1754e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17551da177e4SLinus Torvalds break; 17561da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1757e041c683SAlan Stern ret = blocking_notifier_chain_register( 1758e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 17591da177e4SLinus Torvalds break; 17601da177e4SLinus Torvalds default: 17611da177e4SLinus Torvalds ret = -EINVAL; 17621da177e4SLinus Torvalds } 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds return ret; 17651da177e4SLinus Torvalds } 17661da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds /** 17691da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 17701da177e4SLinus Torvalds * @nb: notifier block to be unregistered 17711da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17721da177e4SLinus Torvalds * 17731da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 17741da177e4SLinus Torvalds * 17751da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1776e041c683SAlan Stern * blocking_notifier_chain_unregister. 17771da177e4SLinus Torvalds */ 17781da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 17791da177e4SLinus Torvalds { 17801da177e4SLinus Torvalds int ret; 17811da177e4SLinus Torvalds 1782d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1783d5aaffa9SDirk Brandewie return -EINVAL; 1784d5aaffa9SDirk Brandewie 17851da177e4SLinus Torvalds switch (list) { 17861da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1787b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1788e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17891da177e4SLinus Torvalds break; 17901da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1791e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1792e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 17931da177e4SLinus Torvalds break; 17941da177e4SLinus Torvalds default: 17951da177e4SLinus Torvalds ret = -EINVAL; 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds return ret; 17991da177e4SLinus Torvalds } 18001da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds /********************************************************************* 18041da177e4SLinus Torvalds * GOVERNORS * 18051da177e4SLinus Torvalds *********************************************************************/ 18061da177e4SLinus Torvalds 18071da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 18081da177e4SLinus Torvalds unsigned int target_freq, 18091da177e4SLinus Torvalds unsigned int relation) 18101da177e4SLinus Torvalds { 18111da177e4SLinus Torvalds int retval = -EINVAL; 18127249924eSViresh Kumar unsigned int old_target_freq = target_freq; 1813c32b6b8eSAshok Raj 1814a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1815a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1816a7b422cdSKonrad Rzeszutek Wilk 18177249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 18187249924eSViresh Kumar if (target_freq > policy->max) 18197249924eSViresh Kumar target_freq = policy->max; 18207249924eSViresh Kumar if (target_freq < policy->min) 18217249924eSViresh Kumar target_freq = policy->min; 18227249924eSViresh Kumar 18237249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 18247249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 18255a1c0228SViresh Kumar 18269c0ebcf7SViresh Kumar /* 18279c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 18289c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 18299c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 18309c0ebcf7SViresh Kumar * calls. 18319c0ebcf7SViresh Kumar */ 18325a1c0228SViresh Kumar if (target_freq == policy->cur) 18335a1c0228SViresh Kumar return 0; 18345a1c0228SViresh Kumar 18351c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 18361c3d85ddSRafael J. Wysocki retval = cpufreq_driver->target(policy, target_freq, relation); 18379c0ebcf7SViresh Kumar else if (cpufreq_driver->target_index) { 18389c0ebcf7SViresh Kumar struct cpufreq_frequency_table *freq_table; 1839d4019f0aSViresh Kumar struct cpufreq_freqs freqs; 1840d4019f0aSViresh Kumar bool notify; 18419c0ebcf7SViresh Kumar int index; 184290d45d17SAshok Raj 18439c0ebcf7SViresh Kumar freq_table = cpufreq_frequency_get_table(policy->cpu); 18449c0ebcf7SViresh Kumar if (unlikely(!freq_table)) { 18459c0ebcf7SViresh Kumar pr_err("%s: Unable to find freq_table\n", __func__); 18469c0ebcf7SViresh Kumar goto out; 18479c0ebcf7SViresh Kumar } 18489c0ebcf7SViresh Kumar 18499c0ebcf7SViresh Kumar retval = cpufreq_frequency_table_target(policy, freq_table, 18509c0ebcf7SViresh Kumar target_freq, relation, &index); 18519c0ebcf7SViresh Kumar if (unlikely(retval)) { 18529c0ebcf7SViresh Kumar pr_err("%s: Unable to find matching freq\n", __func__); 18539c0ebcf7SViresh Kumar goto out; 18549c0ebcf7SViresh Kumar } 18559c0ebcf7SViresh Kumar 1856d4019f0aSViresh Kumar if (freq_table[index].frequency == policy->cur) { 18579c0ebcf7SViresh Kumar retval = 0; 1858d4019f0aSViresh Kumar goto out; 1859d4019f0aSViresh Kumar } 1860d4019f0aSViresh Kumar 1861d4019f0aSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 1862d4019f0aSViresh Kumar 1863d4019f0aSViresh Kumar if (notify) { 1864d4019f0aSViresh Kumar freqs.old = policy->cur; 1865d4019f0aSViresh Kumar freqs.new = freq_table[index].frequency; 1866d4019f0aSViresh Kumar freqs.flags = 0; 1867d4019f0aSViresh Kumar 1868d4019f0aSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 1869e837f9b5SJoe Perches __func__, policy->cpu, freqs.old, freqs.new); 1870d4019f0aSViresh Kumar 1871d4019f0aSViresh Kumar cpufreq_notify_transition(policy, &freqs, 1872d4019f0aSViresh Kumar CPUFREQ_PRECHANGE); 1873d4019f0aSViresh Kumar } 1874d4019f0aSViresh Kumar 18759c0ebcf7SViresh Kumar retval = cpufreq_driver->target_index(policy, index); 1876d4019f0aSViresh Kumar if (retval) 1877d4019f0aSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", 1878d4019f0aSViresh Kumar __func__, retval); 1879d4019f0aSViresh Kumar 1880ab1b1c4eSViresh Kumar if (notify) 1881ab1b1c4eSViresh Kumar cpufreq_notify_post_transition(policy, &freqs, retval); 18829c0ebcf7SViresh Kumar } 18839c0ebcf7SViresh Kumar 18849c0ebcf7SViresh Kumar out: 18851da177e4SLinus Torvalds return retval; 18861da177e4SLinus Torvalds } 18871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 18901da177e4SLinus Torvalds unsigned int target_freq, 18911da177e4SLinus Torvalds unsigned int relation) 18921da177e4SLinus Torvalds { 1893f1829e4aSJulia Lawall int ret = -EINVAL; 18941da177e4SLinus Torvalds 1895ad7722daSviresh kumar down_write(&policy->rwsem); 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 18981da177e4SLinus Torvalds 1899ad7722daSviresh kumar up_write(&policy->rwsem); 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds return ret; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 19041da177e4SLinus Torvalds 1905153d7f3fSArjan van de Ven /* 1906153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1907153d7f3fSArjan van de Ven */ 19081da177e4SLinus Torvalds 1909e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1910e08f5f5bSGautham R Shenoy unsigned int event) 19111da177e4SLinus Torvalds { 1912cc993cabSDave Jones int ret; 19136afde10cSThomas Renninger 19146afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 19156afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 19166afde10cSThomas Renninger That this is the case is already ensured in Kconfig 19176afde10cSThomas Renninger */ 19186afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 19196afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 19206afde10cSThomas Renninger #else 19216afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 19226afde10cSThomas Renninger #endif 19231c256245SThomas Renninger 19242f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 19252f0aea93SViresh Kumar if (cpufreq_suspended) 19262f0aea93SViresh Kumar return 0; 19272f0aea93SViresh Kumar 19281c256245SThomas Renninger if (policy->governor->max_transition_latency && 19291c256245SThomas Renninger policy->cpuinfo.transition_latency > 19301c256245SThomas Renninger policy->governor->max_transition_latency) { 19316afde10cSThomas Renninger if (!gov) 19326afde10cSThomas Renninger return -EINVAL; 19336afde10cSThomas Renninger else { 1934e837f9b5SJoe Perches pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", 1935e837f9b5SJoe Perches policy->governor->name, gov->name); 19361c256245SThomas Renninger policy->governor = gov; 19371c256245SThomas Renninger } 19386afde10cSThomas Renninger } 19391da177e4SLinus Torvalds 1940fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 19411da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 19421da177e4SLinus Torvalds return -EINVAL; 19431da177e4SLinus Torvalds 19442d06d8c4SDominik Brodowski pr_debug("__cpufreq_governor for CPU %u, event %u\n", 1945e08f5f5bSGautham R Shenoy policy->cpu, event); 194695731ebbSXiaoguang Chen 194795731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 194856d07db2SSrivatsa S. Bhat if ((policy->governor_enabled && event == CPUFREQ_GOV_START) 1949f73d3933SViresh Kumar || (!policy->governor_enabled 1950f73d3933SViresh Kumar && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) { 195195731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 195295731ebbSXiaoguang Chen return -EBUSY; 195395731ebbSXiaoguang Chen } 195495731ebbSXiaoguang Chen 195595731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 195695731ebbSXiaoguang Chen policy->governor_enabled = false; 195795731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 195895731ebbSXiaoguang Chen policy->governor_enabled = true; 195995731ebbSXiaoguang Chen 196095731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 196195731ebbSXiaoguang Chen 19621da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 19631da177e4SLinus Torvalds 19644d5dcc42SViresh Kumar if (!ret) { 19654d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 19668e53695fSViresh Kumar policy->governor->initialized++; 19674d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 19688e53695fSViresh Kumar policy->governor->initialized--; 196995731ebbSXiaoguang Chen } else { 197095731ebbSXiaoguang Chen /* Restore original values */ 197195731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 197295731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 197395731ebbSXiaoguang Chen policy->governor_enabled = true; 197495731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 197595731ebbSXiaoguang Chen policy->governor_enabled = false; 197695731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 19774d5dcc42SViresh Kumar } 1978b394058fSViresh Kumar 1979fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 1980fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 19811da177e4SLinus Torvalds module_put(policy->governor->owner); 19821da177e4SLinus Torvalds 19831da177e4SLinus Torvalds return ret; 19841da177e4SLinus Torvalds } 19851da177e4SLinus Torvalds 19861da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 19871da177e4SLinus Torvalds { 19883bcb09a3SJeremy Fitzhardinge int err; 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds if (!governor) 19911da177e4SLinus Torvalds return -EINVAL; 19921da177e4SLinus Torvalds 1993a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1994a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1995a7b422cdSKonrad Rzeszutek Wilk 19963fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 19971da177e4SLinus Torvalds 1998b394058fSViresh Kumar governor->initialized = 0; 19993bcb09a3SJeremy Fitzhardinge err = -EBUSY; 20003bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 20013bcb09a3SJeremy Fitzhardinge err = 0; 20021da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 20033bcb09a3SJeremy Fitzhardinge } 20041da177e4SLinus Torvalds 20053fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20063bcb09a3SJeremy Fitzhardinge return err; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 20111da177e4SLinus Torvalds { 201290e41bacSPrarit Bhargava int cpu; 201390e41bacSPrarit Bhargava 20141da177e4SLinus Torvalds if (!governor) 20151da177e4SLinus Torvalds return; 20161da177e4SLinus Torvalds 2017a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2018a7b422cdSKonrad Rzeszutek Wilk return; 2019a7b422cdSKonrad Rzeszutek Wilk 202090e41bacSPrarit Bhargava for_each_present_cpu(cpu) { 202190e41bacSPrarit Bhargava if (cpu_online(cpu)) 202290e41bacSPrarit Bhargava continue; 202390e41bacSPrarit Bhargava if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name)) 202490e41bacSPrarit Bhargava strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0"); 202590e41bacSPrarit Bhargava } 202690e41bacSPrarit Bhargava 20273fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 20281da177e4SLinus Torvalds list_del(&governor->governor_list); 20293fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20301da177e4SLinus Torvalds return; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds 20351da177e4SLinus Torvalds /********************************************************************* 20361da177e4SLinus Torvalds * POLICY INTERFACE * 20371da177e4SLinus Torvalds *********************************************************************/ 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds /** 20401da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 204129464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 204229464f28SDave Jones * is written 20431da177e4SLinus Torvalds * 20441da177e4SLinus Torvalds * Reads the current cpufreq policy. 20451da177e4SLinus Torvalds */ 20461da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 20471da177e4SLinus Torvalds { 20481da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 20491da177e4SLinus Torvalds if (!policy) 20501da177e4SLinus Torvalds return -EINVAL; 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 20531da177e4SLinus Torvalds if (!cpu_policy) 20541da177e4SLinus Torvalds return -EINVAL; 20551da177e4SLinus Torvalds 2056d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 20591da177e4SLinus Torvalds return 0; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 20621da177e4SLinus Torvalds 2063153d7f3fSArjan van de Ven /* 2064037ce839SViresh Kumar * policy : current policy. 2065037ce839SViresh Kumar * new_policy: policy to be set. 2066153d7f3fSArjan van de Ven */ 2067037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 20683a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 20691da177e4SLinus Torvalds { 2070d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2071d9a789c7SRafael J. Wysocki int ret; 20721da177e4SLinus Torvalds 2073e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2074e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 20751da177e4SLinus Torvalds 2076d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 20771da177e4SLinus Torvalds 2078d9a789c7SRafael J. Wysocki if (new_policy->min > policy->max || new_policy->max < policy->min) 2079d9a789c7SRafael J. Wysocki return -EINVAL; 20809c9a43edSMattia Dongili 20811da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 20823a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 20831da177e4SLinus Torvalds if (ret) 2084d9a789c7SRafael J. Wysocki return ret; 20851da177e4SLinus Torvalds 20861da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2087e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 20883a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 2091e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 20923a3e9e06SViresh Kumar CPUFREQ_INCOMPATIBLE, new_policy); 20931da177e4SLinus Torvalds 2094bb176f7dSViresh Kumar /* 2095bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2096bb176f7dSViresh Kumar * different to the first one 2097bb176f7dSViresh Kumar */ 20983a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2099e041c683SAlan Stern if (ret) 2100d9a789c7SRafael J. Wysocki return ret; 21011da177e4SLinus Torvalds 21021da177e4SLinus Torvalds /* notification of the new policy */ 2103e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21043a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 21051da177e4SLinus Torvalds 21063a3e9e06SViresh Kumar policy->min = new_policy->min; 21073a3e9e06SViresh Kumar policy->max = new_policy->max; 21081da177e4SLinus Torvalds 21092d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 21103a3e9e06SViresh Kumar policy->min, policy->max); 21111da177e4SLinus Torvalds 21121c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 21133a3e9e06SViresh Kumar policy->policy = new_policy->policy; 21142d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2115d9a789c7SRafael J. Wysocki return cpufreq_driver->setpolicy(new_policy); 2116d9a789c7SRafael J. Wysocki } 2117d9a789c7SRafael J. Wysocki 2118d9a789c7SRafael J. Wysocki if (new_policy->governor == policy->governor) 2119d9a789c7SRafael J. Wysocki goto out; 21201da177e4SLinus Torvalds 21212d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 21221da177e4SLinus Torvalds 2123d9a789c7SRafael J. Wysocki /* save old, working values */ 2124d9a789c7SRafael J. Wysocki old_gov = policy->governor; 21251da177e4SLinus Torvalds /* end old governor */ 2126d9a789c7SRafael J. Wysocki if (old_gov) { 21273a3e9e06SViresh Kumar __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 2128ad7722daSviresh kumar up_write(&policy->rwsem); 2129d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2130ad7722daSviresh kumar down_write(&policy->rwsem); 21317bd353a9SViresh Kumar } 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds /* start new governor */ 21343a3e9e06SViresh Kumar policy->governor = new_policy->governor; 21353a3e9e06SViresh Kumar if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) { 2136d9a789c7SRafael J. Wysocki if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) 2137d9a789c7SRafael J. Wysocki goto out; 2138d9a789c7SRafael J. Wysocki 2139ad7722daSviresh kumar up_write(&policy->rwsem); 2140d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2141ad7722daSviresh kumar down_write(&policy->rwsem); 2142955ef483SViresh Kumar } 21437bd353a9SViresh Kumar 21441da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2145d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 21461da177e4SLinus Torvalds if (old_gov) { 21473a3e9e06SViresh Kumar policy->governor = old_gov; 2148d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); 2149d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_START); 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152d9a789c7SRafael J. Wysocki return -EINVAL; 2153d9a789c7SRafael J. Wysocki 2154d9a789c7SRafael J. Wysocki out: 2155d9a789c7SRafael J. Wysocki pr_debug("governor: change or update limits\n"); 2156d9a789c7SRafael J. Wysocki return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds 21591da177e4SLinus Torvalds /** 21601da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 21611da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 21621da177e4SLinus Torvalds * 216325985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 21641da177e4SLinus Torvalds * at different times. 21651da177e4SLinus Torvalds */ 21661da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 21671da177e4SLinus Torvalds { 21683a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 21693a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 2170f1829e4aSJulia Lawall int ret; 21711da177e4SLinus Torvalds 21723a3e9e06SViresh Kumar if (!policy) { 2173f1829e4aSJulia Lawall ret = -ENODEV; 2174f1829e4aSJulia Lawall goto no_policy; 2175f1829e4aSJulia Lawall } 21761da177e4SLinus Torvalds 2177ad7722daSviresh kumar down_write(&policy->rwsem); 21781da177e4SLinus Torvalds 21792d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 2180d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 21813a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 21823a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 21833a3e9e06SViresh Kumar new_policy.policy = policy->user_policy.policy; 21843a3e9e06SViresh Kumar new_policy.governor = policy->user_policy.governor; 21851da177e4SLinus Torvalds 2186bb176f7dSViresh Kumar /* 2187bb176f7dSViresh Kumar * BIOS might change freq behind our back 2188bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2189bb176f7dSViresh Kumar */ 21902ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 21913a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 2192bd0fa9bbSViresh Kumar if (WARN_ON(!new_policy.cur)) { 2193bd0fa9bbSViresh Kumar ret = -EIO; 2194bd0fa9bbSViresh Kumar goto no_policy; 2195bd0fa9bbSViresh Kumar } 2196bd0fa9bbSViresh Kumar 21973a3e9e06SViresh Kumar if (!policy->cur) { 2198e837f9b5SJoe Perches pr_debug("Driver did not initialize current freq\n"); 21993a3e9e06SViresh Kumar policy->cur = new_policy.cur; 2200a85f7bd3SThomas Renninger } else { 22019c0ebcf7SViresh Kumar if (policy->cur != new_policy.cur && has_target()) 22023a3e9e06SViresh Kumar cpufreq_out_of_sync(cpu, policy->cur, 22033a3e9e06SViresh Kumar new_policy.cur); 22040961dd0dSThomas Renninger } 2205a85f7bd3SThomas Renninger } 22060961dd0dSThomas Renninger 2207037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 22081da177e4SLinus Torvalds 2209ad7722daSviresh kumar up_write(&policy->rwsem); 22105a01f2e8SVenkatesh Pallipadi 22113a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 2212f1829e4aSJulia Lawall no_policy: 22131da177e4SLinus Torvalds return ret; 22141da177e4SLinus Torvalds } 22151da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 22161da177e4SLinus Torvalds 22172760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 2218c32b6b8eSAshok Raj unsigned long action, void *hcpu) 2219c32b6b8eSAshok Raj { 2220c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 22218a25a2fdSKay Sievers struct device *dev; 2222c32b6b8eSAshok Raj 22238a25a2fdSKay Sievers dev = get_cpu_device(cpu); 22248a25a2fdSKay Sievers if (dev) { 22255302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 2226c32b6b8eSAshok Raj case CPU_ONLINE: 222796bbbe4aSViresh Kumar __cpufreq_add_dev(dev, NULL); 2228c32b6b8eSAshok Raj break; 22295302c3fbSSrivatsa S. Bhat 2230c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 223196bbbe4aSViresh Kumar __cpufreq_remove_dev_prepare(dev, NULL); 22321aee40acSSrivatsa S. Bhat break; 22331aee40acSSrivatsa S. Bhat 22341aee40acSSrivatsa S. Bhat case CPU_POST_DEAD: 223596bbbe4aSViresh Kumar __cpufreq_remove_dev_finish(dev, NULL); 2236c32b6b8eSAshok Raj break; 22375302c3fbSSrivatsa S. Bhat 22385a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 223996bbbe4aSViresh Kumar __cpufreq_add_dev(dev, NULL); 2240c32b6b8eSAshok Raj break; 2241c32b6b8eSAshok Raj } 2242c32b6b8eSAshok Raj } 2243c32b6b8eSAshok Raj return NOTIFY_OK; 2244c32b6b8eSAshok Raj } 2245c32b6b8eSAshok Raj 22469c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2247c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2248c32b6b8eSAshok Raj }; 22491da177e4SLinus Torvalds 22501da177e4SLinus Torvalds /********************************************************************* 22516f19efc0SLukasz Majewski * BOOST * 22526f19efc0SLukasz Majewski *********************************************************************/ 22536f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 22546f19efc0SLukasz Majewski { 22556f19efc0SLukasz Majewski struct cpufreq_frequency_table *freq_table; 22566f19efc0SLukasz Majewski struct cpufreq_policy *policy; 22576f19efc0SLukasz Majewski int ret = -EINVAL; 22586f19efc0SLukasz Majewski 22596f19efc0SLukasz Majewski list_for_each_entry(policy, &cpufreq_policy_list, policy_list) { 22606f19efc0SLukasz Majewski freq_table = cpufreq_frequency_get_table(policy->cpu); 22616f19efc0SLukasz Majewski if (freq_table) { 22626f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 22636f19efc0SLukasz Majewski freq_table); 22646f19efc0SLukasz Majewski if (ret) { 22656f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 22666f19efc0SLukasz Majewski __func__); 22676f19efc0SLukasz Majewski break; 22686f19efc0SLukasz Majewski } 22696f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 22706f19efc0SLukasz Majewski __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 22716f19efc0SLukasz Majewski } 22726f19efc0SLukasz Majewski } 22736f19efc0SLukasz Majewski 22746f19efc0SLukasz Majewski return ret; 22756f19efc0SLukasz Majewski } 22766f19efc0SLukasz Majewski 22776f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 22786f19efc0SLukasz Majewski { 22796f19efc0SLukasz Majewski unsigned long flags; 22806f19efc0SLukasz Majewski int ret = 0; 22816f19efc0SLukasz Majewski 22826f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 22836f19efc0SLukasz Majewski return 0; 22846f19efc0SLukasz Majewski 22856f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 22866f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 22876f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 22886f19efc0SLukasz Majewski 22896f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 22906f19efc0SLukasz Majewski if (ret) { 22916f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 22926f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 22936f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 22946f19efc0SLukasz Majewski 2295e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2296e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 22976f19efc0SLukasz Majewski } 22986f19efc0SLukasz Majewski 22996f19efc0SLukasz Majewski return ret; 23006f19efc0SLukasz Majewski } 23016f19efc0SLukasz Majewski 23026f19efc0SLukasz Majewski int cpufreq_boost_supported(void) 23036f19efc0SLukasz Majewski { 23046f19efc0SLukasz Majewski if (likely(cpufreq_driver)) 23056f19efc0SLukasz Majewski return cpufreq_driver->boost_supported; 23066f19efc0SLukasz Majewski 23076f19efc0SLukasz Majewski return 0; 23086f19efc0SLukasz Majewski } 23096f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported); 23106f19efc0SLukasz Majewski 23116f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 23126f19efc0SLukasz Majewski { 23136f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 23146f19efc0SLukasz Majewski } 23156f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 23166f19efc0SLukasz Majewski 23176f19efc0SLukasz Majewski /********************************************************************* 23181da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 23191da177e4SLinus Torvalds *********************************************************************/ 23201da177e4SLinus Torvalds 23211da177e4SLinus Torvalds /** 23221da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 23231da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 23241da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 23251da177e4SLinus Torvalds * 23261da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 23271da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 23281da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 23291da177e4SLinus Torvalds * 23301da177e4SLinus Torvalds */ 2331221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 23321da177e4SLinus Torvalds { 23331da177e4SLinus Torvalds unsigned long flags; 23341da177e4SLinus Torvalds int ret; 23351da177e4SLinus Torvalds 2336a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2337a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2338a7b422cdSKonrad Rzeszutek Wilk 23391da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 23409c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 23419832235fSRafael J. Wysocki driver_data->target) || 23429832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 23439832235fSRafael J. Wysocki driver_data->target))) 23441da177e4SLinus Torvalds return -EINVAL; 23451da177e4SLinus Torvalds 23462d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds if (driver_data->setpolicy) 23491da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 23501da177e4SLinus Torvalds 23510d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 23521c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 23530d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23544dea5806SYinghai Lu return -EEXIST; 23551da177e4SLinus Torvalds } 23561c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 23570d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23581da177e4SLinus Torvalds 23596f19efc0SLukasz Majewski if (cpufreq_boost_supported()) { 23606f19efc0SLukasz Majewski /* 23616f19efc0SLukasz Majewski * Check if driver provides function to enable boost - 23626f19efc0SLukasz Majewski * if not, use cpufreq_boost_set_sw as default 23636f19efc0SLukasz Majewski */ 23646f19efc0SLukasz Majewski if (!cpufreq_driver->set_boost) 23656f19efc0SLukasz Majewski cpufreq_driver->set_boost = cpufreq_boost_set_sw; 23666f19efc0SLukasz Majewski 23676f19efc0SLukasz Majewski ret = cpufreq_sysfs_create_file(&boost.attr); 23686f19efc0SLukasz Majewski if (ret) { 23696f19efc0SLukasz Majewski pr_err("%s: cannot register global BOOST sysfs file\n", 23706f19efc0SLukasz Majewski __func__); 23716f19efc0SLukasz Majewski goto err_null_driver; 23726f19efc0SLukasz Majewski } 23736f19efc0SLukasz Majewski } 23746f19efc0SLukasz Majewski 23758a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 23768f5bc2abSJiri Slaby if (ret) 23776f19efc0SLukasz Majewski goto err_boost_unreg; 23781da177e4SLinus Torvalds 23791c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) { 23801da177e4SLinus Torvalds int i; 23811da177e4SLinus Torvalds ret = -ENODEV; 23821da177e4SLinus Torvalds 23831da177e4SLinus Torvalds /* check for at least one working CPU */ 23847a6aedfaSMike Travis for (i = 0; i < nr_cpu_ids; i++) 23857a6aedfaSMike Travis if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) { 23861da177e4SLinus Torvalds ret = 0; 23877a6aedfaSMike Travis break; 23887a6aedfaSMike Travis } 23891da177e4SLinus Torvalds 23901da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 23911da177e4SLinus Torvalds if (ret) { 23922d06d8c4SDominik Brodowski pr_debug("no CPU initialized for driver %s\n", 2393e08f5f5bSGautham R Shenoy driver_data->name); 23948a25a2fdSKay Sievers goto err_if_unreg; 23951da177e4SLinus Torvalds } 23961da177e4SLinus Torvalds } 23971da177e4SLinus Torvalds 239865edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 23992d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 24001da177e4SLinus Torvalds 24018f5bc2abSJiri Slaby return 0; 24028a25a2fdSKay Sievers err_if_unreg: 24038a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 24046f19efc0SLukasz Majewski err_boost_unreg: 24056f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 24066f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 24078f5bc2abSJiri Slaby err_null_driver: 24080d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24091c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24100d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24114d34a67dSDave Jones return ret; 24121da177e4SLinus Torvalds } 24131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 24141da177e4SLinus Torvalds 24151da177e4SLinus Torvalds /** 24161da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 24171da177e4SLinus Torvalds * 24181da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 24191da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 24201da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 24211da177e4SLinus Torvalds * currently not initialised. 24221da177e4SLinus Torvalds */ 2423221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 24241da177e4SLinus Torvalds { 24251da177e4SLinus Torvalds unsigned long flags; 24261da177e4SLinus Torvalds 24271c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 24281da177e4SLinus Torvalds return -EINVAL; 24291da177e4SLinus Torvalds 24302d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 24311da177e4SLinus Torvalds 24328a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 24336f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 24346f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 24356f19efc0SLukasz Majewski 243665edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 24371da177e4SLinus Torvalds 24386eed9404SViresh Kumar down_write(&cpufreq_rwsem); 24390d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24406eed9404SViresh Kumar 24411c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24426eed9404SViresh Kumar 24430d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24446eed9404SViresh Kumar up_write(&cpufreq_rwsem); 24451da177e4SLinus Torvalds 24461da177e4SLinus Torvalds return 0; 24471da177e4SLinus Torvalds } 24481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 24495a01f2e8SVenkatesh Pallipadi 24505a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 24515a01f2e8SVenkatesh Pallipadi { 2452a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2453a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2454a7b422cdSKonrad Rzeszutek Wilk 24552361be23SViresh Kumar cpufreq_global_kobject = kobject_create(); 24568aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 24578aa84ad8SThomas Renninger 24585a01f2e8SVenkatesh Pallipadi return 0; 24595a01f2e8SVenkatesh Pallipadi } 24605a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2461