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 24027e289dcSStratos Karafotis bool cpufreq_next_valid(struct cpufreq_frequency_table **pos) 24127e289dcSStratos Karafotis { 24227e289dcSStratos Karafotis while ((*pos)->frequency != CPUFREQ_TABLE_END) 24327e289dcSStratos Karafotis if ((*pos)->frequency != CPUFREQ_ENTRY_INVALID) 24427e289dcSStratos Karafotis return true; 24527e289dcSStratos Karafotis else 24627e289dcSStratos Karafotis (*pos)++; 24727e289dcSStratos Karafotis return false; 24827e289dcSStratos Karafotis } 24927e289dcSStratos Karafotis EXPORT_SYMBOL_GPL(cpufreq_next_valid); 25027e289dcSStratos Karafotis 2511da177e4SLinus Torvalds /********************************************************************* 2521da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2531da177e4SLinus Torvalds *********************************************************************/ 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds /** 2561da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2571da177e4SLinus Torvalds * 2581da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2591da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2601da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2611da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2621da177e4SLinus Torvalds */ 2631da177e4SLinus Torvalds #ifndef CONFIG_SMP 2641da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2651da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2661da177e4SLinus Torvalds 267858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2701da177e4SLinus Torvalds return; 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2731da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2741da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 275e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 276e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 2771da177e4SLinus Torvalds } 2780b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 279e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 280e08f5f5bSGautham R Shenoy ci->new); 281e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 282e837f9b5SJoe Perches loops_per_jiffy, ci->new); 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds #else 286e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 287e08f5f5bSGautham R Shenoy { 288e08f5f5bSGautham R Shenoy return; 289e08f5f5bSGautham R Shenoy } 2901da177e4SLinus Torvalds #endif 2911da177e4SLinus Torvalds 2920956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 293b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 2961da177e4SLinus Torvalds 297d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 298d5aaffa9SDirk Brandewie return; 299d5aaffa9SDirk Brandewie 3001c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3012d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 302e4472cb3SDave Jones state, freqs->new); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds switch (state) { 305e4472cb3SDave Jones 3061da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 307e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 308e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 309e4472cb3SDave Jones * "old frequency". 3101da177e4SLinus Torvalds */ 3111c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 312e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 313e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 314e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 315e4472cb3SDave Jones freqs->old, policy->cur); 316e4472cb3SDave Jones freqs->old = policy->cur; 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds } 319b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 320e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3211da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3221da177e4SLinus Torvalds break; 323e4472cb3SDave Jones 3241da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3251da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 326e837f9b5SJoe Perches pr_debug("FREQ: %lu - CPU: %lu\n", 327e837f9b5SJoe Perches (unsigned long)freqs->new, (unsigned long)freqs->cpu); 32825e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 329b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 330e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 331e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 332e4472cb3SDave Jones policy->cur = freqs->new; 3331da177e4SLinus Torvalds break; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds } 336bb176f7dSViresh Kumar 337b43a7ffbSViresh Kumar /** 338b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 339b43a7ffbSViresh Kumar * on frequency transition. 340b43a7ffbSViresh Kumar * 341b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 342b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 343b43a7ffbSViresh Kumar * external effects. 344b43a7ffbSViresh Kumar */ 345236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 346b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 347b43a7ffbSViresh Kumar { 348b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 349b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 350b43a7ffbSViresh Kumar } 3511da177e4SLinus Torvalds 352f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 353236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 354f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 355f7ba3b41SViresh Kumar { 356f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 357f7ba3b41SViresh Kumar if (!transition_failed) 358f7ba3b41SViresh Kumar return; 359f7ba3b41SViresh Kumar 360f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 361f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 362f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 363f7ba3b41SViresh Kumar } 364f7ba3b41SViresh Kumar 36512478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 36612478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 36712478cf0SSrivatsa S. Bhat { 368*ca654dc3SSrivatsa S. Bhat 369*ca654dc3SSrivatsa S. Bhat /* 370*ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 371*ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 372*ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 373*ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 374*ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 375*ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 376*ca654dc3SSrivatsa S. Bhat */ 377*ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 378*ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 379*ca654dc3SSrivatsa S. Bhat 38012478cf0SSrivatsa S. Bhat wait: 38112478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 38212478cf0SSrivatsa S. Bhat 38312478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 38412478cf0SSrivatsa S. Bhat 38512478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 38612478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 38712478cf0SSrivatsa S. Bhat goto wait; 38812478cf0SSrivatsa S. Bhat } 38912478cf0SSrivatsa S. Bhat 39012478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 391*ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 39212478cf0SSrivatsa S. Bhat 39312478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 39412478cf0SSrivatsa S. Bhat 39512478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 39612478cf0SSrivatsa S. Bhat } 39712478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 39812478cf0SSrivatsa S. Bhat 39912478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 40012478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 40112478cf0SSrivatsa S. Bhat { 40212478cf0SSrivatsa S. Bhat if (unlikely(WARN_ON(!policy->transition_ongoing))) 40312478cf0SSrivatsa S. Bhat return; 40412478cf0SSrivatsa S. Bhat 40512478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 40612478cf0SSrivatsa S. Bhat 40712478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 408*ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 40912478cf0SSrivatsa S. Bhat 41012478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 41112478cf0SSrivatsa S. Bhat } 41212478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 41312478cf0SSrivatsa S. Bhat 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /********************************************************************* 4161da177e4SLinus Torvalds * SYSFS INTERFACE * 4171da177e4SLinus Torvalds *********************************************************************/ 4188a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 4196f19efc0SLukasz Majewski struct attribute *attr, char *buf) 4206f19efc0SLukasz Majewski { 4216f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 4226f19efc0SLukasz Majewski } 4236f19efc0SLukasz Majewski 4246f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, 4256f19efc0SLukasz Majewski const char *buf, size_t count) 4266f19efc0SLukasz Majewski { 4276f19efc0SLukasz Majewski int ret, enable; 4286f19efc0SLukasz Majewski 4296f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 4306f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 4316f19efc0SLukasz Majewski return -EINVAL; 4326f19efc0SLukasz Majewski 4336f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 434e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 435e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 4366f19efc0SLukasz Majewski return -EINVAL; 4376f19efc0SLukasz Majewski } 4386f19efc0SLukasz Majewski 439e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 440e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 4416f19efc0SLukasz Majewski 4426f19efc0SLukasz Majewski return count; 4436f19efc0SLukasz Majewski } 4446f19efc0SLukasz Majewski define_one_global_rw(boost); 4451da177e4SLinus Torvalds 4463bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 4473bcb09a3SJeremy Fitzhardinge { 4483bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 4493bcb09a3SJeremy Fitzhardinge 4503bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 4513bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 4523bcb09a3SJeremy Fitzhardinge return t; 4533bcb09a3SJeremy Fitzhardinge 4543bcb09a3SJeremy Fitzhardinge return NULL; 4553bcb09a3SJeremy Fitzhardinge } 4563bcb09a3SJeremy Fitzhardinge 4571da177e4SLinus Torvalds /** 4581da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 4591da177e4SLinus Torvalds */ 4601da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 4611da177e4SLinus Torvalds struct cpufreq_governor **governor) 4621da177e4SLinus Torvalds { 4633bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 4643bcb09a3SJeremy Fitzhardinge 4651c3d85ddSRafael J. Wysocki if (!cpufreq_driver) 4663bcb09a3SJeremy Fitzhardinge goto out; 4673bcb09a3SJeremy Fitzhardinge 4681c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 4691da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 4701da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 4713bcb09a3SJeremy Fitzhardinge err = 0; 472e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 473e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 4741da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 4753bcb09a3SJeremy Fitzhardinge err = 0; 4761da177e4SLinus Torvalds } 4779c0ebcf7SViresh Kumar } else if (has_target()) { 4781da177e4SLinus Torvalds struct cpufreq_governor *t; 4793bcb09a3SJeremy Fitzhardinge 4803fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4813bcb09a3SJeremy Fitzhardinge 4823bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 4833bcb09a3SJeremy Fitzhardinge 484ea714970SJeremy Fitzhardinge if (t == NULL) { 485ea714970SJeremy Fitzhardinge int ret; 486ea714970SJeremy Fitzhardinge 487ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4881a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 489ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 490ea714970SJeremy Fitzhardinge 491ea714970SJeremy Fitzhardinge if (ret == 0) 492ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 493ea714970SJeremy Fitzhardinge } 494ea714970SJeremy Fitzhardinge 4953bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4961da177e4SLinus Torvalds *governor = t; 4973bcb09a3SJeremy Fitzhardinge err = 0; 4981da177e4SLinus Torvalds } 4993bcb09a3SJeremy Fitzhardinge 5003bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds out: 5033bcb09a3SJeremy Fitzhardinge return err; 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /** 507e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 508e08f5f5bSGautham R Shenoy * print out cpufreq information 5091da177e4SLinus Torvalds * 5101da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 5111da177e4SLinus Torvalds * "unsigned int". 5121da177e4SLinus Torvalds */ 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds #define show_one(file_name, object) \ 5151da177e4SLinus Torvalds static ssize_t show_##file_name \ 5161da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 5171da177e4SLinus Torvalds { \ 5181da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 5191da177e4SLinus Torvalds } 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 5221da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 523ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 5241da177e4SLinus Torvalds show_one(scaling_min_freq, min); 5251da177e4SLinus Torvalds show_one(scaling_max_freq, max); 5261da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 5271da177e4SLinus Torvalds 528037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 5293a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 5307970e08bSThomas Renninger 5311da177e4SLinus Torvalds /** 5321da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 5331da177e4SLinus Torvalds */ 5341da177e4SLinus Torvalds #define store_one(file_name, object) \ 5351da177e4SLinus Torvalds static ssize_t store_##file_name \ 5361da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 5371da177e4SLinus Torvalds { \ 5385136fa56SSrivatsa S. Bhat int ret; \ 5391da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 5401da177e4SLinus Torvalds \ 5411da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 5421da177e4SLinus Torvalds if (ret) \ 5431da177e4SLinus Torvalds return -EINVAL; \ 5441da177e4SLinus Torvalds \ 5451da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 5461da177e4SLinus Torvalds if (ret != 1) \ 5471da177e4SLinus Torvalds return -EINVAL; \ 5481da177e4SLinus Torvalds \ 549037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 5507970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 5511da177e4SLinus Torvalds \ 5521da177e4SLinus Torvalds return ret ? ret : count; \ 5531da177e4SLinus Torvalds } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds store_one(scaling_min_freq, min); 5561da177e4SLinus Torvalds store_one(scaling_max_freq, max); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds /** 5591da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 5601da177e4SLinus Torvalds */ 561e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 562e08f5f5bSGautham R Shenoy char *buf) 5631da177e4SLinus Torvalds { 5645a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 5651da177e4SLinus Torvalds if (!cur_freq) 5661da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 5671da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds /** 5711da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5721da177e4SLinus Torvalds */ 573905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 5741da177e4SLinus Torvalds { 5751da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 5761da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5771da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5781da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5791da177e4SLinus Torvalds else if (policy->governor) 5804b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 58129464f28SDave Jones policy->governor->name); 5821da177e4SLinus Torvalds return -EINVAL; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds /** 5861da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5871da177e4SLinus Torvalds */ 5881da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 5891da177e4SLinus Torvalds const char *buf, size_t count) 5901da177e4SLinus Torvalds { 5915136fa56SSrivatsa S. Bhat int ret; 5921da177e4SLinus Torvalds char str_governor[16]; 5931da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5961da177e4SLinus Torvalds if (ret) 5971da177e4SLinus Torvalds return ret; 5981da177e4SLinus Torvalds 5991da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 6001da177e4SLinus Torvalds if (ret != 1) 6011da177e4SLinus Torvalds return -EINVAL; 6021da177e4SLinus Torvalds 603e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 604e08f5f5bSGautham R Shenoy &new_policy.governor)) 6051da177e4SLinus Torvalds return -EINVAL; 6061da177e4SLinus Torvalds 607037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 6087970e08bSThomas Renninger 6097970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 6107970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 6117970e08bSThomas Renninger 612e08f5f5bSGautham R Shenoy if (ret) 613e08f5f5bSGautham R Shenoy return ret; 614e08f5f5bSGautham R Shenoy else 615e08f5f5bSGautham R Shenoy return count; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds /** 6191da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 6201da177e4SLinus Torvalds */ 6211da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 6221da177e4SLinus Torvalds { 6231c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds /** 6271da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 6281da177e4SLinus Torvalds */ 6291da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 6301da177e4SLinus Torvalds char *buf) 6311da177e4SLinus Torvalds { 6321da177e4SLinus Torvalds ssize_t i = 0; 6331da177e4SLinus Torvalds struct cpufreq_governor *t; 6341da177e4SLinus Torvalds 6359c0ebcf7SViresh Kumar if (!has_target()) { 6361da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 6371da177e4SLinus Torvalds goto out; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 64129464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 64229464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 6431da177e4SLinus Torvalds goto out; 6444b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 6451da177e4SLinus Torvalds } 6461da177e4SLinus Torvalds out: 6471da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6481da177e4SLinus Torvalds return i; 6491da177e4SLinus Torvalds } 650e8628dd0SDarrick J. Wong 651f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds ssize_t i = 0; 6541da177e4SLinus Torvalds unsigned int cpu; 6551da177e4SLinus Torvalds 656835481d9SRusty Russell for_each_cpu(cpu, mask) { 6571da177e4SLinus Torvalds if (i) 6581da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 6591da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 6601da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 6611da177e4SLinus Torvalds break; 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6641da177e4SLinus Torvalds return i; 6651da177e4SLinus Torvalds } 666f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 6671da177e4SLinus Torvalds 668e8628dd0SDarrick J. Wong /** 669e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 670e8628dd0SDarrick J. Wong * hw coordination is in use 671e8628dd0SDarrick J. Wong */ 672e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 673e8628dd0SDarrick J. Wong { 674f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 675e8628dd0SDarrick J. Wong } 676e8628dd0SDarrick J. Wong 677e8628dd0SDarrick J. Wong /** 678e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 679e8628dd0SDarrick J. Wong */ 680e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 681e8628dd0SDarrick J. Wong { 682f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 683e8628dd0SDarrick J. Wong } 684e8628dd0SDarrick J. Wong 6859e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 6869e76988eSVenki Pallipadi const char *buf, size_t count) 6879e76988eSVenki Pallipadi { 6889e76988eSVenki Pallipadi unsigned int freq = 0; 6899e76988eSVenki Pallipadi unsigned int ret; 6909e76988eSVenki Pallipadi 691879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 6929e76988eSVenki Pallipadi return -EINVAL; 6939e76988eSVenki Pallipadi 6949e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 6959e76988eSVenki Pallipadi if (ret != 1) 6969e76988eSVenki Pallipadi return -EINVAL; 6979e76988eSVenki Pallipadi 6989e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 6999e76988eSVenki Pallipadi 7009e76988eSVenki Pallipadi return count; 7019e76988eSVenki Pallipadi } 7029e76988eSVenki Pallipadi 7039e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 7049e76988eSVenki Pallipadi { 705879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 7069e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 7079e76988eSVenki Pallipadi 7089e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 7099e76988eSVenki Pallipadi } 7101da177e4SLinus Torvalds 711e2f74f35SThomas Renninger /** 7128bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 713e2f74f35SThomas Renninger */ 714e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 715e2f74f35SThomas Renninger { 716e2f74f35SThomas Renninger unsigned int limit; 717e2f74f35SThomas Renninger int ret; 7181c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 7191c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 720e2f74f35SThomas Renninger if (!ret) 721e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 722e2f74f35SThomas Renninger } 723e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 724e2f74f35SThomas Renninger } 725e2f74f35SThomas Renninger 7266dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 7276dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 7286dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 7296dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 7306dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 7316dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 7326dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 7336dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 7346dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 7356dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 7366dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 7376dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 7386dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 7396dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 7421da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 7431da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 744ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 7451da177e4SLinus Torvalds &scaling_min_freq.attr, 7461da177e4SLinus Torvalds &scaling_max_freq.attr, 7471da177e4SLinus Torvalds &affected_cpus.attr, 748e8628dd0SDarrick J. Wong &related_cpus.attr, 7491da177e4SLinus Torvalds &scaling_governor.attr, 7501da177e4SLinus Torvalds &scaling_driver.attr, 7511da177e4SLinus Torvalds &scaling_available_governors.attr, 7529e76988eSVenki Pallipadi &scaling_setspeed.attr, 7531da177e4SLinus Torvalds NULL 7541da177e4SLinus Torvalds }; 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 7571da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7621da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 7631b750e3bSViresh Kumar ssize_t ret; 7646eed9404SViresh Kumar 7656eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 7661b750e3bSViresh Kumar return -EINVAL; 7675a01f2e8SVenkatesh Pallipadi 768ad7722daSviresh kumar down_read(&policy->rwsem); 7695a01f2e8SVenkatesh Pallipadi 770e08f5f5bSGautham R Shenoy if (fattr->show) 771e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 772e08f5f5bSGautham R Shenoy else 773e08f5f5bSGautham R Shenoy ret = -EIO; 774e08f5f5bSGautham R Shenoy 775ad7722daSviresh kumar up_read(&policy->rwsem); 7766eed9404SViresh Kumar up_read(&cpufreq_rwsem); 7771b750e3bSViresh Kumar 7781da177e4SLinus Torvalds return ret; 7791da177e4SLinus Torvalds } 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 7821da177e4SLinus Torvalds const char *buf, size_t count) 7831da177e4SLinus Torvalds { 7841da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7851da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 786a07530b4SDave Jones ssize_t ret = -EINVAL; 7876eed9404SViresh Kumar 7884f750c93SSrivatsa S. Bhat get_online_cpus(); 7894f750c93SSrivatsa S. Bhat 7904f750c93SSrivatsa S. Bhat if (!cpu_online(policy->cpu)) 7914f750c93SSrivatsa S. Bhat goto unlock; 7924f750c93SSrivatsa S. Bhat 7936eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 7944f750c93SSrivatsa S. Bhat goto unlock; 7955a01f2e8SVenkatesh Pallipadi 796ad7722daSviresh kumar down_write(&policy->rwsem); 7975a01f2e8SVenkatesh Pallipadi 798e08f5f5bSGautham R Shenoy if (fattr->store) 799e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 800e08f5f5bSGautham R Shenoy else 801e08f5f5bSGautham R Shenoy ret = -EIO; 802e08f5f5bSGautham R Shenoy 803ad7722daSviresh kumar up_write(&policy->rwsem); 8046eed9404SViresh Kumar 8056eed9404SViresh Kumar up_read(&cpufreq_rwsem); 8064f750c93SSrivatsa S. Bhat unlock: 8074f750c93SSrivatsa S. Bhat put_online_cpus(); 8084f750c93SSrivatsa S. Bhat 8091da177e4SLinus Torvalds return ret; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 8131da177e4SLinus Torvalds { 8141da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8152d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 8161da177e4SLinus Torvalds complete(&policy->kobj_unregister); 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 81952cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 8201da177e4SLinus Torvalds .show = show, 8211da177e4SLinus Torvalds .store = store, 8221da177e4SLinus Torvalds }; 8231da177e4SLinus Torvalds 8241da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 8251da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 8261da177e4SLinus Torvalds .default_attrs = default_attrs, 8271da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 8281da177e4SLinus Torvalds }; 8291da177e4SLinus Torvalds 8302361be23SViresh Kumar struct kobject *cpufreq_global_kobject; 8312361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 8322361be23SViresh Kumar 8332361be23SViresh Kumar static int cpufreq_global_kobject_usage; 8342361be23SViresh Kumar 8352361be23SViresh Kumar int cpufreq_get_global_kobject(void) 8362361be23SViresh Kumar { 8372361be23SViresh Kumar if (!cpufreq_global_kobject_usage++) 8382361be23SViresh Kumar return kobject_add(cpufreq_global_kobject, 8392361be23SViresh Kumar &cpu_subsys.dev_root->kobj, "%s", "cpufreq"); 8402361be23SViresh Kumar 8412361be23SViresh Kumar return 0; 8422361be23SViresh Kumar } 8432361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject); 8442361be23SViresh Kumar 8452361be23SViresh Kumar void cpufreq_put_global_kobject(void) 8462361be23SViresh Kumar { 8472361be23SViresh Kumar if (!--cpufreq_global_kobject_usage) 8482361be23SViresh Kumar kobject_del(cpufreq_global_kobject); 8492361be23SViresh Kumar } 8502361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject); 8512361be23SViresh Kumar 8522361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr) 8532361be23SViresh Kumar { 8542361be23SViresh Kumar int ret = cpufreq_get_global_kobject(); 8552361be23SViresh Kumar 8562361be23SViresh Kumar if (!ret) { 8572361be23SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, attr); 8582361be23SViresh Kumar if (ret) 8592361be23SViresh Kumar cpufreq_put_global_kobject(); 8602361be23SViresh Kumar } 8612361be23SViresh Kumar 8622361be23SViresh Kumar return ret; 8632361be23SViresh Kumar } 8642361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file); 8652361be23SViresh Kumar 8662361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr) 8672361be23SViresh Kumar { 8682361be23SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, attr); 8692361be23SViresh Kumar cpufreq_put_global_kobject(); 8702361be23SViresh Kumar } 8712361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file); 8722361be23SViresh Kumar 87319d6f7ecSDave Jones /* symlink affected CPUs */ 874308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 87519d6f7ecSDave Jones { 87619d6f7ecSDave Jones unsigned int j; 87719d6f7ecSDave Jones int ret = 0; 87819d6f7ecSDave Jones 87919d6f7ecSDave Jones for_each_cpu(j, policy->cpus) { 8808a25a2fdSKay Sievers struct device *cpu_dev; 88119d6f7ecSDave Jones 882308b60e7SViresh Kumar if (j == policy->cpu) 88319d6f7ecSDave Jones continue; 88419d6f7ecSDave Jones 885e8fdde10SViresh Kumar pr_debug("Adding link for CPU: %u\n", j); 8868a25a2fdSKay Sievers cpu_dev = get_cpu_device(j); 8878a25a2fdSKay Sievers ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 88819d6f7ecSDave Jones "cpufreq"); 88971c3461eSRafael J. Wysocki if (ret) 89071c3461eSRafael J. Wysocki break; 89119d6f7ecSDave Jones } 89219d6f7ecSDave Jones return ret; 89319d6f7ecSDave Jones } 89419d6f7ecSDave Jones 895308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy, 8968a25a2fdSKay Sievers struct device *dev) 897909a694eSDave Jones { 898909a694eSDave Jones struct freq_attr **drv_attr; 899909a694eSDave Jones int ret = 0; 900909a694eSDave Jones 901909a694eSDave Jones /* prepare interface data */ 902909a694eSDave Jones ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 9038a25a2fdSKay Sievers &dev->kobj, "cpufreq"); 904909a694eSDave Jones if (ret) 905909a694eSDave Jones return ret; 906909a694eSDave Jones 907909a694eSDave Jones /* set up files for this cpu device */ 9081c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 909909a694eSDave Jones while ((drv_attr) && (*drv_attr)) { 910909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 911909a694eSDave Jones if (ret) 9121c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 913909a694eSDave Jones drv_attr++; 914909a694eSDave Jones } 9151c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 916909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 917909a694eSDave Jones if (ret) 9181c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 919909a694eSDave Jones } 9209c0ebcf7SViresh Kumar if (has_target()) { 921909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 922909a694eSDave Jones if (ret) 9231c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 924909a694eSDave Jones } 9251c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 926e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 927e2f74f35SThomas Renninger if (ret) 9281c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 929e2f74f35SThomas Renninger } 930909a694eSDave Jones 931308b60e7SViresh Kumar ret = cpufreq_add_dev_symlink(policy); 932ecf7e461SDave Jones if (ret) 933ecf7e461SDave Jones goto err_out_kobj_put; 934ecf7e461SDave Jones 935e18f1682SSrivatsa S. Bhat return ret; 936e18f1682SSrivatsa S. Bhat 937e18f1682SSrivatsa S. Bhat err_out_kobj_put: 938e18f1682SSrivatsa S. Bhat kobject_put(&policy->kobj); 939e18f1682SSrivatsa S. Bhat wait_for_completion(&policy->kobj_unregister); 940e18f1682SSrivatsa S. Bhat return ret; 941e18f1682SSrivatsa S. Bhat } 942e18f1682SSrivatsa S. Bhat 943e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy) 944e18f1682SSrivatsa S. Bhat { 9456e2c89d1Sviresh kumar struct cpufreq_governor *gov = NULL; 946e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 947e18f1682SSrivatsa S. Bhat int ret = 0; 948e18f1682SSrivatsa S. Bhat 949d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 950a27a9ab7SJason Baron 9516e2c89d1Sviresh kumar /* Update governor of new_policy to the governor used before hotplug */ 9526e2c89d1Sviresh kumar gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu)); 9536e2c89d1Sviresh kumar if (gov) 9546e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 9556e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 9566e2c89d1Sviresh kumar else 9576e2c89d1Sviresh kumar gov = CPUFREQ_DEFAULT_GOVERNOR; 9586e2c89d1Sviresh kumar 9596e2c89d1Sviresh kumar new_policy.governor = gov; 9606e2c89d1Sviresh kumar 961a27a9ab7SJason Baron /* Use the default policy if its valid. */ 962a27a9ab7SJason Baron if (cpufreq_driver->setpolicy) 9636e2c89d1Sviresh kumar cpufreq_parse_governor(gov->name, &new_policy.policy, NULL); 964ecf7e461SDave Jones 965ecf7e461SDave Jones /* set default policy */ 966037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 967ecf7e461SDave Jones if (ret) { 9682d06d8c4SDominik Brodowski pr_debug("setting policy failed\n"); 9691c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 9701c3d85ddSRafael J. Wysocki cpufreq_driver->exit(policy); 971ecf7e461SDave Jones } 972909a694eSDave Jones } 973909a694eSDave Jones 974fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 975d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, 97642f921a6SViresh Kumar unsigned int cpu, struct device *dev) 977fcf80582SViresh Kumar { 9789c0ebcf7SViresh Kumar int ret = 0; 979fcf80582SViresh Kumar unsigned long flags; 980fcf80582SViresh Kumar 9819c0ebcf7SViresh Kumar if (has_target()) { 9823de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 9833de9bdebSViresh Kumar if (ret) { 9843de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 9853de9bdebSViresh Kumar return ret; 9863de9bdebSViresh Kumar } 9873de9bdebSViresh Kumar } 988fcf80582SViresh Kumar 989ad7722daSviresh kumar down_write(&policy->rwsem); 9902eaa3e2dSViresh Kumar 9910d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 9922eaa3e2dSViresh Kumar 993fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 994fcf80582SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = policy; 9950d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 996fcf80582SViresh Kumar 997ad7722daSviresh kumar up_write(&policy->rwsem); 9982eaa3e2dSViresh Kumar 9999c0ebcf7SViresh Kumar if (has_target()) { 1000e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 1001e5c87b76SStratos Karafotis if (!ret) 1002e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1003e5c87b76SStratos Karafotis 1004e5c87b76SStratos Karafotis if (ret) { 10053de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 10063de9bdebSViresh Kumar return ret; 10073de9bdebSViresh Kumar } 1008820c6ca2SViresh Kumar } 1009fcf80582SViresh Kumar 101042f921a6SViresh Kumar return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"); 1011fcf80582SViresh Kumar } 1012fcf80582SViresh Kumar #endif 10131da177e4SLinus Torvalds 10148414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu) 10158414809cSSrivatsa S. Bhat { 10168414809cSSrivatsa S. Bhat struct cpufreq_policy *policy; 10178414809cSSrivatsa S. Bhat unsigned long flags; 10188414809cSSrivatsa S. Bhat 101944871c9cSLan Tianyu read_lock_irqsave(&cpufreq_driver_lock, flags); 10208414809cSSrivatsa S. Bhat 10218414809cSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data_fallback, cpu); 10228414809cSSrivatsa S. Bhat 102344871c9cSLan Tianyu read_unlock_irqrestore(&cpufreq_driver_lock, flags); 10248414809cSSrivatsa S. Bhat 10256e2c89d1Sviresh kumar policy->governor = NULL; 10266e2c89d1Sviresh kumar 10278414809cSSrivatsa S. Bhat return policy; 10288414809cSSrivatsa S. Bhat } 10298414809cSSrivatsa S. Bhat 1030e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void) 1031e9698cc5SSrivatsa S. Bhat { 1032e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1033e9698cc5SSrivatsa S. Bhat 1034e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1035e9698cc5SSrivatsa S. Bhat if (!policy) 1036e9698cc5SSrivatsa S. Bhat return NULL; 1037e9698cc5SSrivatsa S. Bhat 1038e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1039e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1040e9698cc5SSrivatsa S. Bhat 1041e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1042e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1043e9698cc5SSrivatsa S. Bhat 1044c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1045ad7722daSviresh kumar init_rwsem(&policy->rwsem); 104612478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 104712478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1048ad7722daSviresh kumar 1049e9698cc5SSrivatsa S. Bhat return policy; 1050e9698cc5SSrivatsa S. Bhat 1051e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1052e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1053e9698cc5SSrivatsa S. Bhat err_free_policy: 1054e9698cc5SSrivatsa S. Bhat kfree(policy); 1055e9698cc5SSrivatsa S. Bhat 1056e9698cc5SSrivatsa S. Bhat return NULL; 1057e9698cc5SSrivatsa S. Bhat } 1058e9698cc5SSrivatsa S. Bhat 105942f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 106042f921a6SViresh Kumar { 106142f921a6SViresh Kumar struct kobject *kobj; 106242f921a6SViresh Kumar struct completion *cmp; 106342f921a6SViresh Kumar 1064fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1065fcd7af91SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 1066fcd7af91SViresh Kumar 106742f921a6SViresh Kumar down_read(&policy->rwsem); 106842f921a6SViresh Kumar kobj = &policy->kobj; 106942f921a6SViresh Kumar cmp = &policy->kobj_unregister; 107042f921a6SViresh Kumar up_read(&policy->rwsem); 107142f921a6SViresh Kumar kobject_put(kobj); 107242f921a6SViresh Kumar 107342f921a6SViresh Kumar /* 107442f921a6SViresh Kumar * We need to make sure that the underlying kobj is 107542f921a6SViresh Kumar * actually not referenced anymore by anybody before we 107642f921a6SViresh Kumar * proceed with unloading. 107742f921a6SViresh Kumar */ 107842f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 107942f921a6SViresh Kumar wait_for_completion(cmp); 108042f921a6SViresh Kumar pr_debug("wait complete\n"); 108142f921a6SViresh Kumar } 108242f921a6SViresh Kumar 1083e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy) 1084e9698cc5SSrivatsa S. Bhat { 1085e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1086e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1087e9698cc5SSrivatsa S. Bhat kfree(policy); 1088e9698cc5SSrivatsa S. Bhat } 1089e9698cc5SSrivatsa S. Bhat 10900d66b91eSSrivatsa S. Bhat static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 10910d66b91eSSrivatsa S. Bhat { 109299ec899eSSrivatsa S. Bhat if (WARN_ON(cpu == policy->cpu)) 1093cb38ed5cSSrivatsa S. Bhat return; 1094cb38ed5cSSrivatsa S. Bhat 1095ad7722daSviresh kumar down_write(&policy->rwsem); 10968efd5765SViresh Kumar 10970d66b91eSSrivatsa S. Bhat policy->last_cpu = policy->cpu; 10980d66b91eSSrivatsa S. Bhat policy->cpu = cpu; 10990d66b91eSSrivatsa S. Bhat 1100ad7722daSviresh kumar up_write(&policy->rwsem); 11018efd5765SViresh Kumar 11020d66b91eSSrivatsa S. Bhat blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 11030d66b91eSSrivatsa S. Bhat CPUFREQ_UPDATE_POLICY_CPU, policy); 11040d66b91eSSrivatsa S. Bhat } 11050d66b91eSSrivatsa S. Bhat 110696bbbe4aSViresh Kumar static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 11071da177e4SLinus Torvalds { 1108fcf80582SViresh Kumar unsigned int j, cpu = dev->id; 110965922465SViresh Kumar int ret = -ENOMEM; 11101da177e4SLinus Torvalds struct cpufreq_policy *policy; 11111da177e4SLinus Torvalds unsigned long flags; 111296bbbe4aSViresh Kumar bool recover_policy = cpufreq_suspended; 111390e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU 11141b274294SViresh Kumar struct cpufreq_policy *tpolicy; 111590e41bacSPrarit Bhargava #endif 11161da177e4SLinus Torvalds 1117c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 1118c32b6b8eSAshok Raj return 0; 1119c32b6b8eSAshok Raj 11202d06d8c4SDominik Brodowski pr_debug("adding CPU %u\n", cpu); 11211da177e4SLinus Torvalds 11221da177e4SLinus Torvalds #ifdef CONFIG_SMP 11231da177e4SLinus Torvalds /* check whether a different CPU already registered this 11241da177e4SLinus Torvalds * CPU because it is in the same boat. */ 11251da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 11261da177e4SLinus Torvalds if (unlikely(policy)) { 11278ff69732SDave Jones cpufreq_cpu_put(policy); 11281da177e4SLinus Torvalds return 0; 11291da177e4SLinus Torvalds } 11305025d628SLi Zhong #endif 1131fcf80582SViresh Kumar 11326eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 11336eed9404SViresh Kumar return 0; 11346eed9404SViresh Kumar 1135fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 1136fcf80582SViresh Kumar /* Check if this cpu was hot-unplugged earlier and has siblings */ 11370d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 11381b274294SViresh Kumar list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) { 11391b274294SViresh Kumar if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) { 11400d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 114142f921a6SViresh Kumar ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev); 11426eed9404SViresh Kumar up_read(&cpufreq_rwsem); 11436eed9404SViresh Kumar return ret; 1144fcf80582SViresh Kumar } 11452eaa3e2dSViresh Kumar } 11460d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1147fcf80582SViresh Kumar #endif 11481da177e4SLinus Torvalds 114972368d12SRafael J. Wysocki /* 115072368d12SRafael J. Wysocki * Restore the saved policy when doing light-weight init and fall back 115172368d12SRafael J. Wysocki * to the full init if that fails. 115272368d12SRafael J. Wysocki */ 115396bbbe4aSViresh Kumar policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL; 115472368d12SRafael J. Wysocki if (!policy) { 115596bbbe4aSViresh Kumar recover_policy = false; 1156e9698cc5SSrivatsa S. Bhat policy = cpufreq_policy_alloc(); 1157059019a3SDave Jones if (!policy) 11581da177e4SLinus Torvalds goto nomem_out; 115972368d12SRafael J. Wysocki } 11600d66b91eSSrivatsa S. Bhat 11610d66b91eSSrivatsa S. Bhat /* 11620d66b91eSSrivatsa S. Bhat * In the resume path, since we restore a saved policy, the assignment 11630d66b91eSSrivatsa S. Bhat * to policy->cpu is like an update of the existing policy, rather than 11640d66b91eSSrivatsa S. Bhat * the creation of a brand new one. So we need to perform this update 11650d66b91eSSrivatsa S. Bhat * by invoking update_policy_cpu(). 11660d66b91eSSrivatsa S. Bhat */ 116796bbbe4aSViresh Kumar if (recover_policy && cpu != policy->cpu) 11680d66b91eSSrivatsa S. Bhat update_policy_cpu(policy, cpu); 11690d66b91eSSrivatsa S. Bhat else 11701da177e4SLinus Torvalds policy->cpu = cpu; 11710d66b91eSSrivatsa S. Bhat 1172835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 11731da177e4SLinus Torvalds 11741da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 117565f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 11781da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 11791da177e4SLinus Torvalds */ 11801c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 11811da177e4SLinus Torvalds if (ret) { 11822d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 11832eaa3e2dSViresh Kumar goto err_set_policy_cpu; 11841da177e4SLinus Torvalds } 1185643ae6e8SViresh Kumar 11865a7e56a5SViresh Kumar /* related cpus should atleast have policy->cpus */ 11875a7e56a5SViresh Kumar cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); 11885a7e56a5SViresh Kumar 11895a7e56a5SViresh Kumar /* 11905a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 11915a7e56a5SViresh Kumar * managing offline cpus here. 11925a7e56a5SViresh Kumar */ 11935a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 11945a7e56a5SViresh Kumar 119596bbbe4aSViresh Kumar if (!recover_policy) { 11965a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 11975a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 11985a7e56a5SViresh Kumar } 11995a7e56a5SViresh Kumar 12004e97b631SViresh Kumar down_write(&policy->rwsem); 1201652ed95dSViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1202652ed95dSViresh Kumar for_each_cpu(j, policy->cpus) 1203652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 1204652ed95dSViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1205652ed95dSViresh Kumar 12062ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 1207da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1208da60ce9fSViresh Kumar if (!policy->cur) { 1209da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1210da60ce9fSViresh Kumar goto err_get_freq; 1211da60ce9fSViresh Kumar } 1212da60ce9fSViresh Kumar } 1213da60ce9fSViresh Kumar 1214d3916691SViresh Kumar /* 1215d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1216d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1217d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1218d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1219d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1220d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1221d3916691SViresh Kumar * isn't found in freq-table. 1222d3916691SViresh Kumar * 1223d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1224d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1225d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1226d3916691SViresh Kumar * is initialized to zero). 1227d3916691SViresh Kumar * 1228d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1229d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1230d3916691SViresh Kumar * equal to target-freq. 1231d3916691SViresh Kumar */ 1232d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1233d3916691SViresh Kumar && has_target()) { 1234d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1235d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1236d3916691SViresh Kumar if (ret == -EINVAL) { 1237d3916691SViresh Kumar /* Warn user and fix it */ 1238d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1239d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1240d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1241d3916691SViresh Kumar CPUFREQ_RELATION_L); 1242d3916691SViresh Kumar 1243d3916691SViresh Kumar /* 1244d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1245d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1246d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1247d3916691SViresh Kumar */ 1248d3916691SViresh Kumar BUG_ON(ret); 1249d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1250d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1251d3916691SViresh Kumar } 1252d3916691SViresh Kumar } 1253d3916691SViresh Kumar 1254a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1255a1531acdSThomas Renninger CPUFREQ_START, policy); 1256a1531acdSThomas Renninger 125796bbbe4aSViresh Kumar if (!recover_policy) { 1258308b60e7SViresh Kumar ret = cpufreq_add_dev_interface(policy, dev); 125919d6f7ecSDave Jones if (ret) 12600142f9dcSAhmed S. Darwish goto err_out_unregister; 1261fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1262fcd7af91SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 12639515f4d6SViresh Kumar } 1264c88a1f8bSLukasz Majewski 1265c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1266c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1267c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12688ff69732SDave Jones 1269e18f1682SSrivatsa S. Bhat cpufreq_init_policy(policy); 1270e18f1682SSrivatsa S. Bhat 127196bbbe4aSViresh Kumar if (!recover_policy) { 127208fd8c1cSViresh Kumar policy->user_policy.policy = policy->policy; 127308fd8c1cSViresh Kumar policy->user_policy.governor = policy->governor; 127408fd8c1cSViresh Kumar } 12754e97b631SViresh Kumar up_write(&policy->rwsem); 127608fd8c1cSViresh Kumar 1277038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 12786eed9404SViresh Kumar up_read(&cpufreq_rwsem); 12796eed9404SViresh Kumar 12802d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds return 0; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds err_out_unregister: 1285652ed95dSViresh Kumar err_get_freq: 12860d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 1287474deff7SViresh Kumar for_each_cpu(j, policy->cpus) 12887a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 12890d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12901da177e4SLinus Torvalds 1291da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1292da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 12932eaa3e2dSViresh Kumar err_set_policy_cpu: 129496bbbe4aSViresh Kumar if (recover_policy) { 129572368d12SRafael J. Wysocki /* Do not leave stale fallback data behind. */ 129672368d12SRafael J. Wysocki per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL; 129742f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 129872368d12SRafael J. Wysocki } 1299e9698cc5SSrivatsa S. Bhat cpufreq_policy_free(policy); 130042f921a6SViresh Kumar 13011da177e4SLinus Torvalds nomem_out: 13026eed9404SViresh Kumar up_read(&cpufreq_rwsem); 13036eed9404SViresh Kumar 13041da177e4SLinus Torvalds return ret; 13051da177e4SLinus Torvalds } 13061da177e4SLinus Torvalds 1307a82fab29SSrivatsa S. Bhat /** 1308a82fab29SSrivatsa S. Bhat * cpufreq_add_dev - add a CPU device 1309a82fab29SSrivatsa S. Bhat * 1310a82fab29SSrivatsa S. Bhat * Adds the cpufreq interface for a CPU device. 1311a82fab29SSrivatsa S. Bhat * 1312a82fab29SSrivatsa S. Bhat * The Oracle says: try running cpufreq registration/unregistration concurrently 1313a82fab29SSrivatsa S. Bhat * with with cpu hotplugging and all hell will break loose. Tried to clean this 1314a82fab29SSrivatsa S. Bhat * mess up, but more thorough testing is needed. - Mathieu 1315a82fab29SSrivatsa S. Bhat */ 1316a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 1317a82fab29SSrivatsa S. Bhat { 131896bbbe4aSViresh Kumar return __cpufreq_add_dev(dev, sif); 1319a82fab29SSrivatsa S. Bhat } 1320a82fab29SSrivatsa S. Bhat 13213a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy, 132242f921a6SViresh Kumar unsigned int old_cpu) 1323f9ba680dSSrivatsa S. Bhat { 1324f9ba680dSSrivatsa S. Bhat struct device *cpu_dev; 1325f9ba680dSSrivatsa S. Bhat int ret; 1326f9ba680dSSrivatsa S. Bhat 1327f9ba680dSSrivatsa S. Bhat /* first sibling now owns the new sysfs dir */ 13289c8f1ee4SViresh Kumar cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu)); 1329a82fab29SSrivatsa S. Bhat 1330f9ba680dSSrivatsa S. Bhat sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 13313a3e9e06SViresh Kumar ret = kobject_move(&policy->kobj, &cpu_dev->kobj); 1332f9ba680dSSrivatsa S. Bhat if (ret) { 1333e837f9b5SJoe Perches pr_err("%s: Failed to move kobj: %d\n", __func__, ret); 1334f9ba680dSSrivatsa S. Bhat 1335ad7722daSviresh kumar down_write(&policy->rwsem); 13363a3e9e06SViresh Kumar cpumask_set_cpu(old_cpu, policy->cpus); 1337ad7722daSviresh kumar up_write(&policy->rwsem); 1338f9ba680dSSrivatsa S. Bhat 13393a3e9e06SViresh Kumar ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 1340f9ba680dSSrivatsa S. Bhat "cpufreq"); 1341f9ba680dSSrivatsa S. Bhat 1342f9ba680dSSrivatsa S. Bhat return -EINVAL; 1343f9ba680dSSrivatsa S. Bhat } 1344f9ba680dSSrivatsa S. Bhat 1345f9ba680dSSrivatsa S. Bhat return cpu_dev->id; 1346f9ba680dSSrivatsa S. Bhat } 1347f9ba680dSSrivatsa S. Bhat 1348cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev, 134996bbbe4aSViresh Kumar struct subsys_interface *sif) 13501da177e4SLinus Torvalds { 1351f9ba680dSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 13523de9bdebSViresh Kumar int new_cpu, ret; 13531da177e4SLinus Torvalds unsigned long flags; 13543a3e9e06SViresh Kumar struct cpufreq_policy *policy; 13551da177e4SLinus Torvalds 1356b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 13571da177e4SLinus Torvalds 13580d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 13591da177e4SLinus Torvalds 13603a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 13611da177e4SLinus Torvalds 13628414809cSSrivatsa S. Bhat /* Save the policy somewhere when doing a light-weight tear-down */ 136396bbbe4aSViresh Kumar if (cpufreq_suspended) 13643a3e9e06SViresh Kumar per_cpu(cpufreq_cpu_data_fallback, cpu) = policy; 13658414809cSSrivatsa S. Bhat 13660d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 13671da177e4SLinus Torvalds 13683a3e9e06SViresh Kumar if (!policy) { 1369b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 13701da177e4SLinus Torvalds return -EINVAL; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 13739c0ebcf7SViresh Kumar if (has_target()) { 13743de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 13753de9bdebSViresh Kumar if (ret) { 13763de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 13773de9bdebSViresh Kumar return ret; 13783de9bdebSViresh Kumar } 13793de9bdebSViresh Kumar } 13805a01f2e8SVenkatesh Pallipadi 13811c3d85ddSRafael J. Wysocki if (!cpufreq_driver->setpolicy) 1382fa69e33fSDirk Brandewie strncpy(per_cpu(cpufreq_cpu_governor, cpu), 13833a3e9e06SViresh Kumar policy->governor->name, CPUFREQ_NAME_LEN); 13841da177e4SLinus Torvalds 1385ad7722daSviresh kumar down_read(&policy->rwsem); 13863a3e9e06SViresh Kumar cpus = cpumask_weight(policy->cpus); 1387ad7722daSviresh kumar up_read(&policy->rwsem); 13881da177e4SLinus Torvalds 138961173f25SSrivatsa S. Bhat if (cpu != policy->cpu) { 139073bf0fc2SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 139173bf0fc2SViresh Kumar } else if (cpus > 1) { 139242f921a6SViresh Kumar new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu); 1393f9ba680dSSrivatsa S. Bhat if (new_cpu >= 0) { 13943a3e9e06SViresh Kumar update_policy_cpu(policy, new_cpu); 1395a82fab29SSrivatsa S. Bhat 1396bda9f552SStratos Karafotis if (!cpufreq_suspended) 139775949c9aSViresh Kumar pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n", 139875949c9aSViresh Kumar __func__, new_cpu, cpu); 13991da177e4SLinus Torvalds } 1400367dc4aaSDirk Brandewie } else if (cpufreq_driver->stop_cpu && cpufreq_driver->setpolicy) { 1401367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 14021da177e4SLinus Torvalds } 1403b8eed8afSViresh Kumar 1404cedb70afSSrivatsa S. Bhat return 0; 1405cedb70afSSrivatsa S. Bhat } 1406cedb70afSSrivatsa S. Bhat 1407cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev, 140896bbbe4aSViresh Kumar struct subsys_interface *sif) 1409cedb70afSSrivatsa S. Bhat { 1410cedb70afSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 1411cedb70afSSrivatsa S. Bhat int ret; 1412cedb70afSSrivatsa S. Bhat unsigned long flags; 1413cedb70afSSrivatsa S. Bhat struct cpufreq_policy *policy; 1414cedb70afSSrivatsa S. Bhat 1415cedb70afSSrivatsa S. Bhat read_lock_irqsave(&cpufreq_driver_lock, flags); 1416cedb70afSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data, cpu); 1417cedb70afSSrivatsa S. Bhat read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1418cedb70afSSrivatsa S. Bhat 1419cedb70afSSrivatsa S. Bhat if (!policy) { 1420cedb70afSSrivatsa S. Bhat pr_debug("%s: No cpu_data found\n", __func__); 1421cedb70afSSrivatsa S. Bhat return -EINVAL; 1422cedb70afSSrivatsa S. Bhat } 1423cedb70afSSrivatsa S. Bhat 1424ad7722daSviresh kumar down_write(&policy->rwsem); 1425cedb70afSSrivatsa S. Bhat cpus = cpumask_weight(policy->cpus); 14269c8f1ee4SViresh Kumar 14279c8f1ee4SViresh Kumar if (cpus > 1) 14289c8f1ee4SViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 1429ad7722daSviresh kumar up_write(&policy->rwsem); 1430cedb70afSSrivatsa S. Bhat 1431b8eed8afSViresh Kumar /* If cpu is last user of policy, free policy */ 1432b8eed8afSViresh Kumar if (cpus == 1) { 14339c0ebcf7SViresh Kumar if (has_target()) { 14343de9bdebSViresh Kumar ret = __cpufreq_governor(policy, 14353de9bdebSViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 14363de9bdebSViresh Kumar if (ret) { 14373de9bdebSViresh Kumar pr_err("%s: Failed to exit governor\n", 14383de9bdebSViresh Kumar __func__); 14393de9bdebSViresh Kumar return ret; 14403de9bdebSViresh Kumar } 14413de9bdebSViresh Kumar } 14422a998599SRafael J. Wysocki 144396bbbe4aSViresh Kumar if (!cpufreq_suspended) 144442f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 14451da177e4SLinus Torvalds 14468414809cSSrivatsa S. Bhat /* 14478414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 14488414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 14498414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 14508414809cSSrivatsa S. Bhat */ 14511c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 14523a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 145327ecddc2SJacob Shin 14549515f4d6SViresh Kumar /* Remove policy from list of active policies */ 14559515f4d6SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 14569515f4d6SViresh Kumar list_del(&policy->policy_list); 14579515f4d6SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 14589515f4d6SViresh Kumar 145996bbbe4aSViresh Kumar if (!cpufreq_suspended) 14603a3e9e06SViresh Kumar cpufreq_policy_free(policy); 1461e5c87b76SStratos Karafotis } else if (has_target()) { 1462e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 1463e5c87b76SStratos Karafotis if (!ret) 1464e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1465e5c87b76SStratos Karafotis 1466e5c87b76SStratos Karafotis if (ret) { 1467e5c87b76SStratos Karafotis pr_err("%s: Failed to start governor\n", __func__); 14683de9bdebSViresh Kumar return ret; 14693de9bdebSViresh Kumar } 1470b8eed8afSViresh Kumar } 14711da177e4SLinus Torvalds 1472474deff7SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 14731da177e4SLinus Torvalds return 0; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 1476cedb70afSSrivatsa S. Bhat /** 147727a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1478cedb70afSSrivatsa S. Bhat * 1479cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1480cedb70afSSrivatsa S. Bhat */ 14818a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 14825a01f2e8SVenkatesh Pallipadi { 14838a25a2fdSKay Sievers unsigned int cpu = dev->id; 148427a862e9SViresh Kumar int ret; 1485ec28297aSVenki Pallipadi 1486ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1487ec28297aSVenki Pallipadi return 0; 1488ec28297aSVenki Pallipadi 148996bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_prepare(dev, sif); 149027a862e9SViresh Kumar 149127a862e9SViresh Kumar if (!ret) 149296bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_finish(dev, sif); 149327a862e9SViresh Kumar 149427a862e9SViresh Kumar return ret; 14955a01f2e8SVenkatesh Pallipadi } 14965a01f2e8SVenkatesh Pallipadi 149765f27f38SDavid Howells static void handle_update(struct work_struct *work) 14981da177e4SLinus Torvalds { 149965f27f38SDavid Howells struct cpufreq_policy *policy = 150065f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 150165f27f38SDavid Howells unsigned int cpu = policy->cpu; 15022d06d8c4SDominik Brodowski pr_debug("handle_update for cpu %u called\n", cpu); 15031da177e4SLinus Torvalds cpufreq_update_policy(cpu); 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 15061da177e4SLinus Torvalds /** 1507bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1508bb176f7dSViresh Kumar * in deep trouble. 15091da177e4SLinus Torvalds * @cpu: cpu number 15101da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 15111da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 15121da177e4SLinus Torvalds * 151329464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 151429464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 15151da177e4SLinus Torvalds */ 1516e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1517e08f5f5bSGautham R Shenoy unsigned int new_freq) 15181da177e4SLinus Torvalds { 1519b43a7ffbSViresh Kumar struct cpufreq_policy *policy; 15201da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1521b43a7ffbSViresh Kumar unsigned long flags; 1522b43a7ffbSViresh Kumar 1523e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1524e837f9b5SJoe Perches old_freq, new_freq); 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds freqs.old = old_freq; 15271da177e4SLinus Torvalds freqs.new = new_freq; 1528b43a7ffbSViresh Kumar 1529b43a7ffbSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 1530b43a7ffbSViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 1531b43a7ffbSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1532b43a7ffbSViresh Kumar 15338fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 15348fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds /** 15384ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 153995235ca2SVenkatesh Pallipadi * @cpu: CPU number 154095235ca2SVenkatesh Pallipadi * 154195235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 154295235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 154395235ca2SVenkatesh Pallipadi */ 154495235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 154595235ca2SVenkatesh Pallipadi { 15469e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1547e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 154895235ca2SVenkatesh Pallipadi 15491c3d85ddSRafael J. Wysocki if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 15501c3d85ddSRafael J. Wysocki return cpufreq_driver->get(cpu); 15519e21ba8bSDirk Brandewie 15529e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 155395235ca2SVenkatesh Pallipadi if (policy) { 1554e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 155595235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 155695235ca2SVenkatesh Pallipadi } 155795235ca2SVenkatesh Pallipadi 15584d34a67dSDave Jones return ret_freq; 155995235ca2SVenkatesh Pallipadi } 156095235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 156195235ca2SVenkatesh Pallipadi 15623d737108SJesse Barnes /** 15633d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 15643d737108SJesse Barnes * @cpu: CPU number 15653d737108SJesse Barnes * 15663d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 15673d737108SJesse Barnes */ 15683d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 15693d737108SJesse Barnes { 15703d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15713d737108SJesse Barnes unsigned int ret_freq = 0; 15723d737108SJesse Barnes 15733d737108SJesse Barnes if (policy) { 15743d737108SJesse Barnes ret_freq = policy->max; 15753d737108SJesse Barnes cpufreq_cpu_put(policy); 15763d737108SJesse Barnes } 15773d737108SJesse Barnes 15783d737108SJesse Barnes return ret_freq; 15793d737108SJesse Barnes } 15803d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 15813d737108SJesse Barnes 15825a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 15831da177e4SLinus Torvalds { 15847a6aedfaSMike Travis struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1585e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 15861da177e4SLinus Torvalds 15871c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 15884d34a67dSDave Jones return ret_freq; 15891da177e4SLinus Torvalds 15901c3d85ddSRafael J. Wysocki ret_freq = cpufreq_driver->get(cpu); 15911da177e4SLinus Torvalds 1592e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 15931c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1594e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1595e08f5f5bSGautham R Shenoy saved value exists */ 1596e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1597e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 15981da177e4SLinus Torvalds schedule_work(&policy->update); 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds } 16011da177e4SLinus Torvalds 16024d34a67dSDave Jones return ret_freq; 16035a01f2e8SVenkatesh Pallipadi } 16041da177e4SLinus Torvalds 16055a01f2e8SVenkatesh Pallipadi /** 16065a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 16075a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 16085a01f2e8SVenkatesh Pallipadi * 16095a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 16105a01f2e8SVenkatesh Pallipadi */ 16115a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 16125a01f2e8SVenkatesh Pallipadi { 1613999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 16145a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 16155a01f2e8SVenkatesh Pallipadi 1616999976e0SAaron Plattner if (policy) { 1617ad7722daSviresh kumar down_read(&policy->rwsem); 16185a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 1619ad7722daSviresh kumar up_read(&policy->rwsem); 1620999976e0SAaron Plattner 1621999976e0SAaron Plattner cpufreq_cpu_put(policy); 1622999976e0SAaron Plattner } 16236eed9404SViresh Kumar 16244d34a67dSDave Jones return ret_freq; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 16271da177e4SLinus Torvalds 16288a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 16298a25a2fdSKay Sievers .name = "cpufreq", 16308a25a2fdSKay Sievers .subsys = &cpu_subsys, 16318a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 16328a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1633e00e56dfSRafael J. Wysocki }; 1634e00e56dfSRafael J. Wysocki 1635e28867eaSViresh Kumar /* 1636e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1637e28867eaSViresh Kumar * during suspend.. 163842d4dc3fSBenjamin Herrenschmidt */ 1639e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 164042d4dc3fSBenjamin Herrenschmidt { 1641e28867eaSViresh Kumar int ret; 16424bc5d341SDave Jones 1643e28867eaSViresh Kumar if (!policy->suspend_freq) { 1644e28867eaSViresh Kumar pr_err("%s: suspend_freq can't be zero\n", __func__); 1645e28867eaSViresh Kumar return -EINVAL; 164642d4dc3fSBenjamin Herrenschmidt } 164742d4dc3fSBenjamin Herrenschmidt 1648e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1649e28867eaSViresh Kumar policy->suspend_freq); 1650e28867eaSViresh Kumar 1651e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1652e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1653e28867eaSViresh Kumar if (ret) 1654e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1655e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1656e28867eaSViresh Kumar 1657c9060494SDave Jones return ret; 165842d4dc3fSBenjamin Herrenschmidt } 1659e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 166042d4dc3fSBenjamin Herrenschmidt 166142d4dc3fSBenjamin Herrenschmidt /** 16622f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 16631da177e4SLinus Torvalds * 16642f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 16652f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 16662f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 16672f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 16681da177e4SLinus Torvalds */ 16692f0aea93SViresh Kumar void cpufreq_suspend(void) 16701da177e4SLinus Torvalds { 16713a3e9e06SViresh Kumar struct cpufreq_policy *policy; 16721da177e4SLinus Torvalds 16732f0aea93SViresh Kumar if (!cpufreq_driver) 1674e00e56dfSRafael J. Wysocki return; 16751da177e4SLinus Torvalds 16762f0aea93SViresh Kumar if (!has_target()) 16772f0aea93SViresh Kumar return; 16781da177e4SLinus Torvalds 16792f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 16802f0aea93SViresh Kumar 16812f0aea93SViresh Kumar list_for_each_entry(policy, &cpufreq_policy_list, policy_list) { 16822f0aea93SViresh Kumar if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP)) 16832f0aea93SViresh Kumar pr_err("%s: Failed to stop governor for policy: %p\n", 16842f0aea93SViresh Kumar __func__, policy); 16852f0aea93SViresh Kumar else if (cpufreq_driver->suspend 16862f0aea93SViresh Kumar && cpufreq_driver->suspend(policy)) 16872f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 16882f0aea93SViresh Kumar policy); 16891da177e4SLinus Torvalds } 16901da177e4SLinus Torvalds 16912f0aea93SViresh Kumar cpufreq_suspended = true; 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds 16941da177e4SLinus Torvalds /** 16952f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 16961da177e4SLinus Torvalds * 16972f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 16982f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 16991da177e4SLinus Torvalds */ 17002f0aea93SViresh Kumar void cpufreq_resume(void) 17011da177e4SLinus Torvalds { 17021da177e4SLinus Torvalds struct cpufreq_policy *policy; 17031da177e4SLinus Torvalds 17042f0aea93SViresh Kumar if (!cpufreq_driver) 17051da177e4SLinus Torvalds return; 17061da177e4SLinus Torvalds 17072f0aea93SViresh Kumar if (!has_target()) 17082f0aea93SViresh Kumar return; 17091da177e4SLinus Torvalds 17102f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 17112f0aea93SViresh Kumar 17122f0aea93SViresh Kumar cpufreq_suspended = false; 17132f0aea93SViresh Kumar 17142f0aea93SViresh Kumar list_for_each_entry(policy, &cpufreq_policy_list, policy_list) { 17150c5aa405SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) 17160c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 17170c5aa405SViresh Kumar policy); 17180c5aa405SViresh Kumar else if (__cpufreq_governor(policy, CPUFREQ_GOV_START) 17192f0aea93SViresh Kumar || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS)) 17202f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 17212f0aea93SViresh Kumar __func__, policy); 17222f0aea93SViresh Kumar 17232f0aea93SViresh Kumar /* 17242f0aea93SViresh Kumar * schedule call cpufreq_update_policy() for boot CPU, i.e. last 17252f0aea93SViresh Kumar * policy in list. It will verify that the current freq is in 17262f0aea93SViresh Kumar * sync with what we believe it to be. 17272f0aea93SViresh Kumar */ 17282f0aea93SViresh Kumar if (list_is_last(&policy->policy_list, &cpufreq_policy_list)) 17293a3e9e06SViresh Kumar schedule_work(&policy->update); 17301da177e4SLinus Torvalds } 17312f0aea93SViresh Kumar } 17321da177e4SLinus Torvalds 17339d95046eSBorislav Petkov /** 17349d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17359d95046eSBorislav Petkov * 17369d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17379d95046eSBorislav Petkov * or NULL, if none. 17389d95046eSBorislav Petkov */ 17399d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17409d95046eSBorislav Petkov { 17411c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17421c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17431c3d85ddSRafael J. Wysocki 17441c3d85ddSRafael J. Wysocki return NULL; 17459d95046eSBorislav Petkov } 17469d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17471da177e4SLinus Torvalds 17481da177e4SLinus Torvalds /********************************************************************* 17491da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 17501da177e4SLinus Torvalds *********************************************************************/ 17511da177e4SLinus Torvalds 17521da177e4SLinus Torvalds /** 17531da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 17541da177e4SLinus Torvalds * @nb: notifier function to register 17551da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17561da177e4SLinus Torvalds * 17571da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 17581da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 17591da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 17601da177e4SLinus Torvalds * changes in cpufreq policy. 17611da177e4SLinus Torvalds * 17621da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1763e041c683SAlan Stern * blocking_notifier_chain_register. 17641da177e4SLinus Torvalds */ 17651da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 17661da177e4SLinus Torvalds { 17671da177e4SLinus Torvalds int ret; 17681da177e4SLinus Torvalds 1769d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1770d5aaffa9SDirk Brandewie return -EINVAL; 1771d5aaffa9SDirk Brandewie 177274212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 177374212ca4SCesar Eduardo Barros 17741da177e4SLinus Torvalds switch (list) { 17751da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1776b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1777e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17781da177e4SLinus Torvalds break; 17791da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1780e041c683SAlan Stern ret = blocking_notifier_chain_register( 1781e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 17821da177e4SLinus Torvalds break; 17831da177e4SLinus Torvalds default: 17841da177e4SLinus Torvalds ret = -EINVAL; 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds return ret; 17881da177e4SLinus Torvalds } 17891da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds /** 17921da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 17931da177e4SLinus Torvalds * @nb: notifier block to be unregistered 17941da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17951da177e4SLinus Torvalds * 17961da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 17971da177e4SLinus Torvalds * 17981da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1799e041c683SAlan Stern * blocking_notifier_chain_unregister. 18001da177e4SLinus Torvalds */ 18011da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 18021da177e4SLinus Torvalds { 18031da177e4SLinus Torvalds int ret; 18041da177e4SLinus Torvalds 1805d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1806d5aaffa9SDirk Brandewie return -EINVAL; 1807d5aaffa9SDirk Brandewie 18081da177e4SLinus Torvalds switch (list) { 18091da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1810b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1811e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 18121da177e4SLinus Torvalds break; 18131da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1814e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1815e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18161da177e4SLinus Torvalds break; 18171da177e4SLinus Torvalds default: 18181da177e4SLinus Torvalds ret = -EINVAL; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds return ret; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 18241da177e4SLinus Torvalds 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds /********************************************************************* 18271da177e4SLinus Torvalds * GOVERNORS * 18281da177e4SLinus Torvalds *********************************************************************/ 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 18311da177e4SLinus Torvalds unsigned int target_freq, 18321da177e4SLinus Torvalds unsigned int relation) 18331da177e4SLinus Torvalds { 18341da177e4SLinus Torvalds int retval = -EINVAL; 18357249924eSViresh Kumar unsigned int old_target_freq = target_freq; 1836c32b6b8eSAshok Raj 1837a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1838a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1839a7b422cdSKonrad Rzeszutek Wilk 18407249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 18417249924eSViresh Kumar if (target_freq > policy->max) 18427249924eSViresh Kumar target_freq = policy->max; 18437249924eSViresh Kumar if (target_freq < policy->min) 18447249924eSViresh Kumar target_freq = policy->min; 18457249924eSViresh Kumar 18467249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 18477249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 18485a1c0228SViresh Kumar 18499c0ebcf7SViresh Kumar /* 18509c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 18519c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 18529c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 18539c0ebcf7SViresh Kumar * calls. 18549c0ebcf7SViresh Kumar */ 18555a1c0228SViresh Kumar if (target_freq == policy->cur) 18565a1c0228SViresh Kumar return 0; 18575a1c0228SViresh Kumar 18581c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 18591c3d85ddSRafael J. Wysocki retval = cpufreq_driver->target(policy, target_freq, relation); 18609c0ebcf7SViresh Kumar else if (cpufreq_driver->target_index) { 18619c0ebcf7SViresh Kumar struct cpufreq_frequency_table *freq_table; 1862d4019f0aSViresh Kumar struct cpufreq_freqs freqs; 1863d4019f0aSViresh Kumar bool notify; 18649c0ebcf7SViresh Kumar int index; 186590d45d17SAshok Raj 18669c0ebcf7SViresh Kumar freq_table = cpufreq_frequency_get_table(policy->cpu); 18679c0ebcf7SViresh Kumar if (unlikely(!freq_table)) { 18689c0ebcf7SViresh Kumar pr_err("%s: Unable to find freq_table\n", __func__); 18699c0ebcf7SViresh Kumar goto out; 18709c0ebcf7SViresh Kumar } 18719c0ebcf7SViresh Kumar 18729c0ebcf7SViresh Kumar retval = cpufreq_frequency_table_target(policy, freq_table, 18739c0ebcf7SViresh Kumar target_freq, relation, &index); 18749c0ebcf7SViresh Kumar if (unlikely(retval)) { 18759c0ebcf7SViresh Kumar pr_err("%s: Unable to find matching freq\n", __func__); 18769c0ebcf7SViresh Kumar goto out; 18779c0ebcf7SViresh Kumar } 18789c0ebcf7SViresh Kumar 1879d4019f0aSViresh Kumar if (freq_table[index].frequency == policy->cur) { 18809c0ebcf7SViresh Kumar retval = 0; 1881d4019f0aSViresh Kumar goto out; 1882d4019f0aSViresh Kumar } 1883d4019f0aSViresh Kumar 1884d4019f0aSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 1885d4019f0aSViresh Kumar 1886d4019f0aSViresh Kumar if (notify) { 1887d4019f0aSViresh Kumar freqs.old = policy->cur; 1888d4019f0aSViresh Kumar freqs.new = freq_table[index].frequency; 1889d4019f0aSViresh Kumar freqs.flags = 0; 1890d4019f0aSViresh Kumar 1891d4019f0aSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 1892e837f9b5SJoe Perches __func__, policy->cpu, freqs.old, freqs.new); 1893d4019f0aSViresh Kumar 18948fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 1895d4019f0aSViresh Kumar } 1896d4019f0aSViresh Kumar 18979c0ebcf7SViresh Kumar retval = cpufreq_driver->target_index(policy, index); 1898d4019f0aSViresh Kumar if (retval) 1899d4019f0aSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", 1900d4019f0aSViresh Kumar __func__, retval); 1901d4019f0aSViresh Kumar 1902ab1b1c4eSViresh Kumar if (notify) 19038fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 19049c0ebcf7SViresh Kumar } 19059c0ebcf7SViresh Kumar 19069c0ebcf7SViresh Kumar out: 19071da177e4SLinus Torvalds return retval; 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 19101da177e4SLinus Torvalds 19111da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 19121da177e4SLinus Torvalds unsigned int target_freq, 19131da177e4SLinus Torvalds unsigned int relation) 19141da177e4SLinus Torvalds { 1915f1829e4aSJulia Lawall int ret = -EINVAL; 19161da177e4SLinus Torvalds 1917ad7722daSviresh kumar down_write(&policy->rwsem); 19181da177e4SLinus Torvalds 19191da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 19201da177e4SLinus Torvalds 1921ad7722daSviresh kumar up_write(&policy->rwsem); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds return ret; 19241da177e4SLinus Torvalds } 19251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 19261da177e4SLinus Torvalds 1927153d7f3fSArjan van de Ven /* 1928153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1929153d7f3fSArjan van de Ven */ 19301da177e4SLinus Torvalds 1931e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1932e08f5f5bSGautham R Shenoy unsigned int event) 19331da177e4SLinus Torvalds { 1934cc993cabSDave Jones int ret; 19356afde10cSThomas Renninger 19366afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 19376afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 19386afde10cSThomas Renninger That this is the case is already ensured in Kconfig 19396afde10cSThomas Renninger */ 19406afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 19416afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 19426afde10cSThomas Renninger #else 19436afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 19446afde10cSThomas Renninger #endif 19451c256245SThomas Renninger 19462f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 19472f0aea93SViresh Kumar if (cpufreq_suspended) 19482f0aea93SViresh Kumar return 0; 19492f0aea93SViresh Kumar 19501c256245SThomas Renninger if (policy->governor->max_transition_latency && 19511c256245SThomas Renninger policy->cpuinfo.transition_latency > 19521c256245SThomas Renninger policy->governor->max_transition_latency) { 19536afde10cSThomas Renninger if (!gov) 19546afde10cSThomas Renninger return -EINVAL; 19556afde10cSThomas Renninger else { 1956e837f9b5SJoe Perches pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", 1957e837f9b5SJoe Perches policy->governor->name, gov->name); 19581c256245SThomas Renninger policy->governor = gov; 19591c256245SThomas Renninger } 19606afde10cSThomas Renninger } 19611da177e4SLinus Torvalds 1962fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 19631da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 19641da177e4SLinus Torvalds return -EINVAL; 19651da177e4SLinus Torvalds 19662d06d8c4SDominik Brodowski pr_debug("__cpufreq_governor for CPU %u, event %u\n", 1967e08f5f5bSGautham R Shenoy policy->cpu, event); 196895731ebbSXiaoguang Chen 196995731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 197056d07db2SSrivatsa S. Bhat if ((policy->governor_enabled && event == CPUFREQ_GOV_START) 1971f73d3933SViresh Kumar || (!policy->governor_enabled 1972f73d3933SViresh Kumar && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) { 197395731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 197495731ebbSXiaoguang Chen return -EBUSY; 197595731ebbSXiaoguang Chen } 197695731ebbSXiaoguang Chen 197795731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 197895731ebbSXiaoguang Chen policy->governor_enabled = false; 197995731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 198095731ebbSXiaoguang Chen policy->governor_enabled = true; 198195731ebbSXiaoguang Chen 198295731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 198395731ebbSXiaoguang Chen 19841da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 19851da177e4SLinus Torvalds 19864d5dcc42SViresh Kumar if (!ret) { 19874d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 19888e53695fSViresh Kumar policy->governor->initialized++; 19894d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 19908e53695fSViresh Kumar policy->governor->initialized--; 199195731ebbSXiaoguang Chen } else { 199295731ebbSXiaoguang Chen /* Restore original values */ 199395731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 199495731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 199595731ebbSXiaoguang Chen policy->governor_enabled = true; 199695731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 199795731ebbSXiaoguang Chen policy->governor_enabled = false; 199895731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 19994d5dcc42SViresh Kumar } 2000b394058fSViresh Kumar 2001fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 2002fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 20031da177e4SLinus Torvalds module_put(policy->governor->owner); 20041da177e4SLinus Torvalds 20051da177e4SLinus Torvalds return ret; 20061da177e4SLinus Torvalds } 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 20091da177e4SLinus Torvalds { 20103bcb09a3SJeremy Fitzhardinge int err; 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds if (!governor) 20131da177e4SLinus Torvalds return -EINVAL; 20141da177e4SLinus Torvalds 2015a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2016a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2017a7b422cdSKonrad Rzeszutek Wilk 20183fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 20191da177e4SLinus Torvalds 2020b394058fSViresh Kumar governor->initialized = 0; 20213bcb09a3SJeremy Fitzhardinge err = -EBUSY; 20223bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 20233bcb09a3SJeremy Fitzhardinge err = 0; 20241da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 20253bcb09a3SJeremy Fitzhardinge } 20261da177e4SLinus Torvalds 20273fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20283bcb09a3SJeremy Fitzhardinge return err; 20291da177e4SLinus Torvalds } 20301da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 20311da177e4SLinus Torvalds 20321da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 20331da177e4SLinus Torvalds { 203490e41bacSPrarit Bhargava int cpu; 203590e41bacSPrarit Bhargava 20361da177e4SLinus Torvalds if (!governor) 20371da177e4SLinus Torvalds return; 20381da177e4SLinus Torvalds 2039a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2040a7b422cdSKonrad Rzeszutek Wilk return; 2041a7b422cdSKonrad Rzeszutek Wilk 204290e41bacSPrarit Bhargava for_each_present_cpu(cpu) { 204390e41bacSPrarit Bhargava if (cpu_online(cpu)) 204490e41bacSPrarit Bhargava continue; 204590e41bacSPrarit Bhargava if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name)) 204690e41bacSPrarit Bhargava strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0"); 204790e41bacSPrarit Bhargava } 204890e41bacSPrarit Bhargava 20493fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 20501da177e4SLinus Torvalds list_del(&governor->governor_list); 20513fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20521da177e4SLinus Torvalds return; 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 20551da177e4SLinus Torvalds 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds /********************************************************************* 20581da177e4SLinus Torvalds * POLICY INTERFACE * 20591da177e4SLinus Torvalds *********************************************************************/ 20601da177e4SLinus Torvalds 20611da177e4SLinus Torvalds /** 20621da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 206329464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 206429464f28SDave Jones * is written 20651da177e4SLinus Torvalds * 20661da177e4SLinus Torvalds * Reads the current cpufreq policy. 20671da177e4SLinus Torvalds */ 20681da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 20691da177e4SLinus Torvalds { 20701da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 20711da177e4SLinus Torvalds if (!policy) 20721da177e4SLinus Torvalds return -EINVAL; 20731da177e4SLinus Torvalds 20741da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 20751da177e4SLinus Torvalds if (!cpu_policy) 20761da177e4SLinus Torvalds return -EINVAL; 20771da177e4SLinus Torvalds 2078d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 20791da177e4SLinus Torvalds 20801da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 20811da177e4SLinus Torvalds return 0; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 20841da177e4SLinus Torvalds 2085153d7f3fSArjan van de Ven /* 2086037ce839SViresh Kumar * policy : current policy. 2087037ce839SViresh Kumar * new_policy: policy to be set. 2088153d7f3fSArjan van de Ven */ 2089037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 20903a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 20911da177e4SLinus Torvalds { 2092d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2093d9a789c7SRafael J. Wysocki int ret; 20941da177e4SLinus Torvalds 2095e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2096e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 20971da177e4SLinus Torvalds 2098d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 20991da177e4SLinus Torvalds 2100d9a789c7SRafael J. Wysocki if (new_policy->min > policy->max || new_policy->max < policy->min) 2101d9a789c7SRafael J. Wysocki return -EINVAL; 21029c9a43edSMattia Dongili 21031da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 21043a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 21051da177e4SLinus Torvalds if (ret) 2106d9a789c7SRafael J. Wysocki return ret; 21071da177e4SLinus Torvalds 21081da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2109e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21103a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 21111da177e4SLinus Torvalds 21121da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 2113e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21143a3e9e06SViresh Kumar CPUFREQ_INCOMPATIBLE, new_policy); 21151da177e4SLinus Torvalds 2116bb176f7dSViresh Kumar /* 2117bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2118bb176f7dSViresh Kumar * different to the first one 2119bb176f7dSViresh Kumar */ 21203a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2121e041c683SAlan Stern if (ret) 2122d9a789c7SRafael J. Wysocki return ret; 21231da177e4SLinus Torvalds 21241da177e4SLinus Torvalds /* notification of the new policy */ 2125e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21263a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 21271da177e4SLinus Torvalds 21283a3e9e06SViresh Kumar policy->min = new_policy->min; 21293a3e9e06SViresh Kumar policy->max = new_policy->max; 21301da177e4SLinus Torvalds 21312d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 21323a3e9e06SViresh Kumar policy->min, policy->max); 21331da177e4SLinus Torvalds 21341c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 21353a3e9e06SViresh Kumar policy->policy = new_policy->policy; 21362d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2137d9a789c7SRafael J. Wysocki return cpufreq_driver->setpolicy(new_policy); 2138d9a789c7SRafael J. Wysocki } 2139d9a789c7SRafael J. Wysocki 2140d9a789c7SRafael J. Wysocki if (new_policy->governor == policy->governor) 2141d9a789c7SRafael J. Wysocki goto out; 21421da177e4SLinus Torvalds 21432d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 21441da177e4SLinus Torvalds 2145d9a789c7SRafael J. Wysocki /* save old, working values */ 2146d9a789c7SRafael J. Wysocki old_gov = policy->governor; 21471da177e4SLinus Torvalds /* end old governor */ 2148d9a789c7SRafael J. Wysocki if (old_gov) { 21493a3e9e06SViresh Kumar __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 2150ad7722daSviresh kumar up_write(&policy->rwsem); 2151d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2152ad7722daSviresh kumar down_write(&policy->rwsem); 21537bd353a9SViresh Kumar } 21541da177e4SLinus Torvalds 21551da177e4SLinus Torvalds /* start new governor */ 21563a3e9e06SViresh Kumar policy->governor = new_policy->governor; 21573a3e9e06SViresh Kumar if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) { 2158d9a789c7SRafael J. Wysocki if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) 2159d9a789c7SRafael J. Wysocki goto out; 2160d9a789c7SRafael J. Wysocki 2161ad7722daSviresh kumar up_write(&policy->rwsem); 2162d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2163ad7722daSviresh kumar down_write(&policy->rwsem); 2164955ef483SViresh Kumar } 21657bd353a9SViresh Kumar 21661da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2167d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 21681da177e4SLinus Torvalds if (old_gov) { 21693a3e9e06SViresh Kumar policy->governor = old_gov; 2170d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); 2171d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_START); 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 2174d9a789c7SRafael J. Wysocki return -EINVAL; 2175d9a789c7SRafael J. Wysocki 2176d9a789c7SRafael J. Wysocki out: 2177d9a789c7SRafael J. Wysocki pr_debug("governor: change or update limits\n"); 2178d9a789c7SRafael J. Wysocki return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 21791da177e4SLinus Torvalds } 21801da177e4SLinus Torvalds 21811da177e4SLinus Torvalds /** 21821da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 21831da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 21841da177e4SLinus Torvalds * 218525985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 21861da177e4SLinus Torvalds * at different times. 21871da177e4SLinus Torvalds */ 21881da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 21891da177e4SLinus Torvalds { 21903a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 21913a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 2192f1829e4aSJulia Lawall int ret; 21931da177e4SLinus Torvalds 21943a3e9e06SViresh Kumar if (!policy) { 2195f1829e4aSJulia Lawall ret = -ENODEV; 2196f1829e4aSJulia Lawall goto no_policy; 2197f1829e4aSJulia Lawall } 21981da177e4SLinus Torvalds 2199ad7722daSviresh kumar down_write(&policy->rwsem); 22001da177e4SLinus Torvalds 22012d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 2202d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 22033a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 22043a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 22053a3e9e06SViresh Kumar new_policy.policy = policy->user_policy.policy; 22063a3e9e06SViresh Kumar new_policy.governor = policy->user_policy.governor; 22071da177e4SLinus Torvalds 2208bb176f7dSViresh Kumar /* 2209bb176f7dSViresh Kumar * BIOS might change freq behind our back 2210bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2211bb176f7dSViresh Kumar */ 22122ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 22133a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 2214bd0fa9bbSViresh Kumar if (WARN_ON(!new_policy.cur)) { 2215bd0fa9bbSViresh Kumar ret = -EIO; 2216bd0fa9bbSViresh Kumar goto no_policy; 2217bd0fa9bbSViresh Kumar } 2218bd0fa9bbSViresh Kumar 22193a3e9e06SViresh Kumar if (!policy->cur) { 2220e837f9b5SJoe Perches pr_debug("Driver did not initialize current freq\n"); 22213a3e9e06SViresh Kumar policy->cur = new_policy.cur; 2222a85f7bd3SThomas Renninger } else { 22239c0ebcf7SViresh Kumar if (policy->cur != new_policy.cur && has_target()) 22243a3e9e06SViresh Kumar cpufreq_out_of_sync(cpu, policy->cur, 22253a3e9e06SViresh Kumar new_policy.cur); 22260961dd0dSThomas Renninger } 2227a85f7bd3SThomas Renninger } 22280961dd0dSThomas Renninger 2229037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 22301da177e4SLinus Torvalds 2231ad7722daSviresh kumar up_write(&policy->rwsem); 22325a01f2e8SVenkatesh Pallipadi 22333a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 2234f1829e4aSJulia Lawall no_policy: 22351da177e4SLinus Torvalds return ret; 22361da177e4SLinus Torvalds } 22371da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 22381da177e4SLinus Torvalds 22392760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 2240c32b6b8eSAshok Raj unsigned long action, void *hcpu) 2241c32b6b8eSAshok Raj { 2242c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 22438a25a2fdSKay Sievers struct device *dev; 2244c32b6b8eSAshok Raj 22458a25a2fdSKay Sievers dev = get_cpu_device(cpu); 22468a25a2fdSKay Sievers if (dev) { 22475302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 2248c32b6b8eSAshok Raj case CPU_ONLINE: 224996bbbe4aSViresh Kumar __cpufreq_add_dev(dev, NULL); 2250c32b6b8eSAshok Raj break; 22515302c3fbSSrivatsa S. Bhat 2252c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 225396bbbe4aSViresh Kumar __cpufreq_remove_dev_prepare(dev, NULL); 22541aee40acSSrivatsa S. Bhat break; 22551aee40acSSrivatsa S. Bhat 22561aee40acSSrivatsa S. Bhat case CPU_POST_DEAD: 225796bbbe4aSViresh Kumar __cpufreq_remove_dev_finish(dev, NULL); 2258c32b6b8eSAshok Raj break; 22595302c3fbSSrivatsa S. Bhat 22605a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 226196bbbe4aSViresh Kumar __cpufreq_add_dev(dev, NULL); 2262c32b6b8eSAshok Raj break; 2263c32b6b8eSAshok Raj } 2264c32b6b8eSAshok Raj } 2265c32b6b8eSAshok Raj return NOTIFY_OK; 2266c32b6b8eSAshok Raj } 2267c32b6b8eSAshok Raj 22689c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2269c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2270c32b6b8eSAshok Raj }; 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds /********************************************************************* 22736f19efc0SLukasz Majewski * BOOST * 22746f19efc0SLukasz Majewski *********************************************************************/ 22756f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 22766f19efc0SLukasz Majewski { 22776f19efc0SLukasz Majewski struct cpufreq_frequency_table *freq_table; 22786f19efc0SLukasz Majewski struct cpufreq_policy *policy; 22796f19efc0SLukasz Majewski int ret = -EINVAL; 22806f19efc0SLukasz Majewski 22816f19efc0SLukasz Majewski list_for_each_entry(policy, &cpufreq_policy_list, policy_list) { 22826f19efc0SLukasz Majewski freq_table = cpufreq_frequency_get_table(policy->cpu); 22836f19efc0SLukasz Majewski if (freq_table) { 22846f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 22856f19efc0SLukasz Majewski freq_table); 22866f19efc0SLukasz Majewski if (ret) { 22876f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 22886f19efc0SLukasz Majewski __func__); 22896f19efc0SLukasz Majewski break; 22906f19efc0SLukasz Majewski } 22916f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 22926f19efc0SLukasz Majewski __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 22936f19efc0SLukasz Majewski } 22946f19efc0SLukasz Majewski } 22956f19efc0SLukasz Majewski 22966f19efc0SLukasz Majewski return ret; 22976f19efc0SLukasz Majewski } 22986f19efc0SLukasz Majewski 22996f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 23006f19efc0SLukasz Majewski { 23016f19efc0SLukasz Majewski unsigned long flags; 23026f19efc0SLukasz Majewski int ret = 0; 23036f19efc0SLukasz Majewski 23046f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 23056f19efc0SLukasz Majewski return 0; 23066f19efc0SLukasz Majewski 23076f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 23086f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 23096f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23106f19efc0SLukasz Majewski 23116f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 23126f19efc0SLukasz Majewski if (ret) { 23136f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 23146f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 23156f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23166f19efc0SLukasz Majewski 2317e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2318e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 23196f19efc0SLukasz Majewski } 23206f19efc0SLukasz Majewski 23216f19efc0SLukasz Majewski return ret; 23226f19efc0SLukasz Majewski } 23236f19efc0SLukasz Majewski 23246f19efc0SLukasz Majewski int cpufreq_boost_supported(void) 23256f19efc0SLukasz Majewski { 23266f19efc0SLukasz Majewski if (likely(cpufreq_driver)) 23276f19efc0SLukasz Majewski return cpufreq_driver->boost_supported; 23286f19efc0SLukasz Majewski 23296f19efc0SLukasz Majewski return 0; 23306f19efc0SLukasz Majewski } 23316f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported); 23326f19efc0SLukasz Majewski 23336f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 23346f19efc0SLukasz Majewski { 23356f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 23366f19efc0SLukasz Majewski } 23376f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 23386f19efc0SLukasz Majewski 23396f19efc0SLukasz Majewski /********************************************************************* 23401da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 23411da177e4SLinus Torvalds *********************************************************************/ 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds /** 23441da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 23451da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 23461da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 23471da177e4SLinus Torvalds * 23481da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 23491da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 23501da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 23511da177e4SLinus Torvalds * 23521da177e4SLinus Torvalds */ 2353221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 23541da177e4SLinus Torvalds { 23551da177e4SLinus Torvalds unsigned long flags; 23561da177e4SLinus Torvalds int ret; 23571da177e4SLinus Torvalds 2358a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2359a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2360a7b422cdSKonrad Rzeszutek Wilk 23611da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 23629c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 23639832235fSRafael J. Wysocki driver_data->target) || 23649832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 23659832235fSRafael J. Wysocki driver_data->target))) 23661da177e4SLinus Torvalds return -EINVAL; 23671da177e4SLinus Torvalds 23682d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 23691da177e4SLinus Torvalds 23701da177e4SLinus Torvalds if (driver_data->setpolicy) 23711da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 23721da177e4SLinus Torvalds 23730d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 23741c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 23750d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23764dea5806SYinghai Lu return -EEXIST; 23771da177e4SLinus Torvalds } 23781c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 23790d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23801da177e4SLinus Torvalds 23816f19efc0SLukasz Majewski if (cpufreq_boost_supported()) { 23826f19efc0SLukasz Majewski /* 23836f19efc0SLukasz Majewski * Check if driver provides function to enable boost - 23846f19efc0SLukasz Majewski * if not, use cpufreq_boost_set_sw as default 23856f19efc0SLukasz Majewski */ 23866f19efc0SLukasz Majewski if (!cpufreq_driver->set_boost) 23876f19efc0SLukasz Majewski cpufreq_driver->set_boost = cpufreq_boost_set_sw; 23886f19efc0SLukasz Majewski 23896f19efc0SLukasz Majewski ret = cpufreq_sysfs_create_file(&boost.attr); 23906f19efc0SLukasz Majewski if (ret) { 23916f19efc0SLukasz Majewski pr_err("%s: cannot register global BOOST sysfs file\n", 23926f19efc0SLukasz Majewski __func__); 23936f19efc0SLukasz Majewski goto err_null_driver; 23946f19efc0SLukasz Majewski } 23956f19efc0SLukasz Majewski } 23966f19efc0SLukasz Majewski 23978a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 23988f5bc2abSJiri Slaby if (ret) 23996f19efc0SLukasz Majewski goto err_boost_unreg; 24001da177e4SLinus Torvalds 24011c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) { 24021da177e4SLinus Torvalds int i; 24031da177e4SLinus Torvalds ret = -ENODEV; 24041da177e4SLinus Torvalds 24051da177e4SLinus Torvalds /* check for at least one working CPU */ 24067a6aedfaSMike Travis for (i = 0; i < nr_cpu_ids; i++) 24077a6aedfaSMike Travis if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) { 24081da177e4SLinus Torvalds ret = 0; 24097a6aedfaSMike Travis break; 24107a6aedfaSMike Travis } 24111da177e4SLinus Torvalds 24121da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 24131da177e4SLinus Torvalds if (ret) { 24142d06d8c4SDominik Brodowski pr_debug("no CPU initialized for driver %s\n", 2415e08f5f5bSGautham R Shenoy driver_data->name); 24168a25a2fdSKay Sievers goto err_if_unreg; 24171da177e4SLinus Torvalds } 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds 242065edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 24212d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 24221da177e4SLinus Torvalds 24238f5bc2abSJiri Slaby return 0; 24248a25a2fdSKay Sievers err_if_unreg: 24258a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 24266f19efc0SLukasz Majewski err_boost_unreg: 24276f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 24286f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 24298f5bc2abSJiri Slaby err_null_driver: 24300d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24311c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24320d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24334d34a67dSDave Jones return ret; 24341da177e4SLinus Torvalds } 24351da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds /** 24381da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 24391da177e4SLinus Torvalds * 24401da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 24411da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 24421da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 24431da177e4SLinus Torvalds * currently not initialised. 24441da177e4SLinus Torvalds */ 2445221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 24461da177e4SLinus Torvalds { 24471da177e4SLinus Torvalds unsigned long flags; 24481da177e4SLinus Torvalds 24491c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 24501da177e4SLinus Torvalds return -EINVAL; 24511da177e4SLinus Torvalds 24522d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 24531da177e4SLinus Torvalds 24548a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 24556f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 24566f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 24576f19efc0SLukasz Majewski 245865edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 24591da177e4SLinus Torvalds 24606eed9404SViresh Kumar down_write(&cpufreq_rwsem); 24610d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24626eed9404SViresh Kumar 24631c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24646eed9404SViresh Kumar 24650d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24666eed9404SViresh Kumar up_write(&cpufreq_rwsem); 24671da177e4SLinus Torvalds 24681da177e4SLinus Torvalds return 0; 24691da177e4SLinus Torvalds } 24701da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 24715a01f2e8SVenkatesh Pallipadi 24725a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 24735a01f2e8SVenkatesh Pallipadi { 2474a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2475a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2476a7b422cdSKonrad Rzeszutek Wilk 24772361be23SViresh Kumar cpufreq_global_kobject = kobject_create(); 24788aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 24798aa84ad8SThomas Renninger 24805a01f2e8SVenkatesh Pallipadi return 0; 24815a01f2e8SVenkatesh Pallipadi } 24825a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2483