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 76a038895eSViresh Kumar bool has_target_index(void) 77a038895eSViresh Kumar { 78a038895eSViresh Kumar return !!cpufreq_driver->target_index; 79a038895eSViresh Kumar } 80a038895eSViresh Kumar 811da177e4SLinus Torvalds /* internal prototypes */ 82d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 83a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy); 84a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy); 85a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy); 861e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy, 871e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov, 881e4f63aeSRafael J. Wysocki unsigned int new_pol); 8945482c70SRafael J. Wysocki 90a9909c21SLee Jones /* 911da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 921da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 931da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 941da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 951da177e4SLinus Torvalds * The mutex locks both lists. 961da177e4SLinus Torvalds */ 97e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 98cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); 991da177e4SLinus Torvalds 100a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 101da584455SViresh Kumar static int cpufreq_disabled(void) 102a7b422cdSKonrad Rzeszutek Wilk { 103a7b422cdSKonrad Rzeszutek Wilk return off; 104a7b422cdSKonrad Rzeszutek Wilk } 105a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 106a7b422cdSKonrad Rzeszutek Wilk { 107a7b422cdSKonrad Rzeszutek Wilk off = 1; 108a7b422cdSKonrad Rzeszutek Wilk } 1093fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1101da177e4SLinus Torvalds 1114d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1124d5dcc42SViresh Kumar { 1130b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1144d5dcc42SViresh Kumar } 1153f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1164d5dcc42SViresh Kumar 117183edb20SYangtao Li static struct kobject *cpufreq_global_kobject; 118183edb20SYangtao Li 119944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 120944e9a03SViresh Kumar { 121944e9a03SViresh Kumar if (have_governor_per_policy()) 122944e9a03SViresh Kumar return &policy->kobj; 123944e9a03SViresh Kumar else 124944e9a03SViresh Kumar return cpufreq_global_kobject; 125944e9a03SViresh Kumar } 126944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 127944e9a03SViresh Kumar 12872a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 12972a4ce34SViresh Kumar { 1305720821bSFrederic Weisbecker struct kernel_cpustat kcpustat; 13172a4ce34SViresh Kumar u64 cur_wall_time; 1325720821bSFrederic Weisbecker u64 idle_time; 13372a4ce34SViresh Kumar u64 busy_time; 13472a4ce34SViresh Kumar 1357fb1327eSFrederic Weisbecker cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); 13672a4ce34SViresh Kumar 1375720821bSFrederic Weisbecker kcpustat_cpu_fetch(&kcpustat, cpu); 1385720821bSFrederic Weisbecker 1395720821bSFrederic Weisbecker busy_time = kcpustat.cpustat[CPUTIME_USER]; 1405720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_SYSTEM]; 1415720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_IRQ]; 1425720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ]; 1435720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_STEAL]; 1445720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_NICE]; 14572a4ce34SViresh Kumar 14672a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 14772a4ce34SViresh Kumar if (wall) 1487fb1327eSFrederic Weisbecker *wall = div_u64(cur_wall_time, NSEC_PER_USEC); 14972a4ce34SViresh Kumar 1507fb1327eSFrederic Weisbecker return div_u64(idle_time, NSEC_PER_USEC); 15172a4ce34SViresh Kumar } 15272a4ce34SViresh Kumar 15372a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 15472a4ce34SViresh Kumar { 15572a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 15672a4ce34SViresh Kumar 15772a4ce34SViresh Kumar if (idle_time == -1ULL) 15872a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 15972a4ce34SViresh Kumar else if (!io_busy) 16072a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 16172a4ce34SViresh Kumar 16272a4ce34SViresh Kumar return idle_time; 16372a4ce34SViresh Kumar } 16472a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 16572a4ce34SViresh Kumar 16670e9e778SViresh Kumar /* 16770e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 16870e9e778SViresh Kumar * drivers of SMP systems. It will do following: 16970e9e778SViresh Kumar * - validate & show freq table passed 17070e9e778SViresh Kumar * - set policies transition latency 17170e9e778SViresh Kumar * - policy->cpus with all possible CPUs 17270e9e778SViresh Kumar */ 173c4dcc8a1SViresh Kumar void cpufreq_generic_init(struct cpufreq_policy *policy, 17470e9e778SViresh Kumar struct cpufreq_frequency_table *table, 17570e9e778SViresh Kumar unsigned int transition_latency) 17670e9e778SViresh Kumar { 17792c99d15SViresh Kumar policy->freq_table = table; 17870e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 17970e9e778SViresh Kumar 18070e9e778SViresh Kumar /* 18158405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 18270e9e778SViresh Kumar * share the clock and voltage and clock. 18370e9e778SViresh Kumar */ 18470e9e778SViresh Kumar cpumask_setall(policy->cpus); 18570e9e778SViresh Kumar } 18670e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 18770e9e778SViresh Kumar 1881f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 189652ed95dSViresh Kumar { 190652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 191652ed95dSViresh Kumar 192988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 193988bed09SViresh Kumar } 1941f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); 195988bed09SViresh Kumar 196988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 197988bed09SViresh Kumar { 198988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 199988bed09SViresh Kumar 200652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 201e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 202e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 203652ed95dSViresh Kumar return 0; 204652ed95dSViresh Kumar } 205652ed95dSViresh Kumar 206652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 207652ed95dSViresh Kumar } 208652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 209652ed95dSViresh Kumar 21050e9c852SViresh Kumar /** 2115d094feaSRafael J. Wysocki * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. 2125d094feaSRafael J. Wysocki * @cpu: CPU to find the policy for. 21350e9c852SViresh Kumar * 2145d094feaSRafael J. Wysocki * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment 2155d094feaSRafael J. Wysocki * the kobject reference counter of that policy. Return a valid policy on 2165d094feaSRafael J. Wysocki * success or NULL on failure. 21750e9c852SViresh Kumar * 2185d094feaSRafael J. Wysocki * The policy returned by this function has to be released with the help of 2195d094feaSRafael J. Wysocki * cpufreq_cpu_put() to balance its kobject reference counter properly. 22050e9c852SViresh Kumar */ 2216eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2221da177e4SLinus Torvalds { 2236eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2241da177e4SLinus Torvalds unsigned long flags; 2251da177e4SLinus Torvalds 2261b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 2276eed9404SViresh Kumar return NULL; 2286eed9404SViresh Kumar 2291da177e4SLinus Torvalds /* get the cpufreq driver */ 2300d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2311da177e4SLinus Torvalds 2326eed9404SViresh Kumar if (cpufreq_driver) { 2331da177e4SLinus Torvalds /* get the CPU */ 234988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 2356eed9404SViresh Kumar if (policy) 2366eed9404SViresh Kumar kobject_get(&policy->kobj); 2376eed9404SViresh Kumar } 2386eed9404SViresh Kumar 2396eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2401da177e4SLinus Torvalds 2413a3e9e06SViresh Kumar return policy; 242a9144436SStephen Boyd } 2431da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2441da177e4SLinus Torvalds 24550e9c852SViresh Kumar /** 2465d094feaSRafael J. Wysocki * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. 2475d094feaSRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_get(). 24850e9c852SViresh Kumar */ 2493a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 250a9144436SStephen Boyd { 2516eed9404SViresh Kumar kobject_put(&policy->kobj); 252a9144436SStephen Boyd } 2531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2541da177e4SLinus Torvalds 255540a3758SRafael J. Wysocki /** 256540a3758SRafael J. Wysocki * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. 257540a3758SRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). 258540a3758SRafael J. Wysocki */ 2599083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy) 260540a3758SRafael J. Wysocki { 261540a3758SRafael J. Wysocki if (WARN_ON(!policy)) 262540a3758SRafael J. Wysocki return; 263540a3758SRafael J. Wysocki 264540a3758SRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 265540a3758SRafael J. Wysocki 266540a3758SRafael J. Wysocki up_write(&policy->rwsem); 267540a3758SRafael J. Wysocki 268540a3758SRafael J. Wysocki cpufreq_cpu_put(policy); 269540a3758SRafael J. Wysocki } 270540a3758SRafael J. Wysocki 271540a3758SRafael J. Wysocki /** 272540a3758SRafael J. Wysocki * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. 273540a3758SRafael J. Wysocki * @cpu: CPU to find the policy for. 274540a3758SRafael J. Wysocki * 275540a3758SRafael J. Wysocki * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and 276540a3758SRafael J. Wysocki * if the policy returned by it is not NULL, acquire its rwsem for writing. 277540a3758SRafael J. Wysocki * Return the policy if it is active or release it and return NULL otherwise. 278540a3758SRafael J. Wysocki * 279540a3758SRafael J. Wysocki * The policy returned by this function has to be released with the help of 280540a3758SRafael J. Wysocki * cpufreq_cpu_release() in order to release its rwsem and balance its usage 281540a3758SRafael J. Wysocki * counter properly. 282540a3758SRafael J. Wysocki */ 2839083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) 284540a3758SRafael J. Wysocki { 285540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 286540a3758SRafael J. Wysocki 287540a3758SRafael J. Wysocki if (!policy) 288540a3758SRafael J. Wysocki return NULL; 289540a3758SRafael J. Wysocki 290540a3758SRafael J. Wysocki down_write(&policy->rwsem); 291540a3758SRafael J. Wysocki 292540a3758SRafael J. Wysocki if (policy_is_inactive(policy)) { 293540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 294540a3758SRafael J. Wysocki return NULL; 295540a3758SRafael J. Wysocki } 296540a3758SRafael J. Wysocki 297540a3758SRafael J. Wysocki return policy; 298540a3758SRafael J. Wysocki } 299540a3758SRafael J. Wysocki 3001da177e4SLinus Torvalds /********************************************************************* 3011da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 3021da177e4SLinus Torvalds *********************************************************************/ 3031da177e4SLinus Torvalds 304ec06e586SRafael J. Wysocki /** 305ec06e586SRafael J. Wysocki * adjust_jiffies - Adjust the system "loops_per_jiffy". 306ec06e586SRafael J. Wysocki * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. 307ec06e586SRafael J. Wysocki * @ci: Frequency change information. 3081da177e4SLinus Torvalds * 3091da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 3101da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 3111da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 3121da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 3131da177e4SLinus Torvalds */ 31439c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 31539c132eeSViresh Kumar { 3161da177e4SLinus Torvalds #ifndef CONFIG_SMP 3171da177e4SLinus Torvalds static unsigned long l_p_j_ref; 3181da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 3211da177e4SLinus Torvalds return; 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 3241da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 3251da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 326e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 327e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 3281da177e4SLinus Torvalds } 3290b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 330e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 331e08f5f5bSGautham R Shenoy ci->new); 332e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 333e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds #endif 33639c132eeSViresh Kumar } 3371da177e4SLinus Torvalds 33820b5324dSViresh Kumar /** 339ec06e586SRafael J. Wysocki * cpufreq_notify_transition - Notify frequency transition and adjust jiffies. 34020b5324dSViresh Kumar * @policy: cpufreq policy to enable fast frequency switching for. 34120b5324dSViresh Kumar * @freqs: contain details of the frequency update. 34220b5324dSViresh Kumar * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. 34320b5324dSViresh Kumar * 344ec06e586SRafael J. Wysocki * This function calls the transition notifiers and adjust_jiffies(). 345ec06e586SRafael J. Wysocki * 346ec06e586SRafael J. Wysocki * It is called twice on all CPU frequency changes that have external effects. 34720b5324dSViresh Kumar */ 34820b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 34920b5324dSViresh Kumar struct cpufreq_freqs *freqs, 35020b5324dSViresh Kumar unsigned int state) 3511da177e4SLinus Torvalds { 352df24014aSViresh Kumar int cpu; 353df24014aSViresh Kumar 3541da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3551da177e4SLinus Torvalds 356d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 357d5aaffa9SDirk Brandewie return; 358d5aaffa9SDirk Brandewie 359df24014aSViresh Kumar freqs->policy = policy; 3601c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3612d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 362e4472cb3SDave Jones state, freqs->new); 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds switch (state) { 3651da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 36620b5324dSViresh Kumar /* 36720b5324dSViresh Kumar * Detect if the driver reported a value as "old frequency" 368e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 369e4472cb3SDave Jones * "old frequency". 3701da177e4SLinus Torvalds */ 37198015228SViresh Kumar if (policy->cur && policy->cur != freqs->old) { 372e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 373e4472cb3SDave Jones freqs->old, policy->cur); 374e4472cb3SDave Jones freqs->old = policy->cur; 3751da177e4SLinus Torvalds } 37620b5324dSViresh Kumar 377b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 378e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 37920b5324dSViresh Kumar 3801da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3811da177e4SLinus Torvalds break; 382e4472cb3SDave Jones 3831da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3841da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 38520b5324dSViresh Kumar pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, 38620b5324dSViresh Kumar cpumask_pr_args(policy->cpus)); 38720b5324dSViresh Kumar 388df24014aSViresh Kumar for_each_cpu(cpu, policy->cpus) 389df24014aSViresh Kumar trace_cpu_frequency(freqs->new, cpu); 390df24014aSViresh Kumar 391b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 392e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 393bb176f7dSViresh Kumar 39420b5324dSViresh Kumar cpufreq_stats_record_transition(policy, freqs->new); 39520b5324dSViresh Kumar policy->cur = freqs->new; 39620b5324dSViresh Kumar } 397b43a7ffbSViresh Kumar } 3981da177e4SLinus Torvalds 399f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 400236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 401f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 402f7ba3b41SViresh Kumar { 403f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 404f7ba3b41SViresh Kumar if (!transition_failed) 405f7ba3b41SViresh Kumar return; 406f7ba3b41SViresh Kumar 407f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 408f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 409f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 410f7ba3b41SViresh Kumar } 411f7ba3b41SViresh Kumar 41212478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 41312478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 41412478cf0SSrivatsa S. Bhat { 415ca654dc3SSrivatsa S. Bhat 416ca654dc3SSrivatsa S. Bhat /* 417ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 418ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 419ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 420ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 421ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 422ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 423ca654dc3SSrivatsa S. Bhat */ 424ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 425ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 426ca654dc3SSrivatsa S. Bhat 42712478cf0SSrivatsa S. Bhat wait: 42812478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 42912478cf0SSrivatsa S. Bhat 43012478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 43112478cf0SSrivatsa S. Bhat 43212478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 43312478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 43412478cf0SSrivatsa S. Bhat goto wait; 43512478cf0SSrivatsa S. Bhat } 43612478cf0SSrivatsa S. Bhat 43712478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 438ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 43912478cf0SSrivatsa S. Bhat 44012478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 44112478cf0SSrivatsa S. Bhat 44212478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 44312478cf0SSrivatsa S. Bhat } 44412478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 44512478cf0SSrivatsa S. Bhat 44612478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 44712478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 44812478cf0SSrivatsa S. Bhat { 4490e7ea2f3SIgor Stoppa if (WARN_ON(!policy->transition_ongoing)) 45012478cf0SSrivatsa S. Bhat return; 45112478cf0SSrivatsa S. Bhat 45212478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 45312478cf0SSrivatsa S. Bhat 4541a0419b0SIonela Voinescu arch_set_freq_scale(policy->related_cpus, 4551a0419b0SIonela Voinescu policy->cur, 4561a0419b0SIonela Voinescu policy->cpuinfo.max_freq); 4571a0419b0SIonela Voinescu 45812478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 459ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 46012478cf0SSrivatsa S. Bhat 46112478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 46212478cf0SSrivatsa S. Bhat } 46312478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 46412478cf0SSrivatsa S. Bhat 465b7898fdaSRafael J. Wysocki /* 466b7898fdaSRafael J. Wysocki * Fast frequency switching status count. Positive means "enabled", negative 467b7898fdaSRafael J. Wysocki * means "disabled" and 0 means "not decided yet". 468b7898fdaSRafael J. Wysocki */ 469b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count; 470b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock); 471b7898fdaSRafael J. Wysocki 472b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void) 473b7898fdaSRafael J. Wysocki { 474b7898fdaSRafael J. Wysocki struct notifier_block *nb; 475b7898fdaSRafael J. Wysocki 476b7898fdaSRafael J. Wysocki pr_info("Registered transition notifiers:\n"); 477b7898fdaSRafael J. Wysocki 478b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_transition_notifier_list.mutex); 479b7898fdaSRafael J. Wysocki 480b7898fdaSRafael J. Wysocki for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) 481d75f773cSSakari Ailus pr_info("%pS\n", nb->notifier_call); 482b7898fdaSRafael J. Wysocki 483b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_transition_notifier_list.mutex); 484b7898fdaSRafael J. Wysocki } 485b7898fdaSRafael J. Wysocki 486b7898fdaSRafael J. Wysocki /** 487b7898fdaSRafael J. Wysocki * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. 488b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to enable fast frequency switching for. 489b7898fdaSRafael J. Wysocki * 490b7898fdaSRafael J. Wysocki * Try to enable fast frequency switching for @policy. 491b7898fdaSRafael J. Wysocki * 492b7898fdaSRafael J. Wysocki * The attempt will fail if there is at least one transition notifier registered 493b7898fdaSRafael J. Wysocki * at this point, as fast frequency switching is quite fundamentally at odds 494b7898fdaSRafael J. Wysocki * with transition notifiers. Thus if successful, it will make registration of 495b7898fdaSRafael J. Wysocki * transition notifiers fail going forward. 496b7898fdaSRafael J. Wysocki */ 497b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) 498b7898fdaSRafael J. Wysocki { 499b7898fdaSRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 500b7898fdaSRafael J. Wysocki 501b7898fdaSRafael J. Wysocki if (!policy->fast_switch_possible) 502b7898fdaSRafael J. Wysocki return; 503b7898fdaSRafael J. Wysocki 504b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 505b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count >= 0) { 506b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 507b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = true; 508b7898fdaSRafael J. Wysocki } else { 509b7898fdaSRafael J. Wysocki pr_warn("CPU%u: Fast frequency switching not enabled\n", 510b7898fdaSRafael J. Wysocki policy->cpu); 511b7898fdaSRafael J. Wysocki cpufreq_list_transition_notifiers(); 512b7898fdaSRafael J. Wysocki } 513b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 514b7898fdaSRafael J. Wysocki } 515b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); 516b7898fdaSRafael J. Wysocki 5176c9d9c81SRafael J. Wysocki /** 5186c9d9c81SRafael J. Wysocki * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. 5196c9d9c81SRafael J. Wysocki * @policy: cpufreq policy to disable fast frequency switching for. 5206c9d9c81SRafael J. Wysocki */ 5216c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) 522b7898fdaSRafael J. Wysocki { 523b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 524b7898fdaSRafael J. Wysocki if (policy->fast_switch_enabled) { 525b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = false; 526b7898fdaSRafael J. Wysocki if (!WARN_ON(cpufreq_fast_switch_count <= 0)) 527b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 528b7898fdaSRafael J. Wysocki } 529b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 530b7898fdaSRafael J. Wysocki } 5316c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); 5321da177e4SLinus Torvalds 533f9ccdec2SViresh Kumar static unsigned int __resolve_freq(struct cpufreq_policy *policy, 534f9ccdec2SViresh Kumar unsigned int target_freq, unsigned int relation) 535f9ccdec2SViresh Kumar { 536f9ccdec2SViresh Kumar unsigned int idx; 537f9ccdec2SViresh Kumar 538b3beca76SViresh Kumar target_freq = clamp_val(target_freq, policy->min, policy->max); 539f9ccdec2SViresh Kumar 5406ca7076fSLukasz Luba if (!policy->freq_table) 541f9ccdec2SViresh Kumar return target_freq; 542b3beca76SViresh Kumar 543b3beca76SViresh Kumar idx = cpufreq_frequency_table_target(policy, target_freq, relation); 544b3beca76SViresh Kumar policy->cached_resolved_idx = idx; 545b3beca76SViresh Kumar policy->cached_target_freq = target_freq; 546b3beca76SViresh Kumar return policy->freq_table[idx].frequency; 547f9ccdec2SViresh Kumar } 548f9ccdec2SViresh Kumar 549e3c06236SSteve Muckle /** 550e3c06236SSteve Muckle * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported 551e3c06236SSteve Muckle * one. 552a9909c21SLee Jones * @policy: associated policy to interrogate 553e3c06236SSteve Muckle * @target_freq: target frequency to resolve. 554e3c06236SSteve Muckle * 555e3c06236SSteve Muckle * The target to driver frequency mapping is cached in the policy. 556e3c06236SSteve Muckle * 557e3c06236SSteve Muckle * Return: Lowest driver-supported frequency greater than or equal to the 558e3c06236SSteve Muckle * given target_freq, subject to policy (min/max) and driver limitations. 559e3c06236SSteve Muckle */ 560e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, 561e3c06236SSteve Muckle unsigned int target_freq) 562e3c06236SSteve Muckle { 563b894d20eSVincent Donnefort return __resolve_freq(policy, target_freq, CPUFREQ_RELATION_LE); 564e3c06236SSteve Muckle } 565ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); 566e3c06236SSteve Muckle 567aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) 568aa7519afSViresh Kumar { 569aa7519afSViresh Kumar unsigned int latency; 570aa7519afSViresh Kumar 571aa7519afSViresh Kumar if (policy->transition_delay_us) 572aa7519afSViresh Kumar return policy->transition_delay_us; 573aa7519afSViresh Kumar 574aa7519afSViresh Kumar latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; 575e948bc8fSViresh Kumar if (latency) { 576e948bc8fSViresh Kumar /* 577e948bc8fSViresh Kumar * For platforms that can change the frequency very fast (< 10 578e948bc8fSViresh Kumar * us), the above formula gives a decent transition delay. But 579e948bc8fSViresh Kumar * for platforms where transition_latency is in milliseconds, it 580e948bc8fSViresh Kumar * ends up giving unrealistic values. 581e948bc8fSViresh Kumar * 582e948bc8fSViresh Kumar * Cap the default transition delay to 10 ms, which seems to be 583e948bc8fSViresh Kumar * a reasonable amount of time after which we should reevaluate 584e948bc8fSViresh Kumar * the frequency. 585e948bc8fSViresh Kumar */ 586e948bc8fSViresh Kumar return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); 587e948bc8fSViresh Kumar } 588aa7519afSViresh Kumar 589aa7519afSViresh Kumar return LATENCY_MULTIPLIER; 590aa7519afSViresh Kumar } 591aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); 592aa7519afSViresh Kumar 5931da177e4SLinus Torvalds /********************************************************************* 5941da177e4SLinus Torvalds * SYSFS INTERFACE * 5951da177e4SLinus Torvalds *********************************************************************/ 5968a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 597625c85a6SViresh Kumar struct kobj_attribute *attr, char *buf) 5986f19efc0SLukasz Majewski { 5996f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 6006f19efc0SLukasz Majewski } 6016f19efc0SLukasz Majewski 602625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, 6036f19efc0SLukasz Majewski const char *buf, size_t count) 6046f19efc0SLukasz Majewski { 6056f19efc0SLukasz Majewski int ret, enable; 6066f19efc0SLukasz Majewski 6076f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 6086f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 6096f19efc0SLukasz Majewski return -EINVAL; 6106f19efc0SLukasz Majewski 6116f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 612e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 613e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 6146f19efc0SLukasz Majewski return -EINVAL; 6156f19efc0SLukasz Majewski } 6166f19efc0SLukasz Majewski 617e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 618e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 6196f19efc0SLukasz Majewski 6206f19efc0SLukasz Majewski return count; 6216f19efc0SLukasz Majewski } 6226f19efc0SLukasz Majewski define_one_global_rw(boost); 6231da177e4SLinus Torvalds 62442f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 6253bcb09a3SJeremy Fitzhardinge { 6263bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 6273bcb09a3SJeremy Fitzhardinge 628f7b27061SViresh Kumar for_each_governor(t) 6297c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 6303bcb09a3SJeremy Fitzhardinge return t; 6313bcb09a3SJeremy Fitzhardinge 6323bcb09a3SJeremy Fitzhardinge return NULL; 6333bcb09a3SJeremy Fitzhardinge } 6343bcb09a3SJeremy Fitzhardinge 6358cc46ae5SViresh Kumar static struct cpufreq_governor *get_governor(const char *str_governor) 6368cc46ae5SViresh Kumar { 6378cc46ae5SViresh Kumar struct cpufreq_governor *t; 6388cc46ae5SViresh Kumar 6398cc46ae5SViresh Kumar mutex_lock(&cpufreq_governor_mutex); 6408cc46ae5SViresh Kumar t = find_governor(str_governor); 6418cc46ae5SViresh Kumar if (!t) 6428cc46ae5SViresh Kumar goto unlock; 6438cc46ae5SViresh Kumar 6448cc46ae5SViresh Kumar if (!try_module_get(t->owner)) 6458cc46ae5SViresh Kumar t = NULL; 6468cc46ae5SViresh Kumar 6478cc46ae5SViresh Kumar unlock: 6488cc46ae5SViresh Kumar mutex_unlock(&cpufreq_governor_mutex); 6498cc46ae5SViresh Kumar 6508cc46ae5SViresh Kumar return t; 6518cc46ae5SViresh Kumar } 6528cc46ae5SViresh Kumar 6531e4f63aeSRafael J. Wysocki static unsigned int cpufreq_parse_policy(char *str_governor) 6541da177e4SLinus Torvalds { 6551e4f63aeSRafael J. Wysocki if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) 6561e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_PERFORMANCE; 6571e4f63aeSRafael J. Wysocki 6581e4f63aeSRafael J. Wysocki if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) 6591e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_POWERSAVE; 6601e4f63aeSRafael J. Wysocki 6611e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_UNKNOWN; 662ab05d97aSYue Hu } 663ab05d97aSYue Hu 664ab05d97aSYue Hu /** 6655ddc6d4eSViresh Kumar * cpufreq_parse_governor - parse a governor string only for has_target() 6661e4f63aeSRafael J. Wysocki * @str_governor: Governor name. 667ab05d97aSYue Hu */ 6681e4f63aeSRafael J. Wysocki static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor) 669ab05d97aSYue Hu { 6701da177e4SLinus Torvalds struct cpufreq_governor *t; 6713bcb09a3SJeremy Fitzhardinge 6728cc46ae5SViresh Kumar t = get_governor(str_governor); 6738cc46ae5SViresh Kumar if (t) 6748cc46ae5SViresh Kumar return t; 6753bcb09a3SJeremy Fitzhardinge 6768cc46ae5SViresh Kumar if (request_module("cpufreq_%s", str_governor)) 6771e4f63aeSRafael J. Wysocki return NULL; 678045149e6SRafael J. Wysocki 6798cc46ae5SViresh Kumar return get_governor(str_governor); 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds 682a9909c21SLee Jones /* 683e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 684e08f5f5bSGautham R Shenoy * print out cpufreq information 6851da177e4SLinus Torvalds * 6861da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 6871da177e4SLinus Torvalds * "unsigned int". 6881da177e4SLinus Torvalds */ 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds #define show_one(file_name, object) \ 6911da177e4SLinus Torvalds static ssize_t show_##file_name \ 6921da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 6931da177e4SLinus Torvalds { \ 6941da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 6981da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 699ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 7001da177e4SLinus Torvalds show_one(scaling_min_freq, min); 7011da177e4SLinus Torvalds show_one(scaling_max_freq, max); 702c034b02eSDirk Brandewie 703f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu) 704f8475cefSLen Brown { 705f8475cefSLen Brown return 0; 706f8475cefSLen Brown } 707f8475cefSLen Brown 70809347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 709c034b02eSDirk Brandewie { 710c034b02eSDirk Brandewie ssize_t ret; 711f8475cefSLen Brown unsigned int freq; 712c034b02eSDirk Brandewie 713f8475cefSLen Brown freq = arch_freq_get_on_cpu(policy->cpu); 714f8475cefSLen Brown if (freq) 715f8475cefSLen Brown ret = sprintf(buf, "%u\n", freq); 716681fe684SViresh Kumar else if (cpufreq_driver->setpolicy && cpufreq_driver->get) 717c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 718c034b02eSDirk Brandewie else 719c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 720c034b02eSDirk Brandewie return ret; 721c034b02eSDirk Brandewie } 7221da177e4SLinus Torvalds 723a9909c21SLee Jones /* 7241da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 7251da177e4SLinus Torvalds */ 7261da177e4SLinus Torvalds #define store_one(file_name, object) \ 7271da177e4SLinus Torvalds static ssize_t store_##file_name \ 7281da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 7291da177e4SLinus Torvalds { \ 73018c49926SViresh Kumar unsigned long val; \ 73118c49926SViresh Kumar int ret; \ 7321da177e4SLinus Torvalds \ 733877d5cd2Sqinyu ret = kstrtoul(buf, 0, &val); \ 734877d5cd2Sqinyu if (ret) \ 735877d5cd2Sqinyu return ret; \ 7361da177e4SLinus Torvalds \ 7373000ce3cSRafael J. Wysocki ret = freq_qos_update_request(policy->object##_freq_req, val);\ 73818c49926SViresh Kumar return ret >= 0 ? count : ret; \ 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds store_one(scaling_min_freq, min); 7421da177e4SLinus Torvalds store_one(scaling_max_freq, max); 7431da177e4SLinus Torvalds 744a9909c21SLee Jones /* 7451da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 7461da177e4SLinus Torvalds */ 747e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 748e08f5f5bSGautham R Shenoy char *buf) 7491da177e4SLinus Torvalds { 750d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 7519b4f603eSRafael J. Wysocki 7529b4f603eSRafael J. Wysocki if (cur_freq) 7531da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 7549b4f603eSRafael J. Wysocki 7559b4f603eSRafael J. Wysocki return sprintf(buf, "<unknown>\n"); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 758a9909c21SLee Jones /* 7591da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 7601da177e4SLinus Torvalds */ 761905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 7641da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 7651da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 7661da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 7671da177e4SLinus Torvalds else if (policy->governor) 7684b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 76929464f28SDave Jones policy->governor->name); 7701da177e4SLinus Torvalds return -EINVAL; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 773a9909c21SLee Jones /* 7741da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 7751da177e4SLinus Torvalds */ 7761da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 7771da177e4SLinus Torvalds const char *buf, size_t count) 7781da177e4SLinus Torvalds { 7791da177e4SLinus Torvalds char str_governor[16]; 7801e4f63aeSRafael J. Wysocki int ret; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 7831da177e4SLinus Torvalds if (ret != 1) 7841da177e4SLinus Torvalds return -EINVAL; 7851da177e4SLinus Torvalds 786ab05d97aSYue Hu if (cpufreq_driver->setpolicy) { 7871e4f63aeSRafael J. Wysocki unsigned int new_pol; 7881e4f63aeSRafael J. Wysocki 7891e4f63aeSRafael J. Wysocki new_pol = cpufreq_parse_policy(str_governor); 7901e4f63aeSRafael J. Wysocki if (!new_pol) 791ab05d97aSYue Hu return -EINVAL; 7921e4f63aeSRafael J. Wysocki 7931e4f63aeSRafael J. Wysocki ret = cpufreq_set_policy(policy, NULL, new_pol); 794ab05d97aSYue Hu } else { 7951e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov; 7961e4f63aeSRafael J. Wysocki 7971e4f63aeSRafael J. Wysocki new_gov = cpufreq_parse_governor(str_governor); 7981e4f63aeSRafael J. Wysocki if (!new_gov) 7991da177e4SLinus Torvalds return -EINVAL; 8001e4f63aeSRafael J. Wysocki 8011e4f63aeSRafael J. Wysocki ret = cpufreq_set_policy(policy, new_gov, 8021e4f63aeSRafael J. Wysocki CPUFREQ_POLICY_UNKNOWN); 8031e4f63aeSRafael J. Wysocki 8041e4f63aeSRafael J. Wysocki module_put(new_gov->owner); 805ab05d97aSYue Hu } 8061da177e4SLinus Torvalds 80788dc4384SViresh Kumar return ret ? ret : count; 8081da177e4SLinus Torvalds } 8091da177e4SLinus Torvalds 810a9909c21SLee Jones /* 8111da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 8121da177e4SLinus Torvalds */ 8131da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 8141da177e4SLinus Torvalds { 8151c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 8161da177e4SLinus Torvalds } 8171da177e4SLinus Torvalds 818a9909c21SLee Jones /* 8191da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 8201da177e4SLinus Torvalds */ 8211da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 8221da177e4SLinus Torvalds char *buf) 8231da177e4SLinus Torvalds { 8241da177e4SLinus Torvalds ssize_t i = 0; 8251da177e4SLinus Torvalds struct cpufreq_governor *t; 8261da177e4SLinus Torvalds 8279c0ebcf7SViresh Kumar if (!has_target()) { 8281da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 8291da177e4SLinus Torvalds goto out; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8328cc46ae5SViresh Kumar mutex_lock(&cpufreq_governor_mutex); 833f7b27061SViresh Kumar for_each_governor(t) { 83429464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 83529464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 8368cc46ae5SViresh Kumar break; 8374b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 8381da177e4SLinus Torvalds } 8398cc46ae5SViresh Kumar mutex_unlock(&cpufreq_governor_mutex); 8401da177e4SLinus Torvalds out: 8411da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8421da177e4SLinus Torvalds return i; 8431da177e4SLinus Torvalds } 844e8628dd0SDarrick J. Wong 845f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 8461da177e4SLinus Torvalds { 8471da177e4SLinus Torvalds ssize_t i = 0; 8481da177e4SLinus Torvalds unsigned int cpu; 8491da177e4SLinus Torvalds 850835481d9SRusty Russell for_each_cpu(cpu, mask) { 8511da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u ", cpu); 8521da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 8531da177e4SLinus Torvalds break; 8541da177e4SLinus Torvalds } 855336e5128SViresh Kumar 856336e5128SViresh Kumar /* Remove the extra space at the end */ 857336e5128SViresh Kumar i--; 858336e5128SViresh Kumar 8591da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8601da177e4SLinus Torvalds return i; 8611da177e4SLinus Torvalds } 862f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 8631da177e4SLinus Torvalds 864a9909c21SLee Jones /* 865e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 866e8628dd0SDarrick J. Wong * hw coordination is in use 867e8628dd0SDarrick J. Wong */ 868e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 869e8628dd0SDarrick J. Wong { 870f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 871e8628dd0SDarrick J. Wong } 872e8628dd0SDarrick J. Wong 873a9909c21SLee Jones /* 874e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 875e8628dd0SDarrick J. Wong */ 876e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 877e8628dd0SDarrick J. Wong { 878f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 879e8628dd0SDarrick J. Wong } 880e8628dd0SDarrick J. Wong 8819e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 8829e76988eSVenki Pallipadi const char *buf, size_t count) 8839e76988eSVenki Pallipadi { 8849e76988eSVenki Pallipadi unsigned int freq = 0; 8859e76988eSVenki Pallipadi unsigned int ret; 8869e76988eSVenki Pallipadi 887879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 8889e76988eSVenki Pallipadi return -EINVAL; 8899e76988eSVenki Pallipadi 8909e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 8919e76988eSVenki Pallipadi if (ret != 1) 8929e76988eSVenki Pallipadi return -EINVAL; 8939e76988eSVenki Pallipadi 8949e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 8959e76988eSVenki Pallipadi 8969e76988eSVenki Pallipadi return count; 8979e76988eSVenki Pallipadi } 8989e76988eSVenki Pallipadi 8999e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 9009e76988eSVenki Pallipadi { 901879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 9029e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 9039e76988eSVenki Pallipadi 9049e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 9059e76988eSVenki Pallipadi } 9061da177e4SLinus Torvalds 907a9909c21SLee Jones /* 9088bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 909e2f74f35SThomas Renninger */ 910e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 911e2f74f35SThomas Renninger { 912e2f74f35SThomas Renninger unsigned int limit; 913e2f74f35SThomas Renninger int ret; 9141c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 915e2f74f35SThomas Renninger if (!ret) 916e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 917e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 918e2f74f35SThomas Renninger } 919e2f74f35SThomas Renninger 9206dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 9216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 9226dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 9236dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 9246dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 9256dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 9266dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 9276dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 9286dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 9296dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 9306dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 9316dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 9326dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 9336dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 9341da177e4SLinus Torvalds 935fe262d5cSGreg Kroah-Hartman static struct attribute *cpufreq_attrs[] = { 9361da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 9371da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 938ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 9391da177e4SLinus Torvalds &scaling_min_freq.attr, 9401da177e4SLinus Torvalds &scaling_max_freq.attr, 9411da177e4SLinus Torvalds &affected_cpus.attr, 942e8628dd0SDarrick J. Wong &related_cpus.attr, 9431da177e4SLinus Torvalds &scaling_governor.attr, 9441da177e4SLinus Torvalds &scaling_driver.attr, 9451da177e4SLinus Torvalds &scaling_available_governors.attr, 9469e76988eSVenki Pallipadi &scaling_setspeed.attr, 9471da177e4SLinus Torvalds NULL 9481da177e4SLinus Torvalds }; 949fe262d5cSGreg Kroah-Hartman ATTRIBUTE_GROUPS(cpufreq); 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 9521da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 9551da177e4SLinus Torvalds { 9561da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9571da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 958d4627a28SSchspa Shi ssize_t ret = -EBUSY; 9596eed9404SViresh Kumar 960e6e8df07SKai Shen if (!fattr->show) 961e6e8df07SKai Shen return -EIO; 962e6e8df07SKai Shen 963ad7722daSviresh kumar down_read(&policy->rwsem); 964d4627a28SSchspa Shi if (likely(!policy_is_inactive(policy))) 965e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 966ad7722daSviresh kumar up_read(&policy->rwsem); 9671b750e3bSViresh Kumar 9681da177e4SLinus Torvalds return ret; 9691da177e4SLinus Torvalds } 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 9721da177e4SLinus Torvalds const char *buf, size_t count) 9731da177e4SLinus Torvalds { 9741da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9751da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 976d4627a28SSchspa Shi ssize_t ret = -EBUSY; 9776eed9404SViresh Kumar 978e6e8df07SKai Shen if (!fattr->store) 979e6e8df07SKai Shen return -EIO; 980e6e8df07SKai Shen 981ad7722daSviresh kumar down_write(&policy->rwsem); 982d4627a28SSchspa Shi if (likely(!policy_is_inactive(policy))) 983e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 984ad7722daSviresh kumar up_write(&policy->rwsem); 9854f750c93SSrivatsa S. Bhat 9861da177e4SLinus Torvalds return ret; 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 9891da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 9901da177e4SLinus Torvalds { 9911da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9922d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 9931da177e4SLinus Torvalds complete(&policy->kobj_unregister); 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds 99652cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 9971da177e4SLinus Torvalds .show = show, 9981da177e4SLinus Torvalds .store = store, 9991da177e4SLinus Torvalds }; 10001da177e4SLinus Torvalds 1001108fcad9SThomas Weißschuh static const struct kobj_type ktype_cpufreq = { 10021da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 1003fe262d5cSGreg Kroah-Hartman .default_groups = cpufreq_groups, 10041da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 10051da177e4SLinus Torvalds }; 10061da177e4SLinus Torvalds 10072c1b5a84SXiongfeng Wang static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu, 10082c1b5a84SXiongfeng Wang struct device *dev) 100987549141SViresh Kumar { 101067d874c3SViresh Kumar if (unlikely(!dev)) 10112f0ba790SRafael J. Wysocki return; 10122f0ba790SRafael J. Wysocki 10132f0ba790SRafael J. Wysocki if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) 10142f0ba790SRafael J. Wysocki return; 10152f0ba790SRafael J. Wysocki 101626619804SViresh Kumar dev_dbg(dev, "%s: Adding symlink\n", __func__); 10172f0ba790SRafael J. Wysocki if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) 10182f0ba790SRafael J. Wysocki dev_err(dev, "cpufreq symlink creation failed\n"); 101987549141SViresh Kumar } 102087549141SViresh Kumar 10215c84c1b8SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu, 102226619804SViresh Kumar struct device *dev) 102387549141SViresh Kumar { 102426619804SViresh Kumar dev_dbg(dev, "%s: Removing symlink\n", __func__); 102526619804SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 10265c84c1b8SViresh Kumar cpumask_clear_cpu(cpu, policy->real_cpus); 102787549141SViresh Kumar } 102887549141SViresh Kumar 1029d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 1030909a694eSDave Jones { 1031909a694eSDave Jones struct freq_attr **drv_attr; 1032909a694eSDave Jones int ret = 0; 1033909a694eSDave Jones 1034909a694eSDave Jones /* set up files for this cpu device */ 10351c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 1036f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 1037909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 1038909a694eSDave Jones if (ret) 10396d4e81edSTomeu Vizoso return ret; 1040909a694eSDave Jones drv_attr++; 1041909a694eSDave Jones } 10421c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 1043909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 1044909a694eSDave Jones if (ret) 10456d4e81edSTomeu Vizoso return ret; 1046909a694eSDave Jones } 1047c034b02eSDirk Brandewie 1048909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 1049909a694eSDave Jones if (ret) 10506d4e81edSTomeu Vizoso return ret; 1051c034b02eSDirk Brandewie 10521c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 1053e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 1054e2f74f35SThomas Renninger if (ret) 10556d4e81edSTomeu Vizoso return ret; 1056e2f74f35SThomas Renninger } 1057909a694eSDave Jones 105826619804SViresh Kumar return 0; 1059e18f1682SSrivatsa S. Bhat } 1060e18f1682SSrivatsa S. Bhat 10617f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy) 1062e18f1682SSrivatsa S. Bhat { 10631e4f63aeSRafael J. Wysocki struct cpufreq_governor *gov = NULL; 10641e4f63aeSRafael J. Wysocki unsigned int pol = CPUFREQ_POLICY_UNKNOWN; 10658cc46ae5SViresh Kumar int ret; 1066ab05d97aSYue Hu 1067ab05d97aSYue Hu if (has_target()) { 10681e4f63aeSRafael J. Wysocki /* Update policy governor to the one used before hotplug. */ 10698cc46ae5SViresh Kumar gov = get_governor(policy->last_governor); 1070de1df26bSRafael J. Wysocki if (gov) { 10716e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 10728412b456SQuentin Perret gov->name, policy->cpu); 10731e4f63aeSRafael J. Wysocki } else { 10748412b456SQuentin Perret gov = get_governor(default_governor); 1075de1df26bSRafael J. Wysocki } 10768412b456SQuentin Perret 10778412b456SQuentin Perret if (!gov) { 10788412b456SQuentin Perret gov = cpufreq_default_governor(); 10798412b456SQuentin Perret __module_get(gov->owner); 10808412b456SQuentin Perret } 10818412b456SQuentin Perret 1082ab05d97aSYue Hu } else { 10838412b456SQuentin Perret 108469030dd1SSrinivas Pandruvada /* Use the default policy if there is no last_policy. */ 1085ab05d97aSYue Hu if (policy->last_policy) { 10861e4f63aeSRafael J. Wysocki pol = policy->last_policy; 10878412b456SQuentin Perret } else { 10888412b456SQuentin Perret pol = cpufreq_parse_policy(default_governor); 1089f5739cb0SRafael J. Wysocki /* 10908412b456SQuentin Perret * In case the default governor is neither "performance" 1091f5739cb0SRafael J. Wysocki * nor "powersave", fall back to the initial policy 1092f5739cb0SRafael J. Wysocki * value set by the driver. 1093f5739cb0SRafael J. Wysocki */ 1094f5739cb0SRafael J. Wysocki if (pol == CPUFREQ_POLICY_UNKNOWN) 1095f5739cb0SRafael J. Wysocki pol = policy->policy; 109669030dd1SSrinivas Pandruvada } 1097f5739cb0SRafael J. Wysocki if (pol != CPUFREQ_POLICY_PERFORMANCE && 1098f5739cb0SRafael J. Wysocki pol != CPUFREQ_POLICY_POWERSAVE) 1099f5739cb0SRafael J. Wysocki return -ENODATA; 1100ab05d97aSYue Hu } 1101ab05d97aSYue Hu 11028cc46ae5SViresh Kumar ret = cpufreq_set_policy(policy, gov, pol); 11038cc46ae5SViresh Kumar if (gov) 11048cc46ae5SViresh Kumar module_put(gov->owner); 11058cc46ae5SViresh Kumar 11068cc46ae5SViresh Kumar return ret; 1107909a694eSDave Jones } 1108909a694eSDave Jones 1109d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1110fcf80582SViresh Kumar { 11119c0ebcf7SViresh Kumar int ret = 0; 1112fcf80582SViresh Kumar 1113bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 1114bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 1115bb29ae15SViresh Kumar return 0; 1116bb29ae15SViresh Kumar 111749f18560SViresh Kumar down_write(&policy->rwsem); 111845482c70SRafael J. Wysocki if (has_target()) 111945482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 1120fcf80582SViresh Kumar 1121fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 11222eaa3e2dSViresh Kumar 11239c0ebcf7SViresh Kumar if (has_target()) { 11240a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 112549f18560SViresh Kumar if (ret) 11263de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 1127820c6ca2SViresh Kumar } 112849f18560SViresh Kumar up_write(&policy->rwsem); 1129fcf80582SViresh Kumar return ret; 1130fcf80582SViresh Kumar } 1131fcf80582SViresh Kumar 1132c57b25bdSViresh Kumar void refresh_frequency_limits(struct cpufreq_policy *policy) 113370a59fdeSViresh Kumar { 113467d874c3SViresh Kumar if (!policy_is_inactive(policy)) { 113570a59fdeSViresh Kumar pr_debug("updating policy for CPU %u\n", policy->cpu); 113670a59fdeSViresh Kumar 11371e4f63aeSRafael J. Wysocki cpufreq_set_policy(policy, policy->governor, policy->policy); 113870a59fdeSViresh Kumar } 113967d874c3SViresh Kumar } 1140c57b25bdSViresh Kumar EXPORT_SYMBOL(refresh_frequency_limits); 114170a59fdeSViresh Kumar 114211eb69b9SViresh Kumar static void handle_update(struct work_struct *work) 114311eb69b9SViresh Kumar { 114411eb69b9SViresh Kumar struct cpufreq_policy *policy = 114511eb69b9SViresh Kumar container_of(work, struct cpufreq_policy, update); 114670a59fdeSViresh Kumar 114770a59fdeSViresh Kumar pr_debug("handle_update for cpu %u called\n", policy->cpu); 114867d874c3SViresh Kumar down_write(&policy->rwsem); 114970a59fdeSViresh Kumar refresh_frequency_limits(policy); 115067d874c3SViresh Kumar up_write(&policy->rwsem); 1151fcf80582SViresh Kumar } 11521da177e4SLinus Torvalds 115367d874c3SViresh Kumar static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq, 115467d874c3SViresh Kumar void *data) 11558414809cSSrivatsa S. Bhat { 115667d874c3SViresh Kumar struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min); 1157e9698cc5SSrivatsa S. Bhat 115867d874c3SViresh Kumar schedule_work(&policy->update); 115967d874c3SViresh Kumar return 0; 1160edd4a893SViresh Kumar } 1161edd4a893SViresh Kumar 116267d874c3SViresh Kumar static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq, 116367d874c3SViresh Kumar void *data) 116467d874c3SViresh Kumar { 116567d874c3SViresh Kumar struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max); 1166ad7722daSviresh kumar 116767d874c3SViresh Kumar schedule_work(&policy->update); 116867d874c3SViresh Kumar return 0; 1169e9698cc5SSrivatsa S. Bhat } 1170e9698cc5SSrivatsa S. Bhat 1171f9f41e3eSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 117242f921a6SViresh Kumar { 117342f921a6SViresh Kumar struct kobject *kobj; 117442f921a6SViresh Kumar struct completion *cmp; 117542f921a6SViresh Kumar 117687549141SViresh Kumar down_write(&policy->rwsem); 11771aefc75bSRafael J. Wysocki cpufreq_stats_free_table(policy); 117842f921a6SViresh Kumar kobj = &policy->kobj; 117942f921a6SViresh Kumar cmp = &policy->kobj_unregister; 118087549141SViresh Kumar up_write(&policy->rwsem); 118142f921a6SViresh Kumar kobject_put(kobj); 118242f921a6SViresh Kumar 118342f921a6SViresh Kumar /* 118442f921a6SViresh Kumar * We need to make sure that the underlying kobj is 118542f921a6SViresh Kumar * actually not referenced anymore by anybody before we 118642f921a6SViresh Kumar * proceed with unloading. 118742f921a6SViresh Kumar */ 118842f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 118942f921a6SViresh Kumar wait_for_completion(cmp); 119042f921a6SViresh Kumar pr_debug("wait complete\n"); 119142f921a6SViresh Kumar } 119242f921a6SViresh Kumar 119367d874c3SViresh Kumar static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 119467d874c3SViresh Kumar { 119567d874c3SViresh Kumar struct cpufreq_policy *policy; 119667d874c3SViresh Kumar struct device *dev = get_cpu_device(cpu); 119767d874c3SViresh Kumar int ret; 119867d874c3SViresh Kumar 119967d874c3SViresh Kumar if (!dev) 120067d874c3SViresh Kumar return NULL; 120167d874c3SViresh Kumar 120267d874c3SViresh Kumar policy = kzalloc(sizeof(*policy), GFP_KERNEL); 120367d874c3SViresh Kumar if (!policy) 120467d874c3SViresh Kumar return NULL; 120567d874c3SViresh Kumar 120667d874c3SViresh Kumar if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 120767d874c3SViresh Kumar goto err_free_policy; 120867d874c3SViresh Kumar 120967d874c3SViresh Kumar if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 121067d874c3SViresh Kumar goto err_free_cpumask; 121167d874c3SViresh Kumar 121267d874c3SViresh Kumar if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 121367d874c3SViresh Kumar goto err_free_rcpumask; 121467d874c3SViresh Kumar 12155c510548SYongqiang Liu init_completion(&policy->kobj_unregister); 121667d874c3SViresh Kumar ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 121767d874c3SViresh Kumar cpufreq_global_kobject, "policy%u", cpu); 121867d874c3SViresh Kumar if (ret) { 121967d874c3SViresh Kumar dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); 122067d874c3SViresh Kumar /* 122167d874c3SViresh Kumar * The entire policy object will be freed below, but the extra 122267d874c3SViresh Kumar * memory allocated for the kobject name needs to be freed by 122367d874c3SViresh Kumar * releasing the kobject. 122467d874c3SViresh Kumar */ 122567d874c3SViresh Kumar kobject_put(&policy->kobj); 122667d874c3SViresh Kumar goto err_free_real_cpus; 122767d874c3SViresh Kumar } 122867d874c3SViresh Kumar 12293000ce3cSRafael J. Wysocki freq_constraints_init(&policy->constraints); 12303000ce3cSRafael J. Wysocki 123167d874c3SViresh Kumar policy->nb_min.notifier_call = cpufreq_notifier_min; 123267d874c3SViresh Kumar policy->nb_max.notifier_call = cpufreq_notifier_max; 123367d874c3SViresh Kumar 12343000ce3cSRafael J. Wysocki ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, 12353000ce3cSRafael J. Wysocki &policy->nb_min); 123667d874c3SViresh Kumar if (ret) { 1237ba6ea77dSLiao Chang dev_err(dev, "Failed to register MIN QoS notifier: %d (CPU%u)\n", 1238ba6ea77dSLiao Chang ret, cpu); 123967d874c3SViresh Kumar goto err_kobj_remove; 124067d874c3SViresh Kumar } 124167d874c3SViresh Kumar 12423000ce3cSRafael J. Wysocki ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, 12433000ce3cSRafael J. Wysocki &policy->nb_max); 124467d874c3SViresh Kumar if (ret) { 1245ba6ea77dSLiao Chang dev_err(dev, "Failed to register MAX QoS notifier: %d (CPU%u)\n", 1246ba6ea77dSLiao Chang ret, cpu); 124767d874c3SViresh Kumar goto err_min_qos_notifier; 124867d874c3SViresh Kumar } 124967d874c3SViresh Kumar 125067d874c3SViresh Kumar INIT_LIST_HEAD(&policy->policy_list); 125167d874c3SViresh Kumar init_rwsem(&policy->rwsem); 125267d874c3SViresh Kumar spin_lock_init(&policy->transition_lock); 125367d874c3SViresh Kumar init_waitqueue_head(&policy->transition_wait); 125467d874c3SViresh Kumar INIT_WORK(&policy->update, handle_update); 125567d874c3SViresh Kumar 125667d874c3SViresh Kumar policy->cpu = cpu; 125767d874c3SViresh Kumar return policy; 125867d874c3SViresh Kumar 125967d874c3SViresh Kumar err_min_qos_notifier: 12603000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 12613000ce3cSRafael J. Wysocki &policy->nb_min); 126267d874c3SViresh Kumar err_kobj_remove: 126367d874c3SViresh Kumar cpufreq_policy_put_kobj(policy); 126467d874c3SViresh Kumar err_free_real_cpus: 126567d874c3SViresh Kumar free_cpumask_var(policy->real_cpus); 126667d874c3SViresh Kumar err_free_rcpumask: 126767d874c3SViresh Kumar free_cpumask_var(policy->related_cpus); 126867d874c3SViresh Kumar err_free_cpumask: 126967d874c3SViresh Kumar free_cpumask_var(policy->cpus); 127067d874c3SViresh Kumar err_free_policy: 127167d874c3SViresh Kumar kfree(policy); 127267d874c3SViresh Kumar 127367d874c3SViresh Kumar return NULL; 127467d874c3SViresh Kumar } 127567d874c3SViresh Kumar 1276f9f41e3eSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy) 1277e9698cc5SSrivatsa S. Bhat { 1278988bed09SViresh Kumar unsigned long flags; 1279988bed09SViresh Kumar int cpu; 1280988bed09SViresh Kumar 1281a2f6a7acSViresh Kumar /* 1282a2f6a7acSViresh Kumar * The callers must ensure the policy is inactive by now, to avoid any 1283a2f6a7acSViresh Kumar * races with show()/store() callbacks. 1284a2f6a7acSViresh Kumar */ 1285a2f6a7acSViresh Kumar if (unlikely(!policy_is_inactive(policy))) 1286a2f6a7acSViresh Kumar pr_warn("%s: Freeing active policy\n", __func__); 1287a2f6a7acSViresh Kumar 1288988bed09SViresh Kumar /* Remove policy from list */ 1289988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1290988bed09SViresh Kumar list_del(&policy->policy_list); 1291988bed09SViresh Kumar 1292988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1293988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1294988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1295988bed09SViresh Kumar 12963000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, 12973000ce3cSRafael J. Wysocki &policy->nb_max); 12983000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 12993000ce3cSRafael J. Wysocki &policy->nb_min); 13006a149036SViresh Kumar 13016941051dSSudeep Holla /* Cancel any pending policy->update work before freeing the policy. */ 13026941051dSSudeep Holla cancel_work_sync(&policy->update); 13036a149036SViresh Kumar 13046a149036SViresh Kumar if (policy->max_freq_req) { 13056a149036SViresh Kumar /* 13061e81d3e0STang Yizhou * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY 13071e81d3e0STang Yizhou * notification, since CPUFREQ_CREATE_POLICY notification was 13081e81d3e0STang Yizhou * sent after adding max_freq_req earlier. 13096a149036SViresh Kumar */ 13106a149036SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 13116a149036SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 13123000ce3cSRafael J. Wysocki freq_qos_remove_request(policy->max_freq_req); 13136a149036SViresh Kumar } 13146a149036SViresh Kumar 13153000ce3cSRafael J. Wysocki freq_qos_remove_request(policy->min_freq_req); 131618c49926SViresh Kumar kfree(policy->min_freq_req); 131767d874c3SViresh Kumar 1318f9f41e3eSViresh Kumar cpufreq_policy_put_kobj(policy); 1319559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1320e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1321e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1322e9698cc5SSrivatsa S. Bhat kfree(policy); 1323e9698cc5SSrivatsa S. Bhat } 1324e9698cc5SSrivatsa S. Bhat 13250b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 13261da177e4SLinus Torvalds { 13277f0c020aSViresh Kumar struct cpufreq_policy *policy; 1328194d99c7SRafael J. Wysocki bool new_policy; 13291da177e4SLinus Torvalds unsigned long flags; 13300b275352SRafael J. Wysocki unsigned int j; 13310b275352SRafael J. Wysocki int ret; 1332c32b6b8eSAshok Raj 13330b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 13346eed9404SViresh Kumar 1335bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 13369104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 133711ce707eSRafael J. Wysocki if (policy) { 13389104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 133911ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1340d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 13411da177e4SLinus Torvalds 134211ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1343194d99c7SRafael J. Wysocki new_policy = false; 134411ce707eSRafael J. Wysocki down_write(&policy->rwsem); 134511ce707eSRafael J. Wysocki policy->cpu = cpu; 134611ce707eSRafael J. Wysocki policy->governor = NULL; 134711ce707eSRafael J. Wysocki } else { 1348194d99c7SRafael J. Wysocki new_policy = true; 1349a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1350059019a3SDave Jones if (!policy) 1351d4d854d6SRafael J. Wysocki return -ENOMEM; 1352514ff1bcSSchspa Shi down_write(&policy->rwsem); 135372368d12SRafael J. Wysocki } 13540d66b91eSSrivatsa S. Bhat 135591a12e91SViresh Kumar if (!new_policy && cpufreq_driver->online) { 135668315f1aSPierre Gondois /* Recover policy->cpus using related_cpus */ 135768315f1aSPierre Gondois cpumask_copy(policy->cpus, policy->related_cpus); 135868315f1aSPierre Gondois 135991a12e91SViresh Kumar ret = cpufreq_driver->online(policy); 136091a12e91SViresh Kumar if (ret) { 136191a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 136291a12e91SViresh Kumar __LINE__); 136391a12e91SViresh Kumar goto out_exit_policy; 136491a12e91SViresh Kumar } 136591a12e91SViresh Kumar } else { 1366835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 13671da177e4SLinus Torvalds 136891a12e91SViresh Kumar /* 136991a12e91SViresh Kumar * Call driver. From then on the cpufreq must be able 137091a12e91SViresh Kumar * to accept all calls to ->verify and ->setpolicy for this CPU. 13711da177e4SLinus Torvalds */ 13721c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 13731da177e4SLinus Torvalds if (ret) { 137491a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 137591a12e91SViresh Kumar __LINE__); 13768101f997SViresh Kumar goto out_free_policy; 13771da177e4SLinus Torvalds } 1378643ae6e8SViresh Kumar 13793b718057SRafael J. Wysocki /* 13803b718057SRafael J. Wysocki * The initialization has succeeded and the policy is online. 13813b718057SRafael J. Wysocki * If there is a problem with its frequency table, take it 13823b718057SRafael J. Wysocki * offline and drop it. 13833b718057SRafael J. Wysocki */ 1384d417e069SViresh Kumar ret = cpufreq_table_validate_and_sort(policy); 1385d417e069SViresh Kumar if (ret) 13863b718057SRafael J. Wysocki goto out_offline_policy; 1387d417e069SViresh Kumar 13884d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 13890998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 13904d1f3a5bSRafael J. Wysocki } 1391559ed407SRafael J. Wysocki 13925a7e56a5SViresh Kumar /* 13935a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 13945a7e56a5SViresh Kumar * managing offline cpus here. 13955a7e56a5SViresh Kumar */ 13965a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 13975a7e56a5SViresh Kumar 1398194d99c7SRafael J. Wysocki if (new_policy) { 13992f0ba790SRafael J. Wysocki for_each_cpu(j, policy->related_cpus) { 1400652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 14012c1b5a84SXiongfeng Wang add_cpu_dev_symlink(policy, j, get_cpu_device(j)); 14022f0ba790SRafael J. Wysocki } 140318c49926SViresh Kumar 140418c49926SViresh Kumar policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), 140518c49926SViresh Kumar GFP_KERNEL); 1406b96f0384SWang ShaoBo if (!policy->min_freq_req) { 1407b96f0384SWang ShaoBo ret = -ENOMEM; 140818c49926SViresh Kumar goto out_destroy_policy; 1409b96f0384SWang ShaoBo } 141018c49926SViresh Kumar 14113000ce3cSRafael J. Wysocki ret = freq_qos_add_request(&policy->constraints, 14123000ce3cSRafael J. Wysocki policy->min_freq_req, FREQ_QOS_MIN, 1413521223d8SRafael J. Wysocki FREQ_QOS_MIN_DEFAULT_VALUE); 141418c49926SViresh Kumar if (ret < 0) { 141518c49926SViresh Kumar /* 14163000ce3cSRafael J. Wysocki * So we don't call freq_qos_remove_request() for an 141718c49926SViresh Kumar * uninitialized request. 141818c49926SViresh Kumar */ 141918c49926SViresh Kumar kfree(policy->min_freq_req); 142018c49926SViresh Kumar policy->min_freq_req = NULL; 142118c49926SViresh Kumar goto out_destroy_policy; 142218c49926SViresh Kumar } 142318c49926SViresh Kumar 142418c49926SViresh Kumar /* 142518c49926SViresh Kumar * This must be initialized right here to avoid calling 14263000ce3cSRafael J. Wysocki * freq_qos_remove_request() on uninitialized request in case 142718c49926SViresh Kumar * of errors. 142818c49926SViresh Kumar */ 142918c49926SViresh Kumar policy->max_freq_req = policy->min_freq_req + 1; 143018c49926SViresh Kumar 14313000ce3cSRafael J. Wysocki ret = freq_qos_add_request(&policy->constraints, 14323000ce3cSRafael J. Wysocki policy->max_freq_req, FREQ_QOS_MAX, 1433521223d8SRafael J. Wysocki FREQ_QOS_MAX_DEFAULT_VALUE); 143418c49926SViresh Kumar if (ret < 0) { 143518c49926SViresh Kumar policy->max_freq_req = NULL; 143618c49926SViresh Kumar goto out_destroy_policy; 143718c49926SViresh Kumar } 14386a149036SViresh Kumar 14396a149036SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 14406a149036SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 1441988bed09SViresh Kumar } 1442652ed95dSViresh Kumar 14435ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target()) { 1444da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1445da60ce9fSViresh Kumar if (!policy->cur) { 1446b96f0384SWang ShaoBo ret = -EIO; 1447da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1448d417e069SViresh Kumar goto out_destroy_policy; 1449da60ce9fSViresh Kumar } 1450da60ce9fSViresh Kumar } 1451da60ce9fSViresh Kumar 1452d3916691SViresh Kumar /* 1453d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1454d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1455d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1456d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1457d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1458d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1459d3916691SViresh Kumar * isn't found in freq-table. 1460d3916691SViresh Kumar * 1461d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1462d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1463d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1464d3916691SViresh Kumar * is initialized to zero). 1465d3916691SViresh Kumar * 1466d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1467d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1468d3916691SViresh Kumar * equal to target-freq. 1469d3916691SViresh Kumar */ 1470d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1471d3916691SViresh Kumar && has_target()) { 147297148d0aSViresh Kumar unsigned int old_freq = policy->cur; 147397148d0aSViresh Kumar 1474d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 147597148d0aSViresh Kumar ret = cpufreq_frequency_table_get_index(policy, old_freq); 1476d3916691SViresh Kumar if (ret == -EINVAL) { 147797148d0aSViresh Kumar ret = __cpufreq_driver_target(policy, old_freq - 1, 1478d3916691SViresh Kumar CPUFREQ_RELATION_L); 1479d3916691SViresh Kumar 1480d3916691SViresh Kumar /* 1481d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1482d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1483d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1484d3916691SViresh Kumar */ 1485d3916691SViresh Kumar BUG_ON(ret); 148697148d0aSViresh Kumar pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n", 148797148d0aSViresh Kumar __func__, policy->cpu, old_freq, policy->cur); 1488d3916691SViresh Kumar } 1489d3916691SViresh Kumar } 1490d3916691SViresh Kumar 1491194d99c7SRafael J. Wysocki if (new_policy) { 1492d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 149319d6f7ecSDave Jones if (ret) 1494d417e069SViresh Kumar goto out_destroy_policy; 14951aefc75bSRafael J. Wysocki 14961aefc75bSRafael J. Wysocki cpufreq_stats_create_table(policy); 1497c88a1f8bSLukasz Majewski 1498c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1499c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1500c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1501c17495b0SViresh Kumar 1502c17495b0SViresh Kumar /* 1503c17495b0SViresh Kumar * Register with the energy model before 1504c17495b0SViresh Kumar * sched_cpufreq_governor_change() is called, which will result 1505c17495b0SViresh Kumar * in rebuilding of the sched domains, which should only be done 1506c17495b0SViresh Kumar * once the energy model is properly initialized for the policy 1507c17495b0SViresh Kumar * first. 1508c17495b0SViresh Kumar * 1509c17495b0SViresh Kumar * Also, this should be called before the policy is registered 1510c17495b0SViresh Kumar * with cooling framework. 1511c17495b0SViresh Kumar */ 1512c17495b0SViresh Kumar if (cpufreq_driver->register_em) 1513c17495b0SViresh Kumar cpufreq_driver->register_em(policy); 1514988bed09SViresh Kumar } 15158ff69732SDave Jones 15167f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 15177f0fa40fSViresh Kumar if (ret) { 15187f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 15197f0fa40fSViresh Kumar __func__, cpu, ret); 1520d417e069SViresh Kumar goto out_destroy_policy; 152108fd8c1cSViresh Kumar } 1522e18f1682SSrivatsa S. Bhat 15234e97b631SViresh Kumar up_write(&policy->rwsem); 152408fd8c1cSViresh Kumar 1525038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 15267c45cf31SViresh Kumar 15274f774c4aSBjorn Andersson /* Callback for handling stuff after policy is ready */ 15284f774c4aSBjorn Andersson if (cpufreq_driver->ready) 15294f774c4aSBjorn Andersson cpufreq_driver->ready(policy); 15304f774c4aSBjorn Andersson 1531bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) 15325c238a8bSAmit Kucheria policy->cdev = of_cpufreq_cooling_register(policy); 15335c238a8bSAmit Kucheria 15342d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds return 0; 15371da177e4SLinus Torvalds 1538d417e069SViresh Kumar out_destroy_policy: 1539b24b6478SViresh Kumar for_each_cpu(j, policy->real_cpus) 15405c84c1b8SViresh Kumar remove_cpu_dev_symlink(policy, j, get_cpu_device(j)); 1541b24b6478SViresh Kumar 15423b718057SRafael J. Wysocki out_offline_policy: 15433b718057SRafael J. Wysocki if (cpufreq_driver->offline) 15443b718057SRafael J. Wysocki cpufreq_driver->offline(policy); 15453b718057SRafael J. Wysocki 1546d417e069SViresh Kumar out_exit_policy: 1547da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1548da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 15492f0ba790SRafael J. Wysocki 15508101f997SViresh Kumar out_free_policy: 1551a2f6a7acSViresh Kumar cpumask_clear(policy->cpus); 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 */ 173544295af5SSanjay Chandrashekara if (abs(policy->cur - new_freq) < KHZ_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)) { 1946*1f464cb4SLiao Chang pr_err("%s: Failed to resume driver: %s\n", __func__, 1947*1f464cb4SLiao Chang cpufreq_driver->name); 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) 1954*1f464cb4SLiao Chang pr_err("%s: Failed to start governor for CPU%u's policy\n", 1955*1f464cb4SLiao Chang __func__, policy->cpu); 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) || 2831b4a11fa3SWyes Karny (!driver_data->online != !driver_data->offline) || 2832b4a11fa3SWyes Karny (driver_data->adjust_perf && !driver_data->fast_switch)) 28331da177e4SLinus Torvalds return -EINVAL; 28341da177e4SLinus Torvalds 28352d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 28361da177e4SLinus Torvalds 2837fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2838a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 2839fdd320daSRafael J. Wysocki 28400d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 28411c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 28420d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2843fdd320daSRafael J. Wysocki ret = -EEXIST; 2844fdd320daSRafael J. Wysocki goto out; 28451da177e4SLinus Torvalds } 28461c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 28470d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 28481da177e4SLinus Torvalds 2849874f6353SIonela Voinescu /* 2850874f6353SIonela Voinescu * Mark support for the scheduler's frequency invariance engine for 2851874f6353SIonela Voinescu * drivers that implement target(), target_index() or fast_switch(). 2852874f6353SIonela Voinescu */ 2853874f6353SIonela Voinescu if (!cpufreq_driver->setpolicy) { 2854874f6353SIonela Voinescu static_branch_enable_cpuslocked(&cpufreq_freq_invariance); 2855874f6353SIonela Voinescu pr_debug("supports frequency invariance"); 2856874f6353SIonela Voinescu } 2857874f6353SIonela Voinescu 2858bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2859bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2860bc68b7dfSViresh Kumar 28617a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 286244139ed4SViresh Kumar ret = create_boost_sysfs_file(); 286344139ed4SViresh Kumar if (ret) 28646f19efc0SLukasz Majewski goto err_null_driver; 28657a6c79f2SRafael J. Wysocki } 28666f19efc0SLukasz Majewski 28678a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 28688f5bc2abSJiri Slaby if (ret) 28696f19efc0SLukasz Majewski goto err_boost_unreg; 28701da177e4SLinus Torvalds 28715ae4a4b4SViresh Kumar if (unlikely(list_empty(&cpufreq_policy_list))) { 28721da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 28736c770036SDavid Arcari ret = -ENODEV; 2874ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2875e08f5f5bSGautham R Shenoy driver_data->name); 28768a25a2fdSKay Sievers goto err_if_unreg; 28771da177e4SLinus Torvalds } 28781da177e4SLinus Torvalds 2879a92551e4SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, 2880a92551e4SSebastian Andrzej Siewior "cpufreq:online", 2881c4a3fa26SChen Yu cpuhp_cpufreq_online, 2882c4a3fa26SChen Yu cpuhp_cpufreq_offline); 288327622b06SSebastian Andrzej Siewior if (ret < 0) 288427622b06SSebastian Andrzej Siewior goto err_if_unreg; 288527622b06SSebastian Andrzej Siewior hp_online = ret; 28865372e054SSebastian Andrzej Siewior ret = 0; 288727622b06SSebastian Andrzej Siewior 28882d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 28893834abb4SPankaj Gupta goto out; 2890fdd320daSRafael J. Wysocki 28918a25a2fdSKay Sievers err_if_unreg: 28928a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 28936f19efc0SLukasz Majewski err_boost_unreg: 289444139ed4SViresh Kumar remove_boost_sysfs_file(); 28958f5bc2abSJiri Slaby err_null_driver: 28960d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 28971c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 28980d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 28993834abb4SPankaj Gupta out: 2900a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 29013834abb4SPankaj Gupta return ret; 29021da177e4SLinus Torvalds } 29031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 29041da177e4SLinus Torvalds 2905a9909c21SLee Jones /* 29061da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 29071da177e4SLinus Torvalds * 29081da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 29091da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 29101da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 29111da177e4SLinus Torvalds * currently not initialised. 29121da177e4SLinus Torvalds */ 2913dd329e1eSUwe Kleine-König void cpufreq_unregister_driver(struct cpufreq_driver *driver) 29141da177e4SLinus Torvalds { 29151da177e4SLinus Torvalds unsigned long flags; 29161da177e4SLinus Torvalds 2917dd329e1eSUwe Kleine-König if (WARN_ON(!cpufreq_driver || (driver != cpufreq_driver))) 2918dd329e1eSUwe Kleine-König return; 29191da177e4SLinus Torvalds 29202d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 29211da177e4SLinus Torvalds 2922454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2923a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 29248a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 292544139ed4SViresh Kumar remove_boost_sysfs_file(); 2926874f6353SIonela Voinescu static_branch_disable_cpuslocked(&cpufreq_freq_invariance); 2927a92551e4SSebastian Andrzej Siewior cpuhp_remove_state_nocalls_cpuslocked(hp_online); 29281da177e4SLinus Torvalds 29290d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 29306eed9404SViresh Kumar 29311c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 29326eed9404SViresh Kumar 29330d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2934a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 29351da177e4SLinus Torvalds } 29361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 29375a01f2e8SVenkatesh Pallipadi 29385a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 29395a01f2e8SVenkatesh Pallipadi { 29408412b456SQuentin Perret struct cpufreq_governor *gov = cpufreq_default_governor(); 29412744a63cSGreg Kroah-Hartman struct device *dev_root; 29428412b456SQuentin Perret 2943a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2944a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2945a7b422cdSKonrad Rzeszutek Wilk 29462744a63cSGreg Kroah-Hartman dev_root = bus_get_dev_root(&cpu_subsys); 29472744a63cSGreg Kroah-Hartman if (dev_root) { 29482744a63cSGreg Kroah-Hartman cpufreq_global_kobject = kobject_create_and_add("cpufreq", &dev_root->kobj); 29492744a63cSGreg Kroah-Hartman put_device(dev_root); 29502744a63cSGreg Kroah-Hartman } 29518aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 29528aa84ad8SThomas Renninger 29538412b456SQuentin Perret if (!strlen(default_governor)) 29548412b456SQuentin Perret strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN); 29558412b456SQuentin Perret 29565a01f2e8SVenkatesh Pallipadi return 0; 29575a01f2e8SVenkatesh Pallipadi } 2958d82f2692SLen Brown module_param(off, int, 0444); 29598412b456SQuentin Perret module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); 29605a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2961