1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/drivers/cpufreq/cpufreq.c 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2001 Russell King 61da177e4SLinus Torvalds * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> 7bb176f7dSViresh Kumar * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> 81da177e4SLinus Torvalds * 9c32b6b8eSAshok Raj * Oct 2005 - Ashok Raj <ashok.raj@intel.com> 10c32b6b8eSAshok Raj * Added handling for CPU hotplug 118ff69732SDave Jones * Feb 2006 - Jacob Shin <jacob.shin@amd.com> 128ff69732SDave Jones * Fix handling for CPU hotplug -- affected CPUs 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds 15db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 16db701151SViresh Kumar 175ff0a268SViresh Kumar #include <linux/cpu.h> 181da177e4SLinus Torvalds #include <linux/cpufreq.h> 195c238a8bSAmit Kucheria #include <linux/cpu_cooling.h> 201da177e4SLinus Torvalds #include <linux/delay.h> 211da177e4SLinus Torvalds #include <linux/device.h> 225ff0a268SViresh Kumar #include <linux/init.h> 235ff0a268SViresh Kumar #include <linux/kernel_stat.h> 245ff0a268SViresh Kumar #include <linux/module.h> 253fc54d37Sakpm@osdl.org #include <linux/mutex.h> 2667d874c3SViresh Kumar #include <linux/pm_qos.h> 275ff0a268SViresh Kumar #include <linux/slab.h> 282f0aea93SViresh Kumar #include <linux/suspend.h> 2990de2a4aSDoug Anderson #include <linux/syscore_ops.h> 305ff0a268SViresh Kumar #include <linux/tick.h> 31f55ae08cSViresh Kumar #include <linux/units.h> 326f4f2723SThomas Renninger #include <trace/events/power.h> 336f4f2723SThomas Renninger 34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list); 35f963735aSViresh Kumar 36f963735aSViresh Kumar /* Macros to iterate over CPU policies */ 37f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active) \ 38fd7dc7e6SEric Biggers list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ 39fd7dc7e6SEric Biggers if ((__active) == !policy_is_inactive(__policy)) 40f963735aSViresh Kumar 41f963735aSViresh Kumar #define for_each_active_policy(__policy) \ 42f963735aSViresh Kumar for_each_suitable_policy(__policy, true) 43f963735aSViresh Kumar #define for_each_inactive_policy(__policy) \ 44f963735aSViresh Kumar for_each_suitable_policy(__policy, false) 45f963735aSViresh Kumar 46f7b27061SViresh Kumar /* Iterate over governors */ 47f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 48f7b27061SViresh Kumar #define for_each_governor(__governor) \ 49f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 50f7b27061SViresh Kumar 518412b456SQuentin Perret static char default_governor[CPUFREQ_NAME_LEN]; 528412b456SQuentin Perret 53a9909c21SLee Jones /* 54cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 551da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 561da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 571da177e4SLinus Torvalds */ 581c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 597a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 60bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 61bb176f7dSViresh Kumar 62874f6353SIonela Voinescu static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance); 63874f6353SIonela Voinescu bool cpufreq_supports_freq_invariance(void) 64874f6353SIonela Voinescu { 65874f6353SIonela Voinescu return static_branch_likely(&cpufreq_freq_invariance); 66874f6353SIonela Voinescu } 67874f6353SIonela Voinescu 682f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 692f0aea93SViresh Kumar static bool cpufreq_suspended; 701da177e4SLinus Torvalds 719c0ebcf7SViresh Kumar static inline bool has_target(void) 729c0ebcf7SViresh Kumar { 739c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 749c0ebcf7SViresh Kumar } 759c0ebcf7SViresh Kumar 761da177e4SLinus Torvalds /* internal prototypes */ 77d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 78a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy); 79a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy); 80a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy); 811e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy, 821e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov, 831e4f63aeSRafael J. Wysocki unsigned int new_pol); 8445482c70SRafael J. Wysocki 85a9909c21SLee Jones /* 861da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 871da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 881da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 891da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 901da177e4SLinus Torvalds * The mutex locks both lists. 911da177e4SLinus Torvalds */ 92e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 93cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); 941da177e4SLinus Torvalds 95a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 96da584455SViresh Kumar static int cpufreq_disabled(void) 97a7b422cdSKonrad Rzeszutek Wilk { 98a7b422cdSKonrad Rzeszutek Wilk return off; 99a7b422cdSKonrad Rzeszutek Wilk } 100a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 101a7b422cdSKonrad Rzeszutek Wilk { 102a7b422cdSKonrad Rzeszutek Wilk off = 1; 103a7b422cdSKonrad Rzeszutek Wilk } 1043fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1051da177e4SLinus Torvalds 1064d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1074d5dcc42SViresh Kumar { 1080b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1094d5dcc42SViresh Kumar } 1103f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1114d5dcc42SViresh Kumar 112183edb20SYangtao Li static struct kobject *cpufreq_global_kobject; 113183edb20SYangtao Li 114944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 115944e9a03SViresh Kumar { 116944e9a03SViresh Kumar if (have_governor_per_policy()) 117944e9a03SViresh Kumar return &policy->kobj; 118944e9a03SViresh Kumar else 119944e9a03SViresh Kumar return cpufreq_global_kobject; 120944e9a03SViresh Kumar } 121944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 122944e9a03SViresh Kumar 12372a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 12472a4ce34SViresh Kumar { 1255720821bSFrederic Weisbecker struct kernel_cpustat kcpustat; 12672a4ce34SViresh Kumar u64 cur_wall_time; 1275720821bSFrederic Weisbecker u64 idle_time; 12872a4ce34SViresh Kumar u64 busy_time; 12972a4ce34SViresh Kumar 1307fb1327eSFrederic Weisbecker cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); 13172a4ce34SViresh Kumar 1325720821bSFrederic Weisbecker kcpustat_cpu_fetch(&kcpustat, cpu); 1335720821bSFrederic Weisbecker 1345720821bSFrederic Weisbecker busy_time = kcpustat.cpustat[CPUTIME_USER]; 1355720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_SYSTEM]; 1365720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_IRQ]; 1375720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ]; 1385720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_STEAL]; 1395720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_NICE]; 14072a4ce34SViresh Kumar 14172a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 14272a4ce34SViresh Kumar if (wall) 1437fb1327eSFrederic Weisbecker *wall = div_u64(cur_wall_time, NSEC_PER_USEC); 14472a4ce34SViresh Kumar 1457fb1327eSFrederic Weisbecker return div_u64(idle_time, NSEC_PER_USEC); 14672a4ce34SViresh Kumar } 14772a4ce34SViresh Kumar 14872a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 14972a4ce34SViresh Kumar { 15072a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 15172a4ce34SViresh Kumar 15272a4ce34SViresh Kumar if (idle_time == -1ULL) 15372a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 15472a4ce34SViresh Kumar else if (!io_busy) 15572a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 15672a4ce34SViresh Kumar 15772a4ce34SViresh Kumar return idle_time; 15872a4ce34SViresh Kumar } 15972a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 16072a4ce34SViresh Kumar 16170e9e778SViresh Kumar /* 16270e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 16370e9e778SViresh Kumar * drivers of SMP systems. It will do following: 16470e9e778SViresh Kumar * - validate & show freq table passed 16570e9e778SViresh Kumar * - set policies transition latency 16670e9e778SViresh Kumar * - policy->cpus with all possible CPUs 16770e9e778SViresh Kumar */ 168c4dcc8a1SViresh Kumar void cpufreq_generic_init(struct cpufreq_policy *policy, 16970e9e778SViresh Kumar struct cpufreq_frequency_table *table, 17070e9e778SViresh Kumar unsigned int transition_latency) 17170e9e778SViresh Kumar { 17292c99d15SViresh Kumar policy->freq_table = table; 17370e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 17470e9e778SViresh Kumar 17570e9e778SViresh Kumar /* 17658405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 17770e9e778SViresh Kumar * share the clock and voltage and clock. 17870e9e778SViresh Kumar */ 17970e9e778SViresh Kumar cpumask_setall(policy->cpus); 18070e9e778SViresh Kumar } 18170e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 18270e9e778SViresh Kumar 1831f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 184652ed95dSViresh Kumar { 185652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 186652ed95dSViresh Kumar 187988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 188988bed09SViresh Kumar } 1891f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); 190988bed09SViresh Kumar 191988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 192988bed09SViresh Kumar { 193988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 194988bed09SViresh Kumar 195652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 196e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 197e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 198652ed95dSViresh Kumar return 0; 199652ed95dSViresh Kumar } 200652ed95dSViresh Kumar 201652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 202652ed95dSViresh Kumar } 203652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 204652ed95dSViresh Kumar 20550e9c852SViresh Kumar /** 2065d094feaSRafael J. Wysocki * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. 2075d094feaSRafael J. Wysocki * @cpu: CPU to find the policy for. 20850e9c852SViresh Kumar * 2095d094feaSRafael J. Wysocki * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment 2105d094feaSRafael J. Wysocki * the kobject reference counter of that policy. Return a valid policy on 2115d094feaSRafael J. Wysocki * success or NULL on failure. 21250e9c852SViresh Kumar * 2135d094feaSRafael J. Wysocki * The policy returned by this function has to be released with the help of 2145d094feaSRafael J. Wysocki * cpufreq_cpu_put() to balance its kobject reference counter properly. 21550e9c852SViresh Kumar */ 2166eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2171da177e4SLinus Torvalds { 2186eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2191da177e4SLinus Torvalds unsigned long flags; 2201da177e4SLinus Torvalds 2211b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 2226eed9404SViresh Kumar return NULL; 2236eed9404SViresh Kumar 2241da177e4SLinus Torvalds /* get the cpufreq driver */ 2250d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2261da177e4SLinus Torvalds 2276eed9404SViresh Kumar if (cpufreq_driver) { 2281da177e4SLinus Torvalds /* get the CPU */ 229988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 2306eed9404SViresh Kumar if (policy) 2316eed9404SViresh Kumar kobject_get(&policy->kobj); 2326eed9404SViresh Kumar } 2336eed9404SViresh Kumar 2346eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2351da177e4SLinus Torvalds 2363a3e9e06SViresh Kumar return policy; 237a9144436SStephen Boyd } 2381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2391da177e4SLinus Torvalds 24050e9c852SViresh Kumar /** 2415d094feaSRafael J. Wysocki * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. 2425d094feaSRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_get(). 24350e9c852SViresh Kumar */ 2443a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 245a9144436SStephen Boyd { 2466eed9404SViresh Kumar kobject_put(&policy->kobj); 247a9144436SStephen Boyd } 2481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2491da177e4SLinus Torvalds 250540a3758SRafael J. Wysocki /** 251540a3758SRafael J. Wysocki * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. 252540a3758SRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). 253540a3758SRafael J. Wysocki */ 2549083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy) 255540a3758SRafael J. Wysocki { 256540a3758SRafael J. Wysocki if (WARN_ON(!policy)) 257540a3758SRafael J. Wysocki return; 258540a3758SRafael J. Wysocki 259540a3758SRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 260540a3758SRafael J. Wysocki 261540a3758SRafael J. Wysocki up_write(&policy->rwsem); 262540a3758SRafael J. Wysocki 263540a3758SRafael J. Wysocki cpufreq_cpu_put(policy); 264540a3758SRafael J. Wysocki } 265540a3758SRafael J. Wysocki 266540a3758SRafael J. Wysocki /** 267540a3758SRafael J. Wysocki * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. 268540a3758SRafael J. Wysocki * @cpu: CPU to find the policy for. 269540a3758SRafael J. Wysocki * 270540a3758SRafael J. Wysocki * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and 271540a3758SRafael J. Wysocki * if the policy returned by it is not NULL, acquire its rwsem for writing. 272540a3758SRafael J. Wysocki * Return the policy if it is active or release it and return NULL otherwise. 273540a3758SRafael J. Wysocki * 274540a3758SRafael J. Wysocki * The policy returned by this function has to be released with the help of 275540a3758SRafael J. Wysocki * cpufreq_cpu_release() in order to release its rwsem and balance its usage 276540a3758SRafael J. Wysocki * counter properly. 277540a3758SRafael J. Wysocki */ 2789083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) 279540a3758SRafael J. Wysocki { 280540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 281540a3758SRafael J. Wysocki 282540a3758SRafael J. Wysocki if (!policy) 283540a3758SRafael J. Wysocki return NULL; 284540a3758SRafael J. Wysocki 285540a3758SRafael J. Wysocki down_write(&policy->rwsem); 286540a3758SRafael J. Wysocki 287540a3758SRafael J. Wysocki if (policy_is_inactive(policy)) { 288540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 289540a3758SRafael J. Wysocki return NULL; 290540a3758SRafael J. Wysocki } 291540a3758SRafael J. Wysocki 292540a3758SRafael J. Wysocki return policy; 293540a3758SRafael J. Wysocki } 294540a3758SRafael J. Wysocki 2951da177e4SLinus Torvalds /********************************************************************* 2961da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2971da177e4SLinus Torvalds *********************************************************************/ 2981da177e4SLinus Torvalds 299ec06e586SRafael J. Wysocki /** 300ec06e586SRafael J. Wysocki * adjust_jiffies - Adjust the system "loops_per_jiffy". 301ec06e586SRafael J. Wysocki * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. 302ec06e586SRafael J. Wysocki * @ci: Frequency change information. 3031da177e4SLinus Torvalds * 3041da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 3051da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 3061da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 3071da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 3081da177e4SLinus Torvalds */ 30939c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 31039c132eeSViresh Kumar { 3111da177e4SLinus Torvalds #ifndef CONFIG_SMP 3121da177e4SLinus Torvalds static unsigned long l_p_j_ref; 3131da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 3161da177e4SLinus Torvalds return; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 3191da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 3201da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 321e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 322e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 3231da177e4SLinus Torvalds } 3240b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 325e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 326e08f5f5bSGautham R Shenoy ci->new); 327e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 328e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds #endif 33139c132eeSViresh Kumar } 3321da177e4SLinus Torvalds 33320b5324dSViresh Kumar /** 334ec06e586SRafael J. Wysocki * cpufreq_notify_transition - Notify frequency transition and adjust jiffies. 33520b5324dSViresh Kumar * @policy: cpufreq policy to enable fast frequency switching for. 33620b5324dSViresh Kumar * @freqs: contain details of the frequency update. 33720b5324dSViresh Kumar * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. 33820b5324dSViresh Kumar * 339ec06e586SRafael J. Wysocki * This function calls the transition notifiers and adjust_jiffies(). 340ec06e586SRafael J. Wysocki * 341ec06e586SRafael J. Wysocki * It is called twice on all CPU frequency changes that have external effects. 34220b5324dSViresh Kumar */ 34320b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 34420b5324dSViresh Kumar struct cpufreq_freqs *freqs, 34520b5324dSViresh Kumar unsigned int state) 3461da177e4SLinus Torvalds { 347df24014aSViresh Kumar int cpu; 348df24014aSViresh Kumar 3491da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3501da177e4SLinus Torvalds 351d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 352d5aaffa9SDirk Brandewie return; 353d5aaffa9SDirk Brandewie 354df24014aSViresh Kumar freqs->policy = policy; 3551c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3562d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 357e4472cb3SDave Jones state, freqs->new); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds switch (state) { 3601da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 36120b5324dSViresh Kumar /* 36220b5324dSViresh Kumar * Detect if the driver reported a value as "old frequency" 363e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 364e4472cb3SDave Jones * "old frequency". 3651da177e4SLinus Torvalds */ 36698015228SViresh Kumar if (policy->cur && policy->cur != freqs->old) { 367e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 368e4472cb3SDave Jones freqs->old, policy->cur); 369e4472cb3SDave Jones freqs->old = policy->cur; 3701da177e4SLinus Torvalds } 37120b5324dSViresh Kumar 372b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 373e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 37420b5324dSViresh Kumar 3751da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3761da177e4SLinus Torvalds break; 377e4472cb3SDave Jones 3781da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3791da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 38020b5324dSViresh Kumar pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, 38120b5324dSViresh Kumar cpumask_pr_args(policy->cpus)); 38220b5324dSViresh Kumar 383df24014aSViresh Kumar for_each_cpu(cpu, policy->cpus) 384df24014aSViresh Kumar trace_cpu_frequency(freqs->new, cpu); 385df24014aSViresh Kumar 386b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 387e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 388bb176f7dSViresh Kumar 38920b5324dSViresh Kumar cpufreq_stats_record_transition(policy, freqs->new); 39020b5324dSViresh Kumar policy->cur = freqs->new; 39120b5324dSViresh Kumar } 392b43a7ffbSViresh Kumar } 3931da177e4SLinus Torvalds 394f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 395236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 396f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 397f7ba3b41SViresh Kumar { 398f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 399f7ba3b41SViresh Kumar if (!transition_failed) 400f7ba3b41SViresh Kumar return; 401f7ba3b41SViresh Kumar 402f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 403f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 404f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 405f7ba3b41SViresh Kumar } 406f7ba3b41SViresh Kumar 40712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 40812478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 40912478cf0SSrivatsa S. Bhat { 410ca654dc3SSrivatsa S. Bhat 411ca654dc3SSrivatsa S. Bhat /* 412ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 413ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 414ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 415ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 416ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 417ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 418ca654dc3SSrivatsa S. Bhat */ 419ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 420ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 421ca654dc3SSrivatsa S. Bhat 42212478cf0SSrivatsa S. Bhat wait: 42312478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 42412478cf0SSrivatsa S. Bhat 42512478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 42612478cf0SSrivatsa S. Bhat 42712478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 42812478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 42912478cf0SSrivatsa S. Bhat goto wait; 43012478cf0SSrivatsa S. Bhat } 43112478cf0SSrivatsa S. Bhat 43212478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 433ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 43412478cf0SSrivatsa S. Bhat 43512478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 43612478cf0SSrivatsa S. Bhat 43712478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 43812478cf0SSrivatsa S. Bhat } 43912478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 44012478cf0SSrivatsa S. Bhat 44112478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 44212478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 44312478cf0SSrivatsa S. Bhat { 4440e7ea2f3SIgor Stoppa if (WARN_ON(!policy->transition_ongoing)) 44512478cf0SSrivatsa S. Bhat return; 44612478cf0SSrivatsa S. Bhat 44712478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 44812478cf0SSrivatsa S. Bhat 4491a0419b0SIonela Voinescu arch_set_freq_scale(policy->related_cpus, 4501a0419b0SIonela Voinescu policy->cur, 4511a0419b0SIonela Voinescu policy->cpuinfo.max_freq); 4521a0419b0SIonela Voinescu 45312478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 454ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 45512478cf0SSrivatsa S. Bhat 45612478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 45712478cf0SSrivatsa S. Bhat } 45812478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 45912478cf0SSrivatsa S. Bhat 460b7898fdaSRafael J. Wysocki /* 461b7898fdaSRafael J. Wysocki * Fast frequency switching status count. Positive means "enabled", negative 462b7898fdaSRafael J. Wysocki * means "disabled" and 0 means "not decided yet". 463b7898fdaSRafael J. Wysocki */ 464b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count; 465b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock); 466b7898fdaSRafael J. Wysocki 467b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void) 468b7898fdaSRafael J. Wysocki { 469b7898fdaSRafael J. Wysocki struct notifier_block *nb; 470b7898fdaSRafael J. Wysocki 471b7898fdaSRafael J. Wysocki pr_info("Registered transition notifiers:\n"); 472b7898fdaSRafael J. Wysocki 473b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_transition_notifier_list.mutex); 474b7898fdaSRafael J. Wysocki 475b7898fdaSRafael J. Wysocki for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) 476d75f773cSSakari Ailus pr_info("%pS\n", nb->notifier_call); 477b7898fdaSRafael J. Wysocki 478b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_transition_notifier_list.mutex); 479b7898fdaSRafael J. Wysocki } 480b7898fdaSRafael J. Wysocki 481b7898fdaSRafael J. Wysocki /** 482b7898fdaSRafael J. Wysocki * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. 483b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to enable fast frequency switching for. 484b7898fdaSRafael J. Wysocki * 485b7898fdaSRafael J. Wysocki * Try to enable fast frequency switching for @policy. 486b7898fdaSRafael J. Wysocki * 487b7898fdaSRafael J. Wysocki * The attempt will fail if there is at least one transition notifier registered 488b7898fdaSRafael J. Wysocki * at this point, as fast frequency switching is quite fundamentally at odds 489b7898fdaSRafael J. Wysocki * with transition notifiers. Thus if successful, it will make registration of 490b7898fdaSRafael J. Wysocki * transition notifiers fail going forward. 491b7898fdaSRafael J. Wysocki */ 492b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) 493b7898fdaSRafael J. Wysocki { 494b7898fdaSRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 495b7898fdaSRafael J. Wysocki 496b7898fdaSRafael J. Wysocki if (!policy->fast_switch_possible) 497b7898fdaSRafael J. Wysocki return; 498b7898fdaSRafael J. Wysocki 499b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 500b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count >= 0) { 501b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 502b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = true; 503b7898fdaSRafael J. Wysocki } else { 504b7898fdaSRafael J. Wysocki pr_warn("CPU%u: Fast frequency switching not enabled\n", 505b7898fdaSRafael J. Wysocki policy->cpu); 506b7898fdaSRafael J. Wysocki cpufreq_list_transition_notifiers(); 507b7898fdaSRafael J. Wysocki } 508b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 509b7898fdaSRafael J. Wysocki } 510b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); 511b7898fdaSRafael J. Wysocki 5126c9d9c81SRafael J. Wysocki /** 5136c9d9c81SRafael J. Wysocki * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. 5146c9d9c81SRafael J. Wysocki * @policy: cpufreq policy to disable fast frequency switching for. 5156c9d9c81SRafael J. Wysocki */ 5166c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) 517b7898fdaSRafael J. Wysocki { 518b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 519b7898fdaSRafael J. Wysocki if (policy->fast_switch_enabled) { 520b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = false; 521b7898fdaSRafael J. Wysocki if (!WARN_ON(cpufreq_fast_switch_count <= 0)) 522b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 523b7898fdaSRafael J. Wysocki } 524b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 525b7898fdaSRafael J. Wysocki } 5266c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); 5271da177e4SLinus Torvalds 528f9ccdec2SViresh Kumar static unsigned int __resolve_freq(struct cpufreq_policy *policy, 529f9ccdec2SViresh Kumar unsigned int target_freq, unsigned int relation) 530f9ccdec2SViresh Kumar { 531f9ccdec2SViresh Kumar unsigned int idx; 532f9ccdec2SViresh Kumar 533b3beca76SViresh Kumar target_freq = clamp_val(target_freq, policy->min, policy->max); 534f9ccdec2SViresh Kumar 535b3beca76SViresh Kumar if (!cpufreq_driver->target_index) 536f9ccdec2SViresh Kumar return target_freq; 537b3beca76SViresh Kumar 538b3beca76SViresh Kumar idx = cpufreq_frequency_table_target(policy, target_freq, relation); 539b3beca76SViresh Kumar policy->cached_resolved_idx = idx; 540b3beca76SViresh Kumar policy->cached_target_freq = target_freq; 541b3beca76SViresh Kumar return policy->freq_table[idx].frequency; 542f9ccdec2SViresh Kumar } 543f9ccdec2SViresh Kumar 544e3c06236SSteve Muckle /** 545e3c06236SSteve Muckle * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported 546e3c06236SSteve Muckle * one. 547a9909c21SLee Jones * @policy: associated policy to interrogate 548e3c06236SSteve Muckle * @target_freq: target frequency to resolve. 549e3c06236SSteve Muckle * 550e3c06236SSteve Muckle * The target to driver frequency mapping is cached in the policy. 551e3c06236SSteve Muckle * 552e3c06236SSteve Muckle * Return: Lowest driver-supported frequency greater than or equal to the 553e3c06236SSteve Muckle * given target_freq, subject to policy (min/max) and driver limitations. 554e3c06236SSteve Muckle */ 555e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, 556e3c06236SSteve Muckle unsigned int target_freq) 557e3c06236SSteve Muckle { 558b894d20eSVincent Donnefort return __resolve_freq(policy, target_freq, CPUFREQ_RELATION_LE); 559e3c06236SSteve Muckle } 560ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); 561e3c06236SSteve Muckle 562aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) 563aa7519afSViresh Kumar { 564aa7519afSViresh Kumar unsigned int latency; 565aa7519afSViresh Kumar 566aa7519afSViresh Kumar if (policy->transition_delay_us) 567aa7519afSViresh Kumar return policy->transition_delay_us; 568aa7519afSViresh Kumar 569aa7519afSViresh Kumar latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; 570e948bc8fSViresh Kumar if (latency) { 571e948bc8fSViresh Kumar /* 572e948bc8fSViresh Kumar * For platforms that can change the frequency very fast (< 10 573e948bc8fSViresh Kumar * us), the above formula gives a decent transition delay. But 574e948bc8fSViresh Kumar * for platforms where transition_latency is in milliseconds, it 575e948bc8fSViresh Kumar * ends up giving unrealistic values. 576e948bc8fSViresh Kumar * 577e948bc8fSViresh Kumar * Cap the default transition delay to 10 ms, which seems to be 578e948bc8fSViresh Kumar * a reasonable amount of time after which we should reevaluate 579e948bc8fSViresh Kumar * the frequency. 580e948bc8fSViresh Kumar */ 581e948bc8fSViresh Kumar return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); 582e948bc8fSViresh Kumar } 583aa7519afSViresh Kumar 584aa7519afSViresh Kumar return LATENCY_MULTIPLIER; 585aa7519afSViresh Kumar } 586aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); 587aa7519afSViresh Kumar 5881da177e4SLinus Torvalds /********************************************************************* 5891da177e4SLinus Torvalds * SYSFS INTERFACE * 5901da177e4SLinus Torvalds *********************************************************************/ 5918a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 592625c85a6SViresh Kumar struct kobj_attribute *attr, char *buf) 5936f19efc0SLukasz Majewski { 5946f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 5956f19efc0SLukasz Majewski } 5966f19efc0SLukasz Majewski 597625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, 5986f19efc0SLukasz Majewski const char *buf, size_t count) 5996f19efc0SLukasz Majewski { 6006f19efc0SLukasz Majewski int ret, enable; 6016f19efc0SLukasz Majewski 6026f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 6036f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 6046f19efc0SLukasz Majewski return -EINVAL; 6056f19efc0SLukasz Majewski 6066f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 607e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 608e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 6096f19efc0SLukasz Majewski return -EINVAL; 6106f19efc0SLukasz Majewski } 6116f19efc0SLukasz Majewski 612e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 613e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 6146f19efc0SLukasz Majewski 6156f19efc0SLukasz Majewski return count; 6166f19efc0SLukasz Majewski } 6176f19efc0SLukasz Majewski define_one_global_rw(boost); 6181da177e4SLinus Torvalds 61942f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 6203bcb09a3SJeremy Fitzhardinge { 6213bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 6223bcb09a3SJeremy Fitzhardinge 623f7b27061SViresh Kumar for_each_governor(t) 6247c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 6253bcb09a3SJeremy Fitzhardinge return t; 6263bcb09a3SJeremy Fitzhardinge 6273bcb09a3SJeremy Fitzhardinge return NULL; 6283bcb09a3SJeremy Fitzhardinge } 6293bcb09a3SJeremy Fitzhardinge 6308cc46ae5SViresh Kumar static struct cpufreq_governor *get_governor(const char *str_governor) 6318cc46ae5SViresh Kumar { 6328cc46ae5SViresh Kumar struct cpufreq_governor *t; 6338cc46ae5SViresh Kumar 6348cc46ae5SViresh Kumar mutex_lock(&cpufreq_governor_mutex); 6358cc46ae5SViresh Kumar t = find_governor(str_governor); 6368cc46ae5SViresh Kumar if (!t) 6378cc46ae5SViresh Kumar goto unlock; 6388cc46ae5SViresh Kumar 6398cc46ae5SViresh Kumar if (!try_module_get(t->owner)) 6408cc46ae5SViresh Kumar t = NULL; 6418cc46ae5SViresh Kumar 6428cc46ae5SViresh Kumar unlock: 6438cc46ae5SViresh Kumar mutex_unlock(&cpufreq_governor_mutex); 6448cc46ae5SViresh Kumar 6458cc46ae5SViresh Kumar return t; 6468cc46ae5SViresh Kumar } 6478cc46ae5SViresh Kumar 6481e4f63aeSRafael J. Wysocki static unsigned int cpufreq_parse_policy(char *str_governor) 6491da177e4SLinus Torvalds { 6501e4f63aeSRafael J. Wysocki if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) 6511e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_PERFORMANCE; 6521e4f63aeSRafael J. Wysocki 6531e4f63aeSRafael J. Wysocki if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) 6541e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_POWERSAVE; 6551e4f63aeSRafael J. Wysocki 6561e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_UNKNOWN; 657ab05d97aSYue Hu } 658ab05d97aSYue Hu 659ab05d97aSYue Hu /** 6605ddc6d4eSViresh Kumar * cpufreq_parse_governor - parse a governor string only for has_target() 6611e4f63aeSRafael J. Wysocki * @str_governor: Governor name. 662ab05d97aSYue Hu */ 6631e4f63aeSRafael J. Wysocki static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor) 664ab05d97aSYue Hu { 6651da177e4SLinus Torvalds struct cpufreq_governor *t; 6663bcb09a3SJeremy Fitzhardinge 6678cc46ae5SViresh Kumar t = get_governor(str_governor); 6688cc46ae5SViresh Kumar if (t) 6698cc46ae5SViresh Kumar return t; 6703bcb09a3SJeremy Fitzhardinge 6718cc46ae5SViresh Kumar if (request_module("cpufreq_%s", str_governor)) 6721e4f63aeSRafael J. Wysocki return NULL; 673045149e6SRafael J. Wysocki 6748cc46ae5SViresh Kumar return get_governor(str_governor); 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 677a9909c21SLee Jones /* 678e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 679e08f5f5bSGautham R Shenoy * print out cpufreq information 6801da177e4SLinus Torvalds * 6811da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 6821da177e4SLinus Torvalds * "unsigned int". 6831da177e4SLinus Torvalds */ 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds #define show_one(file_name, object) \ 6861da177e4SLinus Torvalds static ssize_t show_##file_name \ 6871da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 6881da177e4SLinus Torvalds { \ 6891da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 6901da177e4SLinus Torvalds } 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 6931da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 694ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 6951da177e4SLinus Torvalds show_one(scaling_min_freq, min); 6961da177e4SLinus Torvalds show_one(scaling_max_freq, max); 697c034b02eSDirk Brandewie 698f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu) 699f8475cefSLen Brown { 700f8475cefSLen Brown return 0; 701f8475cefSLen Brown } 702f8475cefSLen Brown 70309347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 704c034b02eSDirk Brandewie { 705c034b02eSDirk Brandewie ssize_t ret; 706f8475cefSLen Brown unsigned int freq; 707c034b02eSDirk Brandewie 708f8475cefSLen Brown freq = arch_freq_get_on_cpu(policy->cpu); 709f8475cefSLen Brown if (freq) 710f8475cefSLen Brown ret = sprintf(buf, "%u\n", freq); 711681fe684SViresh Kumar else if (cpufreq_driver->setpolicy && cpufreq_driver->get) 712c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 713c034b02eSDirk Brandewie else 714c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 715c034b02eSDirk Brandewie return ret; 716c034b02eSDirk Brandewie } 7171da177e4SLinus Torvalds 718a9909c21SLee Jones /* 7191da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds #define store_one(file_name, object) \ 7221da177e4SLinus Torvalds static ssize_t store_##file_name \ 7231da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 7241da177e4SLinus Torvalds { \ 72518c49926SViresh Kumar unsigned long val; \ 72618c49926SViresh Kumar int ret; \ 7271da177e4SLinus Torvalds \ 72818c49926SViresh Kumar ret = sscanf(buf, "%lu", &val); \ 7291da177e4SLinus Torvalds if (ret != 1) \ 7301da177e4SLinus Torvalds return -EINVAL; \ 7311da177e4SLinus Torvalds \ 7323000ce3cSRafael J. Wysocki ret = freq_qos_update_request(policy->object##_freq_req, val);\ 73318c49926SViresh Kumar return ret >= 0 ? count : ret; \ 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds store_one(scaling_min_freq, min); 7371da177e4SLinus Torvalds store_one(scaling_max_freq, max); 7381da177e4SLinus Torvalds 739a9909c21SLee Jones /* 7401da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 7411da177e4SLinus Torvalds */ 742e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 743e08f5f5bSGautham R Shenoy char *buf) 7441da177e4SLinus Torvalds { 745d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 7469b4f603eSRafael J. Wysocki 7479b4f603eSRafael J. Wysocki if (cur_freq) 7481da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 7499b4f603eSRafael J. Wysocki 7509b4f603eSRafael J. Wysocki return sprintf(buf, "<unknown>\n"); 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753a9909c21SLee Jones /* 7541da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 7551da177e4SLinus Torvalds */ 756905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 7571da177e4SLinus Torvalds { 7581da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 7591da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 7601da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 7611da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 7621da177e4SLinus Torvalds else if (policy->governor) 7634b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 76429464f28SDave Jones policy->governor->name); 7651da177e4SLinus Torvalds return -EINVAL; 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 768a9909c21SLee Jones /* 7691da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 7701da177e4SLinus Torvalds */ 7711da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 7721da177e4SLinus Torvalds const char *buf, size_t count) 7731da177e4SLinus Torvalds { 7741da177e4SLinus Torvalds char str_governor[16]; 7751e4f63aeSRafael J. Wysocki int ret; 7761da177e4SLinus Torvalds 7771da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 7781da177e4SLinus Torvalds if (ret != 1) 7791da177e4SLinus Torvalds return -EINVAL; 7801da177e4SLinus Torvalds 781ab05d97aSYue Hu if (cpufreq_driver->setpolicy) { 7821e4f63aeSRafael J. Wysocki unsigned int new_pol; 7831e4f63aeSRafael J. Wysocki 7841e4f63aeSRafael J. Wysocki new_pol = cpufreq_parse_policy(str_governor); 7851e4f63aeSRafael J. Wysocki if (!new_pol) 786ab05d97aSYue Hu return -EINVAL; 7871e4f63aeSRafael J. Wysocki 7881e4f63aeSRafael J. Wysocki ret = cpufreq_set_policy(policy, NULL, new_pol); 789ab05d97aSYue Hu } else { 7901e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov; 7911e4f63aeSRafael J. Wysocki 7921e4f63aeSRafael J. Wysocki new_gov = cpufreq_parse_governor(str_governor); 7931e4f63aeSRafael J. Wysocki if (!new_gov) 7941da177e4SLinus Torvalds return -EINVAL; 7951e4f63aeSRafael J. Wysocki 7961e4f63aeSRafael J. Wysocki ret = cpufreq_set_policy(policy, new_gov, 7971e4f63aeSRafael J. Wysocki CPUFREQ_POLICY_UNKNOWN); 7981e4f63aeSRafael J. Wysocki 7991e4f63aeSRafael J. Wysocki module_put(new_gov->owner); 800ab05d97aSYue Hu } 8011da177e4SLinus Torvalds 80288dc4384SViresh Kumar return ret ? ret : count; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 805a9909c21SLee Jones /* 8061da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 8071da177e4SLinus Torvalds */ 8081da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 8091da177e4SLinus Torvalds { 8101c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 813a9909c21SLee Jones /* 8141da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 8151da177e4SLinus Torvalds */ 8161da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 8171da177e4SLinus Torvalds char *buf) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds ssize_t i = 0; 8201da177e4SLinus Torvalds struct cpufreq_governor *t; 8211da177e4SLinus Torvalds 8229c0ebcf7SViresh Kumar if (!has_target()) { 8231da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 8241da177e4SLinus Torvalds goto out; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 8278cc46ae5SViresh Kumar mutex_lock(&cpufreq_governor_mutex); 828f7b27061SViresh Kumar for_each_governor(t) { 82929464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 83029464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 8318cc46ae5SViresh Kumar break; 8324b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 8331da177e4SLinus Torvalds } 8348cc46ae5SViresh Kumar mutex_unlock(&cpufreq_governor_mutex); 8351da177e4SLinus Torvalds out: 8361da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8371da177e4SLinus Torvalds return i; 8381da177e4SLinus Torvalds } 839e8628dd0SDarrick J. Wong 840f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 8411da177e4SLinus Torvalds { 8421da177e4SLinus Torvalds ssize_t i = 0; 8431da177e4SLinus Torvalds unsigned int cpu; 8441da177e4SLinus Torvalds 845835481d9SRusty Russell for_each_cpu(cpu, mask) { 8461da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u ", cpu); 8471da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 8481da177e4SLinus Torvalds break; 8491da177e4SLinus Torvalds } 850*336e5128SViresh Kumar 851*336e5128SViresh Kumar /* Remove the extra space at the end */ 852*336e5128SViresh Kumar i--; 853*336e5128SViresh Kumar 8541da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8551da177e4SLinus Torvalds return i; 8561da177e4SLinus Torvalds } 857f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 8581da177e4SLinus Torvalds 859a9909c21SLee Jones /* 860e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 861e8628dd0SDarrick J. Wong * hw coordination is in use 862e8628dd0SDarrick J. Wong */ 863e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 864e8628dd0SDarrick J. Wong { 865f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 866e8628dd0SDarrick J. Wong } 867e8628dd0SDarrick J. Wong 868a9909c21SLee Jones /* 869e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 870e8628dd0SDarrick J. Wong */ 871e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 872e8628dd0SDarrick J. Wong { 873f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 874e8628dd0SDarrick J. Wong } 875e8628dd0SDarrick J. Wong 8769e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 8779e76988eSVenki Pallipadi const char *buf, size_t count) 8789e76988eSVenki Pallipadi { 8799e76988eSVenki Pallipadi unsigned int freq = 0; 8809e76988eSVenki Pallipadi unsigned int ret; 8819e76988eSVenki Pallipadi 882879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 8839e76988eSVenki Pallipadi return -EINVAL; 8849e76988eSVenki Pallipadi 8859e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 8869e76988eSVenki Pallipadi if (ret != 1) 8879e76988eSVenki Pallipadi return -EINVAL; 8889e76988eSVenki Pallipadi 8899e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 8909e76988eSVenki Pallipadi 8919e76988eSVenki Pallipadi return count; 8929e76988eSVenki Pallipadi } 8939e76988eSVenki Pallipadi 8949e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 8959e76988eSVenki Pallipadi { 896879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 8979e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 8989e76988eSVenki Pallipadi 8999e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 9009e76988eSVenki Pallipadi } 9011da177e4SLinus Torvalds 902a9909c21SLee Jones /* 9038bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 904e2f74f35SThomas Renninger */ 905e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 906e2f74f35SThomas Renninger { 907e2f74f35SThomas Renninger unsigned int limit; 908e2f74f35SThomas Renninger int ret; 9091c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 910e2f74f35SThomas Renninger if (!ret) 911e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 912e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 913e2f74f35SThomas Renninger } 914e2f74f35SThomas Renninger 9156dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 9166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 9176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 9186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 9196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 9206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 9216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 9226dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 9236dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 9246dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 9256dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 9266dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 9276dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 9286dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 9291da177e4SLinus Torvalds 930fe262d5cSGreg Kroah-Hartman static struct attribute *cpufreq_attrs[] = { 9311da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 9321da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 933ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 9341da177e4SLinus Torvalds &scaling_min_freq.attr, 9351da177e4SLinus Torvalds &scaling_max_freq.attr, 9361da177e4SLinus Torvalds &affected_cpus.attr, 937e8628dd0SDarrick J. Wong &related_cpus.attr, 9381da177e4SLinus Torvalds &scaling_governor.attr, 9391da177e4SLinus Torvalds &scaling_driver.attr, 9401da177e4SLinus Torvalds &scaling_available_governors.attr, 9419e76988eSVenki Pallipadi &scaling_setspeed.attr, 9421da177e4SLinus Torvalds NULL 9431da177e4SLinus Torvalds }; 944fe262d5cSGreg Kroah-Hartman ATTRIBUTE_GROUPS(cpufreq); 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 9471da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 9501da177e4SLinus Torvalds { 9511da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9521da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 953d4627a28SSchspa Shi ssize_t ret = -EBUSY; 9546eed9404SViresh Kumar 955e6e8df07SKai Shen if (!fattr->show) 956e6e8df07SKai Shen return -EIO; 957e6e8df07SKai Shen 958ad7722daSviresh kumar down_read(&policy->rwsem); 959d4627a28SSchspa Shi if (likely(!policy_is_inactive(policy))) 960e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 961ad7722daSviresh kumar up_read(&policy->rwsem); 9621b750e3bSViresh Kumar 9631da177e4SLinus Torvalds return ret; 9641da177e4SLinus Torvalds } 9651da177e4SLinus Torvalds 9661da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 9671da177e4SLinus Torvalds const char *buf, size_t count) 9681da177e4SLinus Torvalds { 9691da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9701da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 971d4627a28SSchspa Shi ssize_t ret = -EBUSY; 9726eed9404SViresh Kumar 973e6e8df07SKai Shen if (!fattr->store) 974e6e8df07SKai Shen return -EIO; 975e6e8df07SKai Shen 9769b3d9bb3SWaiman Long /* 9779b3d9bb3SWaiman Long * cpus_read_trylock() is used here to work around a circular lock 9789b3d9bb3SWaiman Long * dependency problem with respect to the cpufreq_register_driver(). 9799b3d9bb3SWaiman Long */ 9809b3d9bb3SWaiman Long if (!cpus_read_trylock()) 9819b3d9bb3SWaiman Long return -EBUSY; 9824f750c93SSrivatsa S. Bhat 9836541aef0SRafael J. Wysocki if (cpu_online(policy->cpu)) { 984ad7722daSviresh kumar down_write(&policy->rwsem); 985d4627a28SSchspa Shi if (likely(!policy_is_inactive(policy))) 986e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 987ad7722daSviresh kumar up_write(&policy->rwsem); 9886541aef0SRafael J. Wysocki } 9896541aef0SRafael J. Wysocki 990a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 9914f750c93SSrivatsa S. Bhat 9921da177e4SLinus Torvalds return ret; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 9961da177e4SLinus Torvalds { 9971da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9982d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 9991da177e4SLinus Torvalds complete(&policy->kobj_unregister); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 100252cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 10031da177e4SLinus Torvalds .show = show, 10041da177e4SLinus Torvalds .store = store, 10051da177e4SLinus Torvalds }; 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 10081da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 1009fe262d5cSGreg Kroah-Hartman .default_groups = cpufreq_groups, 10101da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 10111da177e4SLinus Torvalds }; 10121da177e4SLinus Torvalds 10132c1b5a84SXiongfeng Wang static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu, 10142c1b5a84SXiongfeng Wang struct device *dev) 101587549141SViresh Kumar { 101667d874c3SViresh Kumar if (unlikely(!dev)) 10172f0ba790SRafael J. Wysocki return; 10182f0ba790SRafael J. Wysocki 10192f0ba790SRafael J. Wysocki if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) 10202f0ba790SRafael J. Wysocki return; 10212f0ba790SRafael J. Wysocki 102226619804SViresh Kumar dev_dbg(dev, "%s: Adding symlink\n", __func__); 10232f0ba790SRafael J. Wysocki if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) 10242f0ba790SRafael J. Wysocki dev_err(dev, "cpufreq symlink creation failed\n"); 102587549141SViresh Kumar } 102687549141SViresh Kumar 10275c84c1b8SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu, 102826619804SViresh Kumar struct device *dev) 102987549141SViresh Kumar { 103026619804SViresh Kumar dev_dbg(dev, "%s: Removing symlink\n", __func__); 103126619804SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 10325c84c1b8SViresh Kumar cpumask_clear_cpu(cpu, policy->real_cpus); 103387549141SViresh Kumar } 103487549141SViresh Kumar 1035d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 1036909a694eSDave Jones { 1037909a694eSDave Jones struct freq_attr **drv_attr; 1038909a694eSDave Jones int ret = 0; 1039909a694eSDave Jones 1040909a694eSDave Jones /* set up files for this cpu device */ 10411c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 1042f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 1043909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 1044909a694eSDave Jones if (ret) 10456d4e81edSTomeu Vizoso return ret; 1046909a694eSDave Jones drv_attr++; 1047909a694eSDave Jones } 10481c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 1049909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 1050909a694eSDave Jones if (ret) 10516d4e81edSTomeu Vizoso return ret; 1052909a694eSDave Jones } 1053c034b02eSDirk Brandewie 1054909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 1055909a694eSDave Jones if (ret) 10566d4e81edSTomeu Vizoso return ret; 1057c034b02eSDirk Brandewie 10581c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 1059e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 1060e2f74f35SThomas Renninger if (ret) 10616d4e81edSTomeu Vizoso return ret; 1062e2f74f35SThomas Renninger } 1063909a694eSDave Jones 106426619804SViresh Kumar return 0; 1065e18f1682SSrivatsa S. Bhat } 1066e18f1682SSrivatsa S. Bhat 10677f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy) 1068e18f1682SSrivatsa S. Bhat { 10691e4f63aeSRafael J. Wysocki struct cpufreq_governor *gov = NULL; 10701e4f63aeSRafael J. Wysocki unsigned int pol = CPUFREQ_POLICY_UNKNOWN; 10718cc46ae5SViresh Kumar int ret; 1072ab05d97aSYue Hu 1073ab05d97aSYue Hu if (has_target()) { 10741e4f63aeSRafael J. Wysocki /* Update policy governor to the one used before hotplug. */ 10758cc46ae5SViresh Kumar gov = get_governor(policy->last_governor); 1076de1df26bSRafael J. Wysocki if (gov) { 10776e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 10788412b456SQuentin Perret gov->name, policy->cpu); 10791e4f63aeSRafael J. Wysocki } else { 10808412b456SQuentin Perret gov = get_governor(default_governor); 1081de1df26bSRafael J. Wysocki } 10828412b456SQuentin Perret 10838412b456SQuentin Perret if (!gov) { 10848412b456SQuentin Perret gov = cpufreq_default_governor(); 10858412b456SQuentin Perret __module_get(gov->owner); 10868412b456SQuentin Perret } 10878412b456SQuentin Perret 1088ab05d97aSYue Hu } else { 10898412b456SQuentin Perret 109069030dd1SSrinivas Pandruvada /* Use the default policy if there is no last_policy. */ 1091ab05d97aSYue Hu if (policy->last_policy) { 10921e4f63aeSRafael J. Wysocki pol = policy->last_policy; 10938412b456SQuentin Perret } else { 10948412b456SQuentin Perret pol = cpufreq_parse_policy(default_governor); 1095f5739cb0SRafael J. Wysocki /* 10968412b456SQuentin Perret * In case the default governor is neither "performance" 1097f5739cb0SRafael J. Wysocki * nor "powersave", fall back to the initial policy 1098f5739cb0SRafael J. Wysocki * value set by the driver. 1099f5739cb0SRafael J. Wysocki */ 1100f5739cb0SRafael J. Wysocki if (pol == CPUFREQ_POLICY_UNKNOWN) 1101f5739cb0SRafael J. Wysocki pol = policy->policy; 110269030dd1SSrinivas Pandruvada } 1103f5739cb0SRafael J. Wysocki if (pol != CPUFREQ_POLICY_PERFORMANCE && 1104f5739cb0SRafael J. Wysocki pol != CPUFREQ_POLICY_POWERSAVE) 1105f5739cb0SRafael J. Wysocki return -ENODATA; 1106ab05d97aSYue Hu } 1107ab05d97aSYue Hu 11088cc46ae5SViresh Kumar ret = cpufreq_set_policy(policy, gov, pol); 11098cc46ae5SViresh Kumar if (gov) 11108cc46ae5SViresh Kumar module_put(gov->owner); 11118cc46ae5SViresh Kumar 11128cc46ae5SViresh Kumar return ret; 1113909a694eSDave Jones } 1114909a694eSDave Jones 1115d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1116fcf80582SViresh Kumar { 11179c0ebcf7SViresh Kumar int ret = 0; 1118fcf80582SViresh Kumar 1119bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 1120bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 1121bb29ae15SViresh Kumar return 0; 1122bb29ae15SViresh Kumar 112349f18560SViresh Kumar down_write(&policy->rwsem); 112445482c70SRafael J. Wysocki if (has_target()) 112545482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 1126fcf80582SViresh Kumar 1127fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 11282eaa3e2dSViresh Kumar 11299c0ebcf7SViresh Kumar if (has_target()) { 11300a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 113149f18560SViresh Kumar if (ret) 11323de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 1133820c6ca2SViresh Kumar } 113449f18560SViresh Kumar up_write(&policy->rwsem); 1135fcf80582SViresh Kumar return ret; 1136fcf80582SViresh Kumar } 1137fcf80582SViresh Kumar 1138c57b25bdSViresh Kumar void refresh_frequency_limits(struct cpufreq_policy *policy) 113970a59fdeSViresh Kumar { 114067d874c3SViresh Kumar if (!policy_is_inactive(policy)) { 114170a59fdeSViresh Kumar pr_debug("updating policy for CPU %u\n", policy->cpu); 114270a59fdeSViresh Kumar 11431e4f63aeSRafael J. Wysocki cpufreq_set_policy(policy, policy->governor, policy->policy); 114470a59fdeSViresh Kumar } 114567d874c3SViresh Kumar } 1146c57b25bdSViresh Kumar EXPORT_SYMBOL(refresh_frequency_limits); 114770a59fdeSViresh Kumar 114811eb69b9SViresh Kumar static void handle_update(struct work_struct *work) 114911eb69b9SViresh Kumar { 115011eb69b9SViresh Kumar struct cpufreq_policy *policy = 115111eb69b9SViresh Kumar container_of(work, struct cpufreq_policy, update); 115270a59fdeSViresh Kumar 115370a59fdeSViresh Kumar pr_debug("handle_update for cpu %u called\n", policy->cpu); 115467d874c3SViresh Kumar down_write(&policy->rwsem); 115570a59fdeSViresh Kumar refresh_frequency_limits(policy); 115667d874c3SViresh Kumar up_write(&policy->rwsem); 1157fcf80582SViresh Kumar } 11581da177e4SLinus Torvalds 115967d874c3SViresh Kumar static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, 116067d874c3SViresh Kumar void *data) 11618414809cSSrivatsa S. Bhat { 116267d874c3SViresh Kumar struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min); 1163e9698cc5SSrivatsa S. Bhat 116467d874c3SViresh Kumar schedule_work(&policy->update); 116567d874c3SViresh Kumar return 0; 1166edd4a893SViresh Kumar } 1167edd4a893SViresh Kumar 116867d874c3SViresh Kumar static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, 116967d874c3SViresh Kumar void *data) 117067d874c3SViresh Kumar { 117167d874c3SViresh Kumar struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max); 1172ad7722daSviresh kumar 117367d874c3SViresh Kumar schedule_work(&policy->update); 117467d874c3SViresh Kumar return 0; 1175e9698cc5SSrivatsa S. Bhat } 1176e9698cc5SSrivatsa S. Bhat 1177f9f41e3eSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 117842f921a6SViresh Kumar { 117942f921a6SViresh Kumar struct kobject *kobj; 118042f921a6SViresh Kumar struct completion *cmp; 118142f921a6SViresh Kumar 118287549141SViresh Kumar down_write(&policy->rwsem); 11831aefc75bSRafael J. Wysocki cpufreq_stats_free_table(policy); 118442f921a6SViresh Kumar kobj = &policy->kobj; 118542f921a6SViresh Kumar cmp = &policy->kobj_unregister; 118687549141SViresh Kumar up_write(&policy->rwsem); 118742f921a6SViresh Kumar kobject_put(kobj); 118842f921a6SViresh Kumar 118942f921a6SViresh Kumar /* 119042f921a6SViresh Kumar * We need to make sure that the underlying kobj is 119142f921a6SViresh Kumar * actually not referenced anymore by anybody before we 119242f921a6SViresh Kumar * proceed with unloading. 119342f921a6SViresh Kumar */ 119442f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 119542f921a6SViresh Kumar wait_for_completion(cmp); 119642f921a6SViresh Kumar pr_debug("wait complete\n"); 119742f921a6SViresh Kumar } 119842f921a6SViresh Kumar 119967d874c3SViresh Kumar static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 120067d874c3SViresh Kumar { 120167d874c3SViresh Kumar struct cpufreq_policy *policy; 120267d874c3SViresh Kumar struct device *dev = get_cpu_device(cpu); 120367d874c3SViresh Kumar int ret; 120467d874c3SViresh Kumar 120567d874c3SViresh Kumar if (!dev) 120667d874c3SViresh Kumar return NULL; 120767d874c3SViresh Kumar 120867d874c3SViresh Kumar policy = kzalloc(sizeof(*policy), GFP_KERNEL); 120967d874c3SViresh Kumar if (!policy) 121067d874c3SViresh Kumar return NULL; 121167d874c3SViresh Kumar 121267d874c3SViresh Kumar if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 121367d874c3SViresh Kumar goto err_free_policy; 121467d874c3SViresh Kumar 121567d874c3SViresh Kumar if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 121667d874c3SViresh Kumar goto err_free_cpumask; 121767d874c3SViresh Kumar 121867d874c3SViresh Kumar if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 121967d874c3SViresh Kumar goto err_free_rcpumask; 122067d874c3SViresh Kumar 122167d874c3SViresh Kumar ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 122267d874c3SViresh Kumar cpufreq_global_kobject, "policy%u", cpu); 122367d874c3SViresh Kumar if (ret) { 122467d874c3SViresh Kumar dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); 122567d874c3SViresh Kumar /* 122667d874c3SViresh Kumar * The entire policy object will be freed below, but the extra 122767d874c3SViresh Kumar * memory allocated for the kobject name needs to be freed by 122867d874c3SViresh Kumar * releasing the kobject. 122967d874c3SViresh Kumar */ 123067d874c3SViresh Kumar kobject_put(&policy->kobj); 123167d874c3SViresh Kumar goto err_free_real_cpus; 123267d874c3SViresh Kumar } 123367d874c3SViresh Kumar 12343000ce3cSRafael J. Wysocki freq_constraints_init(&policy->constraints); 12353000ce3cSRafael J. Wysocki 123667d874c3SViresh Kumar policy->nb_min.notifier_call = cpufreq_notifier_min; 123767d874c3SViresh Kumar policy->nb_max.notifier_call = cpufreq_notifier_max; 123867d874c3SViresh Kumar 12393000ce3cSRafael J. Wysocki ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, 12403000ce3cSRafael J. Wysocki &policy->nb_min); 124167d874c3SViresh Kumar if (ret) { 124267d874c3SViresh Kumar dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n", 124367d874c3SViresh Kumar ret, cpumask_pr_args(policy->cpus)); 124467d874c3SViresh Kumar goto err_kobj_remove; 124567d874c3SViresh Kumar } 124667d874c3SViresh Kumar 12473000ce3cSRafael J. Wysocki ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, 12483000ce3cSRafael J. Wysocki &policy->nb_max); 124967d874c3SViresh Kumar if (ret) { 125067d874c3SViresh Kumar dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n", 125167d874c3SViresh Kumar ret, cpumask_pr_args(policy->cpus)); 125267d874c3SViresh Kumar goto err_min_qos_notifier; 125367d874c3SViresh Kumar } 125467d874c3SViresh Kumar 125567d874c3SViresh Kumar INIT_LIST_HEAD(&policy->policy_list); 125667d874c3SViresh Kumar init_rwsem(&policy->rwsem); 125767d874c3SViresh Kumar spin_lock_init(&policy->transition_lock); 125867d874c3SViresh Kumar init_waitqueue_head(&policy->transition_wait); 125967d874c3SViresh Kumar init_completion(&policy->kobj_unregister); 126067d874c3SViresh Kumar INIT_WORK(&policy->update, handle_update); 126167d874c3SViresh Kumar 126267d874c3SViresh Kumar policy->cpu = cpu; 126367d874c3SViresh Kumar return policy; 126467d874c3SViresh Kumar 126567d874c3SViresh Kumar err_min_qos_notifier: 12663000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 12673000ce3cSRafael J. Wysocki &policy->nb_min); 126867d874c3SViresh Kumar err_kobj_remove: 126967d874c3SViresh Kumar cpufreq_policy_put_kobj(policy); 127067d874c3SViresh Kumar err_free_real_cpus: 127167d874c3SViresh Kumar free_cpumask_var(policy->real_cpus); 127267d874c3SViresh Kumar err_free_rcpumask: 127367d874c3SViresh Kumar free_cpumask_var(policy->related_cpus); 127467d874c3SViresh Kumar err_free_cpumask: 127567d874c3SViresh Kumar free_cpumask_var(policy->cpus); 127667d874c3SViresh Kumar err_free_policy: 127767d874c3SViresh Kumar kfree(policy); 127867d874c3SViresh Kumar 127967d874c3SViresh Kumar return NULL; 128067d874c3SViresh Kumar } 128167d874c3SViresh Kumar 1282f9f41e3eSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy) 1283e9698cc5SSrivatsa S. Bhat { 1284988bed09SViresh Kumar unsigned long flags; 1285988bed09SViresh Kumar int cpu; 1286988bed09SViresh Kumar 1287988bed09SViresh Kumar /* Remove policy from list */ 1288988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1289988bed09SViresh Kumar list_del(&policy->policy_list); 1290988bed09SViresh Kumar 1291988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1292988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1293988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1294988bed09SViresh Kumar 12953000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, 12963000ce3cSRafael J. Wysocki &policy->nb_max); 12973000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 12983000ce3cSRafael J. Wysocki &policy->nb_min); 12996a149036SViresh Kumar 13006941051dSSudeep Holla /* Cancel any pending policy->update work before freeing the policy. */ 13016941051dSSudeep Holla cancel_work_sync(&policy->update); 13026a149036SViresh Kumar 13036a149036SViresh Kumar if (policy->max_freq_req) { 13046a149036SViresh Kumar /* 13051e81d3e0STang Yizhou * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY 13061e81d3e0STang Yizhou * notification, since CPUFREQ_CREATE_POLICY notification was 13071e81d3e0STang Yizhou * sent after adding max_freq_req earlier. 13086a149036SViresh Kumar */ 13096a149036SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 13106a149036SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 13113000ce3cSRafael J. Wysocki freq_qos_remove_request(policy->max_freq_req); 13126a149036SViresh Kumar } 13136a149036SViresh Kumar 13143000ce3cSRafael J. Wysocki freq_qos_remove_request(policy->min_freq_req); 131518c49926SViresh Kumar kfree(policy->min_freq_req); 131667d874c3SViresh Kumar 1317f9f41e3eSViresh Kumar cpufreq_policy_put_kobj(policy); 1318559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1319e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1320e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1321e9698cc5SSrivatsa S. Bhat kfree(policy); 1322e9698cc5SSrivatsa S. Bhat } 1323e9698cc5SSrivatsa S. Bhat 13240b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 13251da177e4SLinus Torvalds { 13267f0c020aSViresh Kumar struct cpufreq_policy *policy; 1327194d99c7SRafael J. Wysocki bool new_policy; 13281da177e4SLinus Torvalds unsigned long flags; 13290b275352SRafael J. Wysocki unsigned int j; 13300b275352SRafael J. Wysocki int ret; 1331c32b6b8eSAshok Raj 13320b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 13336eed9404SViresh Kumar 1334bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 13359104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 133611ce707eSRafael J. Wysocki if (policy) { 13379104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 133811ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1339d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 13401da177e4SLinus Torvalds 134111ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1342194d99c7SRafael J. Wysocki new_policy = false; 134311ce707eSRafael J. Wysocki down_write(&policy->rwsem); 134411ce707eSRafael J. Wysocki policy->cpu = cpu; 134511ce707eSRafael J. Wysocki policy->governor = NULL; 134611ce707eSRafael J. Wysocki } else { 1347194d99c7SRafael J. Wysocki new_policy = true; 1348a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1349059019a3SDave Jones if (!policy) 1350d4d854d6SRafael J. Wysocki return -ENOMEM; 1351514ff1bcSSchspa Shi down_write(&policy->rwsem); 135272368d12SRafael J. Wysocki } 13530d66b91eSSrivatsa S. Bhat 135491a12e91SViresh Kumar if (!new_policy && cpufreq_driver->online) { 135591a12e91SViresh Kumar ret = cpufreq_driver->online(policy); 135691a12e91SViresh Kumar if (ret) { 135791a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 135891a12e91SViresh Kumar __LINE__); 135991a12e91SViresh Kumar goto out_exit_policy; 136091a12e91SViresh Kumar } 136191a12e91SViresh Kumar 136291a12e91SViresh Kumar /* Recover policy->cpus using related_cpus */ 136391a12e91SViresh Kumar cpumask_copy(policy->cpus, policy->related_cpus); 136491a12e91SViresh Kumar } else { 1365835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 13661da177e4SLinus Torvalds 136791a12e91SViresh Kumar /* 136891a12e91SViresh Kumar * Call driver. From then on the cpufreq must be able 136991a12e91SViresh Kumar * to accept all calls to ->verify and ->setpolicy for this CPU. 13701da177e4SLinus Torvalds */ 13711c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 13721da177e4SLinus Torvalds if (ret) { 137391a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 137491a12e91SViresh Kumar __LINE__); 13758101f997SViresh Kumar goto out_free_policy; 13761da177e4SLinus Torvalds } 1377643ae6e8SViresh Kumar 13783b718057SRafael J. Wysocki /* 13793b718057SRafael J. Wysocki * The initialization has succeeded and the policy is online. 13803b718057SRafael J. Wysocki * If there is a problem with its frequency table, take it 13813b718057SRafael J. Wysocki * offline and drop it. 13823b718057SRafael J. Wysocki */ 1383d417e069SViresh Kumar ret = cpufreq_table_validate_and_sort(policy); 1384d417e069SViresh Kumar if (ret) 13853b718057SRafael J. Wysocki goto out_offline_policy; 1386d417e069SViresh Kumar 13874d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 13880998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 13894d1f3a5bSRafael J. Wysocki } 1390559ed407SRafael J. Wysocki 13915a7e56a5SViresh Kumar /* 13925a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 13935a7e56a5SViresh Kumar * managing offline cpus here. 13945a7e56a5SViresh Kumar */ 13955a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 13965a7e56a5SViresh Kumar 1397194d99c7SRafael J. Wysocki if (new_policy) { 13982f0ba790SRafael J. Wysocki for_each_cpu(j, policy->related_cpus) { 1399652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 14002c1b5a84SXiongfeng Wang add_cpu_dev_symlink(policy, j, get_cpu_device(j)); 14012f0ba790SRafael J. Wysocki } 140218c49926SViresh Kumar 140318c49926SViresh Kumar policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), 140418c49926SViresh Kumar GFP_KERNEL); 1405b96f0384SWang ShaoBo if (!policy->min_freq_req) { 1406b96f0384SWang ShaoBo ret = -ENOMEM; 140718c49926SViresh Kumar goto out_destroy_policy; 1408b96f0384SWang ShaoBo } 140918c49926SViresh Kumar 14103000ce3cSRafael J. Wysocki ret = freq_qos_add_request(&policy->constraints, 14113000ce3cSRafael J. Wysocki policy->min_freq_req, FREQ_QOS_MIN, 1412521223d8SRafael J. Wysocki FREQ_QOS_MIN_DEFAULT_VALUE); 141318c49926SViresh Kumar if (ret < 0) { 141418c49926SViresh Kumar /* 14153000ce3cSRafael J. Wysocki * So we don't call freq_qos_remove_request() for an 141618c49926SViresh Kumar * uninitialized request. 141718c49926SViresh Kumar */ 141818c49926SViresh Kumar kfree(policy->min_freq_req); 141918c49926SViresh Kumar policy->min_freq_req = NULL; 142018c49926SViresh Kumar goto out_destroy_policy; 142118c49926SViresh Kumar } 142218c49926SViresh Kumar 142318c49926SViresh Kumar /* 142418c49926SViresh Kumar * This must be initialized right here to avoid calling 14253000ce3cSRafael J. Wysocki * freq_qos_remove_request() on uninitialized request in case 142618c49926SViresh Kumar * of errors. 142718c49926SViresh Kumar */ 142818c49926SViresh Kumar policy->max_freq_req = policy->min_freq_req + 1; 142918c49926SViresh Kumar 14303000ce3cSRafael J. Wysocki ret = freq_qos_add_request(&policy->constraints, 14313000ce3cSRafael J. Wysocki policy->max_freq_req, FREQ_QOS_MAX, 1432521223d8SRafael J. Wysocki FREQ_QOS_MAX_DEFAULT_VALUE); 143318c49926SViresh Kumar if (ret < 0) { 143418c49926SViresh Kumar policy->max_freq_req = NULL; 143518c49926SViresh Kumar goto out_destroy_policy; 143618c49926SViresh Kumar } 14376a149036SViresh Kumar 14386a149036SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 14396a149036SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 1440988bed09SViresh Kumar } 1441652ed95dSViresh Kumar 14425ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target()) { 1443da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1444da60ce9fSViresh Kumar if (!policy->cur) { 1445b96f0384SWang ShaoBo ret = -EIO; 1446da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1447d417e069SViresh Kumar goto out_destroy_policy; 1448da60ce9fSViresh Kumar } 1449da60ce9fSViresh Kumar } 1450da60ce9fSViresh Kumar 1451d3916691SViresh Kumar /* 1452d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1453d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1454d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1455d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1456d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1457d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1458d3916691SViresh Kumar * isn't found in freq-table. 1459d3916691SViresh Kumar * 1460d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1461d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1462d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1463d3916691SViresh Kumar * is initialized to zero). 1464d3916691SViresh Kumar * 1465d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1466d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1467d3916691SViresh Kumar * equal to target-freq. 1468d3916691SViresh Kumar */ 1469d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1470d3916691SViresh Kumar && has_target()) { 147197148d0aSViresh Kumar unsigned int old_freq = policy->cur; 147297148d0aSViresh Kumar 1473d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 147497148d0aSViresh Kumar ret = cpufreq_frequency_table_get_index(policy, old_freq); 1475d3916691SViresh Kumar if (ret == -EINVAL) { 147697148d0aSViresh Kumar ret = __cpufreq_driver_target(policy, old_freq - 1, 1477d3916691SViresh Kumar CPUFREQ_RELATION_L); 1478d3916691SViresh Kumar 1479d3916691SViresh Kumar /* 1480d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1481d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1482d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1483d3916691SViresh Kumar */ 1484d3916691SViresh Kumar BUG_ON(ret); 148597148d0aSViresh Kumar pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n", 148697148d0aSViresh Kumar __func__, policy->cpu, old_freq, policy->cur); 1487d3916691SViresh Kumar } 1488d3916691SViresh Kumar } 1489d3916691SViresh Kumar 1490194d99c7SRafael J. Wysocki if (new_policy) { 1491d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 149219d6f7ecSDave Jones if (ret) 1493d417e069SViresh Kumar goto out_destroy_policy; 14941aefc75bSRafael J. Wysocki 14951aefc75bSRafael J. Wysocki cpufreq_stats_create_table(policy); 1496c88a1f8bSLukasz Majewski 1497c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1498c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1499c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1500c17495b0SViresh Kumar 1501c17495b0SViresh Kumar /* 1502c17495b0SViresh Kumar * Register with the energy model before 1503c17495b0SViresh Kumar * sched_cpufreq_governor_change() is called, which will result 1504c17495b0SViresh Kumar * in rebuilding of the sched domains, which should only be done 1505c17495b0SViresh Kumar * once the energy model is properly initialized for the policy 1506c17495b0SViresh Kumar * first. 1507c17495b0SViresh Kumar * 1508c17495b0SViresh Kumar * Also, this should be called before the policy is registered 1509c17495b0SViresh Kumar * with cooling framework. 1510c17495b0SViresh Kumar */ 1511c17495b0SViresh Kumar if (cpufreq_driver->register_em) 1512c17495b0SViresh Kumar cpufreq_driver->register_em(policy); 1513988bed09SViresh Kumar } 15148ff69732SDave Jones 15157f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 15167f0fa40fSViresh Kumar if (ret) { 15177f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 15187f0fa40fSViresh Kumar __func__, cpu, ret); 1519d417e069SViresh Kumar goto out_destroy_policy; 152008fd8c1cSViresh Kumar } 1521e18f1682SSrivatsa S. Bhat 15224e97b631SViresh Kumar up_write(&policy->rwsem); 152308fd8c1cSViresh Kumar 1524038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 15257c45cf31SViresh Kumar 15264f774c4aSBjorn Andersson /* Callback for handling stuff after policy is ready */ 15274f774c4aSBjorn Andersson if (cpufreq_driver->ready) 15284f774c4aSBjorn Andersson cpufreq_driver->ready(policy); 15294f774c4aSBjorn Andersson 1530bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) 15315c238a8bSAmit Kucheria policy->cdev = of_cpufreq_cooling_register(policy); 15325c238a8bSAmit Kucheria 15332d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 15341da177e4SLinus Torvalds 15351da177e4SLinus Torvalds return 0; 15361da177e4SLinus Torvalds 1537d417e069SViresh Kumar out_destroy_policy: 1538b24b6478SViresh Kumar for_each_cpu(j, policy->real_cpus) 15395c84c1b8SViresh Kumar remove_cpu_dev_symlink(policy, j, get_cpu_device(j)); 1540b24b6478SViresh Kumar 1541d4627a28SSchspa Shi cpumask_clear(policy->cpus); 154285f0e42bSViresh Kumar 15433b718057SRafael J. Wysocki out_offline_policy: 15443b718057SRafael J. Wysocki if (cpufreq_driver->offline) 15453b718057SRafael J. Wysocki cpufreq_driver->offline(policy); 15463b718057SRafael J. Wysocki 1547d417e069SViresh Kumar out_exit_policy: 1548da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1549da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 15502f0ba790SRafael J. Wysocki 15518101f997SViresh Kumar out_free_policy: 1552514ff1bcSSchspa Shi up_write(&policy->rwsem); 1553514ff1bcSSchspa Shi 1554f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 15551da177e4SLinus Torvalds return ret; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 15580b275352SRafael J. Wysocki /** 15590b275352SRafael J. Wysocki * cpufreq_add_dev - the cpufreq interface for a CPU device. 15600b275352SRafael J. Wysocki * @dev: CPU device. 15610b275352SRafael J. Wysocki * @sif: Subsystem interface structure pointer (not used) 15620b275352SRafael J. Wysocki */ 15630b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 15640b275352SRafael J. Wysocki { 1565a794d613SRafael J. Wysocki struct cpufreq_policy *policy; 15660b275352SRafael J. Wysocki unsigned cpu = dev->id; 156726619804SViresh Kumar int ret; 15680b275352SRafael J. Wysocki 15690b275352SRafael J. Wysocki dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 15700b275352SRafael J. Wysocki 157126619804SViresh Kumar if (cpu_online(cpu)) { 157226619804SViresh Kumar ret = cpufreq_online(cpu); 157326619804SViresh Kumar if (ret) 157426619804SViresh Kumar return ret; 157526619804SViresh Kumar } 1576a794d613SRafael J. Wysocki 157726619804SViresh Kumar /* Create sysfs link on CPU registration */ 1578a794d613SRafael J. Wysocki policy = per_cpu(cpufreq_cpu_data, cpu); 15792f0ba790SRafael J. Wysocki if (policy) 15802c1b5a84SXiongfeng Wang add_cpu_dev_symlink(policy, cpu, dev); 15812f0ba790SRafael J. Wysocki 1582a794d613SRafael J. Wysocki return 0; 15831da177e4SLinus Torvalds } 15841da177e4SLinus Torvalds 1585fddd8f86SRafael J. Wysocki static void __cpufreq_offline(unsigned int cpu, struct cpufreq_policy *policy) 15861da177e4SLinus Torvalds { 158769cee714SViresh Kumar int ret; 15881da177e4SLinus Torvalds 158945482c70SRafael J. Wysocki if (has_target()) 159045482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 1591084f3493SThomas Renninger 15929591becbSViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 15934573237bSViresh Kumar 15949591becbSViresh Kumar if (!policy_is_inactive(policy)) { 1595e1e962c5SRafael J. Wysocki /* Nominate a new CPU if necessary. */ 1596e1e962c5SRafael J. Wysocki if (cpu == policy->cpu) 1597e1e962c5SRafael J. Wysocki policy->cpu = cpumask_any(policy->cpus); 1598e1e962c5SRafael J. Wysocki 1599e1e962c5SRafael J. Wysocki /* Start the governor again for the active policy. */ 16009591becbSViresh Kumar if (has_target()) { 16010a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 16029591becbSViresh Kumar if (ret) 16039591becbSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 16049591becbSViresh Kumar } 160569cee714SViresh Kumar 1606fddd8f86SRafael J. Wysocki return; 160769cee714SViresh Kumar } 160869cee714SViresh Kumar 1609e1e962c5SRafael J. Wysocki if (has_target()) 1610e1e962c5SRafael J. Wysocki strncpy(policy->last_governor, policy->governor->name, 1611e1e962c5SRafael J. Wysocki CPUFREQ_NAME_LEN); 1612e1e962c5SRafael J. Wysocki else 1613e1e962c5SRafael J. Wysocki policy->last_policy = policy->policy; 1614e1e962c5SRafael J. Wysocki 1615bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) { 16165c238a8bSAmit Kucheria cpufreq_cooling_unregister(policy->cdev); 16175c238a8bSAmit Kucheria policy->cdev = NULL; 16185c238a8bSAmit Kucheria } 16195c238a8bSAmit Kucheria 162036be3418SRafael J. Wysocki if (has_target()) 162136be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 16222a998599SRafael J. Wysocki 16238414809cSSrivatsa S. Bhat /* 162491a12e91SViresh Kumar * Perform the ->offline() during light-weight tear-down, as 162591a12e91SViresh Kumar * that allows fast recovery when the CPU comes back. 16268414809cSSrivatsa S. Bhat */ 162791a12e91SViresh Kumar if (cpufreq_driver->offline) { 162891a12e91SViresh Kumar cpufreq_driver->offline(policy); 162991a12e91SViresh Kumar } else if (cpufreq_driver->exit) { 16303a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 163155582bccSSrinivas Pandruvada policy->freq_table = NULL; 163255582bccSSrinivas Pandruvada } 1633fddd8f86SRafael J. Wysocki } 163449f18560SViresh Kumar 1635fddd8f86SRafael J. Wysocki static int cpufreq_offline(unsigned int cpu) 1636fddd8f86SRafael J. Wysocki { 1637fddd8f86SRafael J. Wysocki struct cpufreq_policy *policy; 1638fddd8f86SRafael J. Wysocki 1639fddd8f86SRafael J. Wysocki pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 1640fddd8f86SRafael J. Wysocki 1641fddd8f86SRafael J. Wysocki policy = cpufreq_cpu_get_raw(cpu); 1642fddd8f86SRafael J. Wysocki if (!policy) { 1643fddd8f86SRafael J. Wysocki pr_debug("%s: No cpu_data found\n", __func__); 1644fddd8f86SRafael J. Wysocki return 0; 1645fddd8f86SRafael J. Wysocki } 1646fddd8f86SRafael J. Wysocki 1647fddd8f86SRafael J. Wysocki down_write(&policy->rwsem); 1648fddd8f86SRafael J. Wysocki 1649fddd8f86SRafael J. Wysocki __cpufreq_offline(cpu, policy); 1650fddd8f86SRafael J. Wysocki 165149f18560SViresh Kumar up_write(&policy->rwsem); 165227622b06SSebastian Andrzej Siewior return 0; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds 1655a9909c21SLee Jones /* 165627a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1657cedb70afSSrivatsa S. Bhat * 1658cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1659cedb70afSSrivatsa S. Bhat */ 166071db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 16615a01f2e8SVenkatesh Pallipadi { 16628a25a2fdSKay Sievers unsigned int cpu = dev->id; 166387549141SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 166487549141SViresh Kumar 166587549141SViresh Kumar if (!policy) 166671db87baSViresh Kumar return; 1667ec28297aSVenki Pallipadi 1668f339f354SRafael J. Wysocki down_write(&policy->rwsem); 1669f339f354SRafael J. Wysocki 167069cee714SViresh Kumar if (cpu_online(cpu)) 1671f339f354SRafael J. Wysocki __cpufreq_offline(cpu, policy); 167287549141SViresh Kumar 16735c84c1b8SViresh Kumar remove_cpu_dev_symlink(policy, cpu, dev); 1674f344dae0SViresh Kumar 1675f339f354SRafael J. Wysocki if (!cpumask_empty(policy->real_cpus)) { 1676f339f354SRafael J. Wysocki up_write(&policy->rwsem); 1677f339f354SRafael J. Wysocki return; 1678f339f354SRafael J. Wysocki } 1679f339f354SRafael J. Wysocki 168091a12e91SViresh Kumar /* We did light-weight exit earlier, do full tear down now */ 168191a12e91SViresh Kumar if (cpufreq_driver->offline) 168291a12e91SViresh Kumar cpufreq_driver->exit(policy); 168391a12e91SViresh Kumar 1684f339f354SRafael J. Wysocki up_write(&policy->rwsem); 1685f339f354SRafael J. Wysocki 1686f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 16875a01f2e8SVenkatesh Pallipadi } 16885a01f2e8SVenkatesh Pallipadi 16891da177e4SLinus Torvalds /** 1690ec06e586SRafael J. Wysocki * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference. 1691ec06e586SRafael J. Wysocki * @policy: Policy managing CPUs. 1692ec06e586SRafael J. Wysocki * @new_freq: New CPU frequency. 16931da177e4SLinus Torvalds * 1694ec06e586SRafael J. Wysocki * Adjust to the current frequency first and clean up later by either calling 1695ec06e586SRafael J. Wysocki * cpufreq_update_policy(), or scheduling handle_update(). 16961da177e4SLinus Torvalds */ 1697a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1698e08f5f5bSGautham R Shenoy unsigned int new_freq) 16991da177e4SLinus Torvalds { 17001da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1701b43a7ffbSViresh Kumar 1702e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1703a1e1dc41SViresh Kumar policy->cur, new_freq); 17041da177e4SLinus Torvalds 1705a1e1dc41SViresh Kumar freqs.old = policy->cur; 17061da177e4SLinus Torvalds freqs.new = new_freq; 1707b43a7ffbSViresh Kumar 17088fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 17098fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 17101da177e4SLinus Torvalds } 17111da177e4SLinus Torvalds 17125980752eSViresh Kumar static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update) 17135980752eSViresh Kumar { 17145980752eSViresh Kumar unsigned int new_freq; 17155980752eSViresh Kumar 17165980752eSViresh Kumar new_freq = cpufreq_driver->get(policy->cpu); 17175980752eSViresh Kumar if (!new_freq) 17185980752eSViresh Kumar return 0; 17195980752eSViresh Kumar 17205980752eSViresh Kumar /* 17215980752eSViresh Kumar * If fast frequency switching is used with the given policy, the check 17225980752eSViresh Kumar * against policy->cur is pointless, so skip it in that case. 17235980752eSViresh Kumar */ 17245980752eSViresh Kumar if (policy->fast_switch_enabled || !has_target()) 17255980752eSViresh Kumar return new_freq; 17265980752eSViresh Kumar 17275980752eSViresh Kumar if (policy->cur != new_freq) { 1728f55ae08cSViresh Kumar /* 1729f55ae08cSViresh Kumar * For some platforms, the frequency returned by hardware may be 1730f55ae08cSViresh Kumar * slightly different from what is provided in the frequency 1731f55ae08cSViresh Kumar * table, for example hardware may return 499 MHz instead of 500 1732f55ae08cSViresh Kumar * MHz. In such cases it is better to avoid getting into 1733f55ae08cSViresh Kumar * unnecessary frequency updates. 1734f55ae08cSViresh Kumar */ 1735f55ae08cSViresh Kumar if (abs(policy->cur - new_freq) < HZ_PER_MHZ) 1736f55ae08cSViresh Kumar return policy->cur; 1737f55ae08cSViresh Kumar 17385980752eSViresh Kumar cpufreq_out_of_sync(policy, new_freq); 17395980752eSViresh Kumar if (update) 17405980752eSViresh Kumar schedule_work(&policy->update); 17415980752eSViresh Kumar } 17425980752eSViresh Kumar 17435980752eSViresh Kumar return new_freq; 17445980752eSViresh Kumar } 17455980752eSViresh Kumar 17461da177e4SLinus Torvalds /** 17474ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 174895235ca2SVenkatesh Pallipadi * @cpu: CPU number 174995235ca2SVenkatesh Pallipadi * 175095235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 175195235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 175295235ca2SVenkatesh Pallipadi */ 175395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 175495235ca2SVenkatesh Pallipadi { 17559e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1756e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 1757c75361c0SRichard Cochran unsigned long flags; 175895235ca2SVenkatesh Pallipadi 1759c75361c0SRichard Cochran read_lock_irqsave(&cpufreq_driver_lock, flags); 1760c75361c0SRichard Cochran 1761c75361c0SRichard Cochran if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { 1762c75361c0SRichard Cochran ret_freq = cpufreq_driver->get(cpu); 1763c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1764c75361c0SRichard Cochran return ret_freq; 1765c75361c0SRichard Cochran } 1766c75361c0SRichard Cochran 1767c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 17689e21ba8bSDirk Brandewie 17699e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 177095235ca2SVenkatesh Pallipadi if (policy) { 1771e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 177295235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 177395235ca2SVenkatesh Pallipadi } 177495235ca2SVenkatesh Pallipadi 17754d34a67dSDave Jones return ret_freq; 177695235ca2SVenkatesh Pallipadi } 177795235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 177895235ca2SVenkatesh Pallipadi 17793d737108SJesse Barnes /** 17803d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 17813d737108SJesse Barnes * @cpu: CPU number 17823d737108SJesse Barnes * 17833d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 17843d737108SJesse Barnes */ 17853d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 17863d737108SJesse Barnes { 17873d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 17883d737108SJesse Barnes unsigned int ret_freq = 0; 17893d737108SJesse Barnes 17903d737108SJesse Barnes if (policy) { 17913d737108SJesse Barnes ret_freq = policy->max; 17923d737108SJesse Barnes cpufreq_cpu_put(policy); 17933d737108SJesse Barnes } 17943d737108SJesse Barnes 17953d737108SJesse Barnes return ret_freq; 17963d737108SJesse Barnes } 17973d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 17983d737108SJesse Barnes 1799bbce8eaaSIonela Voinescu /** 1800bbce8eaaSIonela Voinescu * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU 1801bbce8eaaSIonela Voinescu * @cpu: CPU number 1802bbce8eaaSIonela Voinescu * 1803bbce8eaaSIonela Voinescu * The default return value is the max_freq field of cpuinfo. 1804bbce8eaaSIonela Voinescu */ 1805bbce8eaaSIonela Voinescu __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu) 1806bbce8eaaSIonela Voinescu { 1807bbce8eaaSIonela Voinescu struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1808bbce8eaaSIonela Voinescu unsigned int ret_freq = 0; 1809bbce8eaaSIonela Voinescu 1810bbce8eaaSIonela Voinescu if (policy) { 1811bbce8eaaSIonela Voinescu ret_freq = policy->cpuinfo.max_freq; 1812bbce8eaaSIonela Voinescu cpufreq_cpu_put(policy); 1813bbce8eaaSIonela Voinescu } 1814bbce8eaaSIonela Voinescu 1815bbce8eaaSIonela Voinescu return ret_freq; 1816bbce8eaaSIonela Voinescu } 1817bbce8eaaSIonela Voinescu EXPORT_SYMBOL(cpufreq_get_hw_max_freq); 1818bbce8eaaSIonela Voinescu 1819d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 18201da177e4SLinus Torvalds { 18214db7c34cSYue Hu if (unlikely(policy_is_inactive(policy))) 18225980752eSViresh Kumar return 0; 18231da177e4SLinus Torvalds 18245980752eSViresh Kumar return cpufreq_verify_current_freq(policy, true); 18255a01f2e8SVenkatesh Pallipadi } 18261da177e4SLinus Torvalds 18275a01f2e8SVenkatesh Pallipadi /** 18285a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 18295a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 18305a01f2e8SVenkatesh Pallipadi * 18315a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 18325a01f2e8SVenkatesh Pallipadi */ 18335a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 18345a01f2e8SVenkatesh Pallipadi { 1835999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 18365a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 18375a01f2e8SVenkatesh Pallipadi 1838999976e0SAaron Plattner if (policy) { 1839ad7722daSviresh kumar down_read(&policy->rwsem); 18404db7c34cSYue Hu if (cpufreq_driver->get) 1841d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1842ad7722daSviresh kumar up_read(&policy->rwsem); 1843999976e0SAaron Plattner 1844999976e0SAaron Plattner cpufreq_cpu_put(policy); 1845999976e0SAaron Plattner } 18466eed9404SViresh Kumar 18474d34a67dSDave Jones return ret_freq; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 18501da177e4SLinus Torvalds 18518a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 18528a25a2fdSKay Sievers .name = "cpufreq", 18538a25a2fdSKay Sievers .subsys = &cpu_subsys, 18548a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 18558a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1856e00e56dfSRafael J. Wysocki }; 1857e00e56dfSRafael J. Wysocki 1858e28867eaSViresh Kumar /* 1859e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1860e28867eaSViresh Kumar * during suspend.. 186142d4dc3fSBenjamin Herrenschmidt */ 1862e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 186342d4dc3fSBenjamin Herrenschmidt { 1864e28867eaSViresh Kumar int ret; 18654bc5d341SDave Jones 1866e28867eaSViresh Kumar if (!policy->suspend_freq) { 1867201f3716SBartlomiej Zolnierkiewicz pr_debug("%s: suspend_freq not defined\n", __func__); 1868201f3716SBartlomiej Zolnierkiewicz return 0; 186942d4dc3fSBenjamin Herrenschmidt } 187042d4dc3fSBenjamin Herrenschmidt 1871e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1872e28867eaSViresh Kumar policy->suspend_freq); 1873e28867eaSViresh Kumar 1874e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1875e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1876e28867eaSViresh Kumar if (ret) 1877e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1878e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1879e28867eaSViresh Kumar 1880c9060494SDave Jones return ret; 188142d4dc3fSBenjamin Herrenschmidt } 1882e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 188342d4dc3fSBenjamin Herrenschmidt 188442d4dc3fSBenjamin Herrenschmidt /** 1885ec06e586SRafael J. Wysocki * cpufreq_suspend() - Suspend CPUFreq governors. 18861da177e4SLinus Torvalds * 18872f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 18882f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 18892f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 18902f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 18911da177e4SLinus Torvalds */ 18922f0aea93SViresh Kumar void cpufreq_suspend(void) 18931da177e4SLinus Torvalds { 18943a3e9e06SViresh Kumar struct cpufreq_policy *policy; 18951da177e4SLinus Torvalds 18962f0aea93SViresh Kumar if (!cpufreq_driver) 1897e00e56dfSRafael J. Wysocki return; 18981da177e4SLinus Torvalds 1899ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->suspend) 1900b1b12babSViresh Kumar goto suspend; 19011da177e4SLinus Torvalds 19022f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 19032f0aea93SViresh Kumar 1904f963735aSViresh Kumar for_each_active_policy(policy) { 1905ba41e1bcSRafael J. Wysocki if (has_target()) { 190649f18560SViresh Kumar down_write(&policy->rwsem); 190745482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 190849f18560SViresh Kumar up_write(&policy->rwsem); 1909ba41e1bcSRafael J. Wysocki } 1910ba41e1bcSRafael J. Wysocki 1911ba41e1bcSRafael J. Wysocki if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) 1912e9a7cc1dSFlorian Fainelli pr_err("%s: Failed to suspend driver: %s\n", __func__, 1913e9a7cc1dSFlorian Fainelli cpufreq_driver->name); 19141da177e4SLinus Torvalds } 1915b1b12babSViresh Kumar 1916b1b12babSViresh Kumar suspend: 1917b1b12babSViresh Kumar cpufreq_suspended = true; 19181da177e4SLinus Torvalds } 19191da177e4SLinus Torvalds 19201da177e4SLinus Torvalds /** 1921ec06e586SRafael J. Wysocki * cpufreq_resume() - Resume CPUFreq governors. 19221da177e4SLinus Torvalds * 19232f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 19242f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 19251da177e4SLinus Torvalds */ 19262f0aea93SViresh Kumar void cpufreq_resume(void) 19271da177e4SLinus Torvalds { 19281da177e4SLinus Torvalds struct cpufreq_policy *policy; 192949f18560SViresh Kumar int ret; 19301da177e4SLinus Torvalds 19312f0aea93SViresh Kumar if (!cpufreq_driver) 19321da177e4SLinus Torvalds return; 19331da177e4SLinus Torvalds 1934703cbaa6SBo Yan if (unlikely(!cpufreq_suspended)) 1935703cbaa6SBo Yan return; 1936703cbaa6SBo Yan 19378e30444eSLan Tianyu cpufreq_suspended = false; 19388e30444eSLan Tianyu 1939ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->resume) 19402f0aea93SViresh Kumar return; 19411da177e4SLinus Torvalds 19422f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 19432f0aea93SViresh Kumar 1944f963735aSViresh Kumar for_each_active_policy(policy) { 194549f18560SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 19460c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 19470c5aa405SViresh Kumar policy); 1948ba41e1bcSRafael J. Wysocki } else if (has_target()) { 194949f18560SViresh Kumar down_write(&policy->rwsem); 19500a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 195149f18560SViresh Kumar up_write(&policy->rwsem); 195249f18560SViresh Kumar 195349f18560SViresh Kumar if (ret) 19542f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 19552f0aea93SViresh Kumar __func__, policy); 1956c75de0acSViresh Kumar } 195749f18560SViresh Kumar } 19581da177e4SLinus Torvalds } 19591da177e4SLinus Torvalds 19609d95046eSBorislav Petkov /** 1961a62f68f5SRafael J. Wysocki * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones. 1962a62f68f5SRafael J. Wysocki * @flags: Flags to test against the current cpufreq driver's flags. 1963a62f68f5SRafael J. Wysocki * 1964a62f68f5SRafael J. Wysocki * Assumes that the driver is there, so callers must ensure that this is the 1965a62f68f5SRafael J. Wysocki * case. 1966a62f68f5SRafael J. Wysocki */ 1967a62f68f5SRafael J. Wysocki bool cpufreq_driver_test_flags(u16 flags) 1968a62f68f5SRafael J. Wysocki { 1969a62f68f5SRafael J. Wysocki return !!(cpufreq_driver->flags & flags); 1970a62f68f5SRafael J. Wysocki } 1971a62f68f5SRafael J. Wysocki 1972a62f68f5SRafael J. Wysocki /** 1973ec06e586SRafael J. Wysocki * cpufreq_get_current_driver - Return the current driver's name. 19749d95046eSBorislav Petkov * 1975ec06e586SRafael J. Wysocki * Return the name string of the currently registered cpufreq driver or NULL if 1976ec06e586SRafael J. Wysocki * none. 19779d95046eSBorislav Petkov */ 19789d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 19799d95046eSBorislav Petkov { 19801c3d85ddSRafael J. Wysocki if (cpufreq_driver) 19811c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 19821c3d85ddSRafael J. Wysocki 19831c3d85ddSRafael J. Wysocki return NULL; 19849d95046eSBorislav Petkov } 19859d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 19861da177e4SLinus Torvalds 198751315cdfSThomas Petazzoni /** 1988ec06e586SRafael J. Wysocki * cpufreq_get_driver_data - Return current driver data. 198951315cdfSThomas Petazzoni * 1990ec06e586SRafael J. Wysocki * Return the private data of the currently registered cpufreq driver, or NULL 1991ec06e586SRafael J. Wysocki * if no cpufreq driver has been registered. 199251315cdfSThomas Petazzoni */ 199351315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 199451315cdfSThomas Petazzoni { 199551315cdfSThomas Petazzoni if (cpufreq_driver) 199651315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 199751315cdfSThomas Petazzoni 199851315cdfSThomas Petazzoni return NULL; 199951315cdfSThomas Petazzoni } 200051315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 200151315cdfSThomas Petazzoni 20021da177e4SLinus Torvalds /********************************************************************* 20031da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 20041da177e4SLinus Torvalds *********************************************************************/ 20051da177e4SLinus Torvalds 20061da177e4SLinus Torvalds /** 2007ec06e586SRafael J. Wysocki * cpufreq_register_notifier - Register a notifier with cpufreq. 2008ec06e586SRafael J. Wysocki * @nb: notifier function to register. 2009ec06e586SRafael J. Wysocki * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER. 20101da177e4SLinus Torvalds * 2011ec06e586SRafael J. Wysocki * Add a notifier to one of two lists: either a list of notifiers that run on 2012ec06e586SRafael J. Wysocki * clock rate changes (once before and once after every transition), or a list 2013ec06e586SRafael J. Wysocki * of notifiers that ron on cpufreq policy changes. 20141da177e4SLinus Torvalds * 2015ec06e586SRafael J. Wysocki * This function may sleep and it has the same return values as 2016ec06e586SRafael J. Wysocki * blocking_notifier_chain_register(). 20171da177e4SLinus Torvalds */ 20181da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds int ret; 20211da177e4SLinus Torvalds 2022d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 2023d5aaffa9SDirk Brandewie return -EINVAL; 2024d5aaffa9SDirk Brandewie 20251da177e4SLinus Torvalds switch (list) { 20261da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 2027b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 2028b7898fdaSRafael J. Wysocki 2029b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count > 0) { 2030b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 2031b7898fdaSRafael J. Wysocki return -EBUSY; 2032b7898fdaSRafael J. Wysocki } 2033b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 2034e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 2035b7898fdaSRafael J. Wysocki if (!ret) 2036b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 2037b7898fdaSRafael J. Wysocki 2038b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 20391da177e4SLinus Torvalds break; 20401da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 2041e041c683SAlan Stern ret = blocking_notifier_chain_register( 2042e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 20431da177e4SLinus Torvalds break; 20441da177e4SLinus Torvalds default: 20451da177e4SLinus Torvalds ret = -EINVAL; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds return ret; 20491da177e4SLinus Torvalds } 20501da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 20511da177e4SLinus Torvalds 20521da177e4SLinus Torvalds /** 2053ec06e586SRafael J. Wysocki * cpufreq_unregister_notifier - Unregister a notifier from cpufreq. 2054ec06e586SRafael J. Wysocki * @nb: notifier block to be unregistered. 2055ec06e586SRafael J. Wysocki * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER. 20561da177e4SLinus Torvalds * 2057ec06e586SRafael J. Wysocki * Remove a notifier from one of the cpufreq notifier lists. 20581da177e4SLinus Torvalds * 2059ec06e586SRafael J. Wysocki * This function may sleep and it has the same return values as 2060ec06e586SRafael J. Wysocki * blocking_notifier_chain_unregister(). 20611da177e4SLinus Torvalds */ 20621da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 20631da177e4SLinus Torvalds { 20641da177e4SLinus Torvalds int ret; 20651da177e4SLinus Torvalds 2066d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 2067d5aaffa9SDirk Brandewie return -EINVAL; 2068d5aaffa9SDirk Brandewie 20691da177e4SLinus Torvalds switch (list) { 20701da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 2071b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 2072b7898fdaSRafael J. Wysocki 2073b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 2074e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 2075b7898fdaSRafael J. Wysocki if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) 2076b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 2077b7898fdaSRafael J. Wysocki 2078b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 20791da177e4SLinus Torvalds break; 20801da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 2081e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 2082e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 20831da177e4SLinus Torvalds break; 20841da177e4SLinus Torvalds default: 20851da177e4SLinus Torvalds ret = -EINVAL; 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 20881da177e4SLinus Torvalds return ret; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 20911da177e4SLinus Torvalds 20921da177e4SLinus Torvalds 20931da177e4SLinus Torvalds /********************************************************************* 20941da177e4SLinus Torvalds * GOVERNORS * 20951da177e4SLinus Torvalds *********************************************************************/ 20961da177e4SLinus Torvalds 2097b7898fdaSRafael J. Wysocki /** 2098b7898fdaSRafael J. Wysocki * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. 2099b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to switch the frequency for. 2100b7898fdaSRafael J. Wysocki * @target_freq: New frequency to set (may be approximate). 2101b7898fdaSRafael J. Wysocki * 2102b7898fdaSRafael J. Wysocki * Carry out a fast frequency switch without sleeping. 2103b7898fdaSRafael J. Wysocki * 2104b7898fdaSRafael J. Wysocki * The driver's ->fast_switch() callback invoked by this function must be 2105b7898fdaSRafael J. Wysocki * suitable for being called from within RCU-sched read-side critical sections 2106b7898fdaSRafael J. Wysocki * and it is expected to select the minimum available frequency greater than or 2107b7898fdaSRafael J. Wysocki * equal to @target_freq (CPUFREQ_RELATION_L). 2108b7898fdaSRafael J. Wysocki * 2109b7898fdaSRafael J. Wysocki * This function must not be called if policy->fast_switch_enabled is unset. 2110b7898fdaSRafael J. Wysocki * 2111b7898fdaSRafael J. Wysocki * Governors calling this function must guarantee that it will never be invoked 2112b7898fdaSRafael J. Wysocki * twice in parallel for the same policy and that it will never be called in 2113b7898fdaSRafael J. Wysocki * parallel with either ->target() or ->target_index() for the same policy. 2114b7898fdaSRafael J. Wysocki * 2115209887e6SViresh Kumar * Returns the actual frequency set for the CPU. 2116209887e6SViresh Kumar * 2117209887e6SViresh Kumar * If 0 is returned by the driver's ->fast_switch() callback to indicate an 2118209887e6SViresh Kumar * error condition, the hardware configuration must be preserved. 2119b7898fdaSRafael J. Wysocki */ 2120b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, 2121b7898fdaSRafael J. Wysocki unsigned int target_freq) 2122b7898fdaSRafael J. Wysocki { 21231a0419b0SIonela Voinescu unsigned int freq; 212408d8c65eSViresh Kumar int cpu; 2125b7898fdaSRafael J. Wysocki 21261a0419b0SIonela Voinescu target_freq = clamp_val(target_freq, policy->min, policy->max); 21271a0419b0SIonela Voinescu freq = cpufreq_driver->fast_switch(policy, target_freq); 21281a0419b0SIonela Voinescu 212996f60cddSViresh Kumar if (!freq) 213096f60cddSViresh Kumar return 0; 213196f60cddSViresh Kumar 213208d8c65eSViresh Kumar policy->cur = freq; 21331a0419b0SIonela Voinescu arch_set_freq_scale(policy->related_cpus, freq, 21341a0419b0SIonela Voinescu policy->cpuinfo.max_freq); 213596f60cddSViresh Kumar cpufreq_stats_record_transition(policy, freq); 21361a0419b0SIonela Voinescu 213708d8c65eSViresh Kumar if (trace_cpu_frequency_enabled()) { 213808d8c65eSViresh Kumar for_each_cpu(cpu, policy->cpus) 213908d8c65eSViresh Kumar trace_cpu_frequency(freq, cpu); 214008d8c65eSViresh Kumar } 214108d8c65eSViresh Kumar 21421a0419b0SIonela Voinescu return freq; 2143b7898fdaSRafael J. Wysocki } 2144b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); 2145b7898fdaSRafael J. Wysocki 2146ee2cc427SRafael J. Wysocki /** 2147ee2cc427SRafael J. Wysocki * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go. 2148ee2cc427SRafael J. Wysocki * @cpu: Target CPU. 2149ee2cc427SRafael J. Wysocki * @min_perf: Minimum (required) performance level (units of @capacity). 21504e6df217SYue Hu * @target_perf: Target (desired) performance level (units of @capacity). 2151ee2cc427SRafael J. Wysocki * @capacity: Capacity of the target CPU. 2152ee2cc427SRafael J. Wysocki * 2153ee2cc427SRafael J. Wysocki * Carry out a fast performance level switch of @cpu without sleeping. 2154ee2cc427SRafael J. Wysocki * 2155ee2cc427SRafael J. Wysocki * The driver's ->adjust_perf() callback invoked by this function must be 2156ee2cc427SRafael J. Wysocki * suitable for being called from within RCU-sched read-side critical sections 2157ee2cc427SRafael J. Wysocki * and it is expected to select a suitable performance level equal to or above 2158ee2cc427SRafael J. Wysocki * @min_perf and preferably equal to or below @target_perf. 2159ee2cc427SRafael J. Wysocki * 2160ee2cc427SRafael J. Wysocki * This function must not be called if policy->fast_switch_enabled is unset. 2161ee2cc427SRafael J. Wysocki * 2162ee2cc427SRafael J. Wysocki * Governors calling this function must guarantee that it will never be invoked 2163ee2cc427SRafael J. Wysocki * twice in parallel for the same CPU and that it will never be called in 2164ee2cc427SRafael J. Wysocki * parallel with either ->target() or ->target_index() or ->fast_switch() for 2165ee2cc427SRafael J. Wysocki * the same CPU. 2166ee2cc427SRafael J. Wysocki */ 2167ee2cc427SRafael J. Wysocki void cpufreq_driver_adjust_perf(unsigned int cpu, 2168ee2cc427SRafael J. Wysocki unsigned long min_perf, 2169ee2cc427SRafael J. Wysocki unsigned long target_perf, 2170ee2cc427SRafael J. Wysocki unsigned long capacity) 2171ee2cc427SRafael J. Wysocki { 2172ee2cc427SRafael J. Wysocki cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity); 2173ee2cc427SRafael J. Wysocki } 2174ee2cc427SRafael J. Wysocki 2175ee2cc427SRafael J. Wysocki /** 2176ee2cc427SRafael J. Wysocki * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback. 2177ee2cc427SRafael J. Wysocki * 2178ee2cc427SRafael J. Wysocki * Return 'true' if the ->adjust_perf callback is present for the 2179ee2cc427SRafael J. Wysocki * current driver or 'false' otherwise. 2180ee2cc427SRafael J. Wysocki */ 2181ee2cc427SRafael J. Wysocki bool cpufreq_driver_has_adjust_perf(void) 2182ee2cc427SRafael J. Wysocki { 2183ee2cc427SRafael J. Wysocki return !!cpufreq_driver->adjust_perf; 2184ee2cc427SRafael J. Wysocki } 2185ee2cc427SRafael J. Wysocki 21861c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 21871c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 21881c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 21891c03a2d0SViresh Kumar { 21901c03a2d0SViresh Kumar int ret; 21911c03a2d0SViresh Kumar 21921c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 21931c03a2d0SViresh Kumar 21941c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 21951c03a2d0SViresh Kumar if (!freqs->new) 21961c03a2d0SViresh Kumar return 0; 21971c03a2d0SViresh Kumar 21981c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 21991c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 22001c03a2d0SViresh Kumar 22011c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 22021c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 22031c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 22041c03a2d0SViresh Kumar 22051c03a2d0SViresh Kumar if (ret) 22061c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 22071c03a2d0SViresh Kumar __func__, ret); 22081c03a2d0SViresh Kumar 22091c03a2d0SViresh Kumar return ret; 22101c03a2d0SViresh Kumar } 22111c03a2d0SViresh Kumar 221223727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index) 22138d65775dSViresh Kumar { 22141c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 221556a7ff75SRafael J. Wysocki unsigned int restore_freq, intermediate_freq = 0; 221623727845SViresh Kumar unsigned int newfreq = policy->freq_table[index].frequency; 22178d65775dSViresh Kumar int retval = -EINVAL; 22188d65775dSViresh Kumar bool notify; 22198d65775dSViresh Kumar 222023727845SViresh Kumar if (newfreq == policy->cur) 222123727845SViresh Kumar return 0; 222223727845SViresh Kumar 222356a7ff75SRafael J. Wysocki /* Save last value to restore later on errors */ 222456a7ff75SRafael J. Wysocki restore_freq = policy->cur; 222556a7ff75SRafael J. Wysocki 22268d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 22278d65775dSViresh Kumar if (notify) { 22281c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 22291c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 22301c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 22311c03a2d0SViresh Kumar if (retval) 22321c03a2d0SViresh Kumar return retval; 22338d65775dSViresh Kumar 22341c03a2d0SViresh Kumar intermediate_freq = freqs.new; 22351c03a2d0SViresh Kumar /* Set old freq to intermediate */ 22361c03a2d0SViresh Kumar if (intermediate_freq) 22371c03a2d0SViresh Kumar freqs.old = freqs.new; 22381c03a2d0SViresh Kumar } 22391c03a2d0SViresh Kumar 224023727845SViresh Kumar freqs.new = newfreq; 22418d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 22428d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 22438d65775dSViresh Kumar 22448d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 22458d65775dSViresh Kumar } 22468d65775dSViresh Kumar 22478d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 22488d65775dSViresh Kumar if (retval) 22498d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 22508d65775dSViresh Kumar retval); 22518d65775dSViresh Kumar 22521c03a2d0SViresh Kumar if (notify) { 22538d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 22548d65775dSViresh Kumar 22551c03a2d0SViresh Kumar /* 22561c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 22571c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 22581c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 225958405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 22601c03a2d0SViresh Kumar */ 22611c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 22621c03a2d0SViresh Kumar freqs.old = intermediate_freq; 226356a7ff75SRafael J. Wysocki freqs.new = restore_freq; 22641c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 22651c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 22661c03a2d0SViresh Kumar } 22671c03a2d0SViresh Kumar } 22681c03a2d0SViresh Kumar 22698d65775dSViresh Kumar return retval; 22708d65775dSViresh Kumar } 22718d65775dSViresh Kumar 22721da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 22731da177e4SLinus Torvalds unsigned int target_freq, 22741da177e4SLinus Torvalds unsigned int relation) 22751da177e4SLinus Torvalds { 22767249924eSViresh Kumar unsigned int old_target_freq = target_freq; 2277c32b6b8eSAshok Raj 2278a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2279a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2280a7b422cdSKonrad Rzeszutek Wilk 2281f9ccdec2SViresh Kumar target_freq = __resolve_freq(policy, target_freq, relation); 22827249924eSViresh Kumar 22837249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 22847249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 22855a1c0228SViresh Kumar 22869c0ebcf7SViresh Kumar /* 22879c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 22889c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 22899c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 22909c0ebcf7SViresh Kumar * calls. 22919c0ebcf7SViresh Kumar */ 22921c534352SRafael J. Wysocki if (target_freq == policy->cur && 22931c534352SRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS)) 22945a1c0228SViresh Kumar return 0; 22955a1c0228SViresh Kumar 22961f39fa0dSVincent Donnefort if (cpufreq_driver->target) { 22971f39fa0dSVincent Donnefort /* 22981f39fa0dSVincent Donnefort * If the driver hasn't setup a single inefficient frequency, 22991f39fa0dSVincent Donnefort * it's unlikely it knows how to decode CPUFREQ_RELATION_E. 23001f39fa0dSVincent Donnefort */ 23011f39fa0dSVincent Donnefort if (!policy->efficiencies_available) 23021f39fa0dSVincent Donnefort relation &= ~CPUFREQ_RELATION_E; 23031f39fa0dSVincent Donnefort 23046019d23aSRafael J. Wysocki return cpufreq_driver->target(policy, target_freq, relation); 23051f39fa0dSVincent Donnefort } 23066019d23aSRafael J. Wysocki 23076019d23aSRafael J. Wysocki if (!cpufreq_driver->target_index) 23086019d23aSRafael J. Wysocki return -EINVAL; 230990d45d17SAshok Raj 2310f9ccdec2SViresh Kumar return __target_index(policy, policy->cached_resolved_idx); 23116019d23aSRafael J. Wysocki } 23121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 23131da177e4SLinus Torvalds 23141da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 23151da177e4SLinus Torvalds unsigned int target_freq, 23161da177e4SLinus Torvalds unsigned int relation) 23171da177e4SLinus Torvalds { 231862c23a89SColin Ian King int ret; 23191da177e4SLinus Torvalds 2320ad7722daSviresh kumar down_write(&policy->rwsem); 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 23231da177e4SLinus Torvalds 2324ad7722daSviresh kumar up_write(&policy->rwsem); 23251da177e4SLinus Torvalds 23261da177e4SLinus Torvalds return ret; 23271da177e4SLinus Torvalds } 23281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 23291da177e4SLinus Torvalds 2330de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 2331de1df26bSRafael J. Wysocki { 2332de1df26bSRafael J. Wysocki return NULL; 2333de1df26bSRafael J. Wysocki } 2334de1df26bSRafael J. Wysocki 2335a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy) 23361da177e4SLinus Torvalds { 2337cc993cabSDave Jones int ret; 23386afde10cSThomas Renninger 23392f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 23402f0aea93SViresh Kumar if (cpufreq_suspended) 23412f0aea93SViresh Kumar return 0; 2342cb57720bSEthan Zhao /* 2343cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 2344cb57720bSEthan Zhao * notification happened, so check it. 2345cb57720bSEthan Zhao */ 2346cb57720bSEthan Zhao if (!policy->governor) 2347cb57720bSEthan Zhao return -EINVAL; 23482f0aea93SViresh Kumar 2349ed4676e2SViresh Kumar /* Platform doesn't want dynamic frequency switching ? */ 23509a2a9ebcSRafael J. Wysocki if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING && 2351fc4c709fSViresh Kumar cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { 2352de1df26bSRafael J. Wysocki struct cpufreq_governor *gov = cpufreq_fallback_governor(); 2353de1df26bSRafael J. Wysocki 2354de1df26bSRafael J. Wysocki if (gov) { 2355fe829ed8SViresh Kumar pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", 2356e837f9b5SJoe Perches policy->governor->name, gov->name); 23571c256245SThomas Renninger policy->governor = gov; 2358de1df26bSRafael J. Wysocki } else { 2359de1df26bSRafael J. Wysocki return -EINVAL; 23601c256245SThomas Renninger } 23616afde10cSThomas Renninger } 23621da177e4SLinus Torvalds 23631da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 23641da177e4SLinus Torvalds return -EINVAL; 23651da177e4SLinus Torvalds 2366a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 236795731ebbSXiaoguang Chen 2368e788892bSRafael J. Wysocki if (policy->governor->init) { 2369e788892bSRafael J. Wysocki ret = policy->governor->init(policy); 2370a92604b4SRafael J. Wysocki if (ret) { 23711da177e4SLinus Torvalds module_put(policy->governor->owner); 2372a92604b4SRafael J. Wysocki return ret; 23739e8c0a89SRafael J. Wysocki } 237436be3418SRafael J. Wysocki } 23751da177e4SLinus Torvalds 2376ea9364bbSRafael J. Wysocki policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET); 2377ea9364bbSRafael J. Wysocki 2378a92604b4SRafael J. Wysocki return 0; 2379a92604b4SRafael J. Wysocki } 2380a92604b4SRafael J. Wysocki 2381a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy) 2382a92604b4SRafael J. Wysocki { 2383a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2384a92604b4SRafael J. Wysocki return; 2385a92604b4SRafael J. Wysocki 2386a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2387a92604b4SRafael J. Wysocki 2388e788892bSRafael J. Wysocki if (policy->governor->exit) 2389e788892bSRafael J. Wysocki policy->governor->exit(policy); 2390a92604b4SRafael J. Wysocki 23911da177e4SLinus Torvalds module_put(policy->governor->owner); 23921da177e4SLinus Torvalds } 23931da177e4SLinus Torvalds 2394f6ebbcf0SRafael J. Wysocki int cpufreq_start_governor(struct cpufreq_policy *policy) 23950a300767SRafael J. Wysocki { 23960a300767SRafael J. Wysocki int ret; 23970a300767SRafael J. Wysocki 2398a92604b4SRafael J. Wysocki if (cpufreq_suspended) 2399a92604b4SRafael J. Wysocki return 0; 2400a92604b4SRafael J. Wysocki 2401a92604b4SRafael J. Wysocki if (!policy->governor) 2402a92604b4SRafael J. Wysocki return -EINVAL; 2403a92604b4SRafael J. Wysocki 2404a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2405a92604b4SRafael J. Wysocki 2406407d0fffSViresh Kumar if (cpufreq_driver->get) 24075980752eSViresh Kumar cpufreq_verify_current_freq(policy, false); 24083bbf8fe3SRafael J. Wysocki 2409e788892bSRafael J. Wysocki if (policy->governor->start) { 2410e788892bSRafael J. Wysocki ret = policy->governor->start(policy); 2411d6ff44d6SRafael J. Wysocki if (ret) 2412d6ff44d6SRafael J. Wysocki return ret; 2413e788892bSRafael J. Wysocki } 2414d6ff44d6SRafael J. Wysocki 2415e788892bSRafael J. Wysocki if (policy->governor->limits) 2416e788892bSRafael J. Wysocki policy->governor->limits(policy); 2417e788892bSRafael J. Wysocki 2418d6ff44d6SRafael J. Wysocki return 0; 24190a300767SRafael J. Wysocki } 24200a300767SRafael J. Wysocki 2421f6ebbcf0SRafael J. Wysocki void cpufreq_stop_governor(struct cpufreq_policy *policy) 2422a92604b4SRafael J. Wysocki { 2423a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2424a92604b4SRafael J. Wysocki return; 2425a92604b4SRafael J. Wysocki 2426a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2427a92604b4SRafael J. Wysocki 2428e788892bSRafael J. Wysocki if (policy->governor->stop) 2429e788892bSRafael J. Wysocki policy->governor->stop(policy); 2430a92604b4SRafael J. Wysocki } 2431a92604b4SRafael J. Wysocki 2432a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy) 2433a92604b4SRafael J. Wysocki { 2434a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2435a92604b4SRafael J. Wysocki return; 2436a92604b4SRafael J. Wysocki 2437a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2438a92604b4SRafael J. Wysocki 2439e788892bSRafael J. Wysocki if (policy->governor->limits) 2440e788892bSRafael J. Wysocki policy->governor->limits(policy); 24411da177e4SLinus Torvalds } 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 24441da177e4SLinus Torvalds { 24453bcb09a3SJeremy Fitzhardinge int err; 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds if (!governor) 24481da177e4SLinus Torvalds return -EINVAL; 24491da177e4SLinus Torvalds 2450a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2451a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2452a7b422cdSKonrad Rzeszutek Wilk 24533fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 24541da177e4SLinus Torvalds 24553bcb09a3SJeremy Fitzhardinge err = -EBUSY; 245642f91fa1SViresh Kumar if (!find_governor(governor->name)) { 24573bcb09a3SJeremy Fitzhardinge err = 0; 24581da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 24593bcb09a3SJeremy Fitzhardinge } 24601da177e4SLinus Torvalds 24613fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 24623bcb09a3SJeremy Fitzhardinge return err; 24631da177e4SLinus Torvalds } 24641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 24651da177e4SLinus Torvalds 24661da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 24671da177e4SLinus Torvalds { 24684573237bSViresh Kumar struct cpufreq_policy *policy; 24694573237bSViresh Kumar unsigned long flags; 247090e41bacSPrarit Bhargava 24711da177e4SLinus Torvalds if (!governor) 24721da177e4SLinus Torvalds return; 24731da177e4SLinus Torvalds 2474a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2475a7b422cdSKonrad Rzeszutek Wilk return; 2476a7b422cdSKonrad Rzeszutek Wilk 24774573237bSViresh Kumar /* clear last_governor for all inactive policies */ 24784573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 24794573237bSViresh Kumar for_each_inactive_policy(policy) { 248018bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 248118bf3a12SViresh Kumar policy->governor = NULL; 24824573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 248390e41bacSPrarit Bhargava } 248418bf3a12SViresh Kumar } 24854573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 248690e41bacSPrarit Bhargava 24873fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 24881da177e4SLinus Torvalds list_del(&governor->governor_list); 24893fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 24901da177e4SLinus Torvalds } 24911da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 24921da177e4SLinus Torvalds 24931da177e4SLinus Torvalds 24941da177e4SLinus Torvalds /********************************************************************* 24951da177e4SLinus Torvalds * POLICY INTERFACE * 24961da177e4SLinus Torvalds *********************************************************************/ 24971da177e4SLinus Torvalds 24981da177e4SLinus Torvalds /** 24991da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 250029464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 250129464f28SDave Jones * is written 2502a9909c21SLee Jones * @cpu: CPU to find the policy for 25031da177e4SLinus Torvalds * 25041da177e4SLinus Torvalds * Reads the current cpufreq policy. 25051da177e4SLinus Torvalds */ 25061da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 25091da177e4SLinus Torvalds if (!policy) 25101da177e4SLinus Torvalds return -EINVAL; 25111da177e4SLinus Torvalds 25121da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 25131da177e4SLinus Torvalds if (!cpu_policy) 25141da177e4SLinus Torvalds return -EINVAL; 25151da177e4SLinus Torvalds 2516d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 25171da177e4SLinus Torvalds 25181da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 25191da177e4SLinus Torvalds return 0; 25201da177e4SLinus Torvalds } 25211da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 25221da177e4SLinus Torvalds 2523a0dbb819SRafael J. Wysocki /** 2524a0dbb819SRafael J. Wysocki * cpufreq_set_policy - Modify cpufreq policy parameters. 2525a0dbb819SRafael J. Wysocki * @policy: Policy object to modify. 25261e4f63aeSRafael J. Wysocki * @new_gov: Policy governor pointer. 25271e4f63aeSRafael J. Wysocki * @new_pol: Policy value (for drivers with built-in governors). 2528a0dbb819SRafael J. Wysocki * 25291e4f63aeSRafael J. Wysocki * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency 25301e4f63aeSRafael J. Wysocki * limits to be set for the policy, update @policy with the verified limits 25311e4f63aeSRafael J. Wysocki * values and either invoke the driver's ->setpolicy() callback (if present) or 25321e4f63aeSRafael J. Wysocki * carry out a governor update for @policy. That is, run the current governor's 25331e4f63aeSRafael J. Wysocki * ->limits() callback (if @new_gov points to the same object as the one in 25341e4f63aeSRafael J. Wysocki * @policy) or replace the governor for @policy with @new_gov. 2535a0dbb819SRafael J. Wysocki * 2536a0dbb819SRafael J. Wysocki * The cpuinfo part of @policy is not updated by this function. 2537153d7f3fSArjan van de Ven */ 25381e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy, 25391e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov, 25401e4f63aeSRafael J. Wysocki unsigned int new_pol) 25411da177e4SLinus Torvalds { 25421e4f63aeSRafael J. Wysocki struct cpufreq_policy_data new_data; 2543d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2544d9a789c7SRafael J. Wysocki int ret; 25451da177e4SLinus Torvalds 25461e4f63aeSRafael J. Wysocki memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 25471e4f63aeSRafael J. Wysocki new_data.freq_table = policy->freq_table; 25481e4f63aeSRafael J. Wysocki new_data.cpu = policy->cpu; 2549fba9573bSPan Xinhui /* 255067d874c3SViresh Kumar * PM QoS framework collects all the requests from users and provide us 255167d874c3SViresh Kumar * the final aggregated value here. 2552fba9573bSPan Xinhui */ 25531e4f63aeSRafael J. Wysocki new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); 25541e4f63aeSRafael J. Wysocki new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX); 25551e4f63aeSRafael J. Wysocki 25561e4f63aeSRafael J. Wysocki pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 25571e4f63aeSRafael J. Wysocki new_data.cpu, new_data.min, new_data.max); 25589c9a43edSMattia Dongili 2559737ffb27SViresh Kumar /* 2560737ffb27SViresh Kumar * Verify that the CPU speed can be set within these limits and make sure 2561737ffb27SViresh Kumar * that min <= max. 2562737ffb27SViresh Kumar */ 25631e4f63aeSRafael J. Wysocki ret = cpufreq_driver->verify(&new_data); 25641da177e4SLinus Torvalds if (ret) 2565d9a789c7SRafael J. Wysocki return ret; 25661da177e4SLinus Torvalds 256715171769SVincent Donnefort /* 256815171769SVincent Donnefort * Resolve policy min/max to available frequencies. It ensures 256915171769SVincent Donnefort * no frequency resolution will neither overshoot the requested maximum 257015171769SVincent Donnefort * nor undershoot the requested minimum. 257115171769SVincent Donnefort */ 25721e4f63aeSRafael J. Wysocki policy->min = new_data.min; 25731e4f63aeSRafael J. Wysocki policy->max = new_data.max; 257415171769SVincent Donnefort policy->min = __resolve_freq(policy, policy->min, CPUFREQ_RELATION_L); 257515171769SVincent Donnefort policy->max = __resolve_freq(policy, policy->max, CPUFREQ_RELATION_H); 2576601b2185SRuchi Kandoi trace_cpu_frequency_limits(policy); 25771da177e4SLinus Torvalds 2578e3c06236SSteve Muckle policy->cached_target_freq = UINT_MAX; 2579e3c06236SSteve Muckle 25802d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 25813a3e9e06SViresh Kumar policy->min, policy->max); 25821da177e4SLinus Torvalds 25831c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 25841e4f63aeSRafael J. Wysocki policy->policy = new_pol; 25852d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2586167a38dcSRafael J. Wysocki return cpufreq_driver->setpolicy(policy); 2587d9a789c7SRafael J. Wysocki } 2588d9a789c7SRafael J. Wysocki 25891e4f63aeSRafael J. Wysocki if (new_gov == policy->governor) { 25902bb4059eSRafael J. Wysocki pr_debug("governor limits update\n"); 2591a92604b4SRafael J. Wysocki cpufreq_governor_limits(policy); 2592d6ff44d6SRafael J. Wysocki return 0; 25930a300767SRafael J. Wysocki } 25941da177e4SLinus Torvalds 25952d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 25961da177e4SLinus Torvalds 2597d9a789c7SRafael J. Wysocki /* save old, working values */ 2598d9a789c7SRafael J. Wysocki old_gov = policy->governor; 25991da177e4SLinus Torvalds /* end old governor */ 2600d9a789c7SRafael J. Wysocki if (old_gov) { 260145482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 260236be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 26037bd353a9SViresh Kumar } 26041da177e4SLinus Torvalds 26051da177e4SLinus Torvalds /* start new governor */ 26061e4f63aeSRafael J. Wysocki policy->governor = new_gov; 2607a92604b4SRafael J. Wysocki ret = cpufreq_init_governor(policy); 26084bc384aeSViresh Kumar if (!ret) { 26090a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 26100a300767SRafael J. Wysocki if (!ret) { 26112bb4059eSRafael J. Wysocki pr_debug("governor change\n"); 2612531b5c9fSQuentin Perret sched_cpufreq_governor_change(policy, old_gov); 26130a300767SRafael J. Wysocki return 0; 26140a300767SRafael J. Wysocki } 2615b7898fdaSRafael J. Wysocki cpufreq_exit_governor(policy); 2616955ef483SViresh Kumar } 26177bd353a9SViresh Kumar 26181da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2619d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 26201da177e4SLinus Torvalds if (old_gov) { 26213a3e9e06SViresh Kumar policy->governor = old_gov; 2622a92604b4SRafael J. Wysocki if (cpufreq_init_governor(policy)) 26234bc384aeSViresh Kumar policy->governor = NULL; 26244bc384aeSViresh Kumar else 26250a300767SRafael J. Wysocki cpufreq_start_governor(policy); 26261da177e4SLinus Torvalds } 26271da177e4SLinus Torvalds 26284bc384aeSViresh Kumar return ret; 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds /** 2632a0dbb819SRafael J. Wysocki * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. 2633a0dbb819SRafael J. Wysocki * @cpu: CPU to re-evaluate the policy for. 26341da177e4SLinus Torvalds * 2635a0dbb819SRafael J. Wysocki * Update the current frequency for the cpufreq policy of @cpu and use 263618c49926SViresh Kumar * cpufreq_set_policy() to re-apply the min and max limits, which triggers the 263718c49926SViresh Kumar * evaluation of policy notifiers and the cpufreq driver's ->verify() callback 263818c49926SViresh Kumar * for the policy in question, among other things. 26391da177e4SLinus Torvalds */ 264030248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu) 26411da177e4SLinus Torvalds { 2642540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); 26431da177e4SLinus Torvalds 2644fefa8ff8SAaron Plattner if (!policy) 264530248fefSRafael J. Wysocki return; 26461da177e4SLinus Torvalds 2647bb176f7dSViresh Kumar /* 2648bb176f7dSViresh Kumar * BIOS might change freq behind our back 2649bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2650bb176f7dSViresh Kumar */ 26515ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target() && 26525980752eSViresh Kumar (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false)))) 2653742c87bfSRafael J. Wysocki goto unlock; 265430248fefSRafael J. Wysocki 265570a59fdeSViresh Kumar refresh_frequency_limits(policy); 26561da177e4SLinus Torvalds 2657fefa8ff8SAaron Plattner unlock: 2658540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 26591da177e4SLinus Torvalds } 26601da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 26611da177e4SLinus Torvalds 26625a25e3f7SRafael J. Wysocki /** 26635a25e3f7SRafael J. Wysocki * cpufreq_update_limits - Update policy limits for a given CPU. 26645a25e3f7SRafael J. Wysocki * @cpu: CPU to update the policy limits for. 26655a25e3f7SRafael J. Wysocki * 26665a25e3f7SRafael J. Wysocki * Invoke the driver's ->update_limits callback if present or call 26675a25e3f7SRafael J. Wysocki * cpufreq_update_policy() for @cpu. 26685a25e3f7SRafael J. Wysocki */ 26695a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu) 26705a25e3f7SRafael J. Wysocki { 26715a25e3f7SRafael J. Wysocki if (cpufreq_driver->update_limits) 26725a25e3f7SRafael J. Wysocki cpufreq_driver->update_limits(cpu); 26735a25e3f7SRafael J. Wysocki else 26745a25e3f7SRafael J. Wysocki cpufreq_update_policy(cpu); 26755a25e3f7SRafael J. Wysocki } 26765a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits); 26775a25e3f7SRafael J. Wysocki 26781da177e4SLinus Torvalds /********************************************************************* 26796f19efc0SLukasz Majewski * BOOST * 26806f19efc0SLukasz Majewski *********************************************************************/ 2681cf6fada7SXiongfeng Wang static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state) 26826f19efc0SLukasz Majewski { 2683552abb88SRafael J. Wysocki int ret; 2684552abb88SRafael J. Wysocki 2685f8bfc116SViresh Kumar if (!policy->freq_table) 2686552abb88SRafael J. Wysocki return -ENXIO; 2687f8bfc116SViresh Kumar 2688cf6fada7SXiongfeng Wang ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); 26896f19efc0SLukasz Majewski if (ret) { 2690cf6fada7SXiongfeng Wang pr_err("%s: Policy frequency update failed\n", __func__); 2691552abb88SRafael J. Wysocki return ret; 26926f19efc0SLukasz Majewski } 269349f18560SViresh Kumar 26943000ce3cSRafael J. Wysocki ret = freq_qos_update_request(policy->max_freq_req, policy->max); 2695e61a4125SViresh Kumar if (ret < 0) 2696552abb88SRafael J. Wysocki return ret; 26976f19efc0SLukasz Majewski 2698552abb88SRafael J. Wysocki return 0; 26996f19efc0SLukasz Majewski } 27006f19efc0SLukasz Majewski 27016f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 27026f19efc0SLukasz Majewski { 2703cf6fada7SXiongfeng Wang struct cpufreq_policy *policy; 27046f19efc0SLukasz Majewski unsigned long flags; 27056f19efc0SLukasz Majewski int ret = 0; 27066f19efc0SLukasz Majewski 27076f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 27086f19efc0SLukasz Majewski return 0; 27096f19efc0SLukasz Majewski 27106f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 27116f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 27126f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 27136f19efc0SLukasz Majewski 271409681a07SSebastian Andrzej Siewior cpus_read_lock(); 2715cf6fada7SXiongfeng Wang for_each_active_policy(policy) { 2716cf6fada7SXiongfeng Wang ret = cpufreq_driver->set_boost(policy, state); 2717cf6fada7SXiongfeng Wang if (ret) 2718cf6fada7SXiongfeng Wang goto err_reset_state; 2719cf6fada7SXiongfeng Wang } 272009681a07SSebastian Andrzej Siewior cpus_read_unlock(); 2721cf6fada7SXiongfeng Wang 2722cf6fada7SXiongfeng Wang return 0; 2723cf6fada7SXiongfeng Wang 2724cf6fada7SXiongfeng Wang err_reset_state: 272509681a07SSebastian Andrzej Siewior cpus_read_unlock(); 2726cf6fada7SXiongfeng Wang 27276f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 27286f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 27296f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 27306f19efc0SLukasz Majewski 2731e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2732e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 27336f19efc0SLukasz Majewski 27346f19efc0SLukasz Majewski return ret; 27356f19efc0SLukasz Majewski } 27366f19efc0SLukasz Majewski 273741669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void) 27386f19efc0SLukasz Majewski { 273989f98d7eSYue Hu return cpufreq_driver->set_boost; 27406f19efc0SLukasz Majewski } 27416f19efc0SLukasz Majewski 274244139ed4SViresh Kumar static int create_boost_sysfs_file(void) 274344139ed4SViresh Kumar { 274444139ed4SViresh Kumar int ret; 274544139ed4SViresh Kumar 2746c82bd444SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 274744139ed4SViresh Kumar if (ret) 274844139ed4SViresh Kumar pr_err("%s: cannot register global BOOST sysfs file\n", 274944139ed4SViresh Kumar __func__); 275044139ed4SViresh Kumar 275144139ed4SViresh Kumar return ret; 275244139ed4SViresh Kumar } 275344139ed4SViresh Kumar 275444139ed4SViresh Kumar static void remove_boost_sysfs_file(void) 275544139ed4SViresh Kumar { 275644139ed4SViresh Kumar if (cpufreq_boost_supported()) 2757c82bd444SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 275844139ed4SViresh Kumar } 275944139ed4SViresh Kumar 276044139ed4SViresh Kumar int cpufreq_enable_boost_support(void) 276144139ed4SViresh Kumar { 276244139ed4SViresh Kumar if (!cpufreq_driver) 276344139ed4SViresh Kumar return -EINVAL; 276444139ed4SViresh Kumar 276544139ed4SViresh Kumar if (cpufreq_boost_supported()) 276644139ed4SViresh Kumar return 0; 276744139ed4SViresh Kumar 27687a6c79f2SRafael J. Wysocki cpufreq_driver->set_boost = cpufreq_boost_set_sw; 276944139ed4SViresh Kumar 277044139ed4SViresh Kumar /* This will get removed on driver unregister */ 277144139ed4SViresh Kumar return create_boost_sysfs_file(); 277244139ed4SViresh Kumar } 277344139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 277444139ed4SViresh Kumar 27756f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 27766f19efc0SLukasz Majewski { 27776f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 27786f19efc0SLukasz Majewski } 27796f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 27806f19efc0SLukasz Majewski 27816f19efc0SLukasz Majewski /********************************************************************* 27821da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 27831da177e4SLinus Torvalds *********************************************************************/ 278427622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online; 27851da177e4SLinus Torvalds 2786c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu) 2787c4a3fa26SChen Yu { 2788c4a3fa26SChen Yu cpufreq_online(cpu); 2789c4a3fa26SChen Yu 2790c4a3fa26SChen Yu return 0; 2791c4a3fa26SChen Yu } 2792c4a3fa26SChen Yu 2793c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu) 2794c4a3fa26SChen Yu { 2795c4a3fa26SChen Yu cpufreq_offline(cpu); 2796c4a3fa26SChen Yu 2797c4a3fa26SChen Yu return 0; 2798c4a3fa26SChen Yu } 2799c4a3fa26SChen Yu 28001da177e4SLinus Torvalds /** 28011da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 28021da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 28031da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 28041da177e4SLinus Torvalds * 28051da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 280663af4055SEric Biggers * returns zero on success, -EEXIST when another driver got here first 28071da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 28081da177e4SLinus Torvalds * 28091da177e4SLinus Torvalds */ 2810221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 28111da177e4SLinus Torvalds { 28121da177e4SLinus Torvalds unsigned long flags; 28131da177e4SLinus Torvalds int ret; 28141da177e4SLinus Torvalds 2815a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2816a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2817a7b422cdSKonrad Rzeszutek Wilk 281846770be0SViresh Kumar /* 281946770be0SViresh Kumar * The cpufreq core depends heavily on the availability of device 282046770be0SViresh Kumar * structure, make sure they are available before proceeding further. 282146770be0SViresh Kumar */ 282246770be0SViresh Kumar if (!get_cpu_device(0)) 282346770be0SViresh Kumar return -EPROBE_DEFER; 282446770be0SViresh Kumar 28251da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 28269c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 28279832235fSRafael J. Wysocki driver_data->target) || 28289832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 28291c03a2d0SViresh Kumar driver_data->target)) || 2830a9a22b57SViresh Kumar (!driver_data->get_intermediate != !driver_data->target_intermediate) || 283191a12e91SViresh Kumar (!driver_data->online != !driver_data->offline)) 28321da177e4SLinus Torvalds return -EINVAL; 28331da177e4SLinus Torvalds 28342d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 28351da177e4SLinus Torvalds 2836fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2837a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 2838fdd320daSRafael J. Wysocki 28390d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 28401c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 28410d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2842fdd320daSRafael J. Wysocki ret = -EEXIST; 2843fdd320daSRafael J. Wysocki goto out; 28441da177e4SLinus Torvalds } 28451c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 28460d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 28471da177e4SLinus Torvalds 2848874f6353SIonela Voinescu /* 2849874f6353SIonela Voinescu * Mark support for the scheduler's frequency invariance engine for 2850874f6353SIonela Voinescu * drivers that implement target(), target_index() or fast_switch(). 2851874f6353SIonela Voinescu */ 2852874f6353SIonela Voinescu if (!cpufreq_driver->setpolicy) { 2853874f6353SIonela Voinescu static_branch_enable_cpuslocked(&cpufreq_freq_invariance); 2854874f6353SIonela Voinescu pr_debug("supports frequency invariance"); 2855874f6353SIonela Voinescu } 2856874f6353SIonela Voinescu 2857bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2858bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2859bc68b7dfSViresh Kumar 28607a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 286144139ed4SViresh Kumar ret = create_boost_sysfs_file(); 286244139ed4SViresh Kumar if (ret) 28636f19efc0SLukasz Majewski goto err_null_driver; 28647a6c79f2SRafael J. Wysocki } 28656f19efc0SLukasz Majewski 28668a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 28678f5bc2abSJiri Slaby if (ret) 28686f19efc0SLukasz Majewski goto err_boost_unreg; 28691da177e4SLinus Torvalds 28705ae4a4b4SViresh Kumar if (unlikely(list_empty(&cpufreq_policy_list))) { 28711da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 28726c770036SDavid Arcari ret = -ENODEV; 2873ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2874e08f5f5bSGautham R Shenoy driver_data->name); 28758a25a2fdSKay Sievers goto err_if_unreg; 28761da177e4SLinus Torvalds } 28771da177e4SLinus Torvalds 2878a92551e4SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, 2879a92551e4SSebastian Andrzej Siewior "cpufreq:online", 2880c4a3fa26SChen Yu cpuhp_cpufreq_online, 2881c4a3fa26SChen Yu cpuhp_cpufreq_offline); 288227622b06SSebastian Andrzej Siewior if (ret < 0) 288327622b06SSebastian Andrzej Siewior goto err_if_unreg; 288427622b06SSebastian Andrzej Siewior hp_online = ret; 28855372e054SSebastian Andrzej Siewior ret = 0; 288627622b06SSebastian Andrzej Siewior 28872d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 28883834abb4SPankaj Gupta goto out; 2889fdd320daSRafael J. Wysocki 28908a25a2fdSKay Sievers err_if_unreg: 28918a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 28926f19efc0SLukasz Majewski err_boost_unreg: 289344139ed4SViresh Kumar remove_boost_sysfs_file(); 28948f5bc2abSJiri Slaby err_null_driver: 28950d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 28961c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 28970d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 28983834abb4SPankaj Gupta out: 2899a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 29003834abb4SPankaj Gupta return ret; 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 29031da177e4SLinus Torvalds 2904a9909c21SLee Jones /* 29051da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 29061da177e4SLinus Torvalds * 29071da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 29081da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 29091da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 29101da177e4SLinus Torvalds * currently not initialised. 29111da177e4SLinus Torvalds */ 2912221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 29131da177e4SLinus Torvalds { 29141da177e4SLinus Torvalds unsigned long flags; 29151da177e4SLinus Torvalds 29161c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 29171da177e4SLinus Torvalds return -EINVAL; 29181da177e4SLinus Torvalds 29192d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 29201da177e4SLinus Torvalds 2921454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2922a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 29238a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 292444139ed4SViresh Kumar remove_boost_sysfs_file(); 2925874f6353SIonela Voinescu static_branch_disable_cpuslocked(&cpufreq_freq_invariance); 2926a92551e4SSebastian Andrzej Siewior cpuhp_remove_state_nocalls_cpuslocked(hp_online); 29271da177e4SLinus Torvalds 29280d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 29296eed9404SViresh Kumar 29301c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 29316eed9404SViresh Kumar 29320d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2933a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds return 0; 29361da177e4SLinus Torvalds } 29371da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 29385a01f2e8SVenkatesh Pallipadi 29395a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 29405a01f2e8SVenkatesh Pallipadi { 29418412b456SQuentin Perret struct cpufreq_governor *gov = cpufreq_default_governor(); 29428412b456SQuentin Perret 2943a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2944a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2945a7b422cdSKonrad Rzeszutek Wilk 29468eec1020SViresh Kumar cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); 29478aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 29488aa84ad8SThomas Renninger 29498412b456SQuentin Perret if (!strlen(default_governor)) 29508412b456SQuentin Perret strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN); 29518412b456SQuentin Perret 29525a01f2e8SVenkatesh Pallipadi return 0; 29535a01f2e8SVenkatesh Pallipadi } 2954d82f2692SLen Brown module_param(off, int, 0444); 29558412b456SQuentin Perret module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); 29565a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2957