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> 3090de2a4aSDoug Anderson #include <linux/syscore_ops.h> 315ff0a268SViresh Kumar #include <linux/tick.h> 326f4f2723SThomas Renninger #include <trace/events/power.h> 336f4f2723SThomas Renninger 34b4f0676fSViresh Kumar /* Macros to iterate over lists */ 35b4f0676fSViresh Kumar /* Iterate over online CPUs policies */ 36b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list); 37b4f0676fSViresh Kumar #define for_each_policy(__policy) \ 38b4f0676fSViresh Kumar list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) 39b4f0676fSViresh Kumar 40*f7b27061SViresh Kumar /* Iterate over governors */ 41*f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 42*f7b27061SViresh Kumar #define for_each_governor(__governor) \ 43*f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 44*f7b27061SViresh Kumar 451da177e4SLinus Torvalds /** 46cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 471da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 481da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 491da177e4SLinus Torvalds */ 501c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 517a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 528414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback); 53bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 546f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock); 55bb176f7dSViresh Kumar 56084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */ 57e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor); 581da177e4SLinus Torvalds 592f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 602f0aea93SViresh Kumar static bool cpufreq_suspended; 611da177e4SLinus Torvalds 629c0ebcf7SViresh Kumar static inline bool has_target(void) 639c0ebcf7SViresh Kumar { 649c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 659c0ebcf7SViresh Kumar } 669c0ebcf7SViresh Kumar 675a01f2e8SVenkatesh Pallipadi /* 686eed9404SViresh Kumar * rwsem to guarantee that cpufreq driver module doesn't unload during critical 696eed9404SViresh Kumar * sections 706eed9404SViresh Kumar */ 716eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem); 726eed9404SViresh Kumar 731da177e4SLinus Torvalds /* internal prototypes */ 7429464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy, 7529464f28SDave Jones unsigned int event); 76d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 7765f27f38SDavid Howells static void handle_update(struct work_struct *work); 781da177e4SLinus Torvalds 791da177e4SLinus Torvalds /** 801da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 811da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 821da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 831da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 841da177e4SLinus Torvalds * The mutex locks both lists. 851da177e4SLinus Torvalds */ 86e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 87b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 881da177e4SLinus Torvalds 8974212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 90b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 91b4dfdbb3SAlan Stern { 92b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 9374212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 94b4dfdbb3SAlan Stern return 0; 95b4dfdbb3SAlan Stern } 96b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 971da177e4SLinus Torvalds 98a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 99da584455SViresh Kumar static int cpufreq_disabled(void) 100a7b422cdSKonrad Rzeszutek Wilk { 101a7b422cdSKonrad Rzeszutek Wilk return off; 102a7b422cdSKonrad Rzeszutek Wilk } 103a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 104a7b422cdSKonrad Rzeszutek Wilk { 105a7b422cdSKonrad Rzeszutek Wilk off = 1; 106a7b422cdSKonrad Rzeszutek Wilk } 1073fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1081da177e4SLinus Torvalds 1094d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1104d5dcc42SViresh Kumar { 1110b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1124d5dcc42SViresh Kumar } 1133f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1144d5dcc42SViresh Kumar 115944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 116944e9a03SViresh Kumar { 117944e9a03SViresh Kumar if (have_governor_per_policy()) 118944e9a03SViresh Kumar return &policy->kobj; 119944e9a03SViresh Kumar else 120944e9a03SViresh Kumar return cpufreq_global_kobject; 121944e9a03SViresh Kumar } 122944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 123944e9a03SViresh Kumar 12472a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 12572a4ce34SViresh Kumar { 12672a4ce34SViresh Kumar u64 idle_time; 12772a4ce34SViresh Kumar u64 cur_wall_time; 12872a4ce34SViresh Kumar u64 busy_time; 12972a4ce34SViresh Kumar 13072a4ce34SViresh Kumar cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); 13172a4ce34SViresh Kumar 13272a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 13372a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 13472a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 13572a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 13672a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 13772a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 13872a4ce34SViresh Kumar 13972a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 14072a4ce34SViresh Kumar if (wall) 14172a4ce34SViresh Kumar *wall = cputime_to_usecs(cur_wall_time); 14272a4ce34SViresh Kumar 14372a4ce34SViresh Kumar return cputime_to_usecs(idle_time); 14472a4ce34SViresh Kumar } 14572a4ce34SViresh Kumar 14672a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 14772a4ce34SViresh Kumar { 14872a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 14972a4ce34SViresh Kumar 15072a4ce34SViresh Kumar if (idle_time == -1ULL) 15172a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 15272a4ce34SViresh Kumar else if (!io_busy) 15372a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 15472a4ce34SViresh Kumar 15572a4ce34SViresh Kumar return idle_time; 15672a4ce34SViresh Kumar } 15772a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 15872a4ce34SViresh Kumar 15970e9e778SViresh Kumar /* 16070e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 16170e9e778SViresh Kumar * drivers of SMP systems. It will do following: 16270e9e778SViresh Kumar * - validate & show freq table passed 16370e9e778SViresh Kumar * - set policies transition latency 16470e9e778SViresh Kumar * - policy->cpus with all possible CPUs 16570e9e778SViresh Kumar */ 16670e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy, 16770e9e778SViresh Kumar struct cpufreq_frequency_table *table, 16870e9e778SViresh Kumar unsigned int transition_latency) 16970e9e778SViresh Kumar { 17070e9e778SViresh Kumar int ret; 17170e9e778SViresh Kumar 17270e9e778SViresh Kumar ret = cpufreq_table_validate_and_show(policy, table); 17370e9e778SViresh Kumar if (ret) { 17470e9e778SViresh Kumar pr_err("%s: invalid frequency table: %d\n", __func__, ret); 17570e9e778SViresh Kumar return ret; 17670e9e778SViresh Kumar } 17770e9e778SViresh Kumar 17870e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 17970e9e778SViresh Kumar 18070e9e778SViresh Kumar /* 18170e9e778SViresh Kumar * The driver only supports the SMP configuartion where all processors 18270e9e778SViresh Kumar * share the clock and voltage and clock. 18370e9e778SViresh Kumar */ 18470e9e778SViresh Kumar cpumask_setall(policy->cpus); 18570e9e778SViresh Kumar 18670e9e778SViresh Kumar return 0; 18770e9e778SViresh Kumar } 18870e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 18970e9e778SViresh Kumar 190652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 191652ed95dSViresh Kumar { 192652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 193652ed95dSViresh Kumar 194652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 195e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 196e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 197652ed95dSViresh Kumar return 0; 198652ed95dSViresh Kumar } 199652ed95dSViresh Kumar 200652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 201652ed95dSViresh Kumar } 202652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 203652ed95dSViresh Kumar 204e0b3165bSViresh Kumar /* Only for cpufreq core internal use */ 205e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 206e0b3165bSViresh Kumar { 207e0b3165bSViresh Kumar return per_cpu(cpufreq_cpu_data, cpu); 208e0b3165bSViresh Kumar } 209e0b3165bSViresh Kumar 2106eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2111da177e4SLinus Torvalds { 2126eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2131da177e4SLinus Torvalds unsigned long flags; 2141da177e4SLinus Torvalds 2151e63eaf0SViresh Kumar if (cpu >= nr_cpu_ids) 2166eed9404SViresh Kumar return NULL; 2176eed9404SViresh Kumar 2186eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 2196eed9404SViresh Kumar return NULL; 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* get the cpufreq driver */ 2220d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2231da177e4SLinus Torvalds 2246eed9404SViresh Kumar if (cpufreq_driver) { 2251da177e4SLinus Torvalds /* get the CPU */ 2263a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 2276eed9404SViresh Kumar if (policy) 2286eed9404SViresh Kumar kobject_get(&policy->kobj); 2296eed9404SViresh Kumar } 2306eed9404SViresh Kumar 2316eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2321da177e4SLinus Torvalds 2333a3e9e06SViresh Kumar if (!policy) 2346eed9404SViresh Kumar up_read(&cpufreq_rwsem); 2351da177e4SLinus Torvalds 2363a3e9e06SViresh Kumar return policy; 237a9144436SStephen Boyd } 2381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2391da177e4SLinus Torvalds 2403a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 241a9144436SStephen Boyd { 2426eed9404SViresh Kumar kobject_put(&policy->kobj); 2436eed9404SViresh Kumar up_read(&cpufreq_rwsem); 244a9144436SStephen Boyd } 2451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds /********************************************************************* 2481da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2491da177e4SLinus Torvalds *********************************************************************/ 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds /** 2521da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2531da177e4SLinus Torvalds * 2541da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2551da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2561da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2571da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2581da177e4SLinus Torvalds */ 25939c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 26039c132eeSViresh Kumar { 2611da177e4SLinus Torvalds #ifndef CONFIG_SMP 2621da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2631da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2661da177e4SLinus Torvalds return; 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2691da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2701da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 271e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 272e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 2731da177e4SLinus Torvalds } 2740b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 275e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 276e08f5f5bSGautham R Shenoy ci->new); 277e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 278e837f9b5SJoe Perches loops_per_jiffy, ci->new); 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds #endif 28139c132eeSViresh Kumar } 2821da177e4SLinus Torvalds 2830956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 284b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 2851da177e4SLinus Torvalds { 2861da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 2871da177e4SLinus Torvalds 288d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 289d5aaffa9SDirk Brandewie return; 290d5aaffa9SDirk Brandewie 2911c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 2922d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 293e4472cb3SDave Jones state, freqs->new); 2941da177e4SLinus Torvalds 2951da177e4SLinus Torvalds switch (state) { 296e4472cb3SDave Jones 2971da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 298e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 299e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 300e4472cb3SDave Jones * "old frequency". 3011da177e4SLinus Torvalds */ 3021c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 303e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 304e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 305e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 306e4472cb3SDave Jones freqs->old, policy->cur); 307e4472cb3SDave Jones freqs->old = policy->cur; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds } 310b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 311e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3121da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3131da177e4SLinus Torvalds break; 314e4472cb3SDave Jones 3151da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3161da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 317e837f9b5SJoe Perches pr_debug("FREQ: %lu - CPU: %lu\n", 318e837f9b5SJoe Perches (unsigned long)freqs->new, (unsigned long)freqs->cpu); 31925e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 320b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 321e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 322e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 323e4472cb3SDave Jones policy->cur = freqs->new; 3241da177e4SLinus Torvalds break; 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds } 327bb176f7dSViresh Kumar 328b43a7ffbSViresh Kumar /** 329b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 330b43a7ffbSViresh Kumar * on frequency transition. 331b43a7ffbSViresh Kumar * 332b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 333b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 334b43a7ffbSViresh Kumar * external effects. 335b43a7ffbSViresh Kumar */ 336236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 337b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 338b43a7ffbSViresh Kumar { 339b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 340b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 341b43a7ffbSViresh Kumar } 3421da177e4SLinus Torvalds 343f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 344236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 345f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 346f7ba3b41SViresh Kumar { 347f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 348f7ba3b41SViresh Kumar if (!transition_failed) 349f7ba3b41SViresh Kumar return; 350f7ba3b41SViresh Kumar 351f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 352f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 353f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 354f7ba3b41SViresh Kumar } 355f7ba3b41SViresh Kumar 35612478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 35712478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 35812478cf0SSrivatsa S. Bhat { 359ca654dc3SSrivatsa S. Bhat 360ca654dc3SSrivatsa S. Bhat /* 361ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 362ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 363ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 364ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 365ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 366ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 367ca654dc3SSrivatsa S. Bhat */ 368ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 369ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 370ca654dc3SSrivatsa S. Bhat 37112478cf0SSrivatsa S. Bhat wait: 37212478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 37312478cf0SSrivatsa S. Bhat 37412478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 37512478cf0SSrivatsa S. Bhat 37612478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 37712478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 37812478cf0SSrivatsa S. Bhat goto wait; 37912478cf0SSrivatsa S. Bhat } 38012478cf0SSrivatsa S. Bhat 38112478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 382ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 38312478cf0SSrivatsa S. Bhat 38412478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 38512478cf0SSrivatsa S. Bhat 38612478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 38712478cf0SSrivatsa S. Bhat } 38812478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 38912478cf0SSrivatsa S. Bhat 39012478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 39112478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 39212478cf0SSrivatsa S. Bhat { 39312478cf0SSrivatsa S. Bhat if (unlikely(WARN_ON(!policy->transition_ongoing))) 39412478cf0SSrivatsa S. Bhat return; 39512478cf0SSrivatsa S. Bhat 39612478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 39712478cf0SSrivatsa S. Bhat 39812478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 399ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 40012478cf0SSrivatsa S. Bhat 40112478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 40212478cf0SSrivatsa S. Bhat } 40312478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 40412478cf0SSrivatsa S. Bhat 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /********************************************************************* 4071da177e4SLinus Torvalds * SYSFS INTERFACE * 4081da177e4SLinus Torvalds *********************************************************************/ 4098a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 4106f19efc0SLukasz Majewski struct attribute *attr, char *buf) 4116f19efc0SLukasz Majewski { 4126f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 4136f19efc0SLukasz Majewski } 4146f19efc0SLukasz Majewski 4156f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, 4166f19efc0SLukasz Majewski const char *buf, size_t count) 4176f19efc0SLukasz Majewski { 4186f19efc0SLukasz Majewski int ret, enable; 4196f19efc0SLukasz Majewski 4206f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 4216f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 4226f19efc0SLukasz Majewski return -EINVAL; 4236f19efc0SLukasz Majewski 4246f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 425e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 426e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 4276f19efc0SLukasz Majewski return -EINVAL; 4286f19efc0SLukasz Majewski } 4296f19efc0SLukasz Majewski 430e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 431e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 4326f19efc0SLukasz Majewski 4336f19efc0SLukasz Majewski return count; 4346f19efc0SLukasz Majewski } 4356f19efc0SLukasz Majewski define_one_global_rw(boost); 4361da177e4SLinus Torvalds 43742f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 4383bcb09a3SJeremy Fitzhardinge { 4393bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 4403bcb09a3SJeremy Fitzhardinge 441*f7b27061SViresh Kumar for_each_governor(t) 4427c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 4433bcb09a3SJeremy Fitzhardinge return t; 4443bcb09a3SJeremy Fitzhardinge 4453bcb09a3SJeremy Fitzhardinge return NULL; 4463bcb09a3SJeremy Fitzhardinge } 4473bcb09a3SJeremy Fitzhardinge 4481da177e4SLinus Torvalds /** 4491da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 4521da177e4SLinus Torvalds struct cpufreq_governor **governor) 4531da177e4SLinus Torvalds { 4543bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 4553bcb09a3SJeremy Fitzhardinge 4561c3d85ddSRafael J. Wysocki if (!cpufreq_driver) 4573bcb09a3SJeremy Fitzhardinge goto out; 4583bcb09a3SJeremy Fitzhardinge 4591c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 4607c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 4611da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 4623bcb09a3SJeremy Fitzhardinge err = 0; 4637c4f4539SRasmus Villemoes } else if (!strncasecmp(str_governor, "powersave", 464e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 4651da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 4663bcb09a3SJeremy Fitzhardinge err = 0; 4671da177e4SLinus Torvalds } 4682e1cc3a5SViresh Kumar } else { 4691da177e4SLinus Torvalds struct cpufreq_governor *t; 4703bcb09a3SJeremy Fitzhardinge 4713fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4723bcb09a3SJeremy Fitzhardinge 47342f91fa1SViresh Kumar t = find_governor(str_governor); 4743bcb09a3SJeremy Fitzhardinge 475ea714970SJeremy Fitzhardinge if (t == NULL) { 476ea714970SJeremy Fitzhardinge int ret; 477ea714970SJeremy Fitzhardinge 478ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4791a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 480ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 481ea714970SJeremy Fitzhardinge 482ea714970SJeremy Fitzhardinge if (ret == 0) 48342f91fa1SViresh Kumar t = find_governor(str_governor); 484ea714970SJeremy Fitzhardinge } 485ea714970SJeremy Fitzhardinge 4863bcb09a3SJeremy Fitzhardinge if (t != NULL) { 4871da177e4SLinus Torvalds *governor = t; 4883bcb09a3SJeremy Fitzhardinge err = 0; 4891da177e4SLinus Torvalds } 4903bcb09a3SJeremy Fitzhardinge 4913bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds out: 4943bcb09a3SJeremy Fitzhardinge return err; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds /** 498e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 499e08f5f5bSGautham R Shenoy * print out cpufreq information 5001da177e4SLinus Torvalds * 5011da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 5021da177e4SLinus Torvalds * "unsigned int". 5031da177e4SLinus Torvalds */ 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds #define show_one(file_name, object) \ 5061da177e4SLinus Torvalds static ssize_t show_##file_name \ 5071da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 5081da177e4SLinus Torvalds { \ 5091da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 5101da177e4SLinus Torvalds } 5111da177e4SLinus Torvalds 5121da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 5131da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 514ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 5151da177e4SLinus Torvalds show_one(scaling_min_freq, min); 5161da177e4SLinus Torvalds show_one(scaling_max_freq, max); 517c034b02eSDirk Brandewie 51809347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 519c034b02eSDirk Brandewie { 520c034b02eSDirk Brandewie ssize_t ret; 521c034b02eSDirk Brandewie 522c034b02eSDirk Brandewie if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 523c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 524c034b02eSDirk Brandewie else 525c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 526c034b02eSDirk Brandewie return ret; 527c034b02eSDirk Brandewie } 5281da177e4SLinus Torvalds 529037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 5303a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 5317970e08bSThomas Renninger 5321da177e4SLinus Torvalds /** 5331da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 5341da177e4SLinus Torvalds */ 5351da177e4SLinus Torvalds #define store_one(file_name, object) \ 5361da177e4SLinus Torvalds static ssize_t store_##file_name \ 5371da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 5381da177e4SLinus Torvalds { \ 539619c144cSVince Hsu int ret, temp; \ 5401da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 5411da177e4SLinus Torvalds \ 5421da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 5431da177e4SLinus Torvalds if (ret) \ 5441da177e4SLinus Torvalds return -EINVAL; \ 5451da177e4SLinus Torvalds \ 5461da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 5471da177e4SLinus Torvalds if (ret != 1) \ 5481da177e4SLinus Torvalds return -EINVAL; \ 5491da177e4SLinus Torvalds \ 550619c144cSVince Hsu temp = new_policy.object; \ 551037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 552619c144cSVince Hsu if (!ret) \ 553619c144cSVince Hsu policy->user_policy.object = temp; \ 5541da177e4SLinus Torvalds \ 5551da177e4SLinus Torvalds return ret ? ret : count; \ 5561da177e4SLinus Torvalds } 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds store_one(scaling_min_freq, min); 5591da177e4SLinus Torvalds store_one(scaling_max_freq, max); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds /** 5621da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 5631da177e4SLinus Torvalds */ 564e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 565e08f5f5bSGautham R Shenoy char *buf) 5661da177e4SLinus Torvalds { 567d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 5681da177e4SLinus Torvalds if (!cur_freq) 5691da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 5701da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds /** 5741da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5751da177e4SLinus Torvalds */ 576905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 5771da177e4SLinus Torvalds { 5781da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 5791da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 5801da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 5811da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 5821da177e4SLinus Torvalds else if (policy->governor) 5834b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 58429464f28SDave Jones policy->governor->name); 5851da177e4SLinus Torvalds return -EINVAL; 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds /** 5891da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 5901da177e4SLinus Torvalds */ 5911da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 5921da177e4SLinus Torvalds const char *buf, size_t count) 5931da177e4SLinus Torvalds { 5945136fa56SSrivatsa S. Bhat int ret; 5951da177e4SLinus Torvalds char str_governor[16]; 5961da177e4SLinus Torvalds struct cpufreq_policy new_policy; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 5991da177e4SLinus Torvalds if (ret) 6001da177e4SLinus Torvalds return ret; 6011da177e4SLinus Torvalds 6021da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 6031da177e4SLinus Torvalds if (ret != 1) 6041da177e4SLinus Torvalds return -EINVAL; 6051da177e4SLinus Torvalds 606e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 607e08f5f5bSGautham R Shenoy &new_policy.governor)) 6081da177e4SLinus Torvalds return -EINVAL; 6091da177e4SLinus Torvalds 610037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 6117970e08bSThomas Renninger 6127970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 6137970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 6147970e08bSThomas Renninger 615e08f5f5bSGautham R Shenoy if (ret) 616e08f5f5bSGautham R Shenoy return ret; 617e08f5f5bSGautham R Shenoy else 618e08f5f5bSGautham R Shenoy return count; 6191da177e4SLinus Torvalds } 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds /** 6221da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 6231da177e4SLinus Torvalds */ 6241da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 6251da177e4SLinus Torvalds { 6261c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds /** 6301da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 6311da177e4SLinus Torvalds */ 6321da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 6331da177e4SLinus Torvalds char *buf) 6341da177e4SLinus Torvalds { 6351da177e4SLinus Torvalds ssize_t i = 0; 6361da177e4SLinus Torvalds struct cpufreq_governor *t; 6371da177e4SLinus Torvalds 6389c0ebcf7SViresh Kumar if (!has_target()) { 6391da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 6401da177e4SLinus Torvalds goto out; 6411da177e4SLinus Torvalds } 6421da177e4SLinus Torvalds 643*f7b27061SViresh Kumar for_each_governor(t) { 64429464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 64529464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 6461da177e4SLinus Torvalds goto out; 6474b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds out: 6501da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6511da177e4SLinus Torvalds return i; 6521da177e4SLinus Torvalds } 653e8628dd0SDarrick J. Wong 654f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 6551da177e4SLinus Torvalds { 6561da177e4SLinus Torvalds ssize_t i = 0; 6571da177e4SLinus Torvalds unsigned int cpu; 6581da177e4SLinus Torvalds 659835481d9SRusty Russell for_each_cpu(cpu, mask) { 6601da177e4SLinus Torvalds if (i) 6611da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 6621da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 6631da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 6641da177e4SLinus Torvalds break; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6671da177e4SLinus Torvalds return i; 6681da177e4SLinus Torvalds } 669f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 6701da177e4SLinus Torvalds 671e8628dd0SDarrick J. Wong /** 672e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 673e8628dd0SDarrick J. Wong * hw coordination is in use 674e8628dd0SDarrick J. Wong */ 675e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 676e8628dd0SDarrick J. Wong { 677f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 678e8628dd0SDarrick J. Wong } 679e8628dd0SDarrick J. Wong 680e8628dd0SDarrick J. Wong /** 681e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 682e8628dd0SDarrick J. Wong */ 683e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 684e8628dd0SDarrick J. Wong { 685f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 686e8628dd0SDarrick J. Wong } 687e8628dd0SDarrick J. Wong 6889e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 6899e76988eSVenki Pallipadi const char *buf, size_t count) 6909e76988eSVenki Pallipadi { 6919e76988eSVenki Pallipadi unsigned int freq = 0; 6929e76988eSVenki Pallipadi unsigned int ret; 6939e76988eSVenki Pallipadi 694879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 6959e76988eSVenki Pallipadi return -EINVAL; 6969e76988eSVenki Pallipadi 6979e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 6989e76988eSVenki Pallipadi if (ret != 1) 6999e76988eSVenki Pallipadi return -EINVAL; 7009e76988eSVenki Pallipadi 7019e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 7029e76988eSVenki Pallipadi 7039e76988eSVenki Pallipadi return count; 7049e76988eSVenki Pallipadi } 7059e76988eSVenki Pallipadi 7069e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 7079e76988eSVenki Pallipadi { 708879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 7099e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 7109e76988eSVenki Pallipadi 7119e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 7129e76988eSVenki Pallipadi } 7131da177e4SLinus Torvalds 714e2f74f35SThomas Renninger /** 7158bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 716e2f74f35SThomas Renninger */ 717e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 718e2f74f35SThomas Renninger { 719e2f74f35SThomas Renninger unsigned int limit; 720e2f74f35SThomas Renninger int ret; 7211c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 7221c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 723e2f74f35SThomas Renninger if (!ret) 724e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 725e2f74f35SThomas Renninger } 726e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 727e2f74f35SThomas Renninger } 728e2f74f35SThomas Renninger 7296dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 7306dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 7316dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 7326dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 7336dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 7346dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 7356dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 7366dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 7376dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 7386dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 7396dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 7406dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 7416dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 7426dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 7451da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 7461da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 747ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 7481da177e4SLinus Torvalds &scaling_min_freq.attr, 7491da177e4SLinus Torvalds &scaling_max_freq.attr, 7501da177e4SLinus Torvalds &affected_cpus.attr, 751e8628dd0SDarrick J. Wong &related_cpus.attr, 7521da177e4SLinus Torvalds &scaling_governor.attr, 7531da177e4SLinus Torvalds &scaling_driver.attr, 7541da177e4SLinus Torvalds &scaling_available_governors.attr, 7559e76988eSVenki Pallipadi &scaling_setspeed.attr, 7561da177e4SLinus Torvalds NULL 7571da177e4SLinus Torvalds }; 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 7601da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 7631da177e4SLinus Torvalds { 7641da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7651da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 7661b750e3bSViresh Kumar ssize_t ret; 7676eed9404SViresh Kumar 7686eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 7691b750e3bSViresh Kumar return -EINVAL; 7705a01f2e8SVenkatesh Pallipadi 771ad7722daSviresh kumar down_read(&policy->rwsem); 7725a01f2e8SVenkatesh Pallipadi 773e08f5f5bSGautham R Shenoy if (fattr->show) 774e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 775e08f5f5bSGautham R Shenoy else 776e08f5f5bSGautham R Shenoy ret = -EIO; 777e08f5f5bSGautham R Shenoy 778ad7722daSviresh kumar up_read(&policy->rwsem); 7796eed9404SViresh Kumar up_read(&cpufreq_rwsem); 7801b750e3bSViresh Kumar 7811da177e4SLinus Torvalds return ret; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 7851da177e4SLinus Torvalds const char *buf, size_t count) 7861da177e4SLinus Torvalds { 7871da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7881da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 789a07530b4SDave Jones ssize_t ret = -EINVAL; 7906eed9404SViresh Kumar 7914f750c93SSrivatsa S. Bhat get_online_cpus(); 7924f750c93SSrivatsa S. Bhat 7934f750c93SSrivatsa S. Bhat if (!cpu_online(policy->cpu)) 7944f750c93SSrivatsa S. Bhat goto unlock; 7954f750c93SSrivatsa S. Bhat 7966eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 7974f750c93SSrivatsa S. Bhat goto unlock; 7985a01f2e8SVenkatesh Pallipadi 799ad7722daSviresh kumar down_write(&policy->rwsem); 8005a01f2e8SVenkatesh Pallipadi 801e08f5f5bSGautham R Shenoy if (fattr->store) 802e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 803e08f5f5bSGautham R Shenoy else 804e08f5f5bSGautham R Shenoy ret = -EIO; 805e08f5f5bSGautham R Shenoy 806ad7722daSviresh kumar up_write(&policy->rwsem); 8076eed9404SViresh Kumar 8086eed9404SViresh Kumar up_read(&cpufreq_rwsem); 8094f750c93SSrivatsa S. Bhat unlock: 8104f750c93SSrivatsa S. Bhat put_online_cpus(); 8114f750c93SSrivatsa S. Bhat 8121da177e4SLinus Torvalds return ret; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 8161da177e4SLinus Torvalds { 8171da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8182d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 8191da177e4SLinus Torvalds complete(&policy->kobj_unregister); 8201da177e4SLinus Torvalds } 8211da177e4SLinus Torvalds 82252cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 8231da177e4SLinus Torvalds .show = show, 8241da177e4SLinus Torvalds .store = store, 8251da177e4SLinus Torvalds }; 8261da177e4SLinus Torvalds 8271da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 8281da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 8291da177e4SLinus Torvalds .default_attrs = default_attrs, 8301da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 8311da177e4SLinus Torvalds }; 8321da177e4SLinus Torvalds 8332361be23SViresh Kumar struct kobject *cpufreq_global_kobject; 8342361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 8352361be23SViresh Kumar 8362361be23SViresh Kumar static int cpufreq_global_kobject_usage; 8372361be23SViresh Kumar 8382361be23SViresh Kumar int cpufreq_get_global_kobject(void) 8392361be23SViresh Kumar { 8402361be23SViresh Kumar if (!cpufreq_global_kobject_usage++) 8412361be23SViresh Kumar return kobject_add(cpufreq_global_kobject, 8422361be23SViresh Kumar &cpu_subsys.dev_root->kobj, "%s", "cpufreq"); 8432361be23SViresh Kumar 8442361be23SViresh Kumar return 0; 8452361be23SViresh Kumar } 8462361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject); 8472361be23SViresh Kumar 8482361be23SViresh Kumar void cpufreq_put_global_kobject(void) 8492361be23SViresh Kumar { 8502361be23SViresh Kumar if (!--cpufreq_global_kobject_usage) 8512361be23SViresh Kumar kobject_del(cpufreq_global_kobject); 8522361be23SViresh Kumar } 8532361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject); 8542361be23SViresh Kumar 8552361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr) 8562361be23SViresh Kumar { 8572361be23SViresh Kumar int ret = cpufreq_get_global_kobject(); 8582361be23SViresh Kumar 8592361be23SViresh Kumar if (!ret) { 8602361be23SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, attr); 8612361be23SViresh Kumar if (ret) 8622361be23SViresh Kumar cpufreq_put_global_kobject(); 8632361be23SViresh Kumar } 8642361be23SViresh Kumar 8652361be23SViresh Kumar return ret; 8662361be23SViresh Kumar } 8672361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file); 8682361be23SViresh Kumar 8692361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr) 8702361be23SViresh Kumar { 8712361be23SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, attr); 8722361be23SViresh Kumar cpufreq_put_global_kobject(); 8732361be23SViresh Kumar } 8742361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file); 8752361be23SViresh Kumar 87619d6f7ecSDave Jones /* symlink affected CPUs */ 877308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 87819d6f7ecSDave Jones { 87919d6f7ecSDave Jones unsigned int j; 88019d6f7ecSDave Jones int ret = 0; 88119d6f7ecSDave Jones 88219d6f7ecSDave Jones for_each_cpu(j, policy->cpus) { 8838a25a2fdSKay Sievers struct device *cpu_dev; 88419d6f7ecSDave Jones 885308b60e7SViresh Kumar if (j == policy->cpu) 88619d6f7ecSDave Jones continue; 88719d6f7ecSDave Jones 888e8fdde10SViresh Kumar pr_debug("Adding link for CPU: %u\n", j); 8898a25a2fdSKay Sievers cpu_dev = get_cpu_device(j); 8908a25a2fdSKay Sievers ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 89119d6f7ecSDave Jones "cpufreq"); 89271c3461eSRafael J. Wysocki if (ret) 89371c3461eSRafael J. Wysocki break; 89419d6f7ecSDave Jones } 89519d6f7ecSDave Jones return ret; 89619d6f7ecSDave Jones } 89719d6f7ecSDave Jones 898308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy, 8998a25a2fdSKay Sievers struct device *dev) 900909a694eSDave Jones { 901909a694eSDave Jones struct freq_attr **drv_attr; 902909a694eSDave Jones int ret = 0; 903909a694eSDave Jones 904909a694eSDave Jones /* set up files for this cpu device */ 9051c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 906f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 907909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 908909a694eSDave Jones if (ret) 9096d4e81edSTomeu Vizoso return ret; 910909a694eSDave Jones drv_attr++; 911909a694eSDave Jones } 9121c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 913909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 914909a694eSDave Jones if (ret) 9156d4e81edSTomeu Vizoso return ret; 916909a694eSDave Jones } 917c034b02eSDirk Brandewie 918909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 919909a694eSDave Jones if (ret) 9206d4e81edSTomeu Vizoso return ret; 921c034b02eSDirk Brandewie 9221c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 923e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 924e2f74f35SThomas Renninger if (ret) 9256d4e81edSTomeu Vizoso return ret; 926e2f74f35SThomas Renninger } 927909a694eSDave Jones 9286d4e81edSTomeu Vizoso return cpufreq_add_dev_symlink(policy); 929e18f1682SSrivatsa S. Bhat } 930e18f1682SSrivatsa S. Bhat 931e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy) 932e18f1682SSrivatsa S. Bhat { 9336e2c89d1Sviresh kumar struct cpufreq_governor *gov = NULL; 934e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 935e18f1682SSrivatsa S. Bhat int ret = 0; 936e18f1682SSrivatsa S. Bhat 937d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 938a27a9ab7SJason Baron 9396e2c89d1Sviresh kumar /* Update governor of new_policy to the governor used before hotplug */ 94042f91fa1SViresh Kumar gov = find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu)); 9416e2c89d1Sviresh kumar if (gov) 9426e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 9436e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 9446e2c89d1Sviresh kumar else 9456e2c89d1Sviresh kumar gov = CPUFREQ_DEFAULT_GOVERNOR; 9466e2c89d1Sviresh kumar 9476e2c89d1Sviresh kumar new_policy.governor = gov; 9486e2c89d1Sviresh kumar 949a27a9ab7SJason Baron /* Use the default policy if its valid. */ 950a27a9ab7SJason Baron if (cpufreq_driver->setpolicy) 9516e2c89d1Sviresh kumar cpufreq_parse_governor(gov->name, &new_policy.policy, NULL); 952ecf7e461SDave Jones 953ecf7e461SDave Jones /* set default policy */ 954037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 955ecf7e461SDave Jones if (ret) { 9562d06d8c4SDominik Brodowski pr_debug("setting policy failed\n"); 9571c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 9581c3d85ddSRafael J. Wysocki cpufreq_driver->exit(policy); 959ecf7e461SDave Jones } 960909a694eSDave Jones } 961909a694eSDave Jones 962d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, 96342f921a6SViresh Kumar unsigned int cpu, struct device *dev) 964fcf80582SViresh Kumar { 9659c0ebcf7SViresh Kumar int ret = 0; 966fcf80582SViresh Kumar unsigned long flags; 967fcf80582SViresh Kumar 9689c0ebcf7SViresh Kumar if (has_target()) { 9693de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 9703de9bdebSViresh Kumar if (ret) { 9713de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 9723de9bdebSViresh Kumar return ret; 9733de9bdebSViresh Kumar } 9743de9bdebSViresh Kumar } 975fcf80582SViresh Kumar 976ad7722daSviresh kumar down_write(&policy->rwsem); 9772eaa3e2dSViresh Kumar 9780d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 9792eaa3e2dSViresh Kumar 980fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 981fcf80582SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = policy; 9820d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 983fcf80582SViresh Kumar 984ad7722daSviresh kumar up_write(&policy->rwsem); 9852eaa3e2dSViresh Kumar 9869c0ebcf7SViresh Kumar if (has_target()) { 987e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 988e5c87b76SStratos Karafotis if (!ret) 989e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 990e5c87b76SStratos Karafotis 991e5c87b76SStratos Karafotis if (ret) { 9923de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 9933de9bdebSViresh Kumar return ret; 9943de9bdebSViresh Kumar } 995820c6ca2SViresh Kumar } 996fcf80582SViresh Kumar 99742f921a6SViresh Kumar return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"); 998fcf80582SViresh Kumar } 9991da177e4SLinus Torvalds 10008414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu) 10018414809cSSrivatsa S. Bhat { 10028414809cSSrivatsa S. Bhat struct cpufreq_policy *policy; 10038414809cSSrivatsa S. Bhat unsigned long flags; 10048414809cSSrivatsa S. Bhat 100544871c9cSLan Tianyu read_lock_irqsave(&cpufreq_driver_lock, flags); 10068414809cSSrivatsa S. Bhat 10078414809cSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data_fallback, cpu); 10088414809cSSrivatsa S. Bhat 100944871c9cSLan Tianyu read_unlock_irqrestore(&cpufreq_driver_lock, flags); 10108414809cSSrivatsa S. Bhat 101109712f55SGeert Uytterhoeven if (policy) 10126e2c89d1Sviresh kumar policy->governor = NULL; 10136e2c89d1Sviresh kumar 10148414809cSSrivatsa S. Bhat return policy; 10158414809cSSrivatsa S. Bhat } 10168414809cSSrivatsa S. Bhat 1017e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void) 1018e9698cc5SSrivatsa S. Bhat { 1019e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1020e9698cc5SSrivatsa S. Bhat 1021e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1022e9698cc5SSrivatsa S. Bhat if (!policy) 1023e9698cc5SSrivatsa S. Bhat return NULL; 1024e9698cc5SSrivatsa S. Bhat 1025e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1026e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1027e9698cc5SSrivatsa S. Bhat 1028e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1029e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1030e9698cc5SSrivatsa S. Bhat 1031c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1032ad7722daSviresh kumar init_rwsem(&policy->rwsem); 103312478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 103412478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1035818c5712SViresh Kumar init_completion(&policy->kobj_unregister); 1036818c5712SViresh Kumar INIT_WORK(&policy->update, handle_update); 1037ad7722daSviresh kumar 1038e9698cc5SSrivatsa S. Bhat return policy; 1039e9698cc5SSrivatsa S. Bhat 1040e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1041e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1042e9698cc5SSrivatsa S. Bhat err_free_policy: 1043e9698cc5SSrivatsa S. Bhat kfree(policy); 1044e9698cc5SSrivatsa S. Bhat 1045e9698cc5SSrivatsa S. Bhat return NULL; 1046e9698cc5SSrivatsa S. Bhat } 1047e9698cc5SSrivatsa S. Bhat 104842f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 104942f921a6SViresh Kumar { 105042f921a6SViresh Kumar struct kobject *kobj; 105142f921a6SViresh Kumar struct completion *cmp; 105242f921a6SViresh Kumar 1053fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1054fcd7af91SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 1055fcd7af91SViresh Kumar 105642f921a6SViresh Kumar down_read(&policy->rwsem); 105742f921a6SViresh Kumar kobj = &policy->kobj; 105842f921a6SViresh Kumar cmp = &policy->kobj_unregister; 105942f921a6SViresh Kumar up_read(&policy->rwsem); 106042f921a6SViresh Kumar kobject_put(kobj); 106142f921a6SViresh Kumar 106242f921a6SViresh Kumar /* 106342f921a6SViresh Kumar * We need to make sure that the underlying kobj is 106442f921a6SViresh Kumar * actually not referenced anymore by anybody before we 106542f921a6SViresh Kumar * proceed with unloading. 106642f921a6SViresh Kumar */ 106742f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 106842f921a6SViresh Kumar wait_for_completion(cmp); 106942f921a6SViresh Kumar pr_debug("wait complete\n"); 107042f921a6SViresh Kumar } 107142f921a6SViresh Kumar 1072e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy) 1073e9698cc5SSrivatsa S. Bhat { 1074e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1075e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1076e9698cc5SSrivatsa S. Bhat kfree(policy); 1077e9698cc5SSrivatsa S. Bhat } 1078e9698cc5SSrivatsa S. Bhat 10791bfb425bSViresh Kumar static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu, 10801bfb425bSViresh Kumar struct device *cpu_dev) 10810d66b91eSSrivatsa S. Bhat { 10821bfb425bSViresh Kumar int ret; 10831bfb425bSViresh Kumar 108499ec899eSSrivatsa S. Bhat if (WARN_ON(cpu == policy->cpu)) 10851bfb425bSViresh Kumar return 0; 10861bfb425bSViresh Kumar 10871bfb425bSViresh Kumar /* Move kobject to the new policy->cpu */ 10881bfb425bSViresh Kumar ret = kobject_move(&policy->kobj, &cpu_dev->kobj); 10891bfb425bSViresh Kumar if (ret) { 10901bfb425bSViresh Kumar pr_err("%s: Failed to move kobj: %d\n", __func__, ret); 10911bfb425bSViresh Kumar return ret; 10921bfb425bSViresh Kumar } 1093cb38ed5cSSrivatsa S. Bhat 1094ad7722daSviresh kumar down_write(&policy->rwsem); 10950d66b91eSSrivatsa S. Bhat policy->cpu = cpu; 1096ad7722daSviresh kumar up_write(&policy->rwsem); 10978efd5765SViresh Kumar 10981bfb425bSViresh Kumar return 0; 10990d66b91eSSrivatsa S. Bhat } 11000d66b91eSSrivatsa S. Bhat 110196bbbe4aSViresh Kumar static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 11021da177e4SLinus Torvalds { 1103fcf80582SViresh Kumar unsigned int j, cpu = dev->id; 110465922465SViresh Kumar int ret = -ENOMEM; 11057f0c020aSViresh Kumar struct cpufreq_policy *policy; 11061da177e4SLinus Torvalds unsigned long flags; 110796bbbe4aSViresh Kumar bool recover_policy = cpufreq_suspended; 11081da177e4SLinus Torvalds 1109c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 1110c32b6b8eSAshok Raj return 0; 1111c32b6b8eSAshok Raj 11122d06d8c4SDominik Brodowski pr_debug("adding CPU %u\n", cpu); 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* check whether a different CPU already registered this 11151da177e4SLinus Torvalds * CPU because it is in the same boat. */ 1116d7a9771cSViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 1117d7a9771cSViresh Kumar if (unlikely(policy)) 11181da177e4SLinus Torvalds return 0; 1119fcf80582SViresh Kumar 11206eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 11216eed9404SViresh Kumar return 0; 11226eed9404SViresh Kumar 1123fcf80582SViresh Kumar /* Check if this cpu was hot-unplugged earlier and has siblings */ 11240d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 1125b4f0676fSViresh Kumar for_each_policy(policy) { 11267f0c020aSViresh Kumar if (cpumask_test_cpu(cpu, policy->related_cpus)) { 11270d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 11287f0c020aSViresh Kumar ret = cpufreq_add_policy_cpu(policy, cpu, dev); 11296eed9404SViresh Kumar up_read(&cpufreq_rwsem); 11306eed9404SViresh Kumar return ret; 1131fcf80582SViresh Kumar } 11322eaa3e2dSViresh Kumar } 11330d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 11341da177e4SLinus Torvalds 113572368d12SRafael J. Wysocki /* 113672368d12SRafael J. Wysocki * Restore the saved policy when doing light-weight init and fall back 113772368d12SRafael J. Wysocki * to the full init if that fails. 113872368d12SRafael J. Wysocki */ 113996bbbe4aSViresh Kumar policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL; 114072368d12SRafael J. Wysocki if (!policy) { 114196bbbe4aSViresh Kumar recover_policy = false; 1142e9698cc5SSrivatsa S. Bhat policy = cpufreq_policy_alloc(); 1143059019a3SDave Jones if (!policy) 11441da177e4SLinus Torvalds goto nomem_out; 114572368d12SRafael J. Wysocki } 11460d66b91eSSrivatsa S. Bhat 11470d66b91eSSrivatsa S. Bhat /* 11480d66b91eSSrivatsa S. Bhat * In the resume path, since we restore a saved policy, the assignment 11490d66b91eSSrivatsa S. Bhat * to policy->cpu is like an update of the existing policy, rather than 11500d66b91eSSrivatsa S. Bhat * the creation of a brand new one. So we need to perform this update 11510d66b91eSSrivatsa S. Bhat * by invoking update_policy_cpu(). 11520d66b91eSSrivatsa S. Bhat */ 11531bfb425bSViresh Kumar if (recover_policy && cpu != policy->cpu) 11541bfb425bSViresh Kumar WARN_ON(update_policy_cpu(policy, cpu, dev)); 11551bfb425bSViresh Kumar else 11561da177e4SLinus Torvalds policy->cpu = cpu; 11570d66b91eSSrivatsa S. Bhat 1158835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 11611da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 11621da177e4SLinus Torvalds */ 11631c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 11641da177e4SLinus Torvalds if (ret) { 11652d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 11662eaa3e2dSViresh Kumar goto err_set_policy_cpu; 11671da177e4SLinus Torvalds } 1168643ae6e8SViresh Kumar 11696d4e81edSTomeu Vizoso down_write(&policy->rwsem); 11706d4e81edSTomeu Vizoso 11715a7e56a5SViresh Kumar /* related cpus should atleast have policy->cpus */ 11725a7e56a5SViresh Kumar cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); 11735a7e56a5SViresh Kumar 11745a7e56a5SViresh Kumar /* 11755a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 11765a7e56a5SViresh Kumar * managing offline cpus here. 11775a7e56a5SViresh Kumar */ 11785a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 11795a7e56a5SViresh Kumar 118096bbbe4aSViresh Kumar if (!recover_policy) { 11815a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 11825a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 11836d4e81edSTomeu Vizoso 11846d4e81edSTomeu Vizoso /* prepare interface data */ 11856d4e81edSTomeu Vizoso ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 11866d4e81edSTomeu Vizoso &dev->kobj, "cpufreq"); 11876d4e81edSTomeu Vizoso if (ret) { 11886d4e81edSTomeu Vizoso pr_err("%s: failed to init policy->kobj: %d\n", 11896d4e81edSTomeu Vizoso __func__, ret); 11906d4e81edSTomeu Vizoso goto err_init_policy_kobj; 11916d4e81edSTomeu Vizoso } 11925a7e56a5SViresh Kumar } 11935a7e56a5SViresh Kumar 1194652ed95dSViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1195652ed95dSViresh Kumar for_each_cpu(j, policy->cpus) 1196652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 1197652ed95dSViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1198652ed95dSViresh Kumar 11992ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 1200da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1201da60ce9fSViresh Kumar if (!policy->cur) { 1202da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1203da60ce9fSViresh Kumar goto err_get_freq; 1204da60ce9fSViresh Kumar } 1205da60ce9fSViresh Kumar } 1206da60ce9fSViresh Kumar 1207d3916691SViresh Kumar /* 1208d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1209d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1210d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1211d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1212d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1213d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1214d3916691SViresh Kumar * isn't found in freq-table. 1215d3916691SViresh Kumar * 1216d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1217d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1218d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1219d3916691SViresh Kumar * is initialized to zero). 1220d3916691SViresh Kumar * 1221d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1222d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1223d3916691SViresh Kumar * equal to target-freq. 1224d3916691SViresh Kumar */ 1225d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1226d3916691SViresh Kumar && has_target()) { 1227d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1228d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1229d3916691SViresh Kumar if (ret == -EINVAL) { 1230d3916691SViresh Kumar /* Warn user and fix it */ 1231d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1232d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1233d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1234d3916691SViresh Kumar CPUFREQ_RELATION_L); 1235d3916691SViresh Kumar 1236d3916691SViresh Kumar /* 1237d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1238d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1239d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1240d3916691SViresh Kumar */ 1241d3916691SViresh Kumar BUG_ON(ret); 1242d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1243d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1244d3916691SViresh Kumar } 1245d3916691SViresh Kumar } 1246d3916691SViresh Kumar 1247a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1248a1531acdSThomas Renninger CPUFREQ_START, policy); 1249a1531acdSThomas Renninger 125096bbbe4aSViresh Kumar if (!recover_policy) { 1251308b60e7SViresh Kumar ret = cpufreq_add_dev_interface(policy, dev); 125219d6f7ecSDave Jones if (ret) 12530142f9dcSAhmed S. Darwish goto err_out_unregister; 1254fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1255fcd7af91SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 12569515f4d6SViresh Kumar } 1257c88a1f8bSLukasz Majewski 1258c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1259c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1260c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12618ff69732SDave Jones 1262e18f1682SSrivatsa S. Bhat cpufreq_init_policy(policy); 1263e18f1682SSrivatsa S. Bhat 126496bbbe4aSViresh Kumar if (!recover_policy) { 126508fd8c1cSViresh Kumar policy->user_policy.policy = policy->policy; 126608fd8c1cSViresh Kumar policy->user_policy.governor = policy->governor; 126708fd8c1cSViresh Kumar } 12684e97b631SViresh Kumar up_write(&policy->rwsem); 126908fd8c1cSViresh Kumar 1270038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 12717c45cf31SViresh Kumar 12726eed9404SViresh Kumar up_read(&cpufreq_rwsem); 12736eed9404SViresh Kumar 12747c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 12757c45cf31SViresh Kumar if (cpufreq_driver->ready) 12767c45cf31SViresh Kumar cpufreq_driver->ready(policy); 12777c45cf31SViresh Kumar 12782d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds return 0; 12811da177e4SLinus Torvalds 12821da177e4SLinus Torvalds err_out_unregister: 1283652ed95dSViresh Kumar err_get_freq: 12840d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 1285474deff7SViresh Kumar for_each_cpu(j, policy->cpus) 12867a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 12870d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12881da177e4SLinus Torvalds 12896d4e81edSTomeu Vizoso if (!recover_policy) { 12906d4e81edSTomeu Vizoso kobject_put(&policy->kobj); 12916d4e81edSTomeu Vizoso wait_for_completion(&policy->kobj_unregister); 12926d4e81edSTomeu Vizoso } 12936d4e81edSTomeu Vizoso err_init_policy_kobj: 12947106e02bSPrarit Bhargava up_write(&policy->rwsem); 12957106e02bSPrarit Bhargava 1296da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1297da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 12982eaa3e2dSViresh Kumar err_set_policy_cpu: 129996bbbe4aSViresh Kumar if (recover_policy) { 130072368d12SRafael J. Wysocki /* Do not leave stale fallback data behind. */ 130172368d12SRafael J. Wysocki per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL; 130242f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 130372368d12SRafael J. Wysocki } 1304e9698cc5SSrivatsa S. Bhat cpufreq_policy_free(policy); 130542f921a6SViresh Kumar 13061da177e4SLinus Torvalds nomem_out: 13076eed9404SViresh Kumar up_read(&cpufreq_rwsem); 13086eed9404SViresh Kumar 13091da177e4SLinus Torvalds return ret; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 1312a82fab29SSrivatsa S. Bhat /** 1313a82fab29SSrivatsa S. Bhat * cpufreq_add_dev - add a CPU device 1314a82fab29SSrivatsa S. Bhat * 1315a82fab29SSrivatsa S. Bhat * Adds the cpufreq interface for a CPU device. 1316a82fab29SSrivatsa S. Bhat * 1317a82fab29SSrivatsa S. Bhat * The Oracle says: try running cpufreq registration/unregistration concurrently 1318a82fab29SSrivatsa S. Bhat * with with cpu hotplugging and all hell will break loose. Tried to clean this 1319a82fab29SSrivatsa S. Bhat * mess up, but more thorough testing is needed. - Mathieu 1320a82fab29SSrivatsa S. Bhat */ 1321a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 1322a82fab29SSrivatsa S. Bhat { 132396bbbe4aSViresh Kumar return __cpufreq_add_dev(dev, sif); 1324a82fab29SSrivatsa S. Bhat } 1325a82fab29SSrivatsa S. Bhat 1326cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev, 132796bbbe4aSViresh Kumar struct subsys_interface *sif) 13281da177e4SLinus Torvalds { 1329f9ba680dSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 13301bfb425bSViresh Kumar int ret; 13311da177e4SLinus Torvalds unsigned long flags; 13323a3e9e06SViresh Kumar struct cpufreq_policy *policy; 13331da177e4SLinus Torvalds 1334b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 13351da177e4SLinus Torvalds 13360d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 13371da177e4SLinus Torvalds 13383a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 13391da177e4SLinus Torvalds 13408414809cSSrivatsa S. Bhat /* Save the policy somewhere when doing a light-weight tear-down */ 134196bbbe4aSViresh Kumar if (cpufreq_suspended) 13423a3e9e06SViresh Kumar per_cpu(cpufreq_cpu_data_fallback, cpu) = policy; 13438414809cSSrivatsa S. Bhat 13440d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 13451da177e4SLinus Torvalds 13463a3e9e06SViresh Kumar if (!policy) { 1347b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 13481da177e4SLinus Torvalds return -EINVAL; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 13519c0ebcf7SViresh Kumar if (has_target()) { 13523de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 13533de9bdebSViresh Kumar if (ret) { 13543de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 13553de9bdebSViresh Kumar return ret; 13563de9bdebSViresh Kumar } 13575a01f2e8SVenkatesh Pallipadi 1358fa69e33fSDirk Brandewie strncpy(per_cpu(cpufreq_cpu_governor, cpu), 13593a3e9e06SViresh Kumar policy->governor->name, CPUFREQ_NAME_LEN); 1360db5f2995SViresh Kumar } 13611da177e4SLinus Torvalds 1362ad7722daSviresh kumar down_read(&policy->rwsem); 13633a3e9e06SViresh Kumar cpus = cpumask_weight(policy->cpus); 1364ad7722daSviresh kumar up_read(&policy->rwsem); 13651da177e4SLinus Torvalds 136661173f25SSrivatsa S. Bhat if (cpu != policy->cpu) { 136773bf0fc2SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 136873bf0fc2SViresh Kumar } else if (cpus > 1) { 13691bfb425bSViresh Kumar /* Nominate new CPU */ 13701bfb425bSViresh Kumar int new_cpu = cpumask_any_but(policy->cpus, cpu); 13711bfb425bSViresh Kumar struct device *cpu_dev = get_cpu_device(new_cpu); 13721bfb425bSViresh Kumar 13731bfb425bSViresh Kumar sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 13741bfb425bSViresh Kumar ret = update_policy_cpu(policy, new_cpu, cpu_dev); 13751bfb425bSViresh Kumar if (ret) { 13761bfb425bSViresh Kumar if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 13771bfb425bSViresh Kumar "cpufreq")) 13781bfb425bSViresh Kumar pr_err("%s: Failed to restore kobj link to cpu:%d\n", 13791bfb425bSViresh Kumar __func__, cpu_dev->id); 13801bfb425bSViresh Kumar return ret; 13811bfb425bSViresh Kumar } 1382a82fab29SSrivatsa S. Bhat 1383bda9f552SStratos Karafotis if (!cpufreq_suspended) 138475949c9aSViresh Kumar pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n", 138575949c9aSViresh Kumar __func__, new_cpu, cpu); 1386789ca243SPreeti U Murthy } else if (cpufreq_driver->stop_cpu) { 1387367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 13881da177e4SLinus Torvalds } 1389b8eed8afSViresh Kumar 1390cedb70afSSrivatsa S. Bhat return 0; 1391cedb70afSSrivatsa S. Bhat } 1392cedb70afSSrivatsa S. Bhat 1393cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev, 139496bbbe4aSViresh Kumar struct subsys_interface *sif) 1395cedb70afSSrivatsa S. Bhat { 1396cedb70afSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 1397cedb70afSSrivatsa S. Bhat int ret; 1398cedb70afSSrivatsa S. Bhat unsigned long flags; 1399cedb70afSSrivatsa S. Bhat struct cpufreq_policy *policy; 1400cedb70afSSrivatsa S. Bhat 14016ffae8c0SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1402cedb70afSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data, cpu); 14036ffae8c0SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 14046ffae8c0SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1405cedb70afSSrivatsa S. Bhat 1406cedb70afSSrivatsa S. Bhat if (!policy) { 1407cedb70afSSrivatsa S. Bhat pr_debug("%s: No cpu_data found\n", __func__); 1408cedb70afSSrivatsa S. Bhat return -EINVAL; 1409cedb70afSSrivatsa S. Bhat } 1410cedb70afSSrivatsa S. Bhat 1411ad7722daSviresh kumar down_write(&policy->rwsem); 1412cedb70afSSrivatsa S. Bhat cpus = cpumask_weight(policy->cpus); 14139c8f1ee4SViresh Kumar 14149c8f1ee4SViresh Kumar if (cpus > 1) 14159c8f1ee4SViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 1416ad7722daSviresh kumar up_write(&policy->rwsem); 1417cedb70afSSrivatsa S. Bhat 1418b8eed8afSViresh Kumar /* If cpu is last user of policy, free policy */ 1419b8eed8afSViresh Kumar if (cpus == 1) { 14209c0ebcf7SViresh Kumar if (has_target()) { 14213de9bdebSViresh Kumar ret = __cpufreq_governor(policy, 14223de9bdebSViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 14233de9bdebSViresh Kumar if (ret) { 14243de9bdebSViresh Kumar pr_err("%s: Failed to exit governor\n", 14253de9bdebSViresh Kumar __func__); 14263de9bdebSViresh Kumar return ret; 14273de9bdebSViresh Kumar } 14283de9bdebSViresh Kumar } 14292a998599SRafael J. Wysocki 143096bbbe4aSViresh Kumar if (!cpufreq_suspended) 143142f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 14321da177e4SLinus Torvalds 14338414809cSSrivatsa S. Bhat /* 14348414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 14358414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 14368414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 14378414809cSSrivatsa S. Bhat */ 14381c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 14393a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 144027ecddc2SJacob Shin 14419515f4d6SViresh Kumar /* Remove policy from list of active policies */ 14429515f4d6SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 14439515f4d6SViresh Kumar list_del(&policy->policy_list); 14449515f4d6SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 14459515f4d6SViresh Kumar 144696bbbe4aSViresh Kumar if (!cpufreq_suspended) 14473a3e9e06SViresh Kumar cpufreq_policy_free(policy); 1448e5c87b76SStratos Karafotis } else if (has_target()) { 1449e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 1450e5c87b76SStratos Karafotis if (!ret) 1451e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1452e5c87b76SStratos Karafotis 1453e5c87b76SStratos Karafotis if (ret) { 1454e5c87b76SStratos Karafotis pr_err("%s: Failed to start governor\n", __func__); 14553de9bdebSViresh Kumar return ret; 14563de9bdebSViresh Kumar } 1457b8eed8afSViresh Kumar } 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds return 0; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 1462cedb70afSSrivatsa S. Bhat /** 146327a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1464cedb70afSSrivatsa S. Bhat * 1465cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1466cedb70afSSrivatsa S. Bhat */ 14678a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 14685a01f2e8SVenkatesh Pallipadi { 14698a25a2fdSKay Sievers unsigned int cpu = dev->id; 147027a862e9SViresh Kumar int ret; 1471ec28297aSVenki Pallipadi 1472ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1473ec28297aSVenki Pallipadi return 0; 1474ec28297aSVenki Pallipadi 147596bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_prepare(dev, sif); 147627a862e9SViresh Kumar 147727a862e9SViresh Kumar if (!ret) 147896bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_finish(dev, sif); 147927a862e9SViresh Kumar 148027a862e9SViresh Kumar return ret; 14815a01f2e8SVenkatesh Pallipadi } 14825a01f2e8SVenkatesh Pallipadi 148365f27f38SDavid Howells static void handle_update(struct work_struct *work) 14841da177e4SLinus Torvalds { 148565f27f38SDavid Howells struct cpufreq_policy *policy = 148665f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 148765f27f38SDavid Howells unsigned int cpu = policy->cpu; 14882d06d8c4SDominik Brodowski pr_debug("handle_update for cpu %u called\n", cpu); 14891da177e4SLinus Torvalds cpufreq_update_policy(cpu); 14901da177e4SLinus Torvalds } 14911da177e4SLinus Torvalds 14921da177e4SLinus Torvalds /** 1493bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1494bb176f7dSViresh Kumar * in deep trouble. 1495a1e1dc41SViresh Kumar * @policy: policy managing CPUs 14961da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 14971da177e4SLinus Torvalds * 149829464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 149929464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 15001da177e4SLinus Torvalds */ 1501a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1502e08f5f5bSGautham R Shenoy unsigned int new_freq) 15031da177e4SLinus Torvalds { 15041da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1505b43a7ffbSViresh Kumar 1506e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1507a1e1dc41SViresh Kumar policy->cur, new_freq); 15081da177e4SLinus Torvalds 1509a1e1dc41SViresh Kumar freqs.old = policy->cur; 15101da177e4SLinus Torvalds freqs.new = new_freq; 1511b43a7ffbSViresh Kumar 15128fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 15138fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 15141da177e4SLinus Torvalds } 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds /** 15174ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 151895235ca2SVenkatesh Pallipadi * @cpu: CPU number 151995235ca2SVenkatesh Pallipadi * 152095235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 152195235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 152295235ca2SVenkatesh Pallipadi */ 152395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 152495235ca2SVenkatesh Pallipadi { 15259e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1526e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 152795235ca2SVenkatesh Pallipadi 15281c3d85ddSRafael J. Wysocki if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 15291c3d85ddSRafael J. Wysocki return cpufreq_driver->get(cpu); 15309e21ba8bSDirk Brandewie 15319e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 153295235ca2SVenkatesh Pallipadi if (policy) { 1533e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 153495235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 153595235ca2SVenkatesh Pallipadi } 153695235ca2SVenkatesh Pallipadi 15374d34a67dSDave Jones return ret_freq; 153895235ca2SVenkatesh Pallipadi } 153995235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 154095235ca2SVenkatesh Pallipadi 15413d737108SJesse Barnes /** 15423d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 15433d737108SJesse Barnes * @cpu: CPU number 15443d737108SJesse Barnes * 15453d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 15463d737108SJesse Barnes */ 15473d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 15483d737108SJesse Barnes { 15493d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15503d737108SJesse Barnes unsigned int ret_freq = 0; 15513d737108SJesse Barnes 15523d737108SJesse Barnes if (policy) { 15533d737108SJesse Barnes ret_freq = policy->max; 15543d737108SJesse Barnes cpufreq_cpu_put(policy); 15553d737108SJesse Barnes } 15563d737108SJesse Barnes 15573d737108SJesse Barnes return ret_freq; 15583d737108SJesse Barnes } 15593d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 15603d737108SJesse Barnes 1561d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 15621da177e4SLinus Torvalds { 1563e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 15641da177e4SLinus Torvalds 15651c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 15664d34a67dSDave Jones return ret_freq; 15671da177e4SLinus Torvalds 1568d92d50a4SViresh Kumar ret_freq = cpufreq_driver->get(policy->cpu); 15691da177e4SLinus Torvalds 1570e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 15711c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1572e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1573e08f5f5bSGautham R Shenoy saved value exists */ 1574e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1575a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, ret_freq); 15761da177e4SLinus Torvalds schedule_work(&policy->update); 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds 15804d34a67dSDave Jones return ret_freq; 15815a01f2e8SVenkatesh Pallipadi } 15821da177e4SLinus Torvalds 15835a01f2e8SVenkatesh Pallipadi /** 15845a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 15855a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 15865a01f2e8SVenkatesh Pallipadi * 15875a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 15885a01f2e8SVenkatesh Pallipadi */ 15895a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 15905a01f2e8SVenkatesh Pallipadi { 1591999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15925a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 15935a01f2e8SVenkatesh Pallipadi 1594999976e0SAaron Plattner if (policy) { 1595ad7722daSviresh kumar down_read(&policy->rwsem); 1596d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1597ad7722daSviresh kumar up_read(&policy->rwsem); 1598999976e0SAaron Plattner 1599999976e0SAaron Plattner cpufreq_cpu_put(policy); 1600999976e0SAaron Plattner } 16016eed9404SViresh Kumar 16024d34a67dSDave Jones return ret_freq; 16031da177e4SLinus Torvalds } 16041da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 16051da177e4SLinus Torvalds 16068a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 16078a25a2fdSKay Sievers .name = "cpufreq", 16088a25a2fdSKay Sievers .subsys = &cpu_subsys, 16098a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 16108a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1611e00e56dfSRafael J. Wysocki }; 1612e00e56dfSRafael J. Wysocki 1613e28867eaSViresh Kumar /* 1614e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1615e28867eaSViresh Kumar * during suspend.. 161642d4dc3fSBenjamin Herrenschmidt */ 1617e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 161842d4dc3fSBenjamin Herrenschmidt { 1619e28867eaSViresh Kumar int ret; 16204bc5d341SDave Jones 1621e28867eaSViresh Kumar if (!policy->suspend_freq) { 1622e28867eaSViresh Kumar pr_err("%s: suspend_freq can't be zero\n", __func__); 1623e28867eaSViresh Kumar return -EINVAL; 162442d4dc3fSBenjamin Herrenschmidt } 162542d4dc3fSBenjamin Herrenschmidt 1626e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1627e28867eaSViresh Kumar policy->suspend_freq); 1628e28867eaSViresh Kumar 1629e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1630e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1631e28867eaSViresh Kumar if (ret) 1632e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1633e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1634e28867eaSViresh Kumar 1635c9060494SDave Jones return ret; 163642d4dc3fSBenjamin Herrenschmidt } 1637e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 163842d4dc3fSBenjamin Herrenschmidt 163942d4dc3fSBenjamin Herrenschmidt /** 16402f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 16411da177e4SLinus Torvalds * 16422f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 16432f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 16442f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 16452f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 16461da177e4SLinus Torvalds */ 16472f0aea93SViresh Kumar void cpufreq_suspend(void) 16481da177e4SLinus Torvalds { 16493a3e9e06SViresh Kumar struct cpufreq_policy *policy; 16501da177e4SLinus Torvalds 16512f0aea93SViresh Kumar if (!cpufreq_driver) 1652e00e56dfSRafael J. Wysocki return; 16531da177e4SLinus Torvalds 16542f0aea93SViresh Kumar if (!has_target()) 1655b1b12babSViresh Kumar goto suspend; 16561da177e4SLinus Torvalds 16572f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 16582f0aea93SViresh Kumar 1659b4f0676fSViresh Kumar for_each_policy(policy) { 16602f0aea93SViresh Kumar if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP)) 16612f0aea93SViresh Kumar pr_err("%s: Failed to stop governor for policy: %p\n", 16622f0aea93SViresh Kumar __func__, policy); 16632f0aea93SViresh Kumar else if (cpufreq_driver->suspend 16642f0aea93SViresh Kumar && cpufreq_driver->suspend(policy)) 16652f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 16662f0aea93SViresh Kumar policy); 16671da177e4SLinus Torvalds } 1668b1b12babSViresh Kumar 1669b1b12babSViresh Kumar suspend: 1670b1b12babSViresh Kumar cpufreq_suspended = true; 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds /** 16742f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 16751da177e4SLinus Torvalds * 16762f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 16772f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 16781da177e4SLinus Torvalds */ 16792f0aea93SViresh Kumar void cpufreq_resume(void) 16801da177e4SLinus Torvalds { 16811da177e4SLinus Torvalds struct cpufreq_policy *policy; 16821da177e4SLinus Torvalds 16832f0aea93SViresh Kumar if (!cpufreq_driver) 16841da177e4SLinus Torvalds return; 16851da177e4SLinus Torvalds 16868e30444eSLan Tianyu cpufreq_suspended = false; 16878e30444eSLan Tianyu 16882f0aea93SViresh Kumar if (!has_target()) 16892f0aea93SViresh Kumar return; 16901da177e4SLinus Torvalds 16912f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 16922f0aea93SViresh Kumar 1693b4f0676fSViresh Kumar for_each_policy(policy) { 16940c5aa405SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) 16950c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 16960c5aa405SViresh Kumar policy); 16970c5aa405SViresh Kumar else if (__cpufreq_governor(policy, CPUFREQ_GOV_START) 16982f0aea93SViresh Kumar || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS)) 16992f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 17002f0aea93SViresh Kumar __func__, policy); 17012f0aea93SViresh Kumar 17022f0aea93SViresh Kumar /* 17032f0aea93SViresh Kumar * schedule call cpufreq_update_policy() for boot CPU, i.e. last 17042f0aea93SViresh Kumar * policy in list. It will verify that the current freq is in 17052f0aea93SViresh Kumar * sync with what we believe it to be. 17062f0aea93SViresh Kumar */ 17072f0aea93SViresh Kumar if (list_is_last(&policy->policy_list, &cpufreq_policy_list)) 17083a3e9e06SViresh Kumar schedule_work(&policy->update); 17091da177e4SLinus Torvalds } 17102f0aea93SViresh Kumar } 17111da177e4SLinus Torvalds 17129d95046eSBorislav Petkov /** 17139d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17149d95046eSBorislav Petkov * 17159d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17169d95046eSBorislav Petkov * or NULL, if none. 17179d95046eSBorislav Petkov */ 17189d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17199d95046eSBorislav Petkov { 17201c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17211c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17221c3d85ddSRafael J. Wysocki 17231c3d85ddSRafael J. Wysocki return NULL; 17249d95046eSBorislav Petkov } 17259d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17261da177e4SLinus Torvalds 172751315cdfSThomas Petazzoni /** 172851315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 172951315cdfSThomas Petazzoni * 173051315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 173151315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 173251315cdfSThomas Petazzoni */ 173351315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 173451315cdfSThomas Petazzoni { 173551315cdfSThomas Petazzoni if (cpufreq_driver) 173651315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 173751315cdfSThomas Petazzoni 173851315cdfSThomas Petazzoni return NULL; 173951315cdfSThomas Petazzoni } 174051315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 174151315cdfSThomas Petazzoni 17421da177e4SLinus Torvalds /********************************************************************* 17431da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 17441da177e4SLinus Torvalds *********************************************************************/ 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds /** 17471da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 17481da177e4SLinus Torvalds * @nb: notifier function to register 17491da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17501da177e4SLinus Torvalds * 17511da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 17521da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 17531da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 17541da177e4SLinus Torvalds * changes in cpufreq policy. 17551da177e4SLinus Torvalds * 17561da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1757e041c683SAlan Stern * blocking_notifier_chain_register. 17581da177e4SLinus Torvalds */ 17591da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 17601da177e4SLinus Torvalds { 17611da177e4SLinus Torvalds int ret; 17621da177e4SLinus Torvalds 1763d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1764d5aaffa9SDirk Brandewie return -EINVAL; 1765d5aaffa9SDirk Brandewie 176674212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 176774212ca4SCesar Eduardo Barros 17681da177e4SLinus Torvalds switch (list) { 17691da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1770b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1771e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17721da177e4SLinus Torvalds break; 17731da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1774e041c683SAlan Stern ret = blocking_notifier_chain_register( 1775e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 17761da177e4SLinus Torvalds break; 17771da177e4SLinus Torvalds default: 17781da177e4SLinus Torvalds ret = -EINVAL; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds return ret; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds /** 17861da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 17871da177e4SLinus Torvalds * @nb: notifier block to be unregistered 17881da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17891da177e4SLinus Torvalds * 17901da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 17911da177e4SLinus Torvalds * 17921da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1793e041c683SAlan Stern * blocking_notifier_chain_unregister. 17941da177e4SLinus Torvalds */ 17951da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 17961da177e4SLinus Torvalds { 17971da177e4SLinus Torvalds int ret; 17981da177e4SLinus Torvalds 1799d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1800d5aaffa9SDirk Brandewie return -EINVAL; 1801d5aaffa9SDirk Brandewie 18021da177e4SLinus Torvalds switch (list) { 18031da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1804b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1805e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 18061da177e4SLinus Torvalds break; 18071da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1808e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1809e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18101da177e4SLinus Torvalds break; 18111da177e4SLinus Torvalds default: 18121da177e4SLinus Torvalds ret = -EINVAL; 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds return ret; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds /********************************************************************* 18211da177e4SLinus Torvalds * GOVERNORS * 18221da177e4SLinus Torvalds *********************************************************************/ 18231da177e4SLinus Torvalds 18241c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 18251c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 18261c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 18271c03a2d0SViresh Kumar { 18281c03a2d0SViresh Kumar int ret; 18291c03a2d0SViresh Kumar 18301c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 18311c03a2d0SViresh Kumar 18321c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 18331c03a2d0SViresh Kumar if (!freqs->new) 18341c03a2d0SViresh Kumar return 0; 18351c03a2d0SViresh Kumar 18361c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 18371c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 18381c03a2d0SViresh Kumar 18391c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 18401c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 18411c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 18421c03a2d0SViresh Kumar 18431c03a2d0SViresh Kumar if (ret) 18441c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 18451c03a2d0SViresh Kumar __func__, ret); 18461c03a2d0SViresh Kumar 18471c03a2d0SViresh Kumar return ret; 18481c03a2d0SViresh Kumar } 18491c03a2d0SViresh Kumar 18508d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy, 18518d65775dSViresh Kumar struct cpufreq_frequency_table *freq_table, int index) 18528d65775dSViresh Kumar { 18531c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 18541c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 18558d65775dSViresh Kumar int retval = -EINVAL; 18568d65775dSViresh Kumar bool notify; 18578d65775dSViresh Kumar 18588d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 18598d65775dSViresh Kumar if (notify) { 18601c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 18611c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 18621c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 18631c03a2d0SViresh Kumar if (retval) 18641c03a2d0SViresh Kumar return retval; 18658d65775dSViresh Kumar 18661c03a2d0SViresh Kumar intermediate_freq = freqs.new; 18671c03a2d0SViresh Kumar /* Set old freq to intermediate */ 18681c03a2d0SViresh Kumar if (intermediate_freq) 18691c03a2d0SViresh Kumar freqs.old = freqs.new; 18701c03a2d0SViresh Kumar } 18711c03a2d0SViresh Kumar 18721c03a2d0SViresh Kumar freqs.new = freq_table[index].frequency; 18738d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 18748d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 18758d65775dSViresh Kumar 18768d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 18778d65775dSViresh Kumar } 18788d65775dSViresh Kumar 18798d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 18808d65775dSViresh Kumar if (retval) 18818d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 18828d65775dSViresh Kumar retval); 18838d65775dSViresh Kumar 18841c03a2d0SViresh Kumar if (notify) { 18858d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 18868d65775dSViresh Kumar 18871c03a2d0SViresh Kumar /* 18881c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 18891c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 18901c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 18911c03a2d0SViresh Kumar * case we have't switched to intermediate freq at all. 18921c03a2d0SViresh Kumar */ 18931c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 18941c03a2d0SViresh Kumar freqs.old = intermediate_freq; 18951c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 18961c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 18971c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 18981c03a2d0SViresh Kumar } 18991c03a2d0SViresh Kumar } 19001c03a2d0SViresh Kumar 19018d65775dSViresh Kumar return retval; 19028d65775dSViresh Kumar } 19038d65775dSViresh Kumar 19041da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 19051da177e4SLinus Torvalds unsigned int target_freq, 19061da177e4SLinus Torvalds unsigned int relation) 19071da177e4SLinus Torvalds { 19087249924eSViresh Kumar unsigned int old_target_freq = target_freq; 19098d65775dSViresh Kumar int retval = -EINVAL; 1910c32b6b8eSAshok Raj 1911a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1912a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1913a7b422cdSKonrad Rzeszutek Wilk 19147249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 19157249924eSViresh Kumar if (target_freq > policy->max) 19167249924eSViresh Kumar target_freq = policy->max; 19177249924eSViresh Kumar if (target_freq < policy->min) 19187249924eSViresh Kumar target_freq = policy->min; 19197249924eSViresh Kumar 19207249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 19217249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 19225a1c0228SViresh Kumar 19239c0ebcf7SViresh Kumar /* 19249c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 19259c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 19269c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 19279c0ebcf7SViresh Kumar * calls. 19289c0ebcf7SViresh Kumar */ 19295a1c0228SViresh Kumar if (target_freq == policy->cur) 19305a1c0228SViresh Kumar return 0; 19315a1c0228SViresh Kumar 19321c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 19331c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 19341c03a2d0SViresh Kumar 19351c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 19361c3d85ddSRafael J. Wysocki retval = cpufreq_driver->target(policy, target_freq, relation); 19379c0ebcf7SViresh Kumar else if (cpufreq_driver->target_index) { 19389c0ebcf7SViresh Kumar struct cpufreq_frequency_table *freq_table; 19399c0ebcf7SViresh Kumar int index; 194090d45d17SAshok Raj 19419c0ebcf7SViresh Kumar freq_table = cpufreq_frequency_get_table(policy->cpu); 19429c0ebcf7SViresh Kumar if (unlikely(!freq_table)) { 19439c0ebcf7SViresh Kumar pr_err("%s: Unable to find freq_table\n", __func__); 19449c0ebcf7SViresh Kumar goto out; 19459c0ebcf7SViresh Kumar } 19469c0ebcf7SViresh Kumar 19479c0ebcf7SViresh Kumar retval = cpufreq_frequency_table_target(policy, freq_table, 19489c0ebcf7SViresh Kumar target_freq, relation, &index); 19499c0ebcf7SViresh Kumar if (unlikely(retval)) { 19509c0ebcf7SViresh Kumar pr_err("%s: Unable to find matching freq\n", __func__); 19519c0ebcf7SViresh Kumar goto out; 19529c0ebcf7SViresh Kumar } 19539c0ebcf7SViresh Kumar 1954d4019f0aSViresh Kumar if (freq_table[index].frequency == policy->cur) { 19559c0ebcf7SViresh Kumar retval = 0; 1956d4019f0aSViresh Kumar goto out; 1957d4019f0aSViresh Kumar } 1958d4019f0aSViresh Kumar 19598d65775dSViresh Kumar retval = __target_index(policy, freq_table, index); 19609c0ebcf7SViresh Kumar } 19619c0ebcf7SViresh Kumar 19629c0ebcf7SViresh Kumar out: 19631da177e4SLinus Torvalds return retval; 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 19681da177e4SLinus Torvalds unsigned int target_freq, 19691da177e4SLinus Torvalds unsigned int relation) 19701da177e4SLinus Torvalds { 1971f1829e4aSJulia Lawall int ret = -EINVAL; 19721da177e4SLinus Torvalds 1973ad7722daSviresh kumar down_write(&policy->rwsem); 19741da177e4SLinus Torvalds 19751da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 19761da177e4SLinus Torvalds 1977ad7722daSviresh kumar up_write(&policy->rwsem); 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds return ret; 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 19821da177e4SLinus Torvalds 1983e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1984e08f5f5bSGautham R Shenoy unsigned int event) 19851da177e4SLinus Torvalds { 1986cc993cabSDave Jones int ret; 19876afde10cSThomas Renninger 19886afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 19896afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 19906afde10cSThomas Renninger That this is the case is already ensured in Kconfig 19916afde10cSThomas Renninger */ 19926afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 19936afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 19946afde10cSThomas Renninger #else 19956afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 19966afde10cSThomas Renninger #endif 19971c256245SThomas Renninger 19982f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 19992f0aea93SViresh Kumar if (cpufreq_suspended) 20002f0aea93SViresh Kumar return 0; 2001cb57720bSEthan Zhao /* 2002cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 2003cb57720bSEthan Zhao * notification happened, so check it. 2004cb57720bSEthan Zhao */ 2005cb57720bSEthan Zhao if (!policy->governor) 2006cb57720bSEthan Zhao return -EINVAL; 20072f0aea93SViresh Kumar 20081c256245SThomas Renninger if (policy->governor->max_transition_latency && 20091c256245SThomas Renninger policy->cpuinfo.transition_latency > 20101c256245SThomas Renninger policy->governor->max_transition_latency) { 20116afde10cSThomas Renninger if (!gov) 20126afde10cSThomas Renninger return -EINVAL; 20136afde10cSThomas Renninger else { 2014e837f9b5SJoe Perches pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", 2015e837f9b5SJoe Perches policy->governor->name, gov->name); 20161c256245SThomas Renninger policy->governor = gov; 20171c256245SThomas Renninger } 20186afde10cSThomas Renninger } 20191da177e4SLinus Torvalds 2020fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 20211da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 20221da177e4SLinus Torvalds return -EINVAL; 20231da177e4SLinus Torvalds 20242d06d8c4SDominik Brodowski pr_debug("__cpufreq_governor for CPU %u, event %u\n", 2025e08f5f5bSGautham R Shenoy policy->cpu, event); 202695731ebbSXiaoguang Chen 202795731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 202856d07db2SSrivatsa S. Bhat if ((policy->governor_enabled && event == CPUFREQ_GOV_START) 2029f73d3933SViresh Kumar || (!policy->governor_enabled 2030f73d3933SViresh Kumar && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) { 203195731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 203295731ebbSXiaoguang Chen return -EBUSY; 203395731ebbSXiaoguang Chen } 203495731ebbSXiaoguang Chen 203595731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 203695731ebbSXiaoguang Chen policy->governor_enabled = false; 203795731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 203895731ebbSXiaoguang Chen policy->governor_enabled = true; 203995731ebbSXiaoguang Chen 204095731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 204195731ebbSXiaoguang Chen 20421da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 20431da177e4SLinus Torvalds 20444d5dcc42SViresh Kumar if (!ret) { 20454d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 20468e53695fSViresh Kumar policy->governor->initialized++; 20474d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 20488e53695fSViresh Kumar policy->governor->initialized--; 204995731ebbSXiaoguang Chen } else { 205095731ebbSXiaoguang Chen /* Restore original values */ 205195731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 205295731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 205395731ebbSXiaoguang Chen policy->governor_enabled = true; 205495731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 205595731ebbSXiaoguang Chen policy->governor_enabled = false; 205695731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 20574d5dcc42SViresh Kumar } 2058b394058fSViresh Kumar 2059fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 2060fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 20611da177e4SLinus Torvalds module_put(policy->governor->owner); 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds return ret; 20641da177e4SLinus Torvalds } 20651da177e4SLinus Torvalds 20661da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 20671da177e4SLinus Torvalds { 20683bcb09a3SJeremy Fitzhardinge int err; 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds if (!governor) 20711da177e4SLinus Torvalds return -EINVAL; 20721da177e4SLinus Torvalds 2073a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2074a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2075a7b422cdSKonrad Rzeszutek Wilk 20763fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 20771da177e4SLinus Torvalds 2078b394058fSViresh Kumar governor->initialized = 0; 20793bcb09a3SJeremy Fitzhardinge err = -EBUSY; 208042f91fa1SViresh Kumar if (!find_governor(governor->name)) { 20813bcb09a3SJeremy Fitzhardinge err = 0; 20821da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 20833bcb09a3SJeremy Fitzhardinge } 20841da177e4SLinus Torvalds 20853fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20863bcb09a3SJeremy Fitzhardinge return err; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 20911da177e4SLinus Torvalds { 209290e41bacSPrarit Bhargava int cpu; 209390e41bacSPrarit Bhargava 20941da177e4SLinus Torvalds if (!governor) 20951da177e4SLinus Torvalds return; 20961da177e4SLinus Torvalds 2097a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2098a7b422cdSKonrad Rzeszutek Wilk return; 2099a7b422cdSKonrad Rzeszutek Wilk 210090e41bacSPrarit Bhargava for_each_present_cpu(cpu) { 210190e41bacSPrarit Bhargava if (cpu_online(cpu)) 210290e41bacSPrarit Bhargava continue; 210390e41bacSPrarit Bhargava if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name)) 210490e41bacSPrarit Bhargava strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0"); 210590e41bacSPrarit Bhargava } 210690e41bacSPrarit Bhargava 21073fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 21081da177e4SLinus Torvalds list_del(&governor->governor_list); 21093fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 21101da177e4SLinus Torvalds return; 21111da177e4SLinus Torvalds } 21121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 21131da177e4SLinus Torvalds 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds /********************************************************************* 21161da177e4SLinus Torvalds * POLICY INTERFACE * 21171da177e4SLinus Torvalds *********************************************************************/ 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds /** 21201da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 212129464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 212229464f28SDave Jones * is written 21231da177e4SLinus Torvalds * 21241da177e4SLinus Torvalds * Reads the current cpufreq policy. 21251da177e4SLinus Torvalds */ 21261da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 21271da177e4SLinus Torvalds { 21281da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 21291da177e4SLinus Torvalds if (!policy) 21301da177e4SLinus Torvalds return -EINVAL; 21311da177e4SLinus Torvalds 21321da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 21331da177e4SLinus Torvalds if (!cpu_policy) 21341da177e4SLinus Torvalds return -EINVAL; 21351da177e4SLinus Torvalds 2136d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 21371da177e4SLinus Torvalds 21381da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 21391da177e4SLinus Torvalds return 0; 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 21421da177e4SLinus Torvalds 2143153d7f3fSArjan van de Ven /* 2144037ce839SViresh Kumar * policy : current policy. 2145037ce839SViresh Kumar * new_policy: policy to be set. 2146153d7f3fSArjan van de Ven */ 2147037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 21483a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 21491da177e4SLinus Torvalds { 2150d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2151d9a789c7SRafael J. Wysocki int ret; 21521da177e4SLinus Torvalds 2153e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2154e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 21551da177e4SLinus Torvalds 2156d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 21571da177e4SLinus Torvalds 2158d9a789c7SRafael J. Wysocki if (new_policy->min > policy->max || new_policy->max < policy->min) 2159d9a789c7SRafael J. Wysocki return -EINVAL; 21609c9a43edSMattia Dongili 21611da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 21623a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 21631da177e4SLinus Torvalds if (ret) 2164d9a789c7SRafael J. Wysocki return ret; 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2167e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21683a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 2171e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21723a3e9e06SViresh Kumar CPUFREQ_INCOMPATIBLE, new_policy); 21731da177e4SLinus Torvalds 2174bb176f7dSViresh Kumar /* 2175bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2176bb176f7dSViresh Kumar * different to the first one 2177bb176f7dSViresh Kumar */ 21783a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2179e041c683SAlan Stern if (ret) 2180d9a789c7SRafael J. Wysocki return ret; 21811da177e4SLinus Torvalds 21821da177e4SLinus Torvalds /* notification of the new policy */ 2183e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21843a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 21851da177e4SLinus Torvalds 21863a3e9e06SViresh Kumar policy->min = new_policy->min; 21873a3e9e06SViresh Kumar policy->max = new_policy->max; 21881da177e4SLinus Torvalds 21892d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 21903a3e9e06SViresh Kumar policy->min, policy->max); 21911da177e4SLinus Torvalds 21921c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 21933a3e9e06SViresh Kumar policy->policy = new_policy->policy; 21942d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2195d9a789c7SRafael J. Wysocki return cpufreq_driver->setpolicy(new_policy); 2196d9a789c7SRafael J. Wysocki } 2197d9a789c7SRafael J. Wysocki 2198d9a789c7SRafael J. Wysocki if (new_policy->governor == policy->governor) 2199d9a789c7SRafael J. Wysocki goto out; 22001da177e4SLinus Torvalds 22012d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 22021da177e4SLinus Torvalds 2203d9a789c7SRafael J. Wysocki /* save old, working values */ 2204d9a789c7SRafael J. Wysocki old_gov = policy->governor; 22051da177e4SLinus Torvalds /* end old governor */ 2206d9a789c7SRafael J. Wysocki if (old_gov) { 22073a3e9e06SViresh Kumar __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 2208ad7722daSviresh kumar up_write(&policy->rwsem); 2209d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2210ad7722daSviresh kumar down_write(&policy->rwsem); 22117bd353a9SViresh Kumar } 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds /* start new governor */ 22143a3e9e06SViresh Kumar policy->governor = new_policy->governor; 22153a3e9e06SViresh Kumar if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) { 2216d9a789c7SRafael J. Wysocki if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) 2217d9a789c7SRafael J. Wysocki goto out; 2218d9a789c7SRafael J. Wysocki 2219ad7722daSviresh kumar up_write(&policy->rwsem); 2220d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2221ad7722daSviresh kumar down_write(&policy->rwsem); 2222955ef483SViresh Kumar } 22237bd353a9SViresh Kumar 22241da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2225d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 22261da177e4SLinus Torvalds if (old_gov) { 22273a3e9e06SViresh Kumar policy->governor = old_gov; 2228d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); 2229d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_START); 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds 2232d9a789c7SRafael J. Wysocki return -EINVAL; 2233d9a789c7SRafael J. Wysocki 2234d9a789c7SRafael J. Wysocki out: 2235d9a789c7SRafael J. Wysocki pr_debug("governor: change or update limits\n"); 2236d9a789c7SRafael J. Wysocki return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 22371da177e4SLinus Torvalds } 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds /** 22401da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 22411da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 22421da177e4SLinus Torvalds * 224325985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 22441da177e4SLinus Torvalds * at different times. 22451da177e4SLinus Torvalds */ 22461da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 22471da177e4SLinus Torvalds { 22483a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 22493a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 2250f1829e4aSJulia Lawall int ret; 22511da177e4SLinus Torvalds 2252fefa8ff8SAaron Plattner if (!policy) 2253fefa8ff8SAaron Plattner return -ENODEV; 22541da177e4SLinus Torvalds 2255ad7722daSviresh kumar down_write(&policy->rwsem); 22561da177e4SLinus Torvalds 22572d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 2258d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 22593a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 22603a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 22613a3e9e06SViresh Kumar new_policy.policy = policy->user_policy.policy; 22623a3e9e06SViresh Kumar new_policy.governor = policy->user_policy.governor; 22631da177e4SLinus Torvalds 2264bb176f7dSViresh Kumar /* 2265bb176f7dSViresh Kumar * BIOS might change freq behind our back 2266bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2267bb176f7dSViresh Kumar */ 22682ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 22693a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 2270bd0fa9bbSViresh Kumar if (WARN_ON(!new_policy.cur)) { 2271bd0fa9bbSViresh Kumar ret = -EIO; 2272fefa8ff8SAaron Plattner goto unlock; 2273bd0fa9bbSViresh Kumar } 2274bd0fa9bbSViresh Kumar 22753a3e9e06SViresh Kumar if (!policy->cur) { 2276e837f9b5SJoe Perches pr_debug("Driver did not initialize current freq\n"); 22773a3e9e06SViresh Kumar policy->cur = new_policy.cur; 2278a85f7bd3SThomas Renninger } else { 22799c0ebcf7SViresh Kumar if (policy->cur != new_policy.cur && has_target()) 2280a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, new_policy.cur); 22810961dd0dSThomas Renninger } 2282a85f7bd3SThomas Renninger } 22830961dd0dSThomas Renninger 2284037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 22851da177e4SLinus Torvalds 2286fefa8ff8SAaron Plattner unlock: 2287ad7722daSviresh kumar up_write(&policy->rwsem); 22885a01f2e8SVenkatesh Pallipadi 22893a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 22901da177e4SLinus Torvalds return ret; 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 22931da177e4SLinus Torvalds 22942760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 2295c32b6b8eSAshok Raj unsigned long action, void *hcpu) 2296c32b6b8eSAshok Raj { 2297c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 22988a25a2fdSKay Sievers struct device *dev; 2299c32b6b8eSAshok Raj 23008a25a2fdSKay Sievers dev = get_cpu_device(cpu); 23018a25a2fdSKay Sievers if (dev) { 23025302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 2303c32b6b8eSAshok Raj case CPU_ONLINE: 230496bbbe4aSViresh Kumar __cpufreq_add_dev(dev, NULL); 2305c32b6b8eSAshok Raj break; 23065302c3fbSSrivatsa S. Bhat 2307c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 230896bbbe4aSViresh Kumar __cpufreq_remove_dev_prepare(dev, NULL); 23091aee40acSSrivatsa S. Bhat break; 23101aee40acSSrivatsa S. Bhat 23111aee40acSSrivatsa S. Bhat case CPU_POST_DEAD: 231296bbbe4aSViresh Kumar __cpufreq_remove_dev_finish(dev, NULL); 2313c32b6b8eSAshok Raj break; 23145302c3fbSSrivatsa S. Bhat 23155a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 231696bbbe4aSViresh Kumar __cpufreq_add_dev(dev, NULL); 2317c32b6b8eSAshok Raj break; 2318c32b6b8eSAshok Raj } 2319c32b6b8eSAshok Raj } 2320c32b6b8eSAshok Raj return NOTIFY_OK; 2321c32b6b8eSAshok Raj } 2322c32b6b8eSAshok Raj 23239c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2324c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2325c32b6b8eSAshok Raj }; 23261da177e4SLinus Torvalds 23271da177e4SLinus Torvalds /********************************************************************* 23286f19efc0SLukasz Majewski * BOOST * 23296f19efc0SLukasz Majewski *********************************************************************/ 23306f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 23316f19efc0SLukasz Majewski { 23326f19efc0SLukasz Majewski struct cpufreq_frequency_table *freq_table; 23336f19efc0SLukasz Majewski struct cpufreq_policy *policy; 23346f19efc0SLukasz Majewski int ret = -EINVAL; 23356f19efc0SLukasz Majewski 2336b4f0676fSViresh Kumar for_each_policy(policy) { 23376f19efc0SLukasz Majewski freq_table = cpufreq_frequency_get_table(policy->cpu); 23386f19efc0SLukasz Majewski if (freq_table) { 23396f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 23406f19efc0SLukasz Majewski freq_table); 23416f19efc0SLukasz Majewski if (ret) { 23426f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 23436f19efc0SLukasz Majewski __func__); 23446f19efc0SLukasz Majewski break; 23456f19efc0SLukasz Majewski } 23466f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 23476f19efc0SLukasz Majewski __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 23486f19efc0SLukasz Majewski } 23496f19efc0SLukasz Majewski } 23506f19efc0SLukasz Majewski 23516f19efc0SLukasz Majewski return ret; 23526f19efc0SLukasz Majewski } 23536f19efc0SLukasz Majewski 23546f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 23556f19efc0SLukasz Majewski { 23566f19efc0SLukasz Majewski unsigned long flags; 23576f19efc0SLukasz Majewski int ret = 0; 23586f19efc0SLukasz Majewski 23596f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 23606f19efc0SLukasz Majewski return 0; 23616f19efc0SLukasz Majewski 23626f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 23636f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 23646f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23656f19efc0SLukasz Majewski 23666f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 23676f19efc0SLukasz Majewski if (ret) { 23686f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 23696f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 23706f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23716f19efc0SLukasz Majewski 2372e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2373e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 23746f19efc0SLukasz Majewski } 23756f19efc0SLukasz Majewski 23766f19efc0SLukasz Majewski return ret; 23776f19efc0SLukasz Majewski } 23786f19efc0SLukasz Majewski 23796f19efc0SLukasz Majewski int cpufreq_boost_supported(void) 23806f19efc0SLukasz Majewski { 23816f19efc0SLukasz Majewski if (likely(cpufreq_driver)) 23826f19efc0SLukasz Majewski return cpufreq_driver->boost_supported; 23836f19efc0SLukasz Majewski 23846f19efc0SLukasz Majewski return 0; 23856f19efc0SLukasz Majewski } 23866f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported); 23876f19efc0SLukasz Majewski 23886f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 23896f19efc0SLukasz Majewski { 23906f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 23916f19efc0SLukasz Majewski } 23926f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 23936f19efc0SLukasz Majewski 23946f19efc0SLukasz Majewski /********************************************************************* 23951da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 23961da177e4SLinus Torvalds *********************************************************************/ 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds /** 23991da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 24001da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 24011da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 24021da177e4SLinus Torvalds * 24031da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 24041da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 24051da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 24061da177e4SLinus Torvalds * 24071da177e4SLinus Torvalds */ 2408221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 24091da177e4SLinus Torvalds { 24101da177e4SLinus Torvalds unsigned long flags; 24111da177e4SLinus Torvalds int ret; 24121da177e4SLinus Torvalds 2413a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2414a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2415a7b422cdSKonrad Rzeszutek Wilk 24161da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 24179c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 24189832235fSRafael J. Wysocki driver_data->target) || 24199832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 24201c03a2d0SViresh Kumar driver_data->target)) || 24211c03a2d0SViresh Kumar (!!driver_data->get_intermediate != !!driver_data->target_intermediate)) 24221da177e4SLinus Torvalds return -EINVAL; 24231da177e4SLinus Torvalds 24242d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 24251da177e4SLinus Torvalds 24260d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24271c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 24280d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24294dea5806SYinghai Lu return -EEXIST; 24301da177e4SLinus Torvalds } 24311c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 24320d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24331da177e4SLinus Torvalds 2434bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2435bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2436bc68b7dfSViresh Kumar 24376f19efc0SLukasz Majewski if (cpufreq_boost_supported()) { 24386f19efc0SLukasz Majewski /* 24396f19efc0SLukasz Majewski * Check if driver provides function to enable boost - 24406f19efc0SLukasz Majewski * if not, use cpufreq_boost_set_sw as default 24416f19efc0SLukasz Majewski */ 24426f19efc0SLukasz Majewski if (!cpufreq_driver->set_boost) 24436f19efc0SLukasz Majewski cpufreq_driver->set_boost = cpufreq_boost_set_sw; 24446f19efc0SLukasz Majewski 24456f19efc0SLukasz Majewski ret = cpufreq_sysfs_create_file(&boost.attr); 24466f19efc0SLukasz Majewski if (ret) { 24476f19efc0SLukasz Majewski pr_err("%s: cannot register global BOOST sysfs file\n", 24486f19efc0SLukasz Majewski __func__); 24496f19efc0SLukasz Majewski goto err_null_driver; 24506f19efc0SLukasz Majewski } 24516f19efc0SLukasz Majewski } 24526f19efc0SLukasz Majewski 24538a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 24548f5bc2abSJiri Slaby if (ret) 24556f19efc0SLukasz Majewski goto err_boost_unreg; 24561da177e4SLinus Torvalds 2457ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2458ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 24591da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 2460ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2461e08f5f5bSGautham R Shenoy driver_data->name); 24628a25a2fdSKay Sievers goto err_if_unreg; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds 246565edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 24662d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 24671da177e4SLinus Torvalds 24688f5bc2abSJiri Slaby return 0; 24698a25a2fdSKay Sievers err_if_unreg: 24708a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 24716f19efc0SLukasz Majewski err_boost_unreg: 24726f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 24736f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 24748f5bc2abSJiri Slaby err_null_driver: 24750d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24761c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24770d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24784d34a67dSDave Jones return ret; 24791da177e4SLinus Torvalds } 24801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 24811da177e4SLinus Torvalds 24821da177e4SLinus Torvalds /** 24831da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 24841da177e4SLinus Torvalds * 24851da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 24861da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 24871da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 24881da177e4SLinus Torvalds * currently not initialised. 24891da177e4SLinus Torvalds */ 2490221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 24911da177e4SLinus Torvalds { 24921da177e4SLinus Torvalds unsigned long flags; 24931da177e4SLinus Torvalds 24941c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 24951da177e4SLinus Torvalds return -EINVAL; 24961da177e4SLinus Torvalds 24972d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 24981da177e4SLinus Torvalds 24998a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 25006f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 25016f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 25026f19efc0SLukasz Majewski 250365edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 25041da177e4SLinus Torvalds 25056eed9404SViresh Kumar down_write(&cpufreq_rwsem); 25060d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 25076eed9404SViresh Kumar 25081c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 25096eed9404SViresh Kumar 25100d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 25116eed9404SViresh Kumar up_write(&cpufreq_rwsem); 25121da177e4SLinus Torvalds 25131da177e4SLinus Torvalds return 0; 25141da177e4SLinus Torvalds } 25151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 25165a01f2e8SVenkatesh Pallipadi 251790de2a4aSDoug Anderson /* 251890de2a4aSDoug Anderson * Stop cpufreq at shutdown to make sure it isn't holding any locks 251990de2a4aSDoug Anderson * or mutexes when secondary CPUs are halted. 252090de2a4aSDoug Anderson */ 252190de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = { 252290de2a4aSDoug Anderson .shutdown = cpufreq_suspend, 252390de2a4aSDoug Anderson }; 252490de2a4aSDoug Anderson 25255a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 25265a01f2e8SVenkatesh Pallipadi { 2527a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2528a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2529a7b422cdSKonrad Rzeszutek Wilk 25302361be23SViresh Kumar cpufreq_global_kobject = kobject_create(); 25318aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 25328aa84ad8SThomas Renninger 253390de2a4aSDoug Anderson register_syscore_ops(&cpufreq_syscore_ops); 253490de2a4aSDoug Anderson 25355a01f2e8SVenkatesh Pallipadi return 0; 25365a01f2e8SVenkatesh Pallipadi } 25375a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2538