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> 316f4f2723SThomas Renninger #include <trace/events/power.h> 326f4f2723SThomas Renninger 33b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list); 34f963735aSViresh Kumar 35f963735aSViresh Kumar /* Macros to iterate over CPU policies */ 36f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active) \ 37fd7dc7e6SEric Biggers list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ 38fd7dc7e6SEric Biggers if ((__active) == !policy_is_inactive(__policy)) 39f963735aSViresh Kumar 40f963735aSViresh Kumar #define for_each_active_policy(__policy) \ 41f963735aSViresh Kumar for_each_suitable_policy(__policy, true) 42f963735aSViresh Kumar #define for_each_inactive_policy(__policy) \ 43f963735aSViresh Kumar for_each_suitable_policy(__policy, false) 44f963735aSViresh Kumar 45b4f0676fSViresh Kumar #define for_each_policy(__policy) \ 46b4f0676fSViresh Kumar list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) 47b4f0676fSViresh Kumar 48f7b27061SViresh Kumar /* Iterate over governors */ 49f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 50f7b27061SViresh Kumar #define for_each_governor(__governor) \ 51f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 52f7b27061SViresh Kumar 538412b456SQuentin Perret static char default_governor[CPUFREQ_NAME_LEN]; 548412b456SQuentin Perret 55a9909c21SLee Jones /* 56cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 571da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 581da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 591da177e4SLinus Torvalds */ 601c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 617a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 62bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 63bb176f7dSViresh Kumar 64874f6353SIonela Voinescu static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance); 65874f6353SIonela Voinescu bool cpufreq_supports_freq_invariance(void) 66874f6353SIonela Voinescu { 67874f6353SIonela Voinescu return static_branch_likely(&cpufreq_freq_invariance); 68874f6353SIonela Voinescu } 69874f6353SIonela Voinescu 702f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 712f0aea93SViresh Kumar static bool cpufreq_suspended; 721da177e4SLinus Torvalds 739c0ebcf7SViresh Kumar static inline bool has_target(void) 749c0ebcf7SViresh Kumar { 759c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 769c0ebcf7SViresh Kumar } 779c0ebcf7SViresh Kumar 781da177e4SLinus Torvalds /* internal prototypes */ 79d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 80a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy); 81a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy); 82a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy); 831e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy, 841e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov, 851e4f63aeSRafael J. Wysocki unsigned int new_pol); 8645482c70SRafael J. Wysocki 87a9909c21SLee Jones /* 881da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 891da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 901da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 911da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 921da177e4SLinus Torvalds * The mutex locks both lists. 931da177e4SLinus Torvalds */ 94e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 95cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); 961da177e4SLinus Torvalds 97a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 98da584455SViresh Kumar static int cpufreq_disabled(void) 99a7b422cdSKonrad Rzeszutek Wilk { 100a7b422cdSKonrad Rzeszutek Wilk return off; 101a7b422cdSKonrad Rzeszutek Wilk } 102a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 103a7b422cdSKonrad Rzeszutek Wilk { 104a7b422cdSKonrad Rzeszutek Wilk off = 1; 105a7b422cdSKonrad Rzeszutek Wilk } 1063fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1071da177e4SLinus Torvalds 1084d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1094d5dcc42SViresh Kumar { 1100b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1114d5dcc42SViresh Kumar } 1123f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1134d5dcc42SViresh Kumar 114183edb20SYangtao Li static struct kobject *cpufreq_global_kobject; 115183edb20SYangtao Li 116944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 117944e9a03SViresh Kumar { 118944e9a03SViresh Kumar if (have_governor_per_policy()) 119944e9a03SViresh Kumar return &policy->kobj; 120944e9a03SViresh Kumar else 121944e9a03SViresh Kumar return cpufreq_global_kobject; 122944e9a03SViresh Kumar } 123944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 124944e9a03SViresh Kumar 12572a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 12672a4ce34SViresh Kumar { 1275720821bSFrederic Weisbecker struct kernel_cpustat kcpustat; 12872a4ce34SViresh Kumar u64 cur_wall_time; 1295720821bSFrederic Weisbecker u64 idle_time; 13072a4ce34SViresh Kumar u64 busy_time; 13172a4ce34SViresh Kumar 1327fb1327eSFrederic Weisbecker cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); 13372a4ce34SViresh Kumar 1345720821bSFrederic Weisbecker kcpustat_cpu_fetch(&kcpustat, cpu); 1355720821bSFrederic Weisbecker 1365720821bSFrederic Weisbecker busy_time = kcpustat.cpustat[CPUTIME_USER]; 1375720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_SYSTEM]; 1385720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_IRQ]; 1395720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ]; 1405720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_STEAL]; 1415720821bSFrederic Weisbecker busy_time += kcpustat.cpustat[CPUTIME_NICE]; 14272a4ce34SViresh Kumar 14372a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 14472a4ce34SViresh Kumar if (wall) 1457fb1327eSFrederic Weisbecker *wall = div_u64(cur_wall_time, NSEC_PER_USEC); 14672a4ce34SViresh Kumar 1477fb1327eSFrederic Weisbecker return div_u64(idle_time, NSEC_PER_USEC); 14872a4ce34SViresh Kumar } 14972a4ce34SViresh Kumar 15072a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 15172a4ce34SViresh Kumar { 15272a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 15372a4ce34SViresh Kumar 15472a4ce34SViresh Kumar if (idle_time == -1ULL) 15572a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 15672a4ce34SViresh Kumar else if (!io_busy) 15772a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 15872a4ce34SViresh Kumar 15972a4ce34SViresh Kumar return idle_time; 16072a4ce34SViresh Kumar } 16172a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 16272a4ce34SViresh Kumar 16370e9e778SViresh Kumar /* 16470e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 16570e9e778SViresh Kumar * drivers of SMP systems. It will do following: 16670e9e778SViresh Kumar * - validate & show freq table passed 16770e9e778SViresh Kumar * - set policies transition latency 16870e9e778SViresh Kumar * - policy->cpus with all possible CPUs 16970e9e778SViresh Kumar */ 170c4dcc8a1SViresh Kumar void cpufreq_generic_init(struct cpufreq_policy *policy, 17170e9e778SViresh Kumar struct cpufreq_frequency_table *table, 17270e9e778SViresh Kumar unsigned int transition_latency) 17370e9e778SViresh Kumar { 17492c99d15SViresh Kumar policy->freq_table = table; 17570e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 17670e9e778SViresh Kumar 17770e9e778SViresh Kumar /* 17858405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 17970e9e778SViresh Kumar * share the clock and voltage and clock. 18070e9e778SViresh Kumar */ 18170e9e778SViresh Kumar cpumask_setall(policy->cpus); 18270e9e778SViresh Kumar } 18370e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 18470e9e778SViresh Kumar 1851f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 186652ed95dSViresh Kumar { 187652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 188652ed95dSViresh Kumar 189988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 190988bed09SViresh Kumar } 1911f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); 192988bed09SViresh Kumar 193988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 194988bed09SViresh Kumar { 195988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 196988bed09SViresh Kumar 197652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 198e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 199e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 200652ed95dSViresh Kumar return 0; 201652ed95dSViresh Kumar } 202652ed95dSViresh Kumar 203652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 204652ed95dSViresh Kumar } 205652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 206652ed95dSViresh Kumar 20750e9c852SViresh Kumar /** 2085d094feaSRafael J. Wysocki * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. 2095d094feaSRafael J. Wysocki * @cpu: CPU to find the policy for. 21050e9c852SViresh Kumar * 2115d094feaSRafael J. Wysocki * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment 2125d094feaSRafael J. Wysocki * the kobject reference counter of that policy. Return a valid policy on 2135d094feaSRafael J. Wysocki * success or NULL on failure. 21450e9c852SViresh Kumar * 2155d094feaSRafael J. Wysocki * The policy returned by this function has to be released with the help of 2165d094feaSRafael J. Wysocki * cpufreq_cpu_put() to balance its kobject reference counter properly. 21750e9c852SViresh Kumar */ 2186eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2191da177e4SLinus Torvalds { 2206eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2211da177e4SLinus Torvalds unsigned long flags; 2221da177e4SLinus Torvalds 2231b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 2246eed9404SViresh Kumar return NULL; 2256eed9404SViresh Kumar 2261da177e4SLinus Torvalds /* get the cpufreq driver */ 2270d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2281da177e4SLinus Torvalds 2296eed9404SViresh Kumar if (cpufreq_driver) { 2301da177e4SLinus Torvalds /* get the CPU */ 231988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 2326eed9404SViresh Kumar if (policy) 2336eed9404SViresh Kumar kobject_get(&policy->kobj); 2346eed9404SViresh Kumar } 2356eed9404SViresh Kumar 2366eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2371da177e4SLinus Torvalds 2383a3e9e06SViresh Kumar return policy; 239a9144436SStephen Boyd } 2401da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2411da177e4SLinus Torvalds 24250e9c852SViresh Kumar /** 2435d094feaSRafael J. Wysocki * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. 2445d094feaSRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_get(). 24550e9c852SViresh Kumar */ 2463a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 247a9144436SStephen Boyd { 2486eed9404SViresh Kumar kobject_put(&policy->kobj); 249a9144436SStephen Boyd } 2501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2511da177e4SLinus Torvalds 252540a3758SRafael J. Wysocki /** 253540a3758SRafael J. Wysocki * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. 254540a3758SRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). 255540a3758SRafael J. Wysocki */ 2569083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy) 257540a3758SRafael J. Wysocki { 258540a3758SRafael J. Wysocki if (WARN_ON(!policy)) 259540a3758SRafael J. Wysocki return; 260540a3758SRafael J. Wysocki 261540a3758SRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 262540a3758SRafael J. Wysocki 263540a3758SRafael J. Wysocki up_write(&policy->rwsem); 264540a3758SRafael J. Wysocki 265540a3758SRafael J. Wysocki cpufreq_cpu_put(policy); 266540a3758SRafael J. Wysocki } 267540a3758SRafael J. Wysocki 268540a3758SRafael J. Wysocki /** 269540a3758SRafael J. Wysocki * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. 270540a3758SRafael J. Wysocki * @cpu: CPU to find the policy for. 271540a3758SRafael J. Wysocki * 272540a3758SRafael J. Wysocki * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and 273540a3758SRafael J. Wysocki * if the policy returned by it is not NULL, acquire its rwsem for writing. 274540a3758SRafael J. Wysocki * Return the policy if it is active or release it and return NULL otherwise. 275540a3758SRafael J. Wysocki * 276540a3758SRafael J. Wysocki * The policy returned by this function has to be released with the help of 277540a3758SRafael J. Wysocki * cpufreq_cpu_release() in order to release its rwsem and balance its usage 278540a3758SRafael J. Wysocki * counter properly. 279540a3758SRafael J. Wysocki */ 2809083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) 281540a3758SRafael J. Wysocki { 282540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 283540a3758SRafael J. Wysocki 284540a3758SRafael J. Wysocki if (!policy) 285540a3758SRafael J. Wysocki return NULL; 286540a3758SRafael J. Wysocki 287540a3758SRafael J. Wysocki down_write(&policy->rwsem); 288540a3758SRafael J. Wysocki 289540a3758SRafael J. Wysocki if (policy_is_inactive(policy)) { 290540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 291540a3758SRafael J. Wysocki return NULL; 292540a3758SRafael J. Wysocki } 293540a3758SRafael J. Wysocki 294540a3758SRafael J. Wysocki return policy; 295540a3758SRafael J. Wysocki } 296540a3758SRafael J. Wysocki 2971da177e4SLinus Torvalds /********************************************************************* 2981da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2991da177e4SLinus Torvalds *********************************************************************/ 3001da177e4SLinus Torvalds 301a9909c21SLee Jones /* 3021da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 3031da177e4SLinus Torvalds * 3041da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 3051da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 3061da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 3071da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 3081da177e4SLinus Torvalds */ 30939c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 31039c132eeSViresh Kumar { 3111da177e4SLinus Torvalds #ifndef CONFIG_SMP 3121da177e4SLinus Torvalds static unsigned long l_p_j_ref; 3131da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 3161da177e4SLinus Torvalds return; 3171da177e4SLinus Torvalds 3181da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 3191da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 3201da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 321e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 322e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 3231da177e4SLinus Torvalds } 3240b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 325e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 326e08f5f5bSGautham R Shenoy ci->new); 327e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 328e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds #endif 33139c132eeSViresh Kumar } 3321da177e4SLinus Torvalds 33320b5324dSViresh Kumar /** 33420b5324dSViresh Kumar * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies. 33520b5324dSViresh Kumar * @policy: cpufreq policy to enable fast frequency switching for. 33620b5324dSViresh Kumar * @freqs: contain details of the frequency update. 33720b5324dSViresh Kumar * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. 33820b5324dSViresh Kumar * 33920b5324dSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 34020b5324dSViresh Kumar * function. It is called twice on all CPU frequency changes that have 34120b5324dSViresh Kumar * external effects. 34220b5324dSViresh Kumar */ 34320b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 34420b5324dSViresh Kumar struct cpufreq_freqs *freqs, 34520b5324dSViresh Kumar unsigned int state) 3461da177e4SLinus Torvalds { 347df24014aSViresh Kumar int cpu; 348df24014aSViresh Kumar 3491da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3501da177e4SLinus Torvalds 351d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 352d5aaffa9SDirk Brandewie return; 353d5aaffa9SDirk Brandewie 354df24014aSViresh Kumar freqs->policy = policy; 3551c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3562d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 357e4472cb3SDave Jones state, freqs->new); 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds switch (state) { 3601da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 36120b5324dSViresh Kumar /* 36220b5324dSViresh Kumar * Detect if the driver reported a value as "old frequency" 363e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 364e4472cb3SDave Jones * "old frequency". 3651da177e4SLinus Torvalds */ 36698015228SViresh Kumar if (policy->cur && policy->cur != freqs->old) { 367e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 368e4472cb3SDave Jones freqs->old, policy->cur); 369e4472cb3SDave Jones freqs->old = policy->cur; 3701da177e4SLinus Torvalds } 37120b5324dSViresh Kumar 372b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 373e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 37420b5324dSViresh Kumar 3751da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3761da177e4SLinus Torvalds break; 377e4472cb3SDave Jones 3781da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3791da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 38020b5324dSViresh Kumar pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, 38120b5324dSViresh Kumar cpumask_pr_args(policy->cpus)); 38220b5324dSViresh Kumar 383df24014aSViresh Kumar for_each_cpu(cpu, policy->cpus) 384df24014aSViresh Kumar trace_cpu_frequency(freqs->new, cpu); 385df24014aSViresh Kumar 386b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 387e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 388bb176f7dSViresh Kumar 38920b5324dSViresh Kumar cpufreq_stats_record_transition(policy, freqs->new); 39020b5324dSViresh Kumar policy->cur = freqs->new; 39120b5324dSViresh Kumar } 392b43a7ffbSViresh Kumar } 3931da177e4SLinus Torvalds 394f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 395236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 396f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 397f7ba3b41SViresh Kumar { 398f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 399f7ba3b41SViresh Kumar if (!transition_failed) 400f7ba3b41SViresh Kumar return; 401f7ba3b41SViresh Kumar 402f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 403f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 404f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 405f7ba3b41SViresh Kumar } 406f7ba3b41SViresh Kumar 40712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 40812478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 40912478cf0SSrivatsa S. Bhat { 410ca654dc3SSrivatsa S. Bhat 411ca654dc3SSrivatsa S. Bhat /* 412ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 413ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 414ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 415ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 416ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 417ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 418ca654dc3SSrivatsa S. Bhat */ 419ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 420ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 421ca654dc3SSrivatsa S. Bhat 42212478cf0SSrivatsa S. Bhat wait: 42312478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 42412478cf0SSrivatsa S. Bhat 42512478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 42612478cf0SSrivatsa S. Bhat 42712478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 42812478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 42912478cf0SSrivatsa S. Bhat goto wait; 43012478cf0SSrivatsa S. Bhat } 43112478cf0SSrivatsa S. Bhat 43212478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 433ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 43412478cf0SSrivatsa S. Bhat 43512478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 43612478cf0SSrivatsa S. Bhat 43712478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 43812478cf0SSrivatsa S. Bhat } 43912478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 44012478cf0SSrivatsa S. Bhat 44112478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 44212478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 44312478cf0SSrivatsa S. Bhat { 4440e7ea2f3SIgor Stoppa if (WARN_ON(!policy->transition_ongoing)) 44512478cf0SSrivatsa S. Bhat return; 44612478cf0SSrivatsa S. Bhat 44712478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 44812478cf0SSrivatsa S. Bhat 4491a0419b0SIonela Voinescu arch_set_freq_scale(policy->related_cpus, 4501a0419b0SIonela Voinescu policy->cur, 4511a0419b0SIonela Voinescu policy->cpuinfo.max_freq); 4521a0419b0SIonela Voinescu 45312478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 454ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 45512478cf0SSrivatsa S. Bhat 45612478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 45712478cf0SSrivatsa S. Bhat } 45812478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 45912478cf0SSrivatsa S. Bhat 460b7898fdaSRafael J. Wysocki /* 461b7898fdaSRafael J. Wysocki * Fast frequency switching status count. Positive means "enabled", negative 462b7898fdaSRafael J. Wysocki * means "disabled" and 0 means "not decided yet". 463b7898fdaSRafael J. Wysocki */ 464b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count; 465b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock); 466b7898fdaSRafael J. Wysocki 467b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void) 468b7898fdaSRafael J. Wysocki { 469b7898fdaSRafael J. Wysocki struct notifier_block *nb; 470b7898fdaSRafael J. Wysocki 471b7898fdaSRafael J. Wysocki pr_info("Registered transition notifiers:\n"); 472b7898fdaSRafael J. Wysocki 473b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_transition_notifier_list.mutex); 474b7898fdaSRafael J. Wysocki 475b7898fdaSRafael J. Wysocki for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) 476d75f773cSSakari Ailus pr_info("%pS\n", nb->notifier_call); 477b7898fdaSRafael J. Wysocki 478b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_transition_notifier_list.mutex); 479b7898fdaSRafael J. Wysocki } 480b7898fdaSRafael J. Wysocki 481b7898fdaSRafael J. Wysocki /** 482b7898fdaSRafael J. Wysocki * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. 483b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to enable fast frequency switching for. 484b7898fdaSRafael J. Wysocki * 485b7898fdaSRafael J. Wysocki * Try to enable fast frequency switching for @policy. 486b7898fdaSRafael J. Wysocki * 487b7898fdaSRafael J. Wysocki * The attempt will fail if there is at least one transition notifier registered 488b7898fdaSRafael J. Wysocki * at this point, as fast frequency switching is quite fundamentally at odds 489b7898fdaSRafael J. Wysocki * with transition notifiers. Thus if successful, it will make registration of 490b7898fdaSRafael J. Wysocki * transition notifiers fail going forward. 491b7898fdaSRafael J. Wysocki */ 492b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) 493b7898fdaSRafael J. Wysocki { 494b7898fdaSRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 495b7898fdaSRafael J. Wysocki 496b7898fdaSRafael J. Wysocki if (!policy->fast_switch_possible) 497b7898fdaSRafael J. Wysocki return; 498b7898fdaSRafael J. Wysocki 499b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 500b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count >= 0) { 501b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 502b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = true; 503b7898fdaSRafael J. Wysocki } else { 504b7898fdaSRafael J. Wysocki pr_warn("CPU%u: Fast frequency switching not enabled\n", 505b7898fdaSRafael J. Wysocki policy->cpu); 506b7898fdaSRafael J. Wysocki cpufreq_list_transition_notifiers(); 507b7898fdaSRafael J. Wysocki } 508b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 509b7898fdaSRafael J. Wysocki } 510b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); 511b7898fdaSRafael J. Wysocki 5126c9d9c81SRafael J. Wysocki /** 5136c9d9c81SRafael J. Wysocki * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. 5146c9d9c81SRafael J. Wysocki * @policy: cpufreq policy to disable fast frequency switching for. 5156c9d9c81SRafael J. Wysocki */ 5166c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) 517b7898fdaSRafael J. Wysocki { 518b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 519b7898fdaSRafael J. Wysocki if (policy->fast_switch_enabled) { 520b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = false; 521b7898fdaSRafael J. Wysocki if (!WARN_ON(cpufreq_fast_switch_count <= 0)) 522b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 523b7898fdaSRafael J. Wysocki } 524b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 525b7898fdaSRafael J. Wysocki } 5266c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); 5271da177e4SLinus Torvalds 528e3c06236SSteve Muckle /** 529e3c06236SSteve Muckle * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported 530e3c06236SSteve Muckle * one. 531a9909c21SLee Jones * @policy: associated policy to interrogate 532e3c06236SSteve Muckle * @target_freq: target frequency to resolve. 533e3c06236SSteve Muckle * 534e3c06236SSteve Muckle * The target to driver frequency mapping is cached in the policy. 535e3c06236SSteve Muckle * 536e3c06236SSteve Muckle * Return: Lowest driver-supported frequency greater than or equal to the 537e3c06236SSteve Muckle * given target_freq, subject to policy (min/max) and driver limitations. 538e3c06236SSteve Muckle */ 539e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, 540e3c06236SSteve Muckle unsigned int target_freq) 541e3c06236SSteve Muckle { 542e3c06236SSteve Muckle target_freq = clamp_val(target_freq, policy->min, policy->max); 543e3c06236SSteve Muckle policy->cached_target_freq = target_freq; 544abe8bd02SViresh Kumar 545abe8bd02SViresh Kumar if (cpufreq_driver->target_index) { 546292072c3SViresh Kumar unsigned int idx; 547abe8bd02SViresh Kumar 548abe8bd02SViresh Kumar idx = cpufreq_frequency_table_target(policy, target_freq, 549abe8bd02SViresh Kumar CPUFREQ_RELATION_L); 550abe8bd02SViresh Kumar policy->cached_resolved_idx = idx; 551abe8bd02SViresh Kumar return policy->freq_table[idx].frequency; 552abe8bd02SViresh Kumar } 553abe8bd02SViresh Kumar 554e3c06236SSteve Muckle if (cpufreq_driver->resolve_freq) 555e3c06236SSteve Muckle return cpufreq_driver->resolve_freq(policy, target_freq); 556abe8bd02SViresh Kumar 557abe8bd02SViresh Kumar return target_freq; 558e3c06236SSteve Muckle } 559ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); 560e3c06236SSteve Muckle 561aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) 562aa7519afSViresh Kumar { 563aa7519afSViresh Kumar unsigned int latency; 564aa7519afSViresh Kumar 565aa7519afSViresh Kumar if (policy->transition_delay_us) 566aa7519afSViresh Kumar return policy->transition_delay_us; 567aa7519afSViresh Kumar 568aa7519afSViresh Kumar latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; 569e948bc8fSViresh Kumar if (latency) { 570e948bc8fSViresh Kumar /* 571e948bc8fSViresh Kumar * For platforms that can change the frequency very fast (< 10 572e948bc8fSViresh Kumar * us), the above formula gives a decent transition delay. But 573e948bc8fSViresh Kumar * for platforms where transition_latency is in milliseconds, it 574e948bc8fSViresh Kumar * ends up giving unrealistic values. 575e948bc8fSViresh Kumar * 576e948bc8fSViresh Kumar * Cap the default transition delay to 10 ms, which seems to be 577e948bc8fSViresh Kumar * a reasonable amount of time after which we should reevaluate 578e948bc8fSViresh Kumar * the frequency. 579e948bc8fSViresh Kumar */ 580e948bc8fSViresh Kumar return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); 581e948bc8fSViresh Kumar } 582aa7519afSViresh Kumar 583aa7519afSViresh Kumar return LATENCY_MULTIPLIER; 584aa7519afSViresh Kumar } 585aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); 586aa7519afSViresh Kumar 5871da177e4SLinus Torvalds /********************************************************************* 5881da177e4SLinus Torvalds * SYSFS INTERFACE * 5891da177e4SLinus Torvalds *********************************************************************/ 5908a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 591625c85a6SViresh Kumar struct kobj_attribute *attr, char *buf) 5926f19efc0SLukasz Majewski { 5936f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 5946f19efc0SLukasz Majewski } 5956f19efc0SLukasz Majewski 596625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, 5976f19efc0SLukasz Majewski const char *buf, size_t count) 5986f19efc0SLukasz Majewski { 5996f19efc0SLukasz Majewski int ret, enable; 6006f19efc0SLukasz Majewski 6016f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 6026f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 6036f19efc0SLukasz Majewski return -EINVAL; 6046f19efc0SLukasz Majewski 6056f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 606e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 607e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 6086f19efc0SLukasz Majewski return -EINVAL; 6096f19efc0SLukasz Majewski } 6106f19efc0SLukasz Majewski 611e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 612e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 6136f19efc0SLukasz Majewski 6146f19efc0SLukasz Majewski return count; 6156f19efc0SLukasz Majewski } 6166f19efc0SLukasz Majewski define_one_global_rw(boost); 6171da177e4SLinus Torvalds 61842f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 6193bcb09a3SJeremy Fitzhardinge { 6203bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 6213bcb09a3SJeremy Fitzhardinge 622f7b27061SViresh Kumar for_each_governor(t) 6237c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 6243bcb09a3SJeremy Fitzhardinge return t; 6253bcb09a3SJeremy Fitzhardinge 6263bcb09a3SJeremy Fitzhardinge return NULL; 6273bcb09a3SJeremy Fitzhardinge } 6283bcb09a3SJeremy Fitzhardinge 6298cc46ae5SViresh Kumar static struct cpufreq_governor *get_governor(const char *str_governor) 6308cc46ae5SViresh Kumar { 6318cc46ae5SViresh Kumar struct cpufreq_governor *t; 6328cc46ae5SViresh Kumar 6338cc46ae5SViresh Kumar mutex_lock(&cpufreq_governor_mutex); 6348cc46ae5SViresh Kumar t = find_governor(str_governor); 6358cc46ae5SViresh Kumar if (!t) 6368cc46ae5SViresh Kumar goto unlock; 6378cc46ae5SViresh Kumar 6388cc46ae5SViresh Kumar if (!try_module_get(t->owner)) 6398cc46ae5SViresh Kumar t = NULL; 6408cc46ae5SViresh Kumar 6418cc46ae5SViresh Kumar unlock: 6428cc46ae5SViresh Kumar mutex_unlock(&cpufreq_governor_mutex); 6438cc46ae5SViresh Kumar 6448cc46ae5SViresh Kumar return t; 6458cc46ae5SViresh Kumar } 6468cc46ae5SViresh Kumar 6471e4f63aeSRafael J. Wysocki static unsigned int cpufreq_parse_policy(char *str_governor) 6481da177e4SLinus Torvalds { 6491e4f63aeSRafael J. Wysocki if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) 6501e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_PERFORMANCE; 6511e4f63aeSRafael J. Wysocki 6521e4f63aeSRafael J. Wysocki if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) 6531e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_POWERSAVE; 6541e4f63aeSRafael J. Wysocki 6551e4f63aeSRafael J. Wysocki return CPUFREQ_POLICY_UNKNOWN; 656ab05d97aSYue Hu } 657ab05d97aSYue Hu 658ab05d97aSYue Hu /** 6595ddc6d4eSViresh Kumar * cpufreq_parse_governor - parse a governor string only for has_target() 6601e4f63aeSRafael J. Wysocki * @str_governor: Governor name. 661ab05d97aSYue Hu */ 6621e4f63aeSRafael J. Wysocki static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor) 663ab05d97aSYue Hu { 6641da177e4SLinus Torvalds struct cpufreq_governor *t; 6653bcb09a3SJeremy Fitzhardinge 6668cc46ae5SViresh Kumar t = get_governor(str_governor); 6678cc46ae5SViresh Kumar if (t) 6688cc46ae5SViresh Kumar return t; 6693bcb09a3SJeremy Fitzhardinge 6708cc46ae5SViresh Kumar if (request_module("cpufreq_%s", str_governor)) 6711e4f63aeSRafael J. Wysocki return NULL; 672045149e6SRafael J. Wysocki 6738cc46ae5SViresh Kumar return get_governor(str_governor); 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 676a9909c21SLee Jones /* 677e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 678e08f5f5bSGautham R Shenoy * print out cpufreq information 6791da177e4SLinus Torvalds * 6801da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 6811da177e4SLinus Torvalds * "unsigned int". 6821da177e4SLinus Torvalds */ 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds #define show_one(file_name, object) \ 6851da177e4SLinus Torvalds static ssize_t show_##file_name \ 6861da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 6871da177e4SLinus Torvalds { \ 6881da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 6921da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 693ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 6941da177e4SLinus Torvalds show_one(scaling_min_freq, min); 6951da177e4SLinus Torvalds show_one(scaling_max_freq, max); 696c034b02eSDirk Brandewie 697f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu) 698f8475cefSLen Brown { 699f8475cefSLen Brown return 0; 700f8475cefSLen Brown } 701f8475cefSLen Brown 70209347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 703c034b02eSDirk Brandewie { 704c034b02eSDirk Brandewie ssize_t ret; 705f8475cefSLen Brown unsigned int freq; 706c034b02eSDirk Brandewie 707f8475cefSLen Brown freq = arch_freq_get_on_cpu(policy->cpu); 708f8475cefSLen Brown if (freq) 709f8475cefSLen Brown ret = sprintf(buf, "%u\n", freq); 710681fe684SViresh Kumar else if (cpufreq_driver->setpolicy && cpufreq_driver->get) 711c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 712c034b02eSDirk Brandewie else 713c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 714c034b02eSDirk Brandewie return ret; 715c034b02eSDirk Brandewie } 7161da177e4SLinus Torvalds 717a9909c21SLee Jones /* 7181da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 7191da177e4SLinus Torvalds */ 7201da177e4SLinus Torvalds #define store_one(file_name, object) \ 7211da177e4SLinus Torvalds static ssize_t store_##file_name \ 7221da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 7231da177e4SLinus Torvalds { \ 72418c49926SViresh Kumar unsigned long val; \ 72518c49926SViresh Kumar int ret; \ 7261da177e4SLinus Torvalds \ 72718c49926SViresh Kumar ret = sscanf(buf, "%lu", &val); \ 7281da177e4SLinus Torvalds if (ret != 1) \ 7291da177e4SLinus Torvalds return -EINVAL; \ 7301da177e4SLinus Torvalds \ 7313000ce3cSRafael J. Wysocki ret = freq_qos_update_request(policy->object##_freq_req, val);\ 73218c49926SViresh Kumar return ret >= 0 ? count : ret; \ 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds store_one(scaling_min_freq, min); 7361da177e4SLinus Torvalds store_one(scaling_max_freq, max); 7371da177e4SLinus Torvalds 738a9909c21SLee Jones /* 7391da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 7401da177e4SLinus Torvalds */ 741e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 742e08f5f5bSGautham R Shenoy char *buf) 7431da177e4SLinus Torvalds { 744d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 7459b4f603eSRafael J. Wysocki 7469b4f603eSRafael J. Wysocki if (cur_freq) 7471da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 7489b4f603eSRafael J. Wysocki 7499b4f603eSRafael J. Wysocki return sprintf(buf, "<unknown>\n"); 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 752a9909c21SLee Jones /* 7531da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 7541da177e4SLinus Torvalds */ 755905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 7561da177e4SLinus Torvalds { 7571da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 7581da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 7591da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 7601da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 7611da177e4SLinus Torvalds else if (policy->governor) 7624b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 76329464f28SDave Jones policy->governor->name); 7641da177e4SLinus Torvalds return -EINVAL; 7651da177e4SLinus Torvalds } 7661da177e4SLinus Torvalds 767a9909c21SLee Jones /* 7681da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 7691da177e4SLinus Torvalds */ 7701da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 7711da177e4SLinus Torvalds const char *buf, size_t count) 7721da177e4SLinus Torvalds { 7731da177e4SLinus Torvalds char str_governor[16]; 7741e4f63aeSRafael J. Wysocki int ret; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 7771da177e4SLinus Torvalds if (ret != 1) 7781da177e4SLinus Torvalds return -EINVAL; 7791da177e4SLinus Torvalds 780ab05d97aSYue Hu if (cpufreq_driver->setpolicy) { 7811e4f63aeSRafael J. Wysocki unsigned int new_pol; 7821e4f63aeSRafael J. Wysocki 7831e4f63aeSRafael J. Wysocki new_pol = cpufreq_parse_policy(str_governor); 7841e4f63aeSRafael J. Wysocki if (!new_pol) 785ab05d97aSYue Hu return -EINVAL; 7861e4f63aeSRafael J. Wysocki 7871e4f63aeSRafael J. Wysocki ret = cpufreq_set_policy(policy, NULL, new_pol); 788ab05d97aSYue Hu } else { 7891e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov; 7901e4f63aeSRafael J. Wysocki 7911e4f63aeSRafael J. Wysocki new_gov = cpufreq_parse_governor(str_governor); 7921e4f63aeSRafael J. Wysocki if (!new_gov) 7931da177e4SLinus Torvalds return -EINVAL; 7941e4f63aeSRafael J. Wysocki 7951e4f63aeSRafael J. Wysocki ret = cpufreq_set_policy(policy, new_gov, 7961e4f63aeSRafael J. Wysocki CPUFREQ_POLICY_UNKNOWN); 7971e4f63aeSRafael J. Wysocki 7981e4f63aeSRafael J. Wysocki module_put(new_gov->owner); 799ab05d97aSYue Hu } 8001da177e4SLinus Torvalds 80188dc4384SViresh Kumar return ret ? ret : count; 8021da177e4SLinus Torvalds } 8031da177e4SLinus Torvalds 804a9909c21SLee Jones /* 8051da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 8061da177e4SLinus Torvalds */ 8071da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 8081da177e4SLinus Torvalds { 8091c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 812a9909c21SLee Jones /* 8131da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 8141da177e4SLinus Torvalds */ 8151da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 8161da177e4SLinus Torvalds char *buf) 8171da177e4SLinus Torvalds { 8181da177e4SLinus Torvalds ssize_t i = 0; 8191da177e4SLinus Torvalds struct cpufreq_governor *t; 8201da177e4SLinus Torvalds 8219c0ebcf7SViresh Kumar if (!has_target()) { 8221da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 8231da177e4SLinus Torvalds goto out; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 8268cc46ae5SViresh Kumar mutex_lock(&cpufreq_governor_mutex); 827f7b27061SViresh Kumar for_each_governor(t) { 82829464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 82929464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 8308cc46ae5SViresh Kumar break; 8314b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 8321da177e4SLinus Torvalds } 8338cc46ae5SViresh Kumar mutex_unlock(&cpufreq_governor_mutex); 8341da177e4SLinus Torvalds out: 8351da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8361da177e4SLinus Torvalds return i; 8371da177e4SLinus Torvalds } 838e8628dd0SDarrick J. Wong 839f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds ssize_t i = 0; 8421da177e4SLinus Torvalds unsigned int cpu; 8431da177e4SLinus Torvalds 844835481d9SRusty Russell for_each_cpu(cpu, mask) { 8451da177e4SLinus Torvalds if (i) 8461da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 8471da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 8481da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 8491da177e4SLinus Torvalds break; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8521da177e4SLinus Torvalds return i; 8531da177e4SLinus Torvalds } 854f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 8551da177e4SLinus Torvalds 856a9909c21SLee Jones /* 857e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 858e8628dd0SDarrick J. Wong * hw coordination is in use 859e8628dd0SDarrick J. Wong */ 860e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 861e8628dd0SDarrick J. Wong { 862f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 863e8628dd0SDarrick J. Wong } 864e8628dd0SDarrick J. Wong 865a9909c21SLee Jones /* 866e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 867e8628dd0SDarrick J. Wong */ 868e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 869e8628dd0SDarrick J. Wong { 870f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 871e8628dd0SDarrick J. Wong } 872e8628dd0SDarrick J. Wong 8739e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 8749e76988eSVenki Pallipadi const char *buf, size_t count) 8759e76988eSVenki Pallipadi { 8769e76988eSVenki Pallipadi unsigned int freq = 0; 8779e76988eSVenki Pallipadi unsigned int ret; 8789e76988eSVenki Pallipadi 879879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 8809e76988eSVenki Pallipadi return -EINVAL; 8819e76988eSVenki Pallipadi 8829e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 8839e76988eSVenki Pallipadi if (ret != 1) 8849e76988eSVenki Pallipadi return -EINVAL; 8859e76988eSVenki Pallipadi 8869e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 8879e76988eSVenki Pallipadi 8889e76988eSVenki Pallipadi return count; 8899e76988eSVenki Pallipadi } 8909e76988eSVenki Pallipadi 8919e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 8929e76988eSVenki Pallipadi { 893879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 8949e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 8959e76988eSVenki Pallipadi 8969e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 8979e76988eSVenki Pallipadi } 8981da177e4SLinus Torvalds 899a9909c21SLee Jones /* 9008bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 901e2f74f35SThomas Renninger */ 902e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 903e2f74f35SThomas Renninger { 904e2f74f35SThomas Renninger unsigned int limit; 905e2f74f35SThomas Renninger int ret; 9061c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 907e2f74f35SThomas Renninger if (!ret) 908e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 909e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 910e2f74f35SThomas Renninger } 911e2f74f35SThomas Renninger 9126dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 9136dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 9146dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 9156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 9166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 9176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 9186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 9196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 9206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 9216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 9226dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 9236dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 9246dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 9256dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 9281da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 9291da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 930ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 9311da177e4SLinus Torvalds &scaling_min_freq.attr, 9321da177e4SLinus Torvalds &scaling_max_freq.attr, 9331da177e4SLinus Torvalds &affected_cpus.attr, 934e8628dd0SDarrick J. Wong &related_cpus.attr, 9351da177e4SLinus Torvalds &scaling_governor.attr, 9361da177e4SLinus Torvalds &scaling_driver.attr, 9371da177e4SLinus Torvalds &scaling_available_governors.attr, 9389e76988eSVenki Pallipadi &scaling_setspeed.attr, 9391da177e4SLinus Torvalds NULL 9401da177e4SLinus Torvalds }; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 9431da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 9461da177e4SLinus Torvalds { 9471da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9481da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 9491b750e3bSViresh Kumar ssize_t ret; 9506eed9404SViresh Kumar 951e6e8df07SKai Shen if (!fattr->show) 952e6e8df07SKai Shen return -EIO; 953e6e8df07SKai Shen 954ad7722daSviresh kumar down_read(&policy->rwsem); 955e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 956ad7722daSviresh kumar up_read(&policy->rwsem); 9571b750e3bSViresh Kumar 9581da177e4SLinus Torvalds return ret; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 9621da177e4SLinus Torvalds const char *buf, size_t count) 9631da177e4SLinus Torvalds { 9641da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9651da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 966a07530b4SDave Jones ssize_t ret = -EINVAL; 9676eed9404SViresh Kumar 968e6e8df07SKai Shen if (!fattr->store) 969e6e8df07SKai Shen return -EIO; 970e6e8df07SKai Shen 9719b3d9bb3SWaiman Long /* 9729b3d9bb3SWaiman Long * cpus_read_trylock() is used here to work around a circular lock 9739b3d9bb3SWaiman Long * dependency problem with respect to the cpufreq_register_driver(). 9749b3d9bb3SWaiman Long */ 9759b3d9bb3SWaiman Long if (!cpus_read_trylock()) 9769b3d9bb3SWaiman Long return -EBUSY; 9774f750c93SSrivatsa S. Bhat 9786541aef0SRafael J. Wysocki if (cpu_online(policy->cpu)) { 979ad7722daSviresh kumar down_write(&policy->rwsem); 980e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 981ad7722daSviresh kumar up_write(&policy->rwsem); 9826541aef0SRafael J. Wysocki } 9836541aef0SRafael J. Wysocki 984a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 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 10011da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 10021da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 10031da177e4SLinus Torvalds .default_attrs = default_attrs, 10041da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 10051da177e4SLinus Torvalds }; 10061da177e4SLinus Torvalds 10072f0ba790SRafael J. Wysocki static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) 100887549141SViresh Kumar { 10092f0ba790SRafael J. Wysocki struct device *dev = get_cpu_device(cpu); 10102f0ba790SRafael J. Wysocki 101167d874c3SViresh Kumar if (unlikely(!dev)) 10122f0ba790SRafael J. Wysocki return; 10132f0ba790SRafael J. Wysocki 10142f0ba790SRafael J. Wysocki if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) 10152f0ba790SRafael J. Wysocki return; 10162f0ba790SRafael J. Wysocki 101726619804SViresh Kumar dev_dbg(dev, "%s: Adding symlink\n", __func__); 10182f0ba790SRafael J. Wysocki if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) 10192f0ba790SRafael J. Wysocki dev_err(dev, "cpufreq symlink creation failed\n"); 102087549141SViresh Kumar } 102187549141SViresh Kumar 102226619804SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, 102326619804SViresh Kumar struct device *dev) 102487549141SViresh Kumar { 102526619804SViresh Kumar dev_dbg(dev, "%s: Removing symlink\n", __func__); 102626619804SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 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 121567d874c3SViresh Kumar ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 121667d874c3SViresh Kumar cpufreq_global_kobject, "policy%u", cpu); 121767d874c3SViresh Kumar if (ret) { 121867d874c3SViresh Kumar dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret); 121967d874c3SViresh Kumar /* 122067d874c3SViresh Kumar * The entire policy object will be freed below, but the extra 122167d874c3SViresh Kumar * memory allocated for the kobject name needs to be freed by 122267d874c3SViresh Kumar * releasing the kobject. 122367d874c3SViresh Kumar */ 122467d874c3SViresh Kumar kobject_put(&policy->kobj); 122567d874c3SViresh Kumar goto err_free_real_cpus; 122667d874c3SViresh Kumar } 122767d874c3SViresh Kumar 12283000ce3cSRafael J. Wysocki freq_constraints_init(&policy->constraints); 12293000ce3cSRafael J. Wysocki 123067d874c3SViresh Kumar policy->nb_min.notifier_call = cpufreq_notifier_min; 123167d874c3SViresh Kumar policy->nb_max.notifier_call = cpufreq_notifier_max; 123267d874c3SViresh Kumar 12333000ce3cSRafael J. Wysocki ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN, 12343000ce3cSRafael J. Wysocki &policy->nb_min); 123567d874c3SViresh Kumar if (ret) { 123667d874c3SViresh Kumar dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n", 123767d874c3SViresh Kumar ret, cpumask_pr_args(policy->cpus)); 123867d874c3SViresh Kumar goto err_kobj_remove; 123967d874c3SViresh Kumar } 124067d874c3SViresh Kumar 12413000ce3cSRafael J. Wysocki ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX, 12423000ce3cSRafael J. Wysocki &policy->nb_max); 124367d874c3SViresh Kumar if (ret) { 124467d874c3SViresh Kumar dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n", 124567d874c3SViresh Kumar ret, cpumask_pr_args(policy->cpus)); 124667d874c3SViresh Kumar goto err_min_qos_notifier; 124767d874c3SViresh Kumar } 124867d874c3SViresh Kumar 124967d874c3SViresh Kumar INIT_LIST_HEAD(&policy->policy_list); 125067d874c3SViresh Kumar init_rwsem(&policy->rwsem); 125167d874c3SViresh Kumar spin_lock_init(&policy->transition_lock); 125267d874c3SViresh Kumar init_waitqueue_head(&policy->transition_wait); 125367d874c3SViresh Kumar init_completion(&policy->kobj_unregister); 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 1281988bed09SViresh Kumar /* Remove policy from list */ 1282988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1283988bed09SViresh Kumar list_del(&policy->policy_list); 1284988bed09SViresh Kumar 1285988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1286988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1287988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1288988bed09SViresh Kumar 12893000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX, 12903000ce3cSRafael J. Wysocki &policy->nb_max); 12913000ce3cSRafael J. Wysocki freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN, 12923000ce3cSRafael J. Wysocki &policy->nb_min); 12936a149036SViresh Kumar 12946941051dSSudeep Holla /* Cancel any pending policy->update work before freeing the policy. */ 12956941051dSSudeep Holla cancel_work_sync(&policy->update); 12966a149036SViresh Kumar 12976a149036SViresh Kumar if (policy->max_freq_req) { 12986a149036SViresh Kumar /* 12996a149036SViresh Kumar * CPUFREQ_CREATE_POLICY notification is sent only after 13006a149036SViresh Kumar * successfully adding max_freq_req request. 13016a149036SViresh Kumar */ 13026a149036SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 13036a149036SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 13043000ce3cSRafael J. Wysocki freq_qos_remove_request(policy->max_freq_req); 13056a149036SViresh Kumar } 13066a149036SViresh Kumar 13073000ce3cSRafael J. Wysocki freq_qos_remove_request(policy->min_freq_req); 130818c49926SViresh Kumar kfree(policy->min_freq_req); 130967d874c3SViresh Kumar 1310f9f41e3eSViresh Kumar cpufreq_policy_put_kobj(policy); 1311559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1312e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1313e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1314e9698cc5SSrivatsa S. Bhat kfree(policy); 1315e9698cc5SSrivatsa S. Bhat } 1316e9698cc5SSrivatsa S. Bhat 13170b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 13181da177e4SLinus Torvalds { 13197f0c020aSViresh Kumar struct cpufreq_policy *policy; 1320194d99c7SRafael J. Wysocki bool new_policy; 13211da177e4SLinus Torvalds unsigned long flags; 13220b275352SRafael J. Wysocki unsigned int j; 13230b275352SRafael J. Wysocki int ret; 1324c32b6b8eSAshok Raj 13250b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 13266eed9404SViresh Kumar 1327bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 13289104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 132911ce707eSRafael J. Wysocki if (policy) { 13309104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 133111ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1332d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 13331da177e4SLinus Torvalds 133411ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1335194d99c7SRafael J. Wysocki new_policy = false; 133611ce707eSRafael J. Wysocki down_write(&policy->rwsem); 133711ce707eSRafael J. Wysocki policy->cpu = cpu; 133811ce707eSRafael J. Wysocki policy->governor = NULL; 133911ce707eSRafael J. Wysocki up_write(&policy->rwsem); 134011ce707eSRafael J. Wysocki } else { 1341194d99c7SRafael J. Wysocki new_policy = true; 1342a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1343059019a3SDave Jones if (!policy) 1344d4d854d6SRafael J. Wysocki return -ENOMEM; 134572368d12SRafael J. Wysocki } 13460d66b91eSSrivatsa S. Bhat 134791a12e91SViresh Kumar if (!new_policy && cpufreq_driver->online) { 134891a12e91SViresh Kumar ret = cpufreq_driver->online(policy); 134991a12e91SViresh Kumar if (ret) { 135091a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 135191a12e91SViresh Kumar __LINE__); 135291a12e91SViresh Kumar goto out_exit_policy; 135391a12e91SViresh Kumar } 135491a12e91SViresh Kumar 135591a12e91SViresh Kumar /* Recover policy->cpus using related_cpus */ 135691a12e91SViresh Kumar cpumask_copy(policy->cpus, policy->related_cpus); 135791a12e91SViresh Kumar } else { 1358835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 13591da177e4SLinus Torvalds 136091a12e91SViresh Kumar /* 136191a12e91SViresh Kumar * Call driver. From then on the cpufreq must be able 136291a12e91SViresh Kumar * to accept all calls to ->verify and ->setpolicy for this CPU. 13631da177e4SLinus Torvalds */ 13641c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 13651da177e4SLinus Torvalds if (ret) { 136691a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 136791a12e91SViresh Kumar __LINE__); 13688101f997SViresh Kumar goto out_free_policy; 13691da177e4SLinus Torvalds } 1370643ae6e8SViresh Kumar 1371d417e069SViresh Kumar ret = cpufreq_table_validate_and_sort(policy); 1372d417e069SViresh Kumar if (ret) 1373d417e069SViresh Kumar goto out_exit_policy; 1374d417e069SViresh Kumar 13754d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 13760998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 13774d1f3a5bSRafael J. Wysocki } 1378559ed407SRafael J. Wysocki 137991a12e91SViresh Kumar down_write(&policy->rwsem); 13805a7e56a5SViresh Kumar /* 13815a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 13825a7e56a5SViresh Kumar * managing offline cpus here. 13835a7e56a5SViresh Kumar */ 13845a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 13855a7e56a5SViresh Kumar 1386194d99c7SRafael J. Wysocki if (new_policy) { 13872f0ba790SRafael J. Wysocki for_each_cpu(j, policy->related_cpus) { 1388652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 13892f0ba790SRafael J. Wysocki add_cpu_dev_symlink(policy, j); 13902f0ba790SRafael J. Wysocki } 139118c49926SViresh Kumar 139218c49926SViresh Kumar policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req), 139318c49926SViresh Kumar GFP_KERNEL); 139418c49926SViresh Kumar if (!policy->min_freq_req) 139518c49926SViresh Kumar goto out_destroy_policy; 139618c49926SViresh Kumar 13973000ce3cSRafael J. Wysocki ret = freq_qos_add_request(&policy->constraints, 13983000ce3cSRafael J. Wysocki policy->min_freq_req, FREQ_QOS_MIN, 139918c49926SViresh Kumar policy->min); 140018c49926SViresh Kumar if (ret < 0) { 140118c49926SViresh Kumar /* 14023000ce3cSRafael J. Wysocki * So we don't call freq_qos_remove_request() for an 140318c49926SViresh Kumar * uninitialized request. 140418c49926SViresh Kumar */ 140518c49926SViresh Kumar kfree(policy->min_freq_req); 140618c49926SViresh Kumar policy->min_freq_req = NULL; 140718c49926SViresh Kumar goto out_destroy_policy; 140818c49926SViresh Kumar } 140918c49926SViresh Kumar 141018c49926SViresh Kumar /* 141118c49926SViresh Kumar * This must be initialized right here to avoid calling 14123000ce3cSRafael J. Wysocki * freq_qos_remove_request() on uninitialized request in case 141318c49926SViresh Kumar * of errors. 141418c49926SViresh Kumar */ 141518c49926SViresh Kumar policy->max_freq_req = policy->min_freq_req + 1; 141618c49926SViresh Kumar 14173000ce3cSRafael J. Wysocki ret = freq_qos_add_request(&policy->constraints, 14183000ce3cSRafael J. Wysocki policy->max_freq_req, FREQ_QOS_MAX, 141918c49926SViresh Kumar policy->max); 142018c49926SViresh Kumar if (ret < 0) { 142118c49926SViresh Kumar policy->max_freq_req = NULL; 142218c49926SViresh Kumar goto out_destroy_policy; 142318c49926SViresh Kumar } 14246a149036SViresh Kumar 14256a149036SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 14266a149036SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 1427988bed09SViresh Kumar } 1428652ed95dSViresh Kumar 14295ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target()) { 1430da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1431da60ce9fSViresh Kumar if (!policy->cur) { 1432da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1433d417e069SViresh Kumar goto out_destroy_policy; 1434da60ce9fSViresh Kumar } 1435da60ce9fSViresh Kumar } 1436da60ce9fSViresh Kumar 1437d3916691SViresh Kumar /* 1438d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1439d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1440d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1441d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1442d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1443d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1444d3916691SViresh Kumar * isn't found in freq-table. 1445d3916691SViresh Kumar * 1446d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1447d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1448d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1449d3916691SViresh Kumar * is initialized to zero). 1450d3916691SViresh Kumar * 1451d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1452d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1453d3916691SViresh Kumar * equal to target-freq. 1454d3916691SViresh Kumar */ 1455d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1456d3916691SViresh Kumar && has_target()) { 145797148d0aSViresh Kumar unsigned int old_freq = policy->cur; 145897148d0aSViresh Kumar 1459d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 146097148d0aSViresh Kumar ret = cpufreq_frequency_table_get_index(policy, old_freq); 1461d3916691SViresh Kumar if (ret == -EINVAL) { 146297148d0aSViresh Kumar ret = __cpufreq_driver_target(policy, old_freq - 1, 1463d3916691SViresh Kumar CPUFREQ_RELATION_L); 1464d3916691SViresh Kumar 1465d3916691SViresh Kumar /* 1466d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1467d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1468d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1469d3916691SViresh Kumar */ 1470d3916691SViresh Kumar BUG_ON(ret); 147197148d0aSViresh Kumar pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n", 147297148d0aSViresh Kumar __func__, policy->cpu, old_freq, policy->cur); 1473d3916691SViresh Kumar } 1474d3916691SViresh Kumar } 1475d3916691SViresh Kumar 1476194d99c7SRafael J. Wysocki if (new_policy) { 1477d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 147819d6f7ecSDave Jones if (ret) 1479d417e069SViresh Kumar goto out_destroy_policy; 14801aefc75bSRafael J. Wysocki 14811aefc75bSRafael J. Wysocki cpufreq_stats_create_table(policy); 1482c88a1f8bSLukasz Majewski 1483c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1484c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1485c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1486988bed09SViresh Kumar } 14878ff69732SDave Jones 14887f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 14897f0fa40fSViresh Kumar if (ret) { 14907f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 14917f0fa40fSViresh Kumar __func__, cpu, ret); 1492d417e069SViresh Kumar goto out_destroy_policy; 149308fd8c1cSViresh Kumar } 1494e18f1682SSrivatsa S. Bhat 14954e97b631SViresh Kumar up_write(&policy->rwsem); 149608fd8c1cSViresh Kumar 1497038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 14987c45cf31SViresh Kumar 14997c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 15007c45cf31SViresh Kumar if (cpufreq_driver->ready) 15017c45cf31SViresh Kumar cpufreq_driver->ready(policy); 15027c45cf31SViresh Kumar 1503bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) 15045c238a8bSAmit Kucheria policy->cdev = of_cpufreq_cooling_register(policy); 15055c238a8bSAmit Kucheria 15062d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 15071da177e4SLinus Torvalds 15081da177e4SLinus Torvalds return 0; 15091da177e4SLinus Torvalds 1510d417e069SViresh Kumar out_destroy_policy: 1511b24b6478SViresh Kumar for_each_cpu(j, policy->real_cpus) 1512b24b6478SViresh Kumar remove_cpu_dev_symlink(policy, get_cpu_device(j)); 1513b24b6478SViresh Kumar 15147106e02bSPrarit Bhargava up_write(&policy->rwsem); 15157106e02bSPrarit Bhargava 1516d417e069SViresh Kumar out_exit_policy: 1517da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1518da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 15192f0ba790SRafael J. Wysocki 15208101f997SViresh Kumar out_free_policy: 1521f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 15221da177e4SLinus Torvalds return ret; 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15250b275352SRafael J. Wysocki /** 15260b275352SRafael J. Wysocki * cpufreq_add_dev - the cpufreq interface for a CPU device. 15270b275352SRafael J. Wysocki * @dev: CPU device. 15280b275352SRafael J. Wysocki * @sif: Subsystem interface structure pointer (not used) 15290b275352SRafael J. Wysocki */ 15300b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 15310b275352SRafael J. Wysocki { 1532a794d613SRafael J. Wysocki struct cpufreq_policy *policy; 15330b275352SRafael J. Wysocki unsigned cpu = dev->id; 153426619804SViresh Kumar int ret; 15350b275352SRafael J. Wysocki 15360b275352SRafael J. Wysocki dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 15370b275352SRafael J. Wysocki 153826619804SViresh Kumar if (cpu_online(cpu)) { 153926619804SViresh Kumar ret = cpufreq_online(cpu); 154026619804SViresh Kumar if (ret) 154126619804SViresh Kumar return ret; 154226619804SViresh Kumar } 1543a794d613SRafael J. Wysocki 154426619804SViresh Kumar /* Create sysfs link on CPU registration */ 1545a794d613SRafael J. Wysocki policy = per_cpu(cpufreq_cpu_data, cpu); 15462f0ba790SRafael J. Wysocki if (policy) 15472f0ba790SRafael J. Wysocki add_cpu_dev_symlink(policy, cpu); 15482f0ba790SRafael J. Wysocki 1549a794d613SRafael J. Wysocki return 0; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 155227622b06SSebastian Andrzej Siewior static int cpufreq_offline(unsigned int cpu) 15531da177e4SLinus Torvalds { 15543a3e9e06SViresh Kumar struct cpufreq_policy *policy; 155569cee714SViresh Kumar int ret; 15561da177e4SLinus Torvalds 1557b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 15581da177e4SLinus Torvalds 1559988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 15603a3e9e06SViresh Kumar if (!policy) { 1561b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 156227622b06SSebastian Andrzej Siewior return 0; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds 156549f18560SViresh Kumar down_write(&policy->rwsem); 156645482c70SRafael J. Wysocki if (has_target()) 156745482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 15681da177e4SLinus Torvalds 15699591becbSViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 15704573237bSViresh Kumar 15719591becbSViresh Kumar if (policy_is_inactive(policy)) { 15729591becbSViresh Kumar if (has_target()) 15734573237bSViresh Kumar strncpy(policy->last_governor, policy->governor->name, 15744573237bSViresh Kumar CPUFREQ_NAME_LEN); 157569030dd1SSrinivas Pandruvada else 157669030dd1SSrinivas Pandruvada policy->last_policy = policy->policy; 15779591becbSViresh Kumar } else if (cpu == policy->cpu) { 15789591becbSViresh Kumar /* Nominate new CPU */ 15799591becbSViresh Kumar policy->cpu = cpumask_any(policy->cpus); 15809591becbSViresh Kumar } 15811da177e4SLinus Torvalds 15829591becbSViresh Kumar /* Start governor again for active policy */ 15839591becbSViresh Kumar if (!policy_is_inactive(policy)) { 15849591becbSViresh Kumar if (has_target()) { 15850a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 15869591becbSViresh Kumar if (ret) 15879591becbSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 15889591becbSViresh Kumar } 158969cee714SViresh Kumar 159049f18560SViresh Kumar goto unlock; 159169cee714SViresh Kumar } 159269cee714SViresh Kumar 1593bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) { 15945c238a8bSAmit Kucheria cpufreq_cooling_unregister(policy->cdev); 15955c238a8bSAmit Kucheria policy->cdev = NULL; 15965c238a8bSAmit Kucheria } 15975c238a8bSAmit Kucheria 159869cee714SViresh Kumar if (cpufreq_driver->stop_cpu) 1599367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 160087549141SViresh Kumar 160136be3418SRafael J. Wysocki if (has_target()) 160236be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 16032a998599SRafael J. Wysocki 16048414809cSSrivatsa S. Bhat /* 160591a12e91SViresh Kumar * Perform the ->offline() during light-weight tear-down, as 160691a12e91SViresh Kumar * that allows fast recovery when the CPU comes back. 16078414809cSSrivatsa S. Bhat */ 160891a12e91SViresh Kumar if (cpufreq_driver->offline) { 160991a12e91SViresh Kumar cpufreq_driver->offline(policy); 161091a12e91SViresh Kumar } else if (cpufreq_driver->exit) { 16113a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 161255582bccSSrinivas Pandruvada policy->freq_table = NULL; 161355582bccSSrinivas Pandruvada } 161449f18560SViresh Kumar 161549f18560SViresh Kumar unlock: 161649f18560SViresh Kumar up_write(&policy->rwsem); 161727622b06SSebastian Andrzej Siewior return 0; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 1620a9909c21SLee Jones /* 162127a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1622cedb70afSSrivatsa S. Bhat * 1623cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1624cedb70afSSrivatsa S. Bhat */ 162571db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 16265a01f2e8SVenkatesh Pallipadi { 16278a25a2fdSKay Sievers unsigned int cpu = dev->id; 162887549141SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 162987549141SViresh Kumar 163087549141SViresh Kumar if (!policy) 163171db87baSViresh Kumar return; 1632ec28297aSVenki Pallipadi 163369cee714SViresh Kumar if (cpu_online(cpu)) 163469cee714SViresh Kumar cpufreq_offline(cpu); 163587549141SViresh Kumar 1636559ed407SRafael J. Wysocki cpumask_clear_cpu(cpu, policy->real_cpus); 163726619804SViresh Kumar remove_cpu_dev_symlink(policy, dev); 1638f344dae0SViresh Kumar 163991a12e91SViresh Kumar if (cpumask_empty(policy->real_cpus)) { 164091a12e91SViresh Kumar /* We did light-weight exit earlier, do full tear down now */ 164191a12e91SViresh Kumar if (cpufreq_driver->offline) 164291a12e91SViresh Kumar cpufreq_driver->exit(policy); 164391a12e91SViresh Kumar 1644f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 16455a01f2e8SVenkatesh Pallipadi } 164691a12e91SViresh Kumar } 16475a01f2e8SVenkatesh Pallipadi 16481da177e4SLinus Torvalds /** 1649bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1650bb176f7dSViresh Kumar * in deep trouble. 1651a1e1dc41SViresh Kumar * @policy: policy managing CPUs 16521da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 16531da177e4SLinus Torvalds * 165429464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 165529464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 16561da177e4SLinus Torvalds */ 1657a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1658e08f5f5bSGautham R Shenoy unsigned int new_freq) 16591da177e4SLinus Torvalds { 16601da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1661b43a7ffbSViresh Kumar 1662e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1663a1e1dc41SViresh Kumar policy->cur, new_freq); 16641da177e4SLinus Torvalds 1665a1e1dc41SViresh Kumar freqs.old = policy->cur; 16661da177e4SLinus Torvalds freqs.new = new_freq; 1667b43a7ffbSViresh Kumar 16688fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 16698fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16725980752eSViresh Kumar static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update) 16735980752eSViresh Kumar { 16745980752eSViresh Kumar unsigned int new_freq; 16755980752eSViresh Kumar 16765980752eSViresh Kumar new_freq = cpufreq_driver->get(policy->cpu); 16775980752eSViresh Kumar if (!new_freq) 16785980752eSViresh Kumar return 0; 16795980752eSViresh Kumar 16805980752eSViresh Kumar /* 16815980752eSViresh Kumar * If fast frequency switching is used with the given policy, the check 16825980752eSViresh Kumar * against policy->cur is pointless, so skip it in that case. 16835980752eSViresh Kumar */ 16845980752eSViresh Kumar if (policy->fast_switch_enabled || !has_target()) 16855980752eSViresh Kumar return new_freq; 16865980752eSViresh Kumar 16875980752eSViresh Kumar if (policy->cur != new_freq) { 16885980752eSViresh Kumar cpufreq_out_of_sync(policy, new_freq); 16895980752eSViresh Kumar if (update) 16905980752eSViresh Kumar schedule_work(&policy->update); 16915980752eSViresh Kumar } 16925980752eSViresh Kumar 16935980752eSViresh Kumar return new_freq; 16945980752eSViresh Kumar } 16955980752eSViresh Kumar 16961da177e4SLinus Torvalds /** 16974ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 169895235ca2SVenkatesh Pallipadi * @cpu: CPU number 169995235ca2SVenkatesh Pallipadi * 170095235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 170195235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 170295235ca2SVenkatesh Pallipadi */ 170395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 170495235ca2SVenkatesh Pallipadi { 17059e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1706e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 1707c75361c0SRichard Cochran unsigned long flags; 170895235ca2SVenkatesh Pallipadi 1709c75361c0SRichard Cochran read_lock_irqsave(&cpufreq_driver_lock, flags); 1710c75361c0SRichard Cochran 1711c75361c0SRichard Cochran if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { 1712c75361c0SRichard Cochran ret_freq = cpufreq_driver->get(cpu); 1713c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1714c75361c0SRichard Cochran return ret_freq; 1715c75361c0SRichard Cochran } 1716c75361c0SRichard Cochran 1717c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 17189e21ba8bSDirk Brandewie 17199e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 172095235ca2SVenkatesh Pallipadi if (policy) { 1721e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 172295235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 172395235ca2SVenkatesh Pallipadi } 172495235ca2SVenkatesh Pallipadi 17254d34a67dSDave Jones return ret_freq; 172695235ca2SVenkatesh Pallipadi } 172795235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 172895235ca2SVenkatesh Pallipadi 17293d737108SJesse Barnes /** 17303d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 17313d737108SJesse Barnes * @cpu: CPU number 17323d737108SJesse Barnes * 17333d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 17343d737108SJesse Barnes */ 17353d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 17363d737108SJesse Barnes { 17373d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 17383d737108SJesse Barnes unsigned int ret_freq = 0; 17393d737108SJesse Barnes 17403d737108SJesse Barnes if (policy) { 17413d737108SJesse Barnes ret_freq = policy->max; 17423d737108SJesse Barnes cpufreq_cpu_put(policy); 17433d737108SJesse Barnes } 17443d737108SJesse Barnes 17453d737108SJesse Barnes return ret_freq; 17463d737108SJesse Barnes } 17473d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 17483d737108SJesse Barnes 1749bbce8eaaSIonela Voinescu /** 1750bbce8eaaSIonela Voinescu * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU 1751bbce8eaaSIonela Voinescu * @cpu: CPU number 1752bbce8eaaSIonela Voinescu * 1753bbce8eaaSIonela Voinescu * The default return value is the max_freq field of cpuinfo. 1754bbce8eaaSIonela Voinescu */ 1755bbce8eaaSIonela Voinescu __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu) 1756bbce8eaaSIonela Voinescu { 1757bbce8eaaSIonela Voinescu struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1758bbce8eaaSIonela Voinescu unsigned int ret_freq = 0; 1759bbce8eaaSIonela Voinescu 1760bbce8eaaSIonela Voinescu if (policy) { 1761bbce8eaaSIonela Voinescu ret_freq = policy->cpuinfo.max_freq; 1762bbce8eaaSIonela Voinescu cpufreq_cpu_put(policy); 1763bbce8eaaSIonela Voinescu } 1764bbce8eaaSIonela Voinescu 1765bbce8eaaSIonela Voinescu return ret_freq; 1766bbce8eaaSIonela Voinescu } 1767bbce8eaaSIonela Voinescu EXPORT_SYMBOL(cpufreq_get_hw_max_freq); 1768bbce8eaaSIonela Voinescu 1769d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 17701da177e4SLinus Torvalds { 17714db7c34cSYue Hu if (unlikely(policy_is_inactive(policy))) 17725980752eSViresh Kumar return 0; 17731da177e4SLinus Torvalds 17745980752eSViresh Kumar return cpufreq_verify_current_freq(policy, true); 17755a01f2e8SVenkatesh Pallipadi } 17761da177e4SLinus Torvalds 17775a01f2e8SVenkatesh Pallipadi /** 17785a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 17795a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 17805a01f2e8SVenkatesh Pallipadi * 17815a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 17825a01f2e8SVenkatesh Pallipadi */ 17835a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 17845a01f2e8SVenkatesh Pallipadi { 1785999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 17865a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 17875a01f2e8SVenkatesh Pallipadi 1788999976e0SAaron Plattner if (policy) { 1789ad7722daSviresh kumar down_read(&policy->rwsem); 17904db7c34cSYue Hu if (cpufreq_driver->get) 1791d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1792ad7722daSviresh kumar up_read(&policy->rwsem); 1793999976e0SAaron Plattner 1794999976e0SAaron Plattner cpufreq_cpu_put(policy); 1795999976e0SAaron Plattner } 17966eed9404SViresh Kumar 17974d34a67dSDave Jones return ret_freq; 17981da177e4SLinus Torvalds } 17991da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 18001da177e4SLinus Torvalds 18018a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 18028a25a2fdSKay Sievers .name = "cpufreq", 18038a25a2fdSKay Sievers .subsys = &cpu_subsys, 18048a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 18058a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1806e00e56dfSRafael J. Wysocki }; 1807e00e56dfSRafael J. Wysocki 1808e28867eaSViresh Kumar /* 1809e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1810e28867eaSViresh Kumar * during suspend.. 181142d4dc3fSBenjamin Herrenschmidt */ 1812e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 181342d4dc3fSBenjamin Herrenschmidt { 1814e28867eaSViresh Kumar int ret; 18154bc5d341SDave Jones 1816e28867eaSViresh Kumar if (!policy->suspend_freq) { 1817201f3716SBartlomiej Zolnierkiewicz pr_debug("%s: suspend_freq not defined\n", __func__); 1818201f3716SBartlomiej Zolnierkiewicz return 0; 181942d4dc3fSBenjamin Herrenschmidt } 182042d4dc3fSBenjamin Herrenschmidt 1821e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1822e28867eaSViresh Kumar policy->suspend_freq); 1823e28867eaSViresh Kumar 1824e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1825e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1826e28867eaSViresh Kumar if (ret) 1827e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1828e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1829e28867eaSViresh Kumar 1830c9060494SDave Jones return ret; 183142d4dc3fSBenjamin Herrenschmidt } 1832e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 183342d4dc3fSBenjamin Herrenschmidt 183442d4dc3fSBenjamin Herrenschmidt /** 18352f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 18361da177e4SLinus Torvalds * 18372f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 18382f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 18392f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 18402f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 18411da177e4SLinus Torvalds */ 18422f0aea93SViresh Kumar void cpufreq_suspend(void) 18431da177e4SLinus Torvalds { 18443a3e9e06SViresh Kumar struct cpufreq_policy *policy; 18451da177e4SLinus Torvalds 18462f0aea93SViresh Kumar if (!cpufreq_driver) 1847e00e56dfSRafael J. Wysocki return; 18481da177e4SLinus Torvalds 1849ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->suspend) 1850b1b12babSViresh Kumar goto suspend; 18511da177e4SLinus Torvalds 18522f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 18532f0aea93SViresh Kumar 1854f963735aSViresh Kumar for_each_active_policy(policy) { 1855ba41e1bcSRafael J. Wysocki if (has_target()) { 185649f18560SViresh Kumar down_write(&policy->rwsem); 185745482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 185849f18560SViresh Kumar up_write(&policy->rwsem); 1859ba41e1bcSRafael J. Wysocki } 1860ba41e1bcSRafael J. Wysocki 1861ba41e1bcSRafael J. Wysocki if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) 1862e9a7cc1dSFlorian Fainelli pr_err("%s: Failed to suspend driver: %s\n", __func__, 1863e9a7cc1dSFlorian Fainelli cpufreq_driver->name); 18641da177e4SLinus Torvalds } 1865b1b12babSViresh Kumar 1866b1b12babSViresh Kumar suspend: 1867b1b12babSViresh Kumar cpufreq_suspended = true; 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds 18701da177e4SLinus Torvalds /** 18712f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 18721da177e4SLinus Torvalds * 18732f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 18742f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 18751da177e4SLinus Torvalds */ 18762f0aea93SViresh Kumar void cpufreq_resume(void) 18771da177e4SLinus Torvalds { 18781da177e4SLinus Torvalds struct cpufreq_policy *policy; 187949f18560SViresh Kumar int ret; 18801da177e4SLinus Torvalds 18812f0aea93SViresh Kumar if (!cpufreq_driver) 18821da177e4SLinus Torvalds return; 18831da177e4SLinus Torvalds 1884703cbaa6SBo Yan if (unlikely(!cpufreq_suspended)) 1885703cbaa6SBo Yan return; 1886703cbaa6SBo Yan 18878e30444eSLan Tianyu cpufreq_suspended = false; 18888e30444eSLan Tianyu 1889ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->resume) 18902f0aea93SViresh Kumar return; 18911da177e4SLinus Torvalds 18922f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 18932f0aea93SViresh Kumar 1894f963735aSViresh Kumar for_each_active_policy(policy) { 189549f18560SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 18960c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 18970c5aa405SViresh Kumar policy); 1898ba41e1bcSRafael J. Wysocki } else if (has_target()) { 189949f18560SViresh Kumar down_write(&policy->rwsem); 19000a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 190149f18560SViresh Kumar up_write(&policy->rwsem); 190249f18560SViresh Kumar 190349f18560SViresh Kumar if (ret) 19042f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 19052f0aea93SViresh Kumar __func__, policy); 1906c75de0acSViresh Kumar } 190749f18560SViresh Kumar } 19081da177e4SLinus Torvalds } 19091da177e4SLinus Torvalds 19109d95046eSBorislav Petkov /** 1911a62f68f5SRafael J. Wysocki * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones. 1912a62f68f5SRafael J. Wysocki * @flags: Flags to test against the current cpufreq driver's flags. 1913a62f68f5SRafael J. Wysocki * 1914a62f68f5SRafael J. Wysocki * Assumes that the driver is there, so callers must ensure that this is the 1915a62f68f5SRafael J. Wysocki * case. 1916a62f68f5SRafael J. Wysocki */ 1917a62f68f5SRafael J. Wysocki bool cpufreq_driver_test_flags(u16 flags) 1918a62f68f5SRafael J. Wysocki { 1919a62f68f5SRafael J. Wysocki return !!(cpufreq_driver->flags & flags); 1920a62f68f5SRafael J. Wysocki } 1921a62f68f5SRafael J. Wysocki 1922a62f68f5SRafael J. Wysocki /** 19239d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 19249d95046eSBorislav Petkov * 19259d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 19269d95046eSBorislav Petkov * or NULL, if none. 19279d95046eSBorislav Petkov */ 19289d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 19299d95046eSBorislav Petkov { 19301c3d85ddSRafael J. Wysocki if (cpufreq_driver) 19311c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 19321c3d85ddSRafael J. Wysocki 19331c3d85ddSRafael J. Wysocki return NULL; 19349d95046eSBorislav Petkov } 19359d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 19361da177e4SLinus Torvalds 193751315cdfSThomas Petazzoni /** 193851315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 193951315cdfSThomas Petazzoni * 194051315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 194151315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 194251315cdfSThomas Petazzoni */ 194351315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 194451315cdfSThomas Petazzoni { 194551315cdfSThomas Petazzoni if (cpufreq_driver) 194651315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 194751315cdfSThomas Petazzoni 194851315cdfSThomas Petazzoni return NULL; 194951315cdfSThomas Petazzoni } 195051315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 195151315cdfSThomas Petazzoni 19521da177e4SLinus Torvalds /********************************************************************* 19531da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 19541da177e4SLinus Torvalds *********************************************************************/ 19551da177e4SLinus Torvalds 19561da177e4SLinus Torvalds /** 19571da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 19581da177e4SLinus Torvalds * @nb: notifier function to register 19591da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 19601da177e4SLinus Torvalds * 19611da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 19621da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 19631da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 19641da177e4SLinus Torvalds * changes in cpufreq policy. 19651da177e4SLinus Torvalds * 19661da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1967e041c683SAlan Stern * blocking_notifier_chain_register. 19681da177e4SLinus Torvalds */ 19691da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 19701da177e4SLinus Torvalds { 19711da177e4SLinus Torvalds int ret; 19721da177e4SLinus Torvalds 1973d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1974d5aaffa9SDirk Brandewie return -EINVAL; 1975d5aaffa9SDirk Brandewie 19761da177e4SLinus Torvalds switch (list) { 19771da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1978b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 1979b7898fdaSRafael J. Wysocki 1980b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count > 0) { 1981b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 1982b7898fdaSRafael J. Wysocki return -EBUSY; 1983b7898fdaSRafael J. Wysocki } 1984b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1985e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 1986b7898fdaSRafael J. Wysocki if (!ret) 1987b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 1988b7898fdaSRafael J. Wysocki 1989b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 19901da177e4SLinus Torvalds break; 19911da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1992e041c683SAlan Stern ret = blocking_notifier_chain_register( 1993e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 19941da177e4SLinus Torvalds break; 19951da177e4SLinus Torvalds default: 19961da177e4SLinus Torvalds ret = -EINVAL; 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds 19991da177e4SLinus Torvalds return ret; 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 20021da177e4SLinus Torvalds 20031da177e4SLinus Torvalds /** 20041da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 20051da177e4SLinus Torvalds * @nb: notifier block to be unregistered 20061da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 20071da177e4SLinus Torvalds * 20081da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 20091da177e4SLinus Torvalds * 20101da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 2011e041c683SAlan Stern * blocking_notifier_chain_unregister. 20121da177e4SLinus Torvalds */ 20131da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 20141da177e4SLinus Torvalds { 20151da177e4SLinus Torvalds int ret; 20161da177e4SLinus Torvalds 2017d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 2018d5aaffa9SDirk Brandewie return -EINVAL; 2019d5aaffa9SDirk Brandewie 20201da177e4SLinus Torvalds switch (list) { 20211da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 2022b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 2023b7898fdaSRafael J. Wysocki 2024b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 2025e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 2026b7898fdaSRafael J. Wysocki if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) 2027b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 2028b7898fdaSRafael J. Wysocki 2029b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 20301da177e4SLinus Torvalds break; 20311da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 2032e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 2033e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 20341da177e4SLinus Torvalds break; 20351da177e4SLinus Torvalds default: 20361da177e4SLinus Torvalds ret = -EINVAL; 20371da177e4SLinus Torvalds } 20381da177e4SLinus Torvalds 20391da177e4SLinus Torvalds return ret; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds 20441da177e4SLinus Torvalds /********************************************************************* 20451da177e4SLinus Torvalds * GOVERNORS * 20461da177e4SLinus Torvalds *********************************************************************/ 20471da177e4SLinus Torvalds 2048b7898fdaSRafael J. Wysocki /** 2049b7898fdaSRafael J. Wysocki * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. 2050b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to switch the frequency for. 2051b7898fdaSRafael J. Wysocki * @target_freq: New frequency to set (may be approximate). 2052b7898fdaSRafael J. Wysocki * 2053b7898fdaSRafael J. Wysocki * Carry out a fast frequency switch without sleeping. 2054b7898fdaSRafael J. Wysocki * 2055b7898fdaSRafael J. Wysocki * The driver's ->fast_switch() callback invoked by this function must be 2056b7898fdaSRafael J. Wysocki * suitable for being called from within RCU-sched read-side critical sections 2057b7898fdaSRafael J. Wysocki * and it is expected to select the minimum available frequency greater than or 2058b7898fdaSRafael J. Wysocki * equal to @target_freq (CPUFREQ_RELATION_L). 2059b7898fdaSRafael J. Wysocki * 2060b7898fdaSRafael J. Wysocki * This function must not be called if policy->fast_switch_enabled is unset. 2061b7898fdaSRafael J. Wysocki * 2062b7898fdaSRafael J. Wysocki * Governors calling this function must guarantee that it will never be invoked 2063b7898fdaSRafael J. Wysocki * twice in parallel for the same policy and that it will never be called in 2064b7898fdaSRafael J. Wysocki * parallel with either ->target() or ->target_index() for the same policy. 2065b7898fdaSRafael J. Wysocki * 2066209887e6SViresh Kumar * Returns the actual frequency set for the CPU. 2067209887e6SViresh Kumar * 2068209887e6SViresh Kumar * If 0 is returned by the driver's ->fast_switch() callback to indicate an 2069209887e6SViresh Kumar * error condition, the hardware configuration must be preserved. 2070b7898fdaSRafael J. Wysocki */ 2071b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, 2072b7898fdaSRafael J. Wysocki unsigned int target_freq) 2073b7898fdaSRafael J. Wysocki { 20741a0419b0SIonela Voinescu unsigned int freq; 207508d8c65eSViresh Kumar int cpu; 2076b7898fdaSRafael J. Wysocki 20771a0419b0SIonela Voinescu target_freq = clamp_val(target_freq, policy->min, policy->max); 20781a0419b0SIonela Voinescu freq = cpufreq_driver->fast_switch(policy, target_freq); 20791a0419b0SIonela Voinescu 208096f60cddSViresh Kumar if (!freq) 208196f60cddSViresh Kumar return 0; 208296f60cddSViresh Kumar 208308d8c65eSViresh Kumar policy->cur = freq; 20841a0419b0SIonela Voinescu arch_set_freq_scale(policy->related_cpus, freq, 20851a0419b0SIonela Voinescu policy->cpuinfo.max_freq); 208696f60cddSViresh Kumar cpufreq_stats_record_transition(policy, freq); 20871a0419b0SIonela Voinescu 208808d8c65eSViresh Kumar if (trace_cpu_frequency_enabled()) { 208908d8c65eSViresh Kumar for_each_cpu(cpu, policy->cpus) 209008d8c65eSViresh Kumar trace_cpu_frequency(freq, cpu); 209108d8c65eSViresh Kumar } 209208d8c65eSViresh Kumar 20931a0419b0SIonela Voinescu return freq; 2094b7898fdaSRafael J. Wysocki } 2095b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); 2096b7898fdaSRafael J. Wysocki 20971c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 20981c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 20991c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 21001c03a2d0SViresh Kumar { 21011c03a2d0SViresh Kumar int ret; 21021c03a2d0SViresh Kumar 21031c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 21041c03a2d0SViresh Kumar 21051c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 21061c03a2d0SViresh Kumar if (!freqs->new) 21071c03a2d0SViresh Kumar return 0; 21081c03a2d0SViresh Kumar 21091c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 21101c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 21111c03a2d0SViresh Kumar 21121c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 21131c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 21141c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 21151c03a2d0SViresh Kumar 21161c03a2d0SViresh Kumar if (ret) 21171c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 21181c03a2d0SViresh Kumar __func__, ret); 21191c03a2d0SViresh Kumar 21201c03a2d0SViresh Kumar return ret; 21211c03a2d0SViresh Kumar } 21221c03a2d0SViresh Kumar 212323727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index) 21248d65775dSViresh Kumar { 21251c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 21261c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 212723727845SViresh Kumar unsigned int newfreq = policy->freq_table[index].frequency; 21288d65775dSViresh Kumar int retval = -EINVAL; 21298d65775dSViresh Kumar bool notify; 21308d65775dSViresh Kumar 213123727845SViresh Kumar if (newfreq == policy->cur) 213223727845SViresh Kumar return 0; 213323727845SViresh Kumar 21348d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 21358d65775dSViresh Kumar if (notify) { 21361c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 21371c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 21381c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 21391c03a2d0SViresh Kumar if (retval) 21401c03a2d0SViresh Kumar return retval; 21418d65775dSViresh Kumar 21421c03a2d0SViresh Kumar intermediate_freq = freqs.new; 21431c03a2d0SViresh Kumar /* Set old freq to intermediate */ 21441c03a2d0SViresh Kumar if (intermediate_freq) 21451c03a2d0SViresh Kumar freqs.old = freqs.new; 21461c03a2d0SViresh Kumar } 21471c03a2d0SViresh Kumar 214823727845SViresh Kumar freqs.new = newfreq; 21498d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 21508d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 21518d65775dSViresh Kumar 21528d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 21538d65775dSViresh Kumar } 21548d65775dSViresh Kumar 21558d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 21568d65775dSViresh Kumar if (retval) 21578d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 21588d65775dSViresh Kumar retval); 21598d65775dSViresh Kumar 21601c03a2d0SViresh Kumar if (notify) { 21618d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 21628d65775dSViresh Kumar 21631c03a2d0SViresh Kumar /* 21641c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 21651c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 21661c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 216758405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 21681c03a2d0SViresh Kumar */ 21691c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 21701c03a2d0SViresh Kumar freqs.old = intermediate_freq; 21711c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 21721c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 21731c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 21741c03a2d0SViresh Kumar } 21751c03a2d0SViresh Kumar } 21761c03a2d0SViresh Kumar 21778d65775dSViresh Kumar return retval; 21788d65775dSViresh Kumar } 21798d65775dSViresh Kumar 21801da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 21811da177e4SLinus Torvalds unsigned int target_freq, 21821da177e4SLinus Torvalds unsigned int relation) 21831da177e4SLinus Torvalds { 21847249924eSViresh Kumar unsigned int old_target_freq = target_freq; 2185d218ed77SViresh Kumar int index; 2186c32b6b8eSAshok Raj 2187a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2188a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2189a7b422cdSKonrad Rzeszutek Wilk 21907249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 2191910c6e88SViresh Kumar target_freq = clamp_val(target_freq, policy->min, policy->max); 21927249924eSViresh Kumar 21937249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 21947249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 21955a1c0228SViresh Kumar 21969c0ebcf7SViresh Kumar /* 21979c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 21989c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 21999c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 22009c0ebcf7SViresh Kumar * calls. 22019c0ebcf7SViresh Kumar */ 22021c534352SRafael J. Wysocki if (target_freq == policy->cur && 22031c534352SRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS)) 22045a1c0228SViresh Kumar return 0; 22055a1c0228SViresh Kumar 22061c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 22071c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 22081c03a2d0SViresh Kumar 22091c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 22106019d23aSRafael J. Wysocki return cpufreq_driver->target(policy, target_freq, relation); 22116019d23aSRafael J. Wysocki 22126019d23aSRafael J. Wysocki if (!cpufreq_driver->target_index) 22136019d23aSRafael J. Wysocki return -EINVAL; 221490d45d17SAshok Raj 2215d218ed77SViresh Kumar index = cpufreq_frequency_table_target(policy, target_freq, relation); 22169c0ebcf7SViresh Kumar 221723727845SViresh Kumar return __target_index(policy, index); 22186019d23aSRafael J. Wysocki } 22191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 22221da177e4SLinus Torvalds unsigned int target_freq, 22231da177e4SLinus Torvalds unsigned int relation) 22241da177e4SLinus Torvalds { 222562c23a89SColin Ian King int ret; 22261da177e4SLinus Torvalds 2227ad7722daSviresh kumar down_write(&policy->rwsem); 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 22301da177e4SLinus Torvalds 2231ad7722daSviresh kumar up_write(&policy->rwsem); 22321da177e4SLinus Torvalds 22331da177e4SLinus Torvalds return ret; 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 22361da177e4SLinus Torvalds 2237de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 2238de1df26bSRafael J. Wysocki { 2239de1df26bSRafael J. Wysocki return NULL; 2240de1df26bSRafael J. Wysocki } 2241de1df26bSRafael J. Wysocki 2242a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy) 22431da177e4SLinus Torvalds { 2244cc993cabSDave Jones int ret; 22456afde10cSThomas Renninger 22462f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 22472f0aea93SViresh Kumar if (cpufreq_suspended) 22482f0aea93SViresh Kumar return 0; 2249cb57720bSEthan Zhao /* 2250cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 2251cb57720bSEthan Zhao * notification happened, so check it. 2252cb57720bSEthan Zhao */ 2253cb57720bSEthan Zhao if (!policy->governor) 2254cb57720bSEthan Zhao return -EINVAL; 22552f0aea93SViresh Kumar 2256ed4676e2SViresh Kumar /* Platform doesn't want dynamic frequency switching ? */ 2257*9a2a9ebcSRafael J. Wysocki if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING && 2258fc4c709fSViresh Kumar cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { 2259de1df26bSRafael J. Wysocki struct cpufreq_governor *gov = cpufreq_fallback_governor(); 2260de1df26bSRafael J. Wysocki 2261de1df26bSRafael J. Wysocki if (gov) { 2262fe829ed8SViresh Kumar pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", 2263e837f9b5SJoe Perches policy->governor->name, gov->name); 22641c256245SThomas Renninger policy->governor = gov; 2265de1df26bSRafael J. Wysocki } else { 2266de1df26bSRafael J. Wysocki return -EINVAL; 22671c256245SThomas Renninger } 22686afde10cSThomas Renninger } 22691da177e4SLinus Torvalds 22701da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 22711da177e4SLinus Torvalds return -EINVAL; 22721da177e4SLinus Torvalds 2273a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 227495731ebbSXiaoguang Chen 2275e788892bSRafael J. Wysocki if (policy->governor->init) { 2276e788892bSRafael J. Wysocki ret = policy->governor->init(policy); 2277a92604b4SRafael J. Wysocki if (ret) { 22781da177e4SLinus Torvalds module_put(policy->governor->owner); 2279a92604b4SRafael J. Wysocki return ret; 22809e8c0a89SRafael J. Wysocki } 228136be3418SRafael J. Wysocki } 22821da177e4SLinus Torvalds 2283a92604b4SRafael J. Wysocki return 0; 2284a92604b4SRafael J. Wysocki } 2285a92604b4SRafael J. Wysocki 2286a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy) 2287a92604b4SRafael J. Wysocki { 2288a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2289a92604b4SRafael J. Wysocki return; 2290a92604b4SRafael J. Wysocki 2291a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2292a92604b4SRafael J. Wysocki 2293e788892bSRafael J. Wysocki if (policy->governor->exit) 2294e788892bSRafael J. Wysocki policy->governor->exit(policy); 2295a92604b4SRafael J. Wysocki 22961da177e4SLinus Torvalds module_put(policy->governor->owner); 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 2299f6ebbcf0SRafael J. Wysocki int cpufreq_start_governor(struct cpufreq_policy *policy) 23000a300767SRafael J. Wysocki { 23010a300767SRafael J. Wysocki int ret; 23020a300767SRafael J. Wysocki 2303a92604b4SRafael J. Wysocki if (cpufreq_suspended) 2304a92604b4SRafael J. Wysocki return 0; 2305a92604b4SRafael J. Wysocki 2306a92604b4SRafael J. Wysocki if (!policy->governor) 2307a92604b4SRafael J. Wysocki return -EINVAL; 2308a92604b4SRafael J. Wysocki 2309a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2310a92604b4SRafael J. Wysocki 2311407d0fffSViresh Kumar if (cpufreq_driver->get) 23125980752eSViresh Kumar cpufreq_verify_current_freq(policy, false); 23133bbf8fe3SRafael J. Wysocki 2314e788892bSRafael J. Wysocki if (policy->governor->start) { 2315e788892bSRafael J. Wysocki ret = policy->governor->start(policy); 2316d6ff44d6SRafael J. Wysocki if (ret) 2317d6ff44d6SRafael J. Wysocki return ret; 2318e788892bSRafael J. Wysocki } 2319d6ff44d6SRafael J. Wysocki 2320e788892bSRafael J. Wysocki if (policy->governor->limits) 2321e788892bSRafael J. Wysocki policy->governor->limits(policy); 2322e788892bSRafael J. Wysocki 2323d6ff44d6SRafael J. Wysocki return 0; 23240a300767SRafael J. Wysocki } 23250a300767SRafael J. Wysocki 2326f6ebbcf0SRafael J. Wysocki void cpufreq_stop_governor(struct cpufreq_policy *policy) 2327a92604b4SRafael J. Wysocki { 2328a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2329a92604b4SRafael J. Wysocki return; 2330a92604b4SRafael J. Wysocki 2331a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2332a92604b4SRafael J. Wysocki 2333e788892bSRafael J. Wysocki if (policy->governor->stop) 2334e788892bSRafael J. Wysocki policy->governor->stop(policy); 2335a92604b4SRafael J. Wysocki } 2336a92604b4SRafael J. Wysocki 2337a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy) 2338a92604b4SRafael J. Wysocki { 2339a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2340a92604b4SRafael J. Wysocki return; 2341a92604b4SRafael J. Wysocki 2342a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2343a92604b4SRafael J. Wysocki 2344e788892bSRafael J. Wysocki if (policy->governor->limits) 2345e788892bSRafael J. Wysocki policy->governor->limits(policy); 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 23491da177e4SLinus Torvalds { 23503bcb09a3SJeremy Fitzhardinge int err; 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds if (!governor) 23531da177e4SLinus Torvalds return -EINVAL; 23541da177e4SLinus Torvalds 2355a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2356a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2357a7b422cdSKonrad Rzeszutek Wilk 23583fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 23591da177e4SLinus Torvalds 23603bcb09a3SJeremy Fitzhardinge err = -EBUSY; 236142f91fa1SViresh Kumar if (!find_governor(governor->name)) { 23623bcb09a3SJeremy Fitzhardinge err = 0; 23631da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 23643bcb09a3SJeremy Fitzhardinge } 23651da177e4SLinus Torvalds 23663fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 23673bcb09a3SJeremy Fitzhardinge return err; 23681da177e4SLinus Torvalds } 23691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 23701da177e4SLinus Torvalds 23711da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 23721da177e4SLinus Torvalds { 23734573237bSViresh Kumar struct cpufreq_policy *policy; 23744573237bSViresh Kumar unsigned long flags; 237590e41bacSPrarit Bhargava 23761da177e4SLinus Torvalds if (!governor) 23771da177e4SLinus Torvalds return; 23781da177e4SLinus Torvalds 2379a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2380a7b422cdSKonrad Rzeszutek Wilk return; 2381a7b422cdSKonrad Rzeszutek Wilk 23824573237bSViresh Kumar /* clear last_governor for all inactive policies */ 23834573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 23844573237bSViresh Kumar for_each_inactive_policy(policy) { 238518bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 238618bf3a12SViresh Kumar policy->governor = NULL; 23874573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 238890e41bacSPrarit Bhargava } 238918bf3a12SViresh Kumar } 23904573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 239190e41bacSPrarit Bhargava 23923fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 23931da177e4SLinus Torvalds list_del(&governor->governor_list); 23943fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 23951da177e4SLinus Torvalds } 23961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 23971da177e4SLinus Torvalds 23981da177e4SLinus Torvalds 23991da177e4SLinus Torvalds /********************************************************************* 24001da177e4SLinus Torvalds * POLICY INTERFACE * 24011da177e4SLinus Torvalds *********************************************************************/ 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds /** 24041da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 240529464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 240629464f28SDave Jones * is written 2407a9909c21SLee Jones * @cpu: CPU to find the policy for 24081da177e4SLinus Torvalds * 24091da177e4SLinus Torvalds * Reads the current cpufreq policy. 24101da177e4SLinus Torvalds */ 24111da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 24121da177e4SLinus Torvalds { 24131da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 24141da177e4SLinus Torvalds if (!policy) 24151da177e4SLinus Torvalds return -EINVAL; 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 24181da177e4SLinus Torvalds if (!cpu_policy) 24191da177e4SLinus Torvalds return -EINVAL; 24201da177e4SLinus Torvalds 2421d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 24221da177e4SLinus Torvalds 24231da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 24241da177e4SLinus Torvalds return 0; 24251da177e4SLinus Torvalds } 24261da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 24271da177e4SLinus Torvalds 2428a0dbb819SRafael J. Wysocki /** 2429a0dbb819SRafael J. Wysocki * cpufreq_set_policy - Modify cpufreq policy parameters. 2430a0dbb819SRafael J. Wysocki * @policy: Policy object to modify. 24311e4f63aeSRafael J. Wysocki * @new_gov: Policy governor pointer. 24321e4f63aeSRafael J. Wysocki * @new_pol: Policy value (for drivers with built-in governors). 2433a0dbb819SRafael J. Wysocki * 24341e4f63aeSRafael J. Wysocki * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency 24351e4f63aeSRafael J. Wysocki * limits to be set for the policy, update @policy with the verified limits 24361e4f63aeSRafael J. Wysocki * values and either invoke the driver's ->setpolicy() callback (if present) or 24371e4f63aeSRafael J. Wysocki * carry out a governor update for @policy. That is, run the current governor's 24381e4f63aeSRafael J. Wysocki * ->limits() callback (if @new_gov points to the same object as the one in 24391e4f63aeSRafael J. Wysocki * @policy) or replace the governor for @policy with @new_gov. 2440a0dbb819SRafael J. Wysocki * 2441a0dbb819SRafael J. Wysocki * The cpuinfo part of @policy is not updated by this function. 2442153d7f3fSArjan van de Ven */ 24431e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy, 24441e4f63aeSRafael J. Wysocki struct cpufreq_governor *new_gov, 24451e4f63aeSRafael J. Wysocki unsigned int new_pol) 24461da177e4SLinus Torvalds { 24471e4f63aeSRafael J. Wysocki struct cpufreq_policy_data new_data; 2448d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2449d9a789c7SRafael J. Wysocki int ret; 24501da177e4SLinus Torvalds 24511e4f63aeSRafael J. Wysocki memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 24521e4f63aeSRafael J. Wysocki new_data.freq_table = policy->freq_table; 24531e4f63aeSRafael J. Wysocki new_data.cpu = policy->cpu; 2454fba9573bSPan Xinhui /* 245567d874c3SViresh Kumar * PM QoS framework collects all the requests from users and provide us 245667d874c3SViresh Kumar * the final aggregated value here. 2457fba9573bSPan Xinhui */ 24581e4f63aeSRafael J. Wysocki new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN); 24591e4f63aeSRafael J. Wysocki new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX); 24601e4f63aeSRafael J. Wysocki 24611e4f63aeSRafael J. Wysocki pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 24621e4f63aeSRafael J. Wysocki new_data.cpu, new_data.min, new_data.max); 24639c9a43edSMattia Dongili 2464737ffb27SViresh Kumar /* 2465737ffb27SViresh Kumar * Verify that the CPU speed can be set within these limits and make sure 2466737ffb27SViresh Kumar * that min <= max. 2467737ffb27SViresh Kumar */ 24681e4f63aeSRafael J. Wysocki ret = cpufreq_driver->verify(&new_data); 24691da177e4SLinus Torvalds if (ret) 2470d9a789c7SRafael J. Wysocki return ret; 24711da177e4SLinus Torvalds 24721e4f63aeSRafael J. Wysocki policy->min = new_data.min; 24731e4f63aeSRafael J. Wysocki policy->max = new_data.max; 2474601b2185SRuchi Kandoi trace_cpu_frequency_limits(policy); 24751da177e4SLinus Torvalds 2476e3c06236SSteve Muckle policy->cached_target_freq = UINT_MAX; 2477e3c06236SSteve Muckle 24782d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 24793a3e9e06SViresh Kumar policy->min, policy->max); 24801da177e4SLinus Torvalds 24811c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 24821e4f63aeSRafael J. Wysocki policy->policy = new_pol; 24832d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2484167a38dcSRafael J. Wysocki return cpufreq_driver->setpolicy(policy); 2485d9a789c7SRafael J. Wysocki } 2486d9a789c7SRafael J. Wysocki 24871e4f63aeSRafael J. Wysocki if (new_gov == policy->governor) { 24882bb4059eSRafael J. Wysocki pr_debug("governor limits update\n"); 2489a92604b4SRafael J. Wysocki cpufreq_governor_limits(policy); 2490d6ff44d6SRafael J. Wysocki return 0; 24910a300767SRafael J. Wysocki } 24921da177e4SLinus Torvalds 24932d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 24941da177e4SLinus Torvalds 2495d9a789c7SRafael J. Wysocki /* save old, working values */ 2496d9a789c7SRafael J. Wysocki old_gov = policy->governor; 24971da177e4SLinus Torvalds /* end old governor */ 2498d9a789c7SRafael J. Wysocki if (old_gov) { 249945482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 250036be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 25017bd353a9SViresh Kumar } 25021da177e4SLinus Torvalds 25031da177e4SLinus Torvalds /* start new governor */ 25041e4f63aeSRafael J. Wysocki policy->governor = new_gov; 2505a92604b4SRafael J. Wysocki ret = cpufreq_init_governor(policy); 25064bc384aeSViresh Kumar if (!ret) { 25070a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 25080a300767SRafael J. Wysocki if (!ret) { 25092bb4059eSRafael J. Wysocki pr_debug("governor change\n"); 2510531b5c9fSQuentin Perret sched_cpufreq_governor_change(policy, old_gov); 25110a300767SRafael J. Wysocki return 0; 25120a300767SRafael J. Wysocki } 2513b7898fdaSRafael J. Wysocki cpufreq_exit_governor(policy); 2514955ef483SViresh Kumar } 25157bd353a9SViresh Kumar 25161da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2517d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 25181da177e4SLinus Torvalds if (old_gov) { 25193a3e9e06SViresh Kumar policy->governor = old_gov; 2520a92604b4SRafael J. Wysocki if (cpufreq_init_governor(policy)) 25214bc384aeSViresh Kumar policy->governor = NULL; 25224bc384aeSViresh Kumar else 25230a300767SRafael J. Wysocki cpufreq_start_governor(policy); 25241da177e4SLinus Torvalds } 25251da177e4SLinus Torvalds 25264bc384aeSViresh Kumar return ret; 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds /** 2530a0dbb819SRafael J. Wysocki * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. 2531a0dbb819SRafael J. Wysocki * @cpu: CPU to re-evaluate the policy for. 25321da177e4SLinus Torvalds * 2533a0dbb819SRafael J. Wysocki * Update the current frequency for the cpufreq policy of @cpu and use 253418c49926SViresh Kumar * cpufreq_set_policy() to re-apply the min and max limits, which triggers the 253518c49926SViresh Kumar * evaluation of policy notifiers and the cpufreq driver's ->verify() callback 253618c49926SViresh Kumar * for the policy in question, among other things. 25371da177e4SLinus Torvalds */ 253830248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu) 25391da177e4SLinus Torvalds { 2540540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); 25411da177e4SLinus Torvalds 2542fefa8ff8SAaron Plattner if (!policy) 254330248fefSRafael J. Wysocki return; 25441da177e4SLinus Torvalds 2545bb176f7dSViresh Kumar /* 2546bb176f7dSViresh Kumar * BIOS might change freq behind our back 2547bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2548bb176f7dSViresh Kumar */ 25495ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target() && 25505980752eSViresh Kumar (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false)))) 2551742c87bfSRafael J. Wysocki goto unlock; 255230248fefSRafael J. Wysocki 255370a59fdeSViresh Kumar refresh_frequency_limits(policy); 25541da177e4SLinus Torvalds 2555fefa8ff8SAaron Plattner unlock: 2556540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 25591da177e4SLinus Torvalds 25605a25e3f7SRafael J. Wysocki /** 25615a25e3f7SRafael J. Wysocki * cpufreq_update_limits - Update policy limits for a given CPU. 25625a25e3f7SRafael J. Wysocki * @cpu: CPU to update the policy limits for. 25635a25e3f7SRafael J. Wysocki * 25645a25e3f7SRafael J. Wysocki * Invoke the driver's ->update_limits callback if present or call 25655a25e3f7SRafael J. Wysocki * cpufreq_update_policy() for @cpu. 25665a25e3f7SRafael J. Wysocki */ 25675a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu) 25685a25e3f7SRafael J. Wysocki { 25695a25e3f7SRafael J. Wysocki if (cpufreq_driver->update_limits) 25705a25e3f7SRafael J. Wysocki cpufreq_driver->update_limits(cpu); 25715a25e3f7SRafael J. Wysocki else 25725a25e3f7SRafael J. Wysocki cpufreq_update_policy(cpu); 25735a25e3f7SRafael J. Wysocki } 25745a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits); 25755a25e3f7SRafael J. Wysocki 25761da177e4SLinus Torvalds /********************************************************************* 25776f19efc0SLukasz Majewski * BOOST * 25786f19efc0SLukasz Majewski *********************************************************************/ 2579cf6fada7SXiongfeng Wang static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state) 25806f19efc0SLukasz Majewski { 2581552abb88SRafael J. Wysocki int ret; 2582552abb88SRafael J. Wysocki 2583f8bfc116SViresh Kumar if (!policy->freq_table) 2584552abb88SRafael J. Wysocki return -ENXIO; 2585f8bfc116SViresh Kumar 2586cf6fada7SXiongfeng Wang ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table); 25876f19efc0SLukasz Majewski if (ret) { 2588cf6fada7SXiongfeng Wang pr_err("%s: Policy frequency update failed\n", __func__); 2589552abb88SRafael J. Wysocki return ret; 25906f19efc0SLukasz Majewski } 259149f18560SViresh Kumar 25923000ce3cSRafael J. Wysocki ret = freq_qos_update_request(policy->max_freq_req, policy->max); 2593e61a4125SViresh Kumar if (ret < 0) 2594552abb88SRafael J. Wysocki return ret; 25956f19efc0SLukasz Majewski 2596552abb88SRafael J. Wysocki return 0; 25976f19efc0SLukasz Majewski } 25986f19efc0SLukasz Majewski 25996f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 26006f19efc0SLukasz Majewski { 2601cf6fada7SXiongfeng Wang struct cpufreq_policy *policy; 26026f19efc0SLukasz Majewski unsigned long flags; 26036f19efc0SLukasz Majewski int ret = 0; 26046f19efc0SLukasz Majewski 26056f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 26066f19efc0SLukasz Majewski return 0; 26076f19efc0SLukasz Majewski 26086f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 26096f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 26106f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 26116f19efc0SLukasz Majewski 2612cf6fada7SXiongfeng Wang get_online_cpus(); 2613cf6fada7SXiongfeng Wang for_each_active_policy(policy) { 2614cf6fada7SXiongfeng Wang ret = cpufreq_driver->set_boost(policy, state); 2615cf6fada7SXiongfeng Wang if (ret) 2616cf6fada7SXiongfeng Wang goto err_reset_state; 2617cf6fada7SXiongfeng Wang } 2618cf6fada7SXiongfeng Wang put_online_cpus(); 2619cf6fada7SXiongfeng Wang 2620cf6fada7SXiongfeng Wang return 0; 2621cf6fada7SXiongfeng Wang 2622cf6fada7SXiongfeng Wang err_reset_state: 2623cf6fada7SXiongfeng Wang put_online_cpus(); 2624cf6fada7SXiongfeng Wang 26256f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 26266f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 26276f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 26286f19efc0SLukasz Majewski 2629e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2630e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 26316f19efc0SLukasz Majewski 26326f19efc0SLukasz Majewski return ret; 26336f19efc0SLukasz Majewski } 26346f19efc0SLukasz Majewski 263541669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void) 26366f19efc0SLukasz Majewski { 263789f98d7eSYue Hu return cpufreq_driver->set_boost; 26386f19efc0SLukasz Majewski } 26396f19efc0SLukasz Majewski 264044139ed4SViresh Kumar static int create_boost_sysfs_file(void) 264144139ed4SViresh Kumar { 264244139ed4SViresh Kumar int ret; 264344139ed4SViresh Kumar 2644c82bd444SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 264544139ed4SViresh Kumar if (ret) 264644139ed4SViresh Kumar pr_err("%s: cannot register global BOOST sysfs file\n", 264744139ed4SViresh Kumar __func__); 264844139ed4SViresh Kumar 264944139ed4SViresh Kumar return ret; 265044139ed4SViresh Kumar } 265144139ed4SViresh Kumar 265244139ed4SViresh Kumar static void remove_boost_sysfs_file(void) 265344139ed4SViresh Kumar { 265444139ed4SViresh Kumar if (cpufreq_boost_supported()) 2655c82bd444SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 265644139ed4SViresh Kumar } 265744139ed4SViresh Kumar 265844139ed4SViresh Kumar int cpufreq_enable_boost_support(void) 265944139ed4SViresh Kumar { 266044139ed4SViresh Kumar if (!cpufreq_driver) 266144139ed4SViresh Kumar return -EINVAL; 266244139ed4SViresh Kumar 266344139ed4SViresh Kumar if (cpufreq_boost_supported()) 266444139ed4SViresh Kumar return 0; 266544139ed4SViresh Kumar 26667a6c79f2SRafael J. Wysocki cpufreq_driver->set_boost = cpufreq_boost_set_sw; 266744139ed4SViresh Kumar 266844139ed4SViresh Kumar /* This will get removed on driver unregister */ 266944139ed4SViresh Kumar return create_boost_sysfs_file(); 267044139ed4SViresh Kumar } 267144139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 267244139ed4SViresh Kumar 26736f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 26746f19efc0SLukasz Majewski { 26756f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 26766f19efc0SLukasz Majewski } 26776f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 26786f19efc0SLukasz Majewski 26796f19efc0SLukasz Majewski /********************************************************************* 26801da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 26811da177e4SLinus Torvalds *********************************************************************/ 268227622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online; 26831da177e4SLinus Torvalds 2684c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu) 2685c4a3fa26SChen Yu { 2686c4a3fa26SChen Yu cpufreq_online(cpu); 2687c4a3fa26SChen Yu 2688c4a3fa26SChen Yu return 0; 2689c4a3fa26SChen Yu } 2690c4a3fa26SChen Yu 2691c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu) 2692c4a3fa26SChen Yu { 2693c4a3fa26SChen Yu cpufreq_offline(cpu); 2694c4a3fa26SChen Yu 2695c4a3fa26SChen Yu return 0; 2696c4a3fa26SChen Yu } 2697c4a3fa26SChen Yu 26981da177e4SLinus Torvalds /** 26991da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 27001da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 27011da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 27021da177e4SLinus Torvalds * 27031da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 270463af4055SEric Biggers * returns zero on success, -EEXIST when another driver got here first 27051da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 27061da177e4SLinus Torvalds * 27071da177e4SLinus Torvalds */ 2708221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 27091da177e4SLinus Torvalds { 27101da177e4SLinus Torvalds unsigned long flags; 27111da177e4SLinus Torvalds int ret; 27121da177e4SLinus Torvalds 2713a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2714a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2715a7b422cdSKonrad Rzeszutek Wilk 271646770be0SViresh Kumar /* 271746770be0SViresh Kumar * The cpufreq core depends heavily on the availability of device 271846770be0SViresh Kumar * structure, make sure they are available before proceeding further. 271946770be0SViresh Kumar */ 272046770be0SViresh Kumar if (!get_cpu_device(0)) 272146770be0SViresh Kumar return -EPROBE_DEFER; 272246770be0SViresh Kumar 27231da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 27249c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 27259832235fSRafael J. Wysocki driver_data->target) || 27269832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 27271c03a2d0SViresh Kumar driver_data->target)) || 2728a9a22b57SViresh Kumar (!driver_data->get_intermediate != !driver_data->target_intermediate) || 272991a12e91SViresh Kumar (!driver_data->online != !driver_data->offline)) 27301da177e4SLinus Torvalds return -EINVAL; 27311da177e4SLinus Torvalds 27322d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 27331da177e4SLinus Torvalds 2734fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2735a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 2736fdd320daSRafael J. Wysocki 27370d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 27381c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 27390d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2740fdd320daSRafael J. Wysocki ret = -EEXIST; 2741fdd320daSRafael J. Wysocki goto out; 27421da177e4SLinus Torvalds } 27431c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 27440d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 27451da177e4SLinus Torvalds 2746874f6353SIonela Voinescu /* 2747874f6353SIonela Voinescu * Mark support for the scheduler's frequency invariance engine for 2748874f6353SIonela Voinescu * drivers that implement target(), target_index() or fast_switch(). 2749874f6353SIonela Voinescu */ 2750874f6353SIonela Voinescu if (!cpufreq_driver->setpolicy) { 2751874f6353SIonela Voinescu static_branch_enable_cpuslocked(&cpufreq_freq_invariance); 2752874f6353SIonela Voinescu pr_debug("supports frequency invariance"); 2753874f6353SIonela Voinescu } 2754874f6353SIonela Voinescu 2755bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2756bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2757bc68b7dfSViresh Kumar 27587a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 275944139ed4SViresh Kumar ret = create_boost_sysfs_file(); 276044139ed4SViresh Kumar if (ret) 27616f19efc0SLukasz Majewski goto err_null_driver; 27627a6c79f2SRafael J. Wysocki } 27636f19efc0SLukasz Majewski 27648a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 27658f5bc2abSJiri Slaby if (ret) 27666f19efc0SLukasz Majewski goto err_boost_unreg; 27671da177e4SLinus Torvalds 2768ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2769ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 27701da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 27716c770036SDavid Arcari ret = -ENODEV; 2772ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2773e08f5f5bSGautham R Shenoy driver_data->name); 27748a25a2fdSKay Sievers goto err_if_unreg; 27751da177e4SLinus Torvalds } 27761da177e4SLinus Torvalds 2777a92551e4SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, 2778a92551e4SSebastian Andrzej Siewior "cpufreq:online", 2779c4a3fa26SChen Yu cpuhp_cpufreq_online, 2780c4a3fa26SChen Yu cpuhp_cpufreq_offline); 278127622b06SSebastian Andrzej Siewior if (ret < 0) 278227622b06SSebastian Andrzej Siewior goto err_if_unreg; 278327622b06SSebastian Andrzej Siewior hp_online = ret; 27845372e054SSebastian Andrzej Siewior ret = 0; 278527622b06SSebastian Andrzej Siewior 27862d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 27873834abb4SPankaj Gupta goto out; 2788fdd320daSRafael J. Wysocki 27898a25a2fdSKay Sievers err_if_unreg: 27908a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 27916f19efc0SLukasz Majewski err_boost_unreg: 279244139ed4SViresh Kumar remove_boost_sysfs_file(); 27938f5bc2abSJiri Slaby err_null_driver: 27940d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 27951c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 27960d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 27973834abb4SPankaj Gupta out: 2798a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 27993834abb4SPankaj Gupta return ret; 28001da177e4SLinus Torvalds } 28011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 28021da177e4SLinus Torvalds 2803a9909c21SLee Jones /* 28041da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 28051da177e4SLinus Torvalds * 28061da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 28071da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 28081da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 28091da177e4SLinus Torvalds * currently not initialised. 28101da177e4SLinus Torvalds */ 2811221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 28121da177e4SLinus Torvalds { 28131da177e4SLinus Torvalds unsigned long flags; 28141da177e4SLinus Torvalds 28151c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 28161da177e4SLinus Torvalds return -EINVAL; 28171da177e4SLinus Torvalds 28182d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 28191da177e4SLinus Torvalds 2820454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2821a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 28228a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 282344139ed4SViresh Kumar remove_boost_sysfs_file(); 2824874f6353SIonela Voinescu static_branch_disable_cpuslocked(&cpufreq_freq_invariance); 2825a92551e4SSebastian Andrzej Siewior cpuhp_remove_state_nocalls_cpuslocked(hp_online); 28261da177e4SLinus Torvalds 28270d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 28286eed9404SViresh Kumar 28291c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 28306eed9404SViresh Kumar 28310d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2832a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 28331da177e4SLinus Torvalds 28341da177e4SLinus Torvalds return 0; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 28375a01f2e8SVenkatesh Pallipadi 28385a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 28395a01f2e8SVenkatesh Pallipadi { 28408412b456SQuentin Perret struct cpufreq_governor *gov = cpufreq_default_governor(); 28418412b456SQuentin Perret 2842a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2843a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2844a7b422cdSKonrad Rzeszutek Wilk 28458eec1020SViresh Kumar cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); 28468aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 28478aa84ad8SThomas Renninger 28488412b456SQuentin Perret if (!strlen(default_governor)) 28498412b456SQuentin Perret strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN); 28508412b456SQuentin Perret 28515a01f2e8SVenkatesh Pallipadi return 0; 28525a01f2e8SVenkatesh Pallipadi } 2853d82f2692SLen Brown module_param(off, int, 0444); 28548412b456SQuentin Perret module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444); 28555a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2856