11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/cpufreq/cpufreq.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001 Russell King 51da177e4SLinus Torvalds * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> 6bb176f7dSViresh Kumar * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> 71da177e4SLinus Torvalds * 8c32b6b8eSAshok Raj * Oct 2005 - Ashok Raj <ashok.raj@intel.com> 9c32b6b8eSAshok Raj * Added handling for CPU hotplug 108ff69732SDave Jones * Feb 2006 - Jacob Shin <jacob.shin@amd.com> 118ff69732SDave Jones * Fix handling for CPU hotplug -- affected CPUs 12c32b6b8eSAshok Raj * 131da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 141da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 151da177e4SLinus Torvalds * published by the Free Software Foundation. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19db701151SViresh Kumar 205ff0a268SViresh Kumar #include <linux/cpu.h> 211da177e4SLinus Torvalds #include <linux/cpufreq.h> 225c238a8bSAmit Kucheria #include <linux/cpu_cooling.h> 231da177e4SLinus Torvalds #include <linux/delay.h> 241da177e4SLinus Torvalds #include <linux/device.h> 255ff0a268SViresh Kumar #include <linux/init.h> 265ff0a268SViresh Kumar #include <linux/kernel_stat.h> 275ff0a268SViresh Kumar #include <linux/module.h> 283fc54d37Sakpm@osdl.org #include <linux/mutex.h> 295ff0a268SViresh Kumar #include <linux/slab.h> 302f0aea93SViresh Kumar #include <linux/suspend.h> 3190de2a4aSDoug Anderson #include <linux/syscore_ops.h> 325ff0a268SViresh Kumar #include <linux/tick.h> 336f4f2723SThomas Renninger #include <trace/events/power.h> 346f4f2723SThomas Renninger 35b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list); 36f963735aSViresh Kumar 37f963735aSViresh Kumar /* Macros to iterate over CPU policies */ 38f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active) \ 39fd7dc7e6SEric Biggers list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ 40fd7dc7e6SEric Biggers if ((__active) == !policy_is_inactive(__policy)) 41f963735aSViresh Kumar 42f963735aSViresh Kumar #define for_each_active_policy(__policy) \ 43f963735aSViresh Kumar for_each_suitable_policy(__policy, true) 44f963735aSViresh Kumar #define for_each_inactive_policy(__policy) \ 45f963735aSViresh Kumar for_each_suitable_policy(__policy, false) 46f963735aSViresh Kumar 47b4f0676fSViresh Kumar #define for_each_policy(__policy) \ 48b4f0676fSViresh Kumar list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) 49b4f0676fSViresh Kumar 50f7b27061SViresh Kumar /* Iterate over governors */ 51f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 52f7b27061SViresh Kumar #define for_each_governor(__governor) \ 53f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 54f7b27061SViresh Kumar 551da177e4SLinus Torvalds /** 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 642f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 652f0aea93SViresh Kumar static bool cpufreq_suspended; 661da177e4SLinus Torvalds 679c0ebcf7SViresh Kumar static inline bool has_target(void) 689c0ebcf7SViresh Kumar { 699c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 709c0ebcf7SViresh Kumar } 719c0ebcf7SViresh Kumar 721da177e4SLinus Torvalds /* internal prototypes */ 73d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 74a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy); 75a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy); 760a300767SRafael J. Wysocki static int cpufreq_start_governor(struct cpufreq_policy *policy); 77a92604b4SRafael J. Wysocki static void cpufreq_stop_governor(struct cpufreq_policy *policy); 78a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy); 7945482c70SRafael J. Wysocki 801da177e4SLinus Torvalds /** 811da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 821da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 831da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 841da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 851da177e4SLinus Torvalds * The mutex locks both lists. 861da177e4SLinus Torvalds */ 87e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 88cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list); 891da177e4SLinus Torvalds 90a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 91da584455SViresh Kumar static int cpufreq_disabled(void) 92a7b422cdSKonrad Rzeszutek Wilk { 93a7b422cdSKonrad Rzeszutek Wilk return off; 94a7b422cdSKonrad Rzeszutek Wilk } 95a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 96a7b422cdSKonrad Rzeszutek Wilk { 97a7b422cdSKonrad Rzeszutek Wilk off = 1; 98a7b422cdSKonrad Rzeszutek Wilk } 993fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1001da177e4SLinus Torvalds 1014d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1024d5dcc42SViresh Kumar { 1030b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1044d5dcc42SViresh Kumar } 1053f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1064d5dcc42SViresh Kumar 107944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 108944e9a03SViresh Kumar { 109944e9a03SViresh Kumar if (have_governor_per_policy()) 110944e9a03SViresh Kumar return &policy->kobj; 111944e9a03SViresh Kumar else 112944e9a03SViresh Kumar return cpufreq_global_kobject; 113944e9a03SViresh Kumar } 114944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 115944e9a03SViresh Kumar 11672a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 11772a4ce34SViresh Kumar { 11872a4ce34SViresh Kumar u64 idle_time; 11972a4ce34SViresh Kumar u64 cur_wall_time; 12072a4ce34SViresh Kumar u64 busy_time; 12172a4ce34SViresh Kumar 1227fb1327eSFrederic Weisbecker cur_wall_time = jiffies64_to_nsecs(get_jiffies_64()); 12372a4ce34SViresh Kumar 12472a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 12572a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 12672a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 12772a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 12872a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 12972a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 13072a4ce34SViresh Kumar 13172a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 13272a4ce34SViresh Kumar if (wall) 1337fb1327eSFrederic Weisbecker *wall = div_u64(cur_wall_time, NSEC_PER_USEC); 13472a4ce34SViresh Kumar 1357fb1327eSFrederic Weisbecker return div_u64(idle_time, NSEC_PER_USEC); 13672a4ce34SViresh Kumar } 13772a4ce34SViresh Kumar 13872a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 13972a4ce34SViresh Kumar { 14072a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 14172a4ce34SViresh Kumar 14272a4ce34SViresh Kumar if (idle_time == -1ULL) 14372a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 14472a4ce34SViresh Kumar else if (!io_busy) 14572a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 14672a4ce34SViresh Kumar 14772a4ce34SViresh Kumar return idle_time; 14872a4ce34SViresh Kumar } 14972a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 15072a4ce34SViresh Kumar 151e7d5459dSDietmar Eggemann __weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq, 152e7d5459dSDietmar Eggemann unsigned long max_freq) 153e7d5459dSDietmar Eggemann { 154e7d5459dSDietmar Eggemann } 155e7d5459dSDietmar Eggemann EXPORT_SYMBOL_GPL(arch_set_freq_scale); 156e7d5459dSDietmar Eggemann 15770e9e778SViresh Kumar /* 15870e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 15970e9e778SViresh Kumar * drivers of SMP systems. It will do following: 16070e9e778SViresh Kumar * - validate & show freq table passed 16170e9e778SViresh Kumar * - set policies transition latency 16270e9e778SViresh Kumar * - policy->cpus with all possible CPUs 16370e9e778SViresh Kumar */ 16470e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy, 16570e9e778SViresh Kumar struct cpufreq_frequency_table *table, 16670e9e778SViresh Kumar unsigned int transition_latency) 16770e9e778SViresh Kumar { 16892c99d15SViresh Kumar policy->freq_table = table; 16970e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 17070e9e778SViresh Kumar 17170e9e778SViresh Kumar /* 17258405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 17370e9e778SViresh Kumar * share the clock and voltage and clock. 17470e9e778SViresh Kumar */ 17570e9e778SViresh Kumar cpumask_setall(policy->cpus); 17670e9e778SViresh Kumar 17770e9e778SViresh Kumar return 0; 17870e9e778SViresh Kumar } 17970e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 18070e9e778SViresh Kumar 1811f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 182652ed95dSViresh Kumar { 183652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 184652ed95dSViresh Kumar 185988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 186988bed09SViresh Kumar } 1871f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); 188988bed09SViresh Kumar 189988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 190988bed09SViresh Kumar { 191988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 192988bed09SViresh Kumar 193652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 194e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 195e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 196652ed95dSViresh Kumar return 0; 197652ed95dSViresh Kumar } 198652ed95dSViresh Kumar 199652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 200652ed95dSViresh Kumar } 201652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 202652ed95dSViresh Kumar 20350e9c852SViresh Kumar /** 2045d094feaSRafael J. Wysocki * cpufreq_cpu_get - Return policy for a CPU and mark it as busy. 2055d094feaSRafael J. Wysocki * @cpu: CPU to find the policy for. 20650e9c852SViresh Kumar * 2075d094feaSRafael J. Wysocki * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment 2085d094feaSRafael J. Wysocki * the kobject reference counter of that policy. Return a valid policy on 2095d094feaSRafael J. Wysocki * success or NULL on failure. 21050e9c852SViresh Kumar * 2115d094feaSRafael J. Wysocki * The policy returned by this function has to be released with the help of 2125d094feaSRafael J. Wysocki * cpufreq_cpu_put() to balance its kobject reference counter properly. 21350e9c852SViresh Kumar */ 2146eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2151da177e4SLinus Torvalds { 2166eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2171da177e4SLinus Torvalds unsigned long flags; 2181da177e4SLinus Torvalds 2191b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 2206eed9404SViresh Kumar return NULL; 2216eed9404SViresh Kumar 2221da177e4SLinus Torvalds /* get the cpufreq driver */ 2230d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2241da177e4SLinus Torvalds 2256eed9404SViresh Kumar if (cpufreq_driver) { 2261da177e4SLinus Torvalds /* get the CPU */ 227988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 2286eed9404SViresh Kumar if (policy) 2296eed9404SViresh Kumar kobject_get(&policy->kobj); 2306eed9404SViresh Kumar } 2316eed9404SViresh Kumar 2326eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2331da177e4SLinus Torvalds 2343a3e9e06SViresh Kumar return policy; 235a9144436SStephen Boyd } 2361da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2371da177e4SLinus Torvalds 23850e9c852SViresh Kumar /** 2395d094feaSRafael J. Wysocki * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy. 2405d094feaSRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_get(). 24150e9c852SViresh Kumar */ 2423a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 243a9144436SStephen Boyd { 2446eed9404SViresh Kumar kobject_put(&policy->kobj); 245a9144436SStephen Boyd } 2461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2471da177e4SLinus Torvalds 248540a3758SRafael J. Wysocki /** 249540a3758SRafael J. Wysocki * cpufreq_cpu_release - Unlock a policy and decrement its usage counter. 250540a3758SRafael J. Wysocki * @policy: cpufreq policy returned by cpufreq_cpu_acquire(). 251540a3758SRafael J. Wysocki */ 2529083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy) 253540a3758SRafael J. Wysocki { 254540a3758SRafael J. Wysocki if (WARN_ON(!policy)) 255540a3758SRafael J. Wysocki return; 256540a3758SRafael J. Wysocki 257540a3758SRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 258540a3758SRafael J. Wysocki 259540a3758SRafael J. Wysocki up_write(&policy->rwsem); 260540a3758SRafael J. Wysocki 261540a3758SRafael J. Wysocki cpufreq_cpu_put(policy); 262540a3758SRafael J. Wysocki } 263540a3758SRafael J. Wysocki 264540a3758SRafael J. Wysocki /** 265540a3758SRafael J. Wysocki * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it. 266540a3758SRafael J. Wysocki * @cpu: CPU to find the policy for. 267540a3758SRafael J. Wysocki * 268540a3758SRafael J. Wysocki * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and 269540a3758SRafael J. Wysocki * if the policy returned by it is not NULL, acquire its rwsem for writing. 270540a3758SRafael J. Wysocki * Return the policy if it is active or release it and return NULL otherwise. 271540a3758SRafael J. Wysocki * 272540a3758SRafael J. Wysocki * The policy returned by this function has to be released with the help of 273540a3758SRafael J. Wysocki * cpufreq_cpu_release() in order to release its rwsem and balance its usage 274540a3758SRafael J. Wysocki * counter properly. 275540a3758SRafael J. Wysocki */ 2769083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu) 277540a3758SRafael J. Wysocki { 278540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 279540a3758SRafael J. Wysocki 280540a3758SRafael J. Wysocki if (!policy) 281540a3758SRafael J. Wysocki return NULL; 282540a3758SRafael J. Wysocki 283540a3758SRafael J. Wysocki down_write(&policy->rwsem); 284540a3758SRafael J. Wysocki 285540a3758SRafael J. Wysocki if (policy_is_inactive(policy)) { 286540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 287540a3758SRafael J. Wysocki return NULL; 288540a3758SRafael J. Wysocki } 289540a3758SRafael J. Wysocki 290540a3758SRafael J. Wysocki return policy; 291540a3758SRafael J. Wysocki } 292540a3758SRafael J. Wysocki 2931da177e4SLinus Torvalds /********************************************************************* 2941da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2951da177e4SLinus Torvalds *********************************************************************/ 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /** 2981da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2991da177e4SLinus Torvalds * 3001da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 3011da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 3021da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 3031da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 3041da177e4SLinus Torvalds */ 30539c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 30639c132eeSViresh Kumar { 3071da177e4SLinus Torvalds #ifndef CONFIG_SMP 3081da177e4SLinus Torvalds static unsigned long l_p_j_ref; 3091da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 3121da177e4SLinus Torvalds return; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 3151da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 3161da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 317e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 318e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 3191da177e4SLinus Torvalds } 3200b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 321e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 322e08f5f5bSGautham R Shenoy ci->new); 323e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 324e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds #endif 32739c132eeSViresh Kumar } 3281da177e4SLinus Torvalds 32920b5324dSViresh Kumar /** 33020b5324dSViresh Kumar * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies. 33120b5324dSViresh Kumar * @policy: cpufreq policy to enable fast frequency switching for. 33220b5324dSViresh Kumar * @freqs: contain details of the frequency update. 33320b5324dSViresh Kumar * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE. 33420b5324dSViresh Kumar * 33520b5324dSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 33620b5324dSViresh Kumar * function. It is called twice on all CPU frequency changes that have 33720b5324dSViresh Kumar * external effects. 33820b5324dSViresh Kumar */ 33920b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 34020b5324dSViresh Kumar struct cpufreq_freqs *freqs, 34120b5324dSViresh Kumar unsigned int state) 3421da177e4SLinus Torvalds { 343df24014aSViresh Kumar int cpu; 344df24014aSViresh Kumar 3451da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3461da177e4SLinus Torvalds 347d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 348d5aaffa9SDirk Brandewie return; 349d5aaffa9SDirk Brandewie 350df24014aSViresh Kumar freqs->policy = policy; 3511c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3522d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 353e4472cb3SDave Jones state, freqs->new); 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds switch (state) { 3561da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 35720b5324dSViresh Kumar /* 35820b5324dSViresh Kumar * Detect if the driver reported a value as "old frequency" 359e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 360e4472cb3SDave Jones * "old frequency". 3611da177e4SLinus Torvalds */ 3621c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 36320b5324dSViresh Kumar if (policy->cur && (policy->cur != freqs->old)) { 364e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 365e4472cb3SDave Jones freqs->old, policy->cur); 366e4472cb3SDave Jones freqs->old = policy->cur; 3671da177e4SLinus Torvalds } 3681da177e4SLinus Torvalds } 36920b5324dSViresh Kumar 370b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 371e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 37220b5324dSViresh Kumar 3731da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3741da177e4SLinus Torvalds break; 375e4472cb3SDave Jones 3761da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3771da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 37820b5324dSViresh Kumar pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, 37920b5324dSViresh Kumar cpumask_pr_args(policy->cpus)); 38020b5324dSViresh Kumar 381df24014aSViresh Kumar for_each_cpu(cpu, policy->cpus) 382df24014aSViresh Kumar trace_cpu_frequency(freqs->new, cpu); 383df24014aSViresh Kumar 384b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 385e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 386bb176f7dSViresh Kumar 38720b5324dSViresh Kumar cpufreq_stats_record_transition(policy, freqs->new); 38820b5324dSViresh Kumar policy->cur = freqs->new; 38920b5324dSViresh Kumar } 390b43a7ffbSViresh Kumar } 3911da177e4SLinus Torvalds 392f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 393236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 394f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 395f7ba3b41SViresh Kumar { 396f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 397f7ba3b41SViresh Kumar if (!transition_failed) 398f7ba3b41SViresh Kumar return; 399f7ba3b41SViresh Kumar 400f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 401f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 402f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 403f7ba3b41SViresh Kumar } 404f7ba3b41SViresh Kumar 40512478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 40612478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 40712478cf0SSrivatsa S. Bhat { 408ca654dc3SSrivatsa S. Bhat 409ca654dc3SSrivatsa S. Bhat /* 410ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 411ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 412ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 413ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 414ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 415ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 416ca654dc3SSrivatsa S. Bhat */ 417ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 418ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 419ca654dc3SSrivatsa S. Bhat 42012478cf0SSrivatsa S. Bhat wait: 42112478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 42212478cf0SSrivatsa S. Bhat 42312478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 42412478cf0SSrivatsa S. Bhat 42512478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 42612478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 42712478cf0SSrivatsa S. Bhat goto wait; 42812478cf0SSrivatsa S. Bhat } 42912478cf0SSrivatsa S. Bhat 43012478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 431ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 43212478cf0SSrivatsa S. Bhat 43312478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 43412478cf0SSrivatsa S. Bhat 43512478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 43612478cf0SSrivatsa S. Bhat } 43712478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 43812478cf0SSrivatsa S. Bhat 43912478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 44012478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 44112478cf0SSrivatsa S. Bhat { 4420e7ea2f3SIgor Stoppa if (WARN_ON(!policy->transition_ongoing)) 44312478cf0SSrivatsa S. Bhat return; 44412478cf0SSrivatsa S. Bhat 44512478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 44612478cf0SSrivatsa S. Bhat 44712478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 448ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 44912478cf0SSrivatsa S. Bhat 45012478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 45112478cf0SSrivatsa S. Bhat } 45212478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 45312478cf0SSrivatsa S. Bhat 454b7898fdaSRafael J. Wysocki /* 455b7898fdaSRafael J. Wysocki * Fast frequency switching status count. Positive means "enabled", negative 456b7898fdaSRafael J. Wysocki * means "disabled" and 0 means "not decided yet". 457b7898fdaSRafael J. Wysocki */ 458b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count; 459b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock); 460b7898fdaSRafael J. Wysocki 461b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void) 462b7898fdaSRafael J. Wysocki { 463b7898fdaSRafael J. Wysocki struct notifier_block *nb; 464b7898fdaSRafael J. Wysocki 465b7898fdaSRafael J. Wysocki pr_info("Registered transition notifiers:\n"); 466b7898fdaSRafael J. Wysocki 467b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_transition_notifier_list.mutex); 468b7898fdaSRafael J. Wysocki 469b7898fdaSRafael J. Wysocki for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) 470d75f773cSSakari Ailus pr_info("%pS\n", nb->notifier_call); 471b7898fdaSRafael J. Wysocki 472b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_transition_notifier_list.mutex); 473b7898fdaSRafael J. Wysocki } 474b7898fdaSRafael J. Wysocki 475b7898fdaSRafael J. Wysocki /** 476b7898fdaSRafael J. Wysocki * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. 477b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to enable fast frequency switching for. 478b7898fdaSRafael J. Wysocki * 479b7898fdaSRafael J. Wysocki * Try to enable fast frequency switching for @policy. 480b7898fdaSRafael J. Wysocki * 481b7898fdaSRafael J. Wysocki * The attempt will fail if there is at least one transition notifier registered 482b7898fdaSRafael J. Wysocki * at this point, as fast frequency switching is quite fundamentally at odds 483b7898fdaSRafael J. Wysocki * with transition notifiers. Thus if successful, it will make registration of 484b7898fdaSRafael J. Wysocki * transition notifiers fail going forward. 485b7898fdaSRafael J. Wysocki */ 486b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) 487b7898fdaSRafael J. Wysocki { 488b7898fdaSRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 489b7898fdaSRafael J. Wysocki 490b7898fdaSRafael J. Wysocki if (!policy->fast_switch_possible) 491b7898fdaSRafael J. Wysocki return; 492b7898fdaSRafael J. Wysocki 493b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 494b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count >= 0) { 495b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 496b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = true; 497b7898fdaSRafael J. Wysocki } else { 498b7898fdaSRafael J. Wysocki pr_warn("CPU%u: Fast frequency switching not enabled\n", 499b7898fdaSRafael J. Wysocki policy->cpu); 500b7898fdaSRafael J. Wysocki cpufreq_list_transition_notifiers(); 501b7898fdaSRafael J. Wysocki } 502b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 503b7898fdaSRafael J. Wysocki } 504b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); 505b7898fdaSRafael J. Wysocki 5066c9d9c81SRafael J. Wysocki /** 5076c9d9c81SRafael J. Wysocki * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. 5086c9d9c81SRafael J. Wysocki * @policy: cpufreq policy to disable fast frequency switching for. 5096c9d9c81SRafael J. Wysocki */ 5106c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) 511b7898fdaSRafael J. Wysocki { 512b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 513b7898fdaSRafael J. Wysocki if (policy->fast_switch_enabled) { 514b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = false; 515b7898fdaSRafael J. Wysocki if (!WARN_ON(cpufreq_fast_switch_count <= 0)) 516b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 517b7898fdaSRafael J. Wysocki } 518b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 519b7898fdaSRafael J. Wysocki } 5206c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); 5211da177e4SLinus Torvalds 522e3c06236SSteve Muckle /** 523e3c06236SSteve Muckle * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported 524e3c06236SSteve Muckle * one. 525e3c06236SSteve Muckle * @target_freq: target frequency to resolve. 526e3c06236SSteve Muckle * 527e3c06236SSteve Muckle * The target to driver frequency mapping is cached in the policy. 528e3c06236SSteve Muckle * 529e3c06236SSteve Muckle * Return: Lowest driver-supported frequency greater than or equal to the 530e3c06236SSteve Muckle * given target_freq, subject to policy (min/max) and driver limitations. 531e3c06236SSteve Muckle */ 532e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, 533e3c06236SSteve Muckle unsigned int target_freq) 534e3c06236SSteve Muckle { 535e3c06236SSteve Muckle target_freq = clamp_val(target_freq, policy->min, policy->max); 536e3c06236SSteve Muckle policy->cached_target_freq = target_freq; 537abe8bd02SViresh Kumar 538abe8bd02SViresh Kumar if (cpufreq_driver->target_index) { 539abe8bd02SViresh Kumar int idx; 540abe8bd02SViresh Kumar 541abe8bd02SViresh Kumar idx = cpufreq_frequency_table_target(policy, target_freq, 542abe8bd02SViresh Kumar CPUFREQ_RELATION_L); 543abe8bd02SViresh Kumar policy->cached_resolved_idx = idx; 544abe8bd02SViresh Kumar return policy->freq_table[idx].frequency; 545abe8bd02SViresh Kumar } 546abe8bd02SViresh Kumar 547e3c06236SSteve Muckle if (cpufreq_driver->resolve_freq) 548e3c06236SSteve Muckle return cpufreq_driver->resolve_freq(policy, target_freq); 549abe8bd02SViresh Kumar 550abe8bd02SViresh Kumar return target_freq; 551e3c06236SSteve Muckle } 552ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); 553e3c06236SSteve Muckle 554aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) 555aa7519afSViresh Kumar { 556aa7519afSViresh Kumar unsigned int latency; 557aa7519afSViresh Kumar 558aa7519afSViresh Kumar if (policy->transition_delay_us) 559aa7519afSViresh Kumar return policy->transition_delay_us; 560aa7519afSViresh Kumar 561aa7519afSViresh Kumar latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; 562e948bc8fSViresh Kumar if (latency) { 563e948bc8fSViresh Kumar /* 564e948bc8fSViresh Kumar * For platforms that can change the frequency very fast (< 10 565e948bc8fSViresh Kumar * us), the above formula gives a decent transition delay. But 566e948bc8fSViresh Kumar * for platforms where transition_latency is in milliseconds, it 567e948bc8fSViresh Kumar * ends up giving unrealistic values. 568e948bc8fSViresh Kumar * 569e948bc8fSViresh Kumar * Cap the default transition delay to 10 ms, which seems to be 570e948bc8fSViresh Kumar * a reasonable amount of time after which we should reevaluate 571e948bc8fSViresh Kumar * the frequency. 572e948bc8fSViresh Kumar */ 573e948bc8fSViresh Kumar return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); 574e948bc8fSViresh Kumar } 575aa7519afSViresh Kumar 576aa7519afSViresh Kumar return LATENCY_MULTIPLIER; 577aa7519afSViresh Kumar } 578aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); 579aa7519afSViresh Kumar 5801da177e4SLinus Torvalds /********************************************************************* 5811da177e4SLinus Torvalds * SYSFS INTERFACE * 5821da177e4SLinus Torvalds *********************************************************************/ 5838a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 584625c85a6SViresh Kumar struct kobj_attribute *attr, char *buf) 5856f19efc0SLukasz Majewski { 5866f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 5876f19efc0SLukasz Majewski } 5886f19efc0SLukasz Majewski 589625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, 5906f19efc0SLukasz Majewski const char *buf, size_t count) 5916f19efc0SLukasz Majewski { 5926f19efc0SLukasz Majewski int ret, enable; 5936f19efc0SLukasz Majewski 5946f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 5956f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 5966f19efc0SLukasz Majewski return -EINVAL; 5976f19efc0SLukasz Majewski 5986f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 599e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 600e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 6016f19efc0SLukasz Majewski return -EINVAL; 6026f19efc0SLukasz Majewski } 6036f19efc0SLukasz Majewski 604e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 605e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 6066f19efc0SLukasz Majewski 6076f19efc0SLukasz Majewski return count; 6086f19efc0SLukasz Majewski } 6096f19efc0SLukasz Majewski define_one_global_rw(boost); 6101da177e4SLinus Torvalds 61142f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 6123bcb09a3SJeremy Fitzhardinge { 6133bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 6143bcb09a3SJeremy Fitzhardinge 615f7b27061SViresh Kumar for_each_governor(t) 6167c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 6173bcb09a3SJeremy Fitzhardinge return t; 6183bcb09a3SJeremy Fitzhardinge 6193bcb09a3SJeremy Fitzhardinge return NULL; 6203bcb09a3SJeremy Fitzhardinge } 6213bcb09a3SJeremy Fitzhardinge 622ab05d97aSYue Hu static int cpufreq_parse_policy(char *str_governor, 623ae0ff89fSRafael J. Wysocki struct cpufreq_policy *policy) 6241da177e4SLinus Torvalds { 6257c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 626ae0ff89fSRafael J. Wysocki policy->policy = CPUFREQ_POLICY_PERFORMANCE; 627045149e6SRafael J. Wysocki return 0; 628045149e6SRafael J. Wysocki } 629045149e6SRafael J. Wysocki if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { 630ae0ff89fSRafael J. Wysocki policy->policy = CPUFREQ_POLICY_POWERSAVE; 631045149e6SRafael J. Wysocki return 0; 6321da177e4SLinus Torvalds } 633ab05d97aSYue Hu return -EINVAL; 634ab05d97aSYue Hu } 635ab05d97aSYue Hu 636ab05d97aSYue Hu /** 637*5ddc6d4eSViresh Kumar * cpufreq_parse_governor - parse a governor string only for has_target() 638ab05d97aSYue Hu */ 639ab05d97aSYue Hu static int cpufreq_parse_governor(char *str_governor, 640ab05d97aSYue Hu struct cpufreq_policy *policy) 641ab05d97aSYue Hu { 6421da177e4SLinus Torvalds struct cpufreq_governor *t; 6433bcb09a3SJeremy Fitzhardinge 6443fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 6453bcb09a3SJeremy Fitzhardinge 64642f91fa1SViresh Kumar t = find_governor(str_governor); 647045149e6SRafael J. Wysocki if (!t) { 648ea714970SJeremy Fitzhardinge int ret; 649ea714970SJeremy Fitzhardinge 650ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 651045149e6SRafael J. Wysocki 6521a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 653045149e6SRafael J. Wysocki if (ret) 654045149e6SRafael J. Wysocki return -EINVAL; 655045149e6SRafael J. Wysocki 656ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 657ea714970SJeremy Fitzhardinge 65842f91fa1SViresh Kumar t = find_governor(str_governor); 659ea714970SJeremy Fitzhardinge } 660a8b149d3SRafael J. Wysocki if (t && !try_module_get(t->owner)) 661a8b149d3SRafael J. Wysocki t = NULL; 662ea714970SJeremy Fitzhardinge 663045149e6SRafael J. Wysocki mutex_unlock(&cpufreq_governor_mutex); 664045149e6SRafael J. Wysocki 665045149e6SRafael J. Wysocki if (t) { 666ae0ff89fSRafael J. Wysocki policy->governor = t; 667045149e6SRafael J. Wysocki return 0; 668045149e6SRafael J. Wysocki } 6693bcb09a3SJeremy Fitzhardinge 670045149e6SRafael J. Wysocki return -EINVAL; 6711da177e4SLinus Torvalds } 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds /** 674e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 675e08f5f5bSGautham R Shenoy * print out cpufreq information 6761da177e4SLinus Torvalds * 6771da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 6781da177e4SLinus Torvalds * "unsigned int". 6791da177e4SLinus Torvalds */ 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds #define show_one(file_name, object) \ 6821da177e4SLinus Torvalds static ssize_t show_##file_name \ 6831da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 6841da177e4SLinus Torvalds { \ 6851da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 6861da177e4SLinus Torvalds } 6871da177e4SLinus Torvalds 6881da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 6891da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 690ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 6911da177e4SLinus Torvalds show_one(scaling_min_freq, min); 6921da177e4SLinus Torvalds show_one(scaling_max_freq, max); 693c034b02eSDirk Brandewie 694f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu) 695f8475cefSLen Brown { 696f8475cefSLen Brown return 0; 697f8475cefSLen Brown } 698f8475cefSLen Brown 69909347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 700c034b02eSDirk Brandewie { 701c034b02eSDirk Brandewie ssize_t ret; 702f8475cefSLen Brown unsigned int freq; 703c034b02eSDirk Brandewie 704f8475cefSLen Brown freq = arch_freq_get_on_cpu(policy->cpu); 705f8475cefSLen Brown if (freq) 706f8475cefSLen Brown ret = sprintf(buf, "%u\n", freq); 707f8475cefSLen Brown else if (cpufreq_driver && cpufreq_driver->setpolicy && 708f8475cefSLen Brown cpufreq_driver->get) 709c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 710c034b02eSDirk Brandewie else 711c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 712c034b02eSDirk Brandewie return ret; 713c034b02eSDirk Brandewie } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /** 7161da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds #define store_one(file_name, object) \ 7191da177e4SLinus Torvalds static ssize_t store_##file_name \ 7201da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 7211da177e4SLinus Torvalds { \ 722619c144cSVince Hsu int ret, temp; \ 7231da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 7241da177e4SLinus Torvalds \ 7258fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); \ 726c7d1f119STao Wang new_policy.min = policy->user_policy.min; \ 727c7d1f119STao Wang new_policy.max = policy->user_policy.max; \ 7281da177e4SLinus Torvalds \ 7291da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 7301da177e4SLinus Torvalds if (ret != 1) \ 7311da177e4SLinus Torvalds return -EINVAL; \ 7321da177e4SLinus Torvalds \ 733619c144cSVince Hsu temp = new_policy.object; \ 734037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 735619c144cSVince Hsu if (!ret) \ 736619c144cSVince Hsu policy->user_policy.object = temp; \ 7371da177e4SLinus Torvalds \ 7381da177e4SLinus Torvalds return ret ? ret : count; \ 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds store_one(scaling_min_freq, min); 7421da177e4SLinus Torvalds store_one(scaling_max_freq, max); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds /** 7451da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 7461da177e4SLinus Torvalds */ 747e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 748e08f5f5bSGautham R Shenoy char *buf) 7491da177e4SLinus Torvalds { 750d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 7519b4f603eSRafael J. Wysocki 7529b4f603eSRafael J. Wysocki if (cur_freq) 7531da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 7549b4f603eSRafael J. Wysocki 7559b4f603eSRafael J. Wysocki return sprintf(buf, "<unknown>\n"); 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds /** 7591da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 7601da177e4SLinus Torvalds */ 761905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 7641da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 7651da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 7661da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 7671da177e4SLinus Torvalds else if (policy->governor) 7684b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 76929464f28SDave Jones policy->governor->name); 7701da177e4SLinus Torvalds return -EINVAL; 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /** 7741da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 7751da177e4SLinus Torvalds */ 7761da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 7771da177e4SLinus Torvalds const char *buf, size_t count) 7781da177e4SLinus Torvalds { 7795136fa56SSrivatsa S. Bhat int ret; 7801da177e4SLinus Torvalds char str_governor[16]; 7811da177e4SLinus Torvalds struct cpufreq_policy new_policy; 7821da177e4SLinus Torvalds 7838fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 7861da177e4SLinus Torvalds if (ret != 1) 7871da177e4SLinus Torvalds return -EINVAL; 7881da177e4SLinus Torvalds 789ab05d97aSYue Hu if (cpufreq_driver->setpolicy) { 790ab05d97aSYue Hu if (cpufreq_parse_policy(str_governor, &new_policy)) 791ab05d97aSYue Hu return -EINVAL; 792ab05d97aSYue Hu } else { 793ae0ff89fSRafael J. Wysocki if (cpufreq_parse_governor(str_governor, &new_policy)) 7941da177e4SLinus Torvalds return -EINVAL; 795ab05d97aSYue Hu } 7961da177e4SLinus Torvalds 797037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 798a8b149d3SRafael J. Wysocki 799a8b149d3SRafael J. Wysocki if (new_policy.governor) 800a8b149d3SRafael J. Wysocki module_put(new_policy.governor->owner); 801a8b149d3SRafael J. Wysocki 80288dc4384SViresh Kumar return ret ? ret : count; 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds /** 8061da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 8071da177e4SLinus Torvalds */ 8081da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 8091da177e4SLinus Torvalds { 8101c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds /** 8141da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 8151da177e4SLinus Torvalds */ 8161da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 8171da177e4SLinus Torvalds char *buf) 8181da177e4SLinus Torvalds { 8191da177e4SLinus Torvalds ssize_t i = 0; 8201da177e4SLinus Torvalds struct cpufreq_governor *t; 8211da177e4SLinus Torvalds 8229c0ebcf7SViresh Kumar if (!has_target()) { 8231da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 8241da177e4SLinus Torvalds goto out; 8251da177e4SLinus Torvalds } 8261da177e4SLinus Torvalds 827f7b27061SViresh Kumar for_each_governor(t) { 82829464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 82929464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 8301da177e4SLinus Torvalds goto out; 8314b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds out: 8341da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8351da177e4SLinus Torvalds return i; 8361da177e4SLinus Torvalds } 837e8628dd0SDarrick J. Wong 838f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 8391da177e4SLinus Torvalds { 8401da177e4SLinus Torvalds ssize_t i = 0; 8411da177e4SLinus Torvalds unsigned int cpu; 8421da177e4SLinus Torvalds 843835481d9SRusty Russell for_each_cpu(cpu, mask) { 8441da177e4SLinus Torvalds if (i) 8451da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 8461da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 8471da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 8481da177e4SLinus Torvalds break; 8491da177e4SLinus Torvalds } 8501da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8511da177e4SLinus Torvalds return i; 8521da177e4SLinus Torvalds } 853f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 8541da177e4SLinus Torvalds 855e8628dd0SDarrick J. Wong /** 856e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 857e8628dd0SDarrick J. Wong * hw coordination is in use 858e8628dd0SDarrick J. Wong */ 859e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 860e8628dd0SDarrick J. Wong { 861f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 862e8628dd0SDarrick J. Wong } 863e8628dd0SDarrick J. Wong 864e8628dd0SDarrick J. Wong /** 865e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 866e8628dd0SDarrick J. Wong */ 867e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 868e8628dd0SDarrick J. Wong { 869f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 870e8628dd0SDarrick J. Wong } 871e8628dd0SDarrick J. Wong 8729e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 8739e76988eSVenki Pallipadi const char *buf, size_t count) 8749e76988eSVenki Pallipadi { 8759e76988eSVenki Pallipadi unsigned int freq = 0; 8769e76988eSVenki Pallipadi unsigned int ret; 8779e76988eSVenki Pallipadi 878879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 8799e76988eSVenki Pallipadi return -EINVAL; 8809e76988eSVenki Pallipadi 8819e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 8829e76988eSVenki Pallipadi if (ret != 1) 8839e76988eSVenki Pallipadi return -EINVAL; 8849e76988eSVenki Pallipadi 8859e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 8869e76988eSVenki Pallipadi 8879e76988eSVenki Pallipadi return count; 8889e76988eSVenki Pallipadi } 8899e76988eSVenki Pallipadi 8909e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 8919e76988eSVenki Pallipadi { 892879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 8939e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 8949e76988eSVenki Pallipadi 8959e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 8969e76988eSVenki Pallipadi } 8971da177e4SLinus Torvalds 898e2f74f35SThomas Renninger /** 8998bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 900e2f74f35SThomas Renninger */ 901e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 902e2f74f35SThomas Renninger { 903e2f74f35SThomas Renninger unsigned int limit; 904e2f74f35SThomas Renninger int ret; 9051c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 906e2f74f35SThomas Renninger if (!ret) 907e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 908e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 909e2f74f35SThomas Renninger } 910e2f74f35SThomas Renninger 9116dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 9126dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 9136dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 9146dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 9156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 9166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 9176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 9186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 9196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 9206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 9216dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 9226dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 9236dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 9246dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 9271da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 9281da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 929ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 9301da177e4SLinus Torvalds &scaling_min_freq.attr, 9311da177e4SLinus Torvalds &scaling_max_freq.attr, 9321da177e4SLinus Torvalds &affected_cpus.attr, 933e8628dd0SDarrick J. Wong &related_cpus.attr, 9341da177e4SLinus Torvalds &scaling_governor.attr, 9351da177e4SLinus Torvalds &scaling_driver.attr, 9361da177e4SLinus Torvalds &scaling_available_governors.attr, 9379e76988eSVenki Pallipadi &scaling_setspeed.attr, 9381da177e4SLinus Torvalds NULL 9391da177e4SLinus Torvalds }; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 9421da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 9451da177e4SLinus Torvalds { 9461da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9471da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 9481b750e3bSViresh Kumar ssize_t ret; 9496eed9404SViresh Kumar 950ad7722daSviresh kumar down_read(&policy->rwsem); 951e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 952ad7722daSviresh kumar up_read(&policy->rwsem); 9531b750e3bSViresh Kumar 9541da177e4SLinus Torvalds return ret; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 9571da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 9581da177e4SLinus Torvalds const char *buf, size_t count) 9591da177e4SLinus Torvalds { 9601da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9611da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 962a07530b4SDave Jones ssize_t ret = -EINVAL; 9636eed9404SViresh Kumar 9649b3d9bb3SWaiman Long /* 9659b3d9bb3SWaiman Long * cpus_read_trylock() is used here to work around a circular lock 9669b3d9bb3SWaiman Long * dependency problem with respect to the cpufreq_register_driver(). 9679b3d9bb3SWaiman Long */ 9689b3d9bb3SWaiman Long if (!cpus_read_trylock()) 9699b3d9bb3SWaiman Long return -EBUSY; 9704f750c93SSrivatsa S. Bhat 9716541aef0SRafael J. Wysocki if (cpu_online(policy->cpu)) { 972ad7722daSviresh kumar down_write(&policy->rwsem); 973e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 974ad7722daSviresh kumar up_write(&policy->rwsem); 9756541aef0SRafael J. Wysocki } 9766541aef0SRafael J. Wysocki 977a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 9784f750c93SSrivatsa S. Bhat 9791da177e4SLinus Torvalds return ret; 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 9831da177e4SLinus Torvalds { 9841da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9852d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 9861da177e4SLinus Torvalds complete(&policy->kobj_unregister); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds 98952cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 9901da177e4SLinus Torvalds .show = show, 9911da177e4SLinus Torvalds .store = store, 9921da177e4SLinus Torvalds }; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 9951da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 9961da177e4SLinus Torvalds .default_attrs = default_attrs, 9971da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 9981da177e4SLinus Torvalds }; 9991da177e4SLinus Torvalds 10002f0ba790SRafael J. Wysocki static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) 100187549141SViresh Kumar { 10022f0ba790SRafael J. Wysocki struct device *dev = get_cpu_device(cpu); 10032f0ba790SRafael J. Wysocki 10042f0ba790SRafael J. Wysocki if (!dev) 10052f0ba790SRafael J. Wysocki return; 10062f0ba790SRafael J. Wysocki 10072f0ba790SRafael J. Wysocki if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) 10082f0ba790SRafael J. Wysocki return; 10092f0ba790SRafael J. Wysocki 101026619804SViresh Kumar dev_dbg(dev, "%s: Adding symlink\n", __func__); 10112f0ba790SRafael J. Wysocki if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) 10122f0ba790SRafael J. Wysocki dev_err(dev, "cpufreq symlink creation failed\n"); 101387549141SViresh Kumar } 101487549141SViresh Kumar 101526619804SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, 101626619804SViresh Kumar struct device *dev) 101787549141SViresh Kumar { 101826619804SViresh Kumar dev_dbg(dev, "%s: Removing symlink\n", __func__); 101926619804SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 102087549141SViresh Kumar } 102187549141SViresh Kumar 1022d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 1023909a694eSDave Jones { 1024909a694eSDave Jones struct freq_attr **drv_attr; 1025909a694eSDave Jones int ret = 0; 1026909a694eSDave Jones 1027909a694eSDave Jones /* set up files for this cpu device */ 10281c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 1029f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 1030909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 1031909a694eSDave Jones if (ret) 10326d4e81edSTomeu Vizoso return ret; 1033909a694eSDave Jones drv_attr++; 1034909a694eSDave Jones } 10351c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 1036909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 1037909a694eSDave Jones if (ret) 10386d4e81edSTomeu Vizoso return ret; 1039909a694eSDave Jones } 1040c034b02eSDirk Brandewie 1041909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 1042909a694eSDave Jones if (ret) 10436d4e81edSTomeu Vizoso return ret; 1044c034b02eSDirk Brandewie 10451c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 1046e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 1047e2f74f35SThomas Renninger if (ret) 10486d4e81edSTomeu Vizoso return ret; 1049e2f74f35SThomas Renninger } 1050909a694eSDave Jones 105126619804SViresh Kumar return 0; 1052e18f1682SSrivatsa S. Bhat } 1053e18f1682SSrivatsa S. Bhat 1054de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_default_governor(void) 1055de1df26bSRafael J. Wysocki { 1056de1df26bSRafael J. Wysocki return NULL; 1057de1df26bSRafael J. Wysocki } 1058de1df26bSRafael J. Wysocki 10597f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy) 1060e18f1682SSrivatsa S. Bhat { 1061ab05d97aSYue Hu struct cpufreq_governor *gov = NULL, *def_gov = NULL; 1062e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 1063e18f1682SSrivatsa S. Bhat 1064d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 1065a27a9ab7SJason Baron 1066ab05d97aSYue Hu def_gov = cpufreq_default_governor(); 1067ab05d97aSYue Hu 1068ab05d97aSYue Hu if (has_target()) { 1069ab05d97aSYue Hu /* 1070ab05d97aSYue Hu * Update governor of new_policy to the governor used before 1071ab05d97aSYue Hu * hotplug 1072ab05d97aSYue Hu */ 10734573237bSViresh Kumar gov = find_governor(policy->last_governor); 1074de1df26bSRafael J. Wysocki if (gov) { 10756e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 10766e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 1077de1df26bSRafael J. Wysocki } else { 1078ab05d97aSYue Hu if (!def_gov) 1079de1df26bSRafael J. Wysocki return -ENODATA; 1080ab05d97aSYue Hu gov = def_gov; 1081de1df26bSRafael J. Wysocki } 10826e2c89d1Sviresh kumar new_policy.governor = gov; 1083ab05d97aSYue Hu } else { 108469030dd1SSrinivas Pandruvada /* Use the default policy if there is no last_policy. */ 1085ab05d97aSYue Hu if (policy->last_policy) { 108669030dd1SSrinivas Pandruvada new_policy.policy = policy->last_policy; 1087ab05d97aSYue Hu } else { 1088ab05d97aSYue Hu if (!def_gov) 1089ab05d97aSYue Hu return -ENODATA; 1090ab05d97aSYue Hu cpufreq_parse_policy(def_gov->name, &new_policy); 109169030dd1SSrinivas Pandruvada } 1092ab05d97aSYue Hu } 1093ab05d97aSYue Hu 10947f0fa40fSViresh Kumar return cpufreq_set_policy(policy, &new_policy); 1095909a694eSDave Jones } 1096909a694eSDave Jones 1097d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1098fcf80582SViresh Kumar { 10999c0ebcf7SViresh Kumar int ret = 0; 1100fcf80582SViresh Kumar 1101bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 1102bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 1103bb29ae15SViresh Kumar return 0; 1104bb29ae15SViresh Kumar 110549f18560SViresh Kumar down_write(&policy->rwsem); 110645482c70SRafael J. Wysocki if (has_target()) 110745482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 1108fcf80582SViresh Kumar 1109fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 11102eaa3e2dSViresh Kumar 11119c0ebcf7SViresh Kumar if (has_target()) { 11120a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 111349f18560SViresh Kumar if (ret) 11143de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 1115820c6ca2SViresh Kumar } 111649f18560SViresh Kumar up_write(&policy->rwsem); 1117fcf80582SViresh Kumar return ret; 1118fcf80582SViresh Kumar } 1119fcf80582SViresh Kumar 112011eb69b9SViresh Kumar static void handle_update(struct work_struct *work) 112111eb69b9SViresh Kumar { 112211eb69b9SViresh Kumar struct cpufreq_policy *policy = 112311eb69b9SViresh Kumar container_of(work, struct cpufreq_policy, update); 112411eb69b9SViresh Kumar unsigned int cpu = policy->cpu; 112511eb69b9SViresh Kumar pr_debug("handle_update for cpu %u called\n", cpu); 112611eb69b9SViresh Kumar cpufreq_update_policy(cpu); 1127fcf80582SViresh Kumar } 11281da177e4SLinus Torvalds 1129a34e63b1SRafael J. Wysocki static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 11308414809cSSrivatsa S. Bhat { 1131e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1132edd4a893SViresh Kumar int ret; 1133e9698cc5SSrivatsa S. Bhat 1134e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1135e9698cc5SSrivatsa S. Bhat if (!policy) 1136e9698cc5SSrivatsa S. Bhat return NULL; 1137e9698cc5SSrivatsa S. Bhat 1138e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1139e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1140e9698cc5SSrivatsa S. Bhat 1141e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1142e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1143e9698cc5SSrivatsa S. Bhat 1144559ed407SRafael J. Wysocki if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 1145559ed407SRafael J. Wysocki goto err_free_rcpumask; 1146559ed407SRafael J. Wysocki 1147edd4a893SViresh Kumar ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 1148edd4a893SViresh Kumar cpufreq_global_kobject, "policy%u", cpu); 1149edd4a893SViresh Kumar if (ret) { 1150edd4a893SViresh Kumar pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); 11512acb9bdaSRafael J. Wysocki /* 11522acb9bdaSRafael J. Wysocki * The entire policy object will be freed below, but the extra 11532acb9bdaSRafael J. Wysocki * memory allocated for the kobject name needs to be freed by 11542acb9bdaSRafael J. Wysocki * releasing the kobject. 11552acb9bdaSRafael J. Wysocki */ 11564ebe36c9SViresh Kumar kobject_put(&policy->kobj); 1157edd4a893SViresh Kumar goto err_free_real_cpus; 1158edd4a893SViresh Kumar } 1159edd4a893SViresh Kumar 1160c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1161ad7722daSviresh kumar init_rwsem(&policy->rwsem); 116212478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 116312478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1164818c5712SViresh Kumar init_completion(&policy->kobj_unregister); 1165818c5712SViresh Kumar INIT_WORK(&policy->update, handle_update); 1166ad7722daSviresh kumar 1167a34e63b1SRafael J. Wysocki policy->cpu = cpu; 1168e9698cc5SSrivatsa S. Bhat return policy; 1169e9698cc5SSrivatsa S. Bhat 1170edd4a893SViresh Kumar err_free_real_cpus: 1171edd4a893SViresh Kumar free_cpumask_var(policy->real_cpus); 11722fc3384dSViresh Kumar err_free_rcpumask: 11732fc3384dSViresh Kumar free_cpumask_var(policy->related_cpus); 1174e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1175e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1176e9698cc5SSrivatsa S. Bhat err_free_policy: 1177e9698cc5SSrivatsa S. Bhat kfree(policy); 1178e9698cc5SSrivatsa S. Bhat 1179e9698cc5SSrivatsa S. Bhat return NULL; 1180e9698cc5SSrivatsa S. Bhat } 1181e9698cc5SSrivatsa S. Bhat 1182f9f41e3eSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 118342f921a6SViresh Kumar { 118442f921a6SViresh Kumar struct kobject *kobj; 118542f921a6SViresh Kumar struct completion *cmp; 118642f921a6SViresh Kumar 118787549141SViresh Kumar down_write(&policy->rwsem); 11881aefc75bSRafael J. Wysocki cpufreq_stats_free_table(policy); 118942f921a6SViresh Kumar kobj = &policy->kobj; 119042f921a6SViresh Kumar cmp = &policy->kobj_unregister; 119187549141SViresh Kumar up_write(&policy->rwsem); 119242f921a6SViresh Kumar kobject_put(kobj); 119342f921a6SViresh Kumar 119442f921a6SViresh Kumar /* 119542f921a6SViresh Kumar * We need to make sure that the underlying kobj is 119642f921a6SViresh Kumar * actually not referenced anymore by anybody before we 119742f921a6SViresh Kumar * proceed with unloading. 119842f921a6SViresh Kumar */ 119942f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 120042f921a6SViresh Kumar wait_for_completion(cmp); 120142f921a6SViresh Kumar pr_debug("wait complete\n"); 120242f921a6SViresh Kumar } 120342f921a6SViresh Kumar 1204f9f41e3eSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy) 1205e9698cc5SSrivatsa S. Bhat { 1206988bed09SViresh Kumar unsigned long flags; 1207988bed09SViresh Kumar int cpu; 1208988bed09SViresh Kumar 1209988bed09SViresh Kumar /* Remove policy from list */ 1210988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1211988bed09SViresh Kumar list_del(&policy->policy_list); 1212988bed09SViresh Kumar 1213988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1214988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1215988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1216988bed09SViresh Kumar 1217f9f41e3eSViresh Kumar cpufreq_policy_put_kobj(policy); 1218559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1219e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1220e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1221e9698cc5SSrivatsa S. Bhat kfree(policy); 1222e9698cc5SSrivatsa S. Bhat } 1223e9698cc5SSrivatsa S. Bhat 12240b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 12251da177e4SLinus Torvalds { 12267f0c020aSViresh Kumar struct cpufreq_policy *policy; 1227194d99c7SRafael J. Wysocki bool new_policy; 12281da177e4SLinus Torvalds unsigned long flags; 12290b275352SRafael J. Wysocki unsigned int j; 12300b275352SRafael J. Wysocki int ret; 1231c32b6b8eSAshok Raj 12320b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 12336eed9404SViresh Kumar 1234bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 12359104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 123611ce707eSRafael J. Wysocki if (policy) { 12379104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 123811ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1239d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 12401da177e4SLinus Torvalds 124111ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1242194d99c7SRafael J. Wysocki new_policy = false; 124311ce707eSRafael J. Wysocki down_write(&policy->rwsem); 124411ce707eSRafael J. Wysocki policy->cpu = cpu; 124511ce707eSRafael J. Wysocki policy->governor = NULL; 124611ce707eSRafael J. Wysocki up_write(&policy->rwsem); 124711ce707eSRafael J. Wysocki } else { 1248194d99c7SRafael J. Wysocki new_policy = true; 1249a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1250059019a3SDave Jones if (!policy) 1251d4d854d6SRafael J. Wysocki return -ENOMEM; 125272368d12SRafael J. Wysocki } 12530d66b91eSSrivatsa S. Bhat 125491a12e91SViresh Kumar if (!new_policy && cpufreq_driver->online) { 125591a12e91SViresh Kumar ret = cpufreq_driver->online(policy); 125691a12e91SViresh Kumar if (ret) { 125791a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 125891a12e91SViresh Kumar __LINE__); 125991a12e91SViresh Kumar goto out_exit_policy; 126091a12e91SViresh Kumar } 126191a12e91SViresh Kumar 126291a12e91SViresh Kumar /* Recover policy->cpus using related_cpus */ 126391a12e91SViresh Kumar cpumask_copy(policy->cpus, policy->related_cpus); 126491a12e91SViresh Kumar } else { 1265835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 12661da177e4SLinus Torvalds 126791a12e91SViresh Kumar /* 126891a12e91SViresh Kumar * Call driver. From then on the cpufreq must be able 126991a12e91SViresh Kumar * to accept all calls to ->verify and ->setpolicy for this CPU. 12701da177e4SLinus Torvalds */ 12711c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 12721da177e4SLinus Torvalds if (ret) { 127391a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 127491a12e91SViresh Kumar __LINE__); 12758101f997SViresh Kumar goto out_free_policy; 12761da177e4SLinus Torvalds } 1277643ae6e8SViresh Kumar 1278d417e069SViresh Kumar ret = cpufreq_table_validate_and_sort(policy); 1279d417e069SViresh Kumar if (ret) 1280d417e069SViresh Kumar goto out_exit_policy; 1281d417e069SViresh Kumar 12824d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 12830998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 12844d1f3a5bSRafael J. Wysocki } 1285559ed407SRafael J. Wysocki 128691a12e91SViresh Kumar down_write(&policy->rwsem); 12875a7e56a5SViresh Kumar /* 12885a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 12895a7e56a5SViresh Kumar * managing offline cpus here. 12905a7e56a5SViresh Kumar */ 12915a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 12925a7e56a5SViresh Kumar 1293194d99c7SRafael J. Wysocki if (new_policy) { 12945a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 12955a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 12966d4e81edSTomeu Vizoso 12972f0ba790SRafael J. Wysocki for_each_cpu(j, policy->related_cpus) { 1298652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 12992f0ba790SRafael J. Wysocki add_cpu_dev_symlink(policy, j); 13002f0ba790SRafael J. Wysocki } 1301ff010472SViresh Kumar } else { 1302ff010472SViresh Kumar policy->min = policy->user_policy.min; 1303ff010472SViresh Kumar policy->max = policy->user_policy.max; 1304988bed09SViresh Kumar } 1305652ed95dSViresh Kumar 1306*5ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target()) { 1307da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1308da60ce9fSViresh Kumar if (!policy->cur) { 1309da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1310d417e069SViresh Kumar goto out_destroy_policy; 1311da60ce9fSViresh Kumar } 1312da60ce9fSViresh Kumar } 1313da60ce9fSViresh Kumar 1314d3916691SViresh Kumar /* 1315d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1316d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1317d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1318d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1319d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1320d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1321d3916691SViresh Kumar * isn't found in freq-table. 1322d3916691SViresh Kumar * 1323d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1324d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1325d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1326d3916691SViresh Kumar * is initialized to zero). 1327d3916691SViresh Kumar * 1328d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1329d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1330d3916691SViresh Kumar * equal to target-freq. 1331d3916691SViresh Kumar */ 1332d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1333d3916691SViresh Kumar && has_target()) { 1334d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1335d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1336d3916691SViresh Kumar if (ret == -EINVAL) { 1337d3916691SViresh Kumar /* Warn user and fix it */ 1338d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1339d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1340d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1341d3916691SViresh Kumar CPUFREQ_RELATION_L); 1342d3916691SViresh Kumar 1343d3916691SViresh Kumar /* 1344d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1345d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1346d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1347d3916691SViresh Kumar */ 1348d3916691SViresh Kumar BUG_ON(ret); 1349d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1350d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1351d3916691SViresh Kumar } 1352d3916691SViresh Kumar } 1353d3916691SViresh Kumar 1354194d99c7SRafael J. Wysocki if (new_policy) { 1355d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 135619d6f7ecSDave Jones if (ret) 1357d417e069SViresh Kumar goto out_destroy_policy; 13581aefc75bSRafael J. Wysocki 13591aefc75bSRafael J. Wysocki cpufreq_stats_create_table(policy); 1360c88a1f8bSLukasz Majewski 1361c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1362c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1363c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1364988bed09SViresh Kumar } 13658ff69732SDave Jones 13667f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 13677f0fa40fSViresh Kumar if (ret) { 13687f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 13697f0fa40fSViresh Kumar __func__, cpu, ret); 1370d417e069SViresh Kumar goto out_destroy_policy; 137108fd8c1cSViresh Kumar } 1372e18f1682SSrivatsa S. Bhat 13734e97b631SViresh Kumar up_write(&policy->rwsem); 137408fd8c1cSViresh Kumar 1375038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 13767c45cf31SViresh Kumar 13777c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 13787c45cf31SViresh Kumar if (cpufreq_driver->ready) 13797c45cf31SViresh Kumar cpufreq_driver->ready(policy); 13807c45cf31SViresh Kumar 1381bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) 13825c238a8bSAmit Kucheria policy->cdev = of_cpufreq_cooling_register(policy); 13835c238a8bSAmit Kucheria 13842d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds return 0; 13871da177e4SLinus Torvalds 1388d417e069SViresh Kumar out_destroy_policy: 1389b24b6478SViresh Kumar for_each_cpu(j, policy->real_cpus) 1390b24b6478SViresh Kumar remove_cpu_dev_symlink(policy, get_cpu_device(j)); 1391b24b6478SViresh Kumar 13927106e02bSPrarit Bhargava up_write(&policy->rwsem); 13937106e02bSPrarit Bhargava 1394d417e069SViresh Kumar out_exit_policy: 1395da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1396da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 13972f0ba790SRafael J. Wysocki 13988101f997SViresh Kumar out_free_policy: 1399f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 14001da177e4SLinus Torvalds return ret; 14011da177e4SLinus Torvalds } 14021da177e4SLinus Torvalds 14030b275352SRafael J. Wysocki /** 14040b275352SRafael J. Wysocki * cpufreq_add_dev - the cpufreq interface for a CPU device. 14050b275352SRafael J. Wysocki * @dev: CPU device. 14060b275352SRafael J. Wysocki * @sif: Subsystem interface structure pointer (not used) 14070b275352SRafael J. Wysocki */ 14080b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 14090b275352SRafael J. Wysocki { 1410a794d613SRafael J. Wysocki struct cpufreq_policy *policy; 14110b275352SRafael J. Wysocki unsigned cpu = dev->id; 141226619804SViresh Kumar int ret; 14130b275352SRafael J. Wysocki 14140b275352SRafael J. Wysocki dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 14150b275352SRafael J. Wysocki 141626619804SViresh Kumar if (cpu_online(cpu)) { 141726619804SViresh Kumar ret = cpufreq_online(cpu); 141826619804SViresh Kumar if (ret) 141926619804SViresh Kumar return ret; 142026619804SViresh Kumar } 1421a794d613SRafael J. Wysocki 142226619804SViresh Kumar /* Create sysfs link on CPU registration */ 1423a794d613SRafael J. Wysocki policy = per_cpu(cpufreq_cpu_data, cpu); 14242f0ba790SRafael J. Wysocki if (policy) 14252f0ba790SRafael J. Wysocki add_cpu_dev_symlink(policy, cpu); 14262f0ba790SRafael J. Wysocki 1427a794d613SRafael J. Wysocki return 0; 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 143027622b06SSebastian Andrzej Siewior static int cpufreq_offline(unsigned int cpu) 14311da177e4SLinus Torvalds { 14323a3e9e06SViresh Kumar struct cpufreq_policy *policy; 143369cee714SViresh Kumar int ret; 14341da177e4SLinus Torvalds 1435b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 14361da177e4SLinus Torvalds 1437988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 14383a3e9e06SViresh Kumar if (!policy) { 1439b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 144027622b06SSebastian Andrzej Siewior return 0; 14411da177e4SLinus Torvalds } 14421da177e4SLinus Torvalds 144349f18560SViresh Kumar down_write(&policy->rwsem); 144445482c70SRafael J. Wysocki if (has_target()) 144545482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 14461da177e4SLinus Torvalds 14479591becbSViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 14484573237bSViresh Kumar 14499591becbSViresh Kumar if (policy_is_inactive(policy)) { 14509591becbSViresh Kumar if (has_target()) 14514573237bSViresh Kumar strncpy(policy->last_governor, policy->governor->name, 14524573237bSViresh Kumar CPUFREQ_NAME_LEN); 145369030dd1SSrinivas Pandruvada else 145469030dd1SSrinivas Pandruvada policy->last_policy = policy->policy; 14559591becbSViresh Kumar } else if (cpu == policy->cpu) { 14569591becbSViresh Kumar /* Nominate new CPU */ 14579591becbSViresh Kumar policy->cpu = cpumask_any(policy->cpus); 14589591becbSViresh Kumar } 14591da177e4SLinus Torvalds 14609591becbSViresh Kumar /* Start governor again for active policy */ 14619591becbSViresh Kumar if (!policy_is_inactive(policy)) { 14629591becbSViresh Kumar if (has_target()) { 14630a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 14649591becbSViresh Kumar if (ret) 14659591becbSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 14669591becbSViresh Kumar } 146769cee714SViresh Kumar 146849f18560SViresh Kumar goto unlock; 146969cee714SViresh Kumar } 147069cee714SViresh Kumar 1471bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) { 14725c238a8bSAmit Kucheria cpufreq_cooling_unregister(policy->cdev); 14735c238a8bSAmit Kucheria policy->cdev = NULL; 14745c238a8bSAmit Kucheria } 14755c238a8bSAmit Kucheria 147669cee714SViresh Kumar if (cpufreq_driver->stop_cpu) 1477367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 147887549141SViresh Kumar 147936be3418SRafael J. Wysocki if (has_target()) 148036be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 14812a998599SRafael J. Wysocki 14828414809cSSrivatsa S. Bhat /* 148391a12e91SViresh Kumar * Perform the ->offline() during light-weight tear-down, as 148491a12e91SViresh Kumar * that allows fast recovery when the CPU comes back. 14858414809cSSrivatsa S. Bhat */ 148691a12e91SViresh Kumar if (cpufreq_driver->offline) { 148791a12e91SViresh Kumar cpufreq_driver->offline(policy); 148891a12e91SViresh Kumar } else if (cpufreq_driver->exit) { 14893a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 149055582bccSSrinivas Pandruvada policy->freq_table = NULL; 149155582bccSSrinivas Pandruvada } 149249f18560SViresh Kumar 149349f18560SViresh Kumar unlock: 149449f18560SViresh Kumar up_write(&policy->rwsem); 149527622b06SSebastian Andrzej Siewior return 0; 14961da177e4SLinus Torvalds } 14971da177e4SLinus Torvalds 1498cedb70afSSrivatsa S. Bhat /** 149927a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1500cedb70afSSrivatsa S. Bhat * 1501cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1502cedb70afSSrivatsa S. Bhat */ 150371db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 15045a01f2e8SVenkatesh Pallipadi { 15058a25a2fdSKay Sievers unsigned int cpu = dev->id; 150687549141SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 150787549141SViresh Kumar 150887549141SViresh Kumar if (!policy) 150971db87baSViresh Kumar return; 1510ec28297aSVenki Pallipadi 151169cee714SViresh Kumar if (cpu_online(cpu)) 151269cee714SViresh Kumar cpufreq_offline(cpu); 151387549141SViresh Kumar 1514559ed407SRafael J. Wysocki cpumask_clear_cpu(cpu, policy->real_cpus); 151526619804SViresh Kumar remove_cpu_dev_symlink(policy, dev); 1516f344dae0SViresh Kumar 151791a12e91SViresh Kumar if (cpumask_empty(policy->real_cpus)) { 151891a12e91SViresh Kumar /* We did light-weight exit earlier, do full tear down now */ 151991a12e91SViresh Kumar if (cpufreq_driver->offline) 152091a12e91SViresh Kumar cpufreq_driver->exit(policy); 152191a12e91SViresh Kumar 1522f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 15235a01f2e8SVenkatesh Pallipadi } 152491a12e91SViresh Kumar } 15255a01f2e8SVenkatesh Pallipadi 15261da177e4SLinus Torvalds /** 1527bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1528bb176f7dSViresh Kumar * in deep trouble. 1529a1e1dc41SViresh Kumar * @policy: policy managing CPUs 15301da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 15311da177e4SLinus Torvalds * 153229464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 153329464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 15341da177e4SLinus Torvalds */ 1535a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1536e08f5f5bSGautham R Shenoy unsigned int new_freq) 15371da177e4SLinus Torvalds { 15381da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1539b43a7ffbSViresh Kumar 1540e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1541a1e1dc41SViresh Kumar policy->cur, new_freq); 15421da177e4SLinus Torvalds 1543a1e1dc41SViresh Kumar freqs.old = policy->cur; 15441da177e4SLinus Torvalds freqs.new = new_freq; 1545b43a7ffbSViresh Kumar 15468fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 15478fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 15481da177e4SLinus Torvalds } 15491da177e4SLinus Torvalds 15501da177e4SLinus Torvalds /** 15514ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 155295235ca2SVenkatesh Pallipadi * @cpu: CPU number 155395235ca2SVenkatesh Pallipadi * 155495235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 155595235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 155695235ca2SVenkatesh Pallipadi */ 155795235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 155895235ca2SVenkatesh Pallipadi { 15599e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1560e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 1561c75361c0SRichard Cochran unsigned long flags; 156295235ca2SVenkatesh Pallipadi 1563c75361c0SRichard Cochran read_lock_irqsave(&cpufreq_driver_lock, flags); 1564c75361c0SRichard Cochran 1565c75361c0SRichard Cochran if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { 1566c75361c0SRichard Cochran ret_freq = cpufreq_driver->get(cpu); 1567c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1568c75361c0SRichard Cochran return ret_freq; 1569c75361c0SRichard Cochran } 1570c75361c0SRichard Cochran 1571c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 15729e21ba8bSDirk Brandewie 15739e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 157495235ca2SVenkatesh Pallipadi if (policy) { 1575e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 157695235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 157795235ca2SVenkatesh Pallipadi } 157895235ca2SVenkatesh Pallipadi 15794d34a67dSDave Jones return ret_freq; 158095235ca2SVenkatesh Pallipadi } 158195235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 158295235ca2SVenkatesh Pallipadi 15833d737108SJesse Barnes /** 15843d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 15853d737108SJesse Barnes * @cpu: CPU number 15863d737108SJesse Barnes * 15873d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 15883d737108SJesse Barnes */ 15893d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 15903d737108SJesse Barnes { 15913d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15923d737108SJesse Barnes unsigned int ret_freq = 0; 15933d737108SJesse Barnes 15943d737108SJesse Barnes if (policy) { 15953d737108SJesse Barnes ret_freq = policy->max; 15963d737108SJesse Barnes cpufreq_cpu_put(policy); 15973d737108SJesse Barnes } 15983d737108SJesse Barnes 15993d737108SJesse Barnes return ret_freq; 16003d737108SJesse Barnes } 16013d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 16023d737108SJesse Barnes 1603d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 16041da177e4SLinus Torvalds { 1605e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 16061da177e4SLinus Torvalds 16074db7c34cSYue Hu if (unlikely(policy_is_inactive(policy))) 16084d34a67dSDave Jones return ret_freq; 16091da177e4SLinus Torvalds 1610d92d50a4SViresh Kumar ret_freq = cpufreq_driver->get(policy->cpu); 16111da177e4SLinus Torvalds 1612b7898fdaSRafael J. Wysocki /* 16132f661962SSudeep Holla * If fast frequency switching is used with the given policy, the check 1614b7898fdaSRafael J. Wysocki * against policy->cur is pointless, so skip it in that case too. 1615b7898fdaSRafael J. Wysocki */ 16162f661962SSudeep Holla if (policy->fast_switch_enabled) 161711e584cfSViresh Kumar return ret_freq; 161811e584cfSViresh Kumar 1619e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 16201c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1621e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1622e08f5f5bSGautham R Shenoy saved value exists */ 1623e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1624a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, ret_freq); 16251da177e4SLinus Torvalds schedule_work(&policy->update); 16261da177e4SLinus Torvalds } 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 16294d34a67dSDave Jones return ret_freq; 16305a01f2e8SVenkatesh Pallipadi } 16311da177e4SLinus Torvalds 16325a01f2e8SVenkatesh Pallipadi /** 16335a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 16345a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 16355a01f2e8SVenkatesh Pallipadi * 16365a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 16375a01f2e8SVenkatesh Pallipadi */ 16385a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 16395a01f2e8SVenkatesh Pallipadi { 1640999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 16415a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 16425a01f2e8SVenkatesh Pallipadi 1643999976e0SAaron Plattner if (policy) { 1644ad7722daSviresh kumar down_read(&policy->rwsem); 16454db7c34cSYue Hu if (cpufreq_driver->get) 1646d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1647ad7722daSviresh kumar up_read(&policy->rwsem); 1648999976e0SAaron Plattner 1649999976e0SAaron Plattner cpufreq_cpu_put(policy); 1650999976e0SAaron Plattner } 16516eed9404SViresh Kumar 16524d34a67dSDave Jones return ret_freq; 16531da177e4SLinus Torvalds } 16541da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 16551da177e4SLinus Torvalds 1656999f5729SRafael J. Wysocki static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy) 1657999f5729SRafael J. Wysocki { 1658999f5729SRafael J. Wysocki unsigned int new_freq; 1659999f5729SRafael J. Wysocki 1660999f5729SRafael J. Wysocki new_freq = cpufreq_driver->get(policy->cpu); 1661999f5729SRafael J. Wysocki if (!new_freq) 1662999f5729SRafael J. Wysocki return 0; 1663999f5729SRafael J. Wysocki 1664999f5729SRafael J. Wysocki if (!policy->cur) { 1665999f5729SRafael J. Wysocki pr_debug("cpufreq: Driver did not initialize current freq\n"); 1666999f5729SRafael J. Wysocki policy->cur = new_freq; 1667999f5729SRafael J. Wysocki } else if (policy->cur != new_freq && has_target()) { 1668999f5729SRafael J. Wysocki cpufreq_out_of_sync(policy, new_freq); 1669999f5729SRafael J. Wysocki } 1670999f5729SRafael J. Wysocki 1671999f5729SRafael J. Wysocki return new_freq; 1672999f5729SRafael J. Wysocki } 1673999f5729SRafael J. Wysocki 16748a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 16758a25a2fdSKay Sievers .name = "cpufreq", 16768a25a2fdSKay Sievers .subsys = &cpu_subsys, 16778a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 16788a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1679e00e56dfSRafael J. Wysocki }; 1680e00e56dfSRafael J. Wysocki 1681e28867eaSViresh Kumar /* 1682e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1683e28867eaSViresh Kumar * during suspend.. 168442d4dc3fSBenjamin Herrenschmidt */ 1685e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 168642d4dc3fSBenjamin Herrenschmidt { 1687e28867eaSViresh Kumar int ret; 16884bc5d341SDave Jones 1689e28867eaSViresh Kumar if (!policy->suspend_freq) { 1690201f3716SBartlomiej Zolnierkiewicz pr_debug("%s: suspend_freq not defined\n", __func__); 1691201f3716SBartlomiej Zolnierkiewicz return 0; 169242d4dc3fSBenjamin Herrenschmidt } 169342d4dc3fSBenjamin Herrenschmidt 1694e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1695e28867eaSViresh Kumar policy->suspend_freq); 1696e28867eaSViresh Kumar 1697e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1698e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1699e28867eaSViresh Kumar if (ret) 1700e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1701e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1702e28867eaSViresh Kumar 1703c9060494SDave Jones return ret; 170442d4dc3fSBenjamin Herrenschmidt } 1705e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 170642d4dc3fSBenjamin Herrenschmidt 170742d4dc3fSBenjamin Herrenschmidt /** 17082f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 17091da177e4SLinus Torvalds * 17102f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 17112f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 17122f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 17132f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 17141da177e4SLinus Torvalds */ 17152f0aea93SViresh Kumar void cpufreq_suspend(void) 17161da177e4SLinus Torvalds { 17173a3e9e06SViresh Kumar struct cpufreq_policy *policy; 17181da177e4SLinus Torvalds 17192f0aea93SViresh Kumar if (!cpufreq_driver) 1720e00e56dfSRafael J. Wysocki return; 17211da177e4SLinus Torvalds 1722ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->suspend) 1723b1b12babSViresh Kumar goto suspend; 17241da177e4SLinus Torvalds 17252f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 17262f0aea93SViresh Kumar 1727f963735aSViresh Kumar for_each_active_policy(policy) { 1728ba41e1bcSRafael J. Wysocki if (has_target()) { 172949f18560SViresh Kumar down_write(&policy->rwsem); 173045482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 173149f18560SViresh Kumar up_write(&policy->rwsem); 1732ba41e1bcSRafael J. Wysocki } 1733ba41e1bcSRafael J. Wysocki 1734ba41e1bcSRafael J. Wysocki if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) 17352f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 17362f0aea93SViresh Kumar policy); 17371da177e4SLinus Torvalds } 1738b1b12babSViresh Kumar 1739b1b12babSViresh Kumar suspend: 1740b1b12babSViresh Kumar cpufreq_suspended = true; 17411da177e4SLinus Torvalds } 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /** 17442f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 17451da177e4SLinus Torvalds * 17462f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 17472f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 17481da177e4SLinus Torvalds */ 17492f0aea93SViresh Kumar void cpufreq_resume(void) 17501da177e4SLinus Torvalds { 17511da177e4SLinus Torvalds struct cpufreq_policy *policy; 175249f18560SViresh Kumar int ret; 17531da177e4SLinus Torvalds 17542f0aea93SViresh Kumar if (!cpufreq_driver) 17551da177e4SLinus Torvalds return; 17561da177e4SLinus Torvalds 1757703cbaa6SBo Yan if (unlikely(!cpufreq_suspended)) 1758703cbaa6SBo Yan return; 1759703cbaa6SBo Yan 17608e30444eSLan Tianyu cpufreq_suspended = false; 17618e30444eSLan Tianyu 1762ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->resume) 17632f0aea93SViresh Kumar return; 17641da177e4SLinus Torvalds 17652f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 17662f0aea93SViresh Kumar 1767f963735aSViresh Kumar for_each_active_policy(policy) { 176849f18560SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 17690c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 17700c5aa405SViresh Kumar policy); 1771ba41e1bcSRafael J. Wysocki } else if (has_target()) { 177249f18560SViresh Kumar down_write(&policy->rwsem); 17730a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 177449f18560SViresh Kumar up_write(&policy->rwsem); 177549f18560SViresh Kumar 177649f18560SViresh Kumar if (ret) 17772f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 17782f0aea93SViresh Kumar __func__, policy); 1779c75de0acSViresh Kumar } 178049f18560SViresh Kumar } 17811da177e4SLinus Torvalds } 17821da177e4SLinus Torvalds 17839d95046eSBorislav Petkov /** 17849d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17859d95046eSBorislav Petkov * 17869d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17879d95046eSBorislav Petkov * or NULL, if none. 17889d95046eSBorislav Petkov */ 17899d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17909d95046eSBorislav Petkov { 17911c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17921c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17931c3d85ddSRafael J. Wysocki 17941c3d85ddSRafael J. Wysocki return NULL; 17959d95046eSBorislav Petkov } 17969d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17971da177e4SLinus Torvalds 179851315cdfSThomas Petazzoni /** 179951315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 180051315cdfSThomas Petazzoni * 180151315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 180251315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 180351315cdfSThomas Petazzoni */ 180451315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 180551315cdfSThomas Petazzoni { 180651315cdfSThomas Petazzoni if (cpufreq_driver) 180751315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 180851315cdfSThomas Petazzoni 180951315cdfSThomas Petazzoni return NULL; 181051315cdfSThomas Petazzoni } 181151315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 181251315cdfSThomas Petazzoni 18131da177e4SLinus Torvalds /********************************************************************* 18141da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 18151da177e4SLinus Torvalds *********************************************************************/ 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds /** 18181da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 18191da177e4SLinus Torvalds * @nb: notifier function to register 18201da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 18211da177e4SLinus Torvalds * 18221da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 18231da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 18241da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 18251da177e4SLinus Torvalds * changes in cpufreq policy. 18261da177e4SLinus Torvalds * 18271da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1828e041c683SAlan Stern * blocking_notifier_chain_register. 18291da177e4SLinus Torvalds */ 18301da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 18311da177e4SLinus Torvalds { 18321da177e4SLinus Torvalds int ret; 18331da177e4SLinus Torvalds 1834d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1835d5aaffa9SDirk Brandewie return -EINVAL; 1836d5aaffa9SDirk Brandewie 18371da177e4SLinus Torvalds switch (list) { 18381da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1839b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 1840b7898fdaSRafael J. Wysocki 1841b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count > 0) { 1842b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 1843b7898fdaSRafael J. Wysocki return -EBUSY; 1844b7898fdaSRafael J. Wysocki } 1845b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1846e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 1847b7898fdaSRafael J. Wysocki if (!ret) 1848b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 1849b7898fdaSRafael J. Wysocki 1850b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 18511da177e4SLinus Torvalds break; 18521da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1853e041c683SAlan Stern ret = blocking_notifier_chain_register( 1854e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18551da177e4SLinus Torvalds break; 18561da177e4SLinus Torvalds default: 18571da177e4SLinus Torvalds ret = -EINVAL; 18581da177e4SLinus Torvalds } 18591da177e4SLinus Torvalds 18601da177e4SLinus Torvalds return ret; 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds /** 18651da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 18661da177e4SLinus Torvalds * @nb: notifier block to be unregistered 18671da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 18681da177e4SLinus Torvalds * 18691da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 18701da177e4SLinus Torvalds * 18711da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1872e041c683SAlan Stern * blocking_notifier_chain_unregister. 18731da177e4SLinus Torvalds */ 18741da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 18751da177e4SLinus Torvalds { 18761da177e4SLinus Torvalds int ret; 18771da177e4SLinus Torvalds 1878d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1879d5aaffa9SDirk Brandewie return -EINVAL; 1880d5aaffa9SDirk Brandewie 18811da177e4SLinus Torvalds switch (list) { 18821da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1883b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 1884b7898fdaSRafael J. Wysocki 1885b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1886e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 1887b7898fdaSRafael J. Wysocki if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) 1888b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 1889b7898fdaSRafael J. Wysocki 1890b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 18911da177e4SLinus Torvalds break; 18921da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1893e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1894e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18951da177e4SLinus Torvalds break; 18961da177e4SLinus Torvalds default: 18971da177e4SLinus Torvalds ret = -EINVAL; 18981da177e4SLinus Torvalds } 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds return ret; 19011da177e4SLinus Torvalds } 19021da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 19031da177e4SLinus Torvalds 19041da177e4SLinus Torvalds 19051da177e4SLinus Torvalds /********************************************************************* 19061da177e4SLinus Torvalds * GOVERNORS * 19071da177e4SLinus Torvalds *********************************************************************/ 19081da177e4SLinus Torvalds 1909b7898fdaSRafael J. Wysocki /** 1910b7898fdaSRafael J. Wysocki * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. 1911b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to switch the frequency for. 1912b7898fdaSRafael J. Wysocki * @target_freq: New frequency to set (may be approximate). 1913b7898fdaSRafael J. Wysocki * 1914b7898fdaSRafael J. Wysocki * Carry out a fast frequency switch without sleeping. 1915b7898fdaSRafael J. Wysocki * 1916b7898fdaSRafael J. Wysocki * The driver's ->fast_switch() callback invoked by this function must be 1917b7898fdaSRafael J. Wysocki * suitable for being called from within RCU-sched read-side critical sections 1918b7898fdaSRafael J. Wysocki * and it is expected to select the minimum available frequency greater than or 1919b7898fdaSRafael J. Wysocki * equal to @target_freq (CPUFREQ_RELATION_L). 1920b7898fdaSRafael J. Wysocki * 1921b7898fdaSRafael J. Wysocki * This function must not be called if policy->fast_switch_enabled is unset. 1922b7898fdaSRafael J. Wysocki * 1923b7898fdaSRafael J. Wysocki * Governors calling this function must guarantee that it will never be invoked 1924b7898fdaSRafael J. Wysocki * twice in parallel for the same policy and that it will never be called in 1925b7898fdaSRafael J. Wysocki * parallel with either ->target() or ->target_index() for the same policy. 1926b7898fdaSRafael J. Wysocki * 1927209887e6SViresh Kumar * Returns the actual frequency set for the CPU. 1928209887e6SViresh Kumar * 1929209887e6SViresh Kumar * If 0 is returned by the driver's ->fast_switch() callback to indicate an 1930209887e6SViresh Kumar * error condition, the hardware configuration must be preserved. 1931b7898fdaSRafael J. Wysocki */ 1932b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, 1933b7898fdaSRafael J. Wysocki unsigned int target_freq) 1934b7898fdaSRafael J. Wysocki { 1935b9af6948SRafael J. Wysocki target_freq = clamp_val(target_freq, policy->min, policy->max); 1936b7898fdaSRafael J. Wysocki 1937b7898fdaSRafael J. Wysocki return cpufreq_driver->fast_switch(policy, target_freq); 1938b7898fdaSRafael J. Wysocki } 1939b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); 1940b7898fdaSRafael J. Wysocki 19411c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 19421c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 19431c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 19441c03a2d0SViresh Kumar { 19451c03a2d0SViresh Kumar int ret; 19461c03a2d0SViresh Kumar 19471c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 19481c03a2d0SViresh Kumar 19491c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 19501c03a2d0SViresh Kumar if (!freqs->new) 19511c03a2d0SViresh Kumar return 0; 19521c03a2d0SViresh Kumar 19531c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 19541c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 19551c03a2d0SViresh Kumar 19561c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 19571c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 19581c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 19591c03a2d0SViresh Kumar 19601c03a2d0SViresh Kumar if (ret) 19611c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 19621c03a2d0SViresh Kumar __func__, ret); 19631c03a2d0SViresh Kumar 19641c03a2d0SViresh Kumar return ret; 19651c03a2d0SViresh Kumar } 19661c03a2d0SViresh Kumar 196723727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index) 19688d65775dSViresh Kumar { 19691c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 19701c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 197123727845SViresh Kumar unsigned int newfreq = policy->freq_table[index].frequency; 19728d65775dSViresh Kumar int retval = -EINVAL; 19738d65775dSViresh Kumar bool notify; 19748d65775dSViresh Kumar 197523727845SViresh Kumar if (newfreq == policy->cur) 197623727845SViresh Kumar return 0; 197723727845SViresh Kumar 19788d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 19798d65775dSViresh Kumar if (notify) { 19801c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 19811c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 19821c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 19831c03a2d0SViresh Kumar if (retval) 19841c03a2d0SViresh Kumar return retval; 19858d65775dSViresh Kumar 19861c03a2d0SViresh Kumar intermediate_freq = freqs.new; 19871c03a2d0SViresh Kumar /* Set old freq to intermediate */ 19881c03a2d0SViresh Kumar if (intermediate_freq) 19891c03a2d0SViresh Kumar freqs.old = freqs.new; 19901c03a2d0SViresh Kumar } 19911c03a2d0SViresh Kumar 199223727845SViresh Kumar freqs.new = newfreq; 19938d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 19948d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 19958d65775dSViresh Kumar 19968d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 19978d65775dSViresh Kumar } 19988d65775dSViresh Kumar 19998d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 20008d65775dSViresh Kumar if (retval) 20018d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 20028d65775dSViresh Kumar retval); 20038d65775dSViresh Kumar 20041c03a2d0SViresh Kumar if (notify) { 20058d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 20068d65775dSViresh Kumar 20071c03a2d0SViresh Kumar /* 20081c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 20091c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 20101c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 201158405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 20121c03a2d0SViresh Kumar */ 20131c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 20141c03a2d0SViresh Kumar freqs.old = intermediate_freq; 20151c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 20161c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 20171c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 20181c03a2d0SViresh Kumar } 20191c03a2d0SViresh Kumar } 20201c03a2d0SViresh Kumar 20218d65775dSViresh Kumar return retval; 20228d65775dSViresh Kumar } 20238d65775dSViresh Kumar 20241da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 20251da177e4SLinus Torvalds unsigned int target_freq, 20261da177e4SLinus Torvalds unsigned int relation) 20271da177e4SLinus Torvalds { 20287249924eSViresh Kumar unsigned int old_target_freq = target_freq; 2029d218ed77SViresh Kumar int index; 2030c32b6b8eSAshok Raj 2031a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2032a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2033a7b422cdSKonrad Rzeszutek Wilk 20347249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 2035910c6e88SViresh Kumar target_freq = clamp_val(target_freq, policy->min, policy->max); 20367249924eSViresh Kumar 20377249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 20387249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 20395a1c0228SViresh Kumar 20409c0ebcf7SViresh Kumar /* 20419c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 20429c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 20439c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 20449c0ebcf7SViresh Kumar * calls. 20459c0ebcf7SViresh Kumar */ 20465a1c0228SViresh Kumar if (target_freq == policy->cur) 20475a1c0228SViresh Kumar return 0; 20485a1c0228SViresh Kumar 20491c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 20501c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 20511c03a2d0SViresh Kumar 20521c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 20536019d23aSRafael J. Wysocki return cpufreq_driver->target(policy, target_freq, relation); 20546019d23aSRafael J. Wysocki 20556019d23aSRafael J. Wysocki if (!cpufreq_driver->target_index) 20566019d23aSRafael J. Wysocki return -EINVAL; 205790d45d17SAshok Raj 2058d218ed77SViresh Kumar index = cpufreq_frequency_table_target(policy, target_freq, relation); 20599c0ebcf7SViresh Kumar 206023727845SViresh Kumar return __target_index(policy, index); 20616019d23aSRafael J. Wysocki } 20621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 20651da177e4SLinus Torvalds unsigned int target_freq, 20661da177e4SLinus Torvalds unsigned int relation) 20671da177e4SLinus Torvalds { 2068f1829e4aSJulia Lawall int ret = -EINVAL; 20691da177e4SLinus Torvalds 2070ad7722daSviresh kumar down_write(&policy->rwsem); 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 20731da177e4SLinus Torvalds 2074ad7722daSviresh kumar up_write(&policy->rwsem); 20751da177e4SLinus Torvalds 20761da177e4SLinus Torvalds return ret; 20771da177e4SLinus Torvalds } 20781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 20791da177e4SLinus Torvalds 2080de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 2081de1df26bSRafael J. Wysocki { 2082de1df26bSRafael J. Wysocki return NULL; 2083de1df26bSRafael J. Wysocki } 2084de1df26bSRafael J. Wysocki 2085a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy) 20861da177e4SLinus Torvalds { 2087cc993cabSDave Jones int ret; 20886afde10cSThomas Renninger 20892f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 20902f0aea93SViresh Kumar if (cpufreq_suspended) 20912f0aea93SViresh Kumar return 0; 2092cb57720bSEthan Zhao /* 2093cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 2094cb57720bSEthan Zhao * notification happened, so check it. 2095cb57720bSEthan Zhao */ 2096cb57720bSEthan Zhao if (!policy->governor) 2097cb57720bSEthan Zhao return -EINVAL; 20982f0aea93SViresh Kumar 2099ed4676e2SViresh Kumar /* Platform doesn't want dynamic frequency switching ? */ 2100ed4676e2SViresh Kumar if (policy->governor->dynamic_switching && 2101fc4c709fSViresh Kumar cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { 2102de1df26bSRafael J. Wysocki struct cpufreq_governor *gov = cpufreq_fallback_governor(); 2103de1df26bSRafael J. Wysocki 2104de1df26bSRafael J. Wysocki if (gov) { 2105fe829ed8SViresh Kumar pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", 2106e837f9b5SJoe Perches policy->governor->name, gov->name); 21071c256245SThomas Renninger policy->governor = gov; 2108de1df26bSRafael J. Wysocki } else { 2109de1df26bSRafael J. Wysocki return -EINVAL; 21101c256245SThomas Renninger } 21116afde10cSThomas Renninger } 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 21141da177e4SLinus Torvalds return -EINVAL; 21151da177e4SLinus Torvalds 2116a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 211795731ebbSXiaoguang Chen 2118e788892bSRafael J. Wysocki if (policy->governor->init) { 2119e788892bSRafael J. Wysocki ret = policy->governor->init(policy); 2120a92604b4SRafael J. Wysocki if (ret) { 21211da177e4SLinus Torvalds module_put(policy->governor->owner); 2122a92604b4SRafael J. Wysocki return ret; 21239e8c0a89SRafael J. Wysocki } 212436be3418SRafael J. Wysocki } 21251da177e4SLinus Torvalds 2126a92604b4SRafael J. Wysocki return 0; 2127a92604b4SRafael J. Wysocki } 2128a92604b4SRafael J. Wysocki 2129a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy) 2130a92604b4SRafael J. Wysocki { 2131a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2132a92604b4SRafael J. Wysocki return; 2133a92604b4SRafael J. Wysocki 2134a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2135a92604b4SRafael J. Wysocki 2136e788892bSRafael J. Wysocki if (policy->governor->exit) 2137e788892bSRafael J. Wysocki policy->governor->exit(policy); 2138a92604b4SRafael J. Wysocki 21391da177e4SLinus Torvalds module_put(policy->governor->owner); 21401da177e4SLinus Torvalds } 21411da177e4SLinus Torvalds 21420a300767SRafael J. Wysocki static int cpufreq_start_governor(struct cpufreq_policy *policy) 21430a300767SRafael J. Wysocki { 21440a300767SRafael J. Wysocki int ret; 21450a300767SRafael J. Wysocki 2146a92604b4SRafael J. Wysocki if (cpufreq_suspended) 2147a92604b4SRafael J. Wysocki return 0; 2148a92604b4SRafael J. Wysocki 2149a92604b4SRafael J. Wysocki if (!policy->governor) 2150a92604b4SRafael J. Wysocki return -EINVAL; 2151a92604b4SRafael J. Wysocki 2152a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2153a92604b4SRafael J. Wysocki 2154407d0fffSViresh Kumar if (cpufreq_driver->get) 21553bbf8fe3SRafael J. Wysocki cpufreq_update_current_freq(policy); 21563bbf8fe3SRafael J. Wysocki 2157e788892bSRafael J. Wysocki if (policy->governor->start) { 2158e788892bSRafael J. Wysocki ret = policy->governor->start(policy); 2159d6ff44d6SRafael J. Wysocki if (ret) 2160d6ff44d6SRafael J. Wysocki return ret; 2161e788892bSRafael J. Wysocki } 2162d6ff44d6SRafael J. Wysocki 2163e788892bSRafael J. Wysocki if (policy->governor->limits) 2164e788892bSRafael J. Wysocki policy->governor->limits(policy); 2165e788892bSRafael J. Wysocki 2166d6ff44d6SRafael J. Wysocki return 0; 21670a300767SRafael J. Wysocki } 21680a300767SRafael J. Wysocki 2169a92604b4SRafael J. Wysocki static void cpufreq_stop_governor(struct cpufreq_policy *policy) 2170a92604b4SRafael J. Wysocki { 2171a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2172a92604b4SRafael J. Wysocki return; 2173a92604b4SRafael J. Wysocki 2174a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2175a92604b4SRafael J. Wysocki 2176e788892bSRafael J. Wysocki if (policy->governor->stop) 2177e788892bSRafael J. Wysocki policy->governor->stop(policy); 2178a92604b4SRafael J. Wysocki } 2179a92604b4SRafael J. Wysocki 2180a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy) 2181a92604b4SRafael J. Wysocki { 2182a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2183a92604b4SRafael J. Wysocki return; 2184a92604b4SRafael J. Wysocki 2185a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2186a92604b4SRafael J. Wysocki 2187e788892bSRafael J. Wysocki if (policy->governor->limits) 2188e788892bSRafael J. Wysocki policy->governor->limits(policy); 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 21911da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 21921da177e4SLinus Torvalds { 21933bcb09a3SJeremy Fitzhardinge int err; 21941da177e4SLinus Torvalds 21951da177e4SLinus Torvalds if (!governor) 21961da177e4SLinus Torvalds return -EINVAL; 21971da177e4SLinus Torvalds 2198a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2199a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2200a7b422cdSKonrad Rzeszutek Wilk 22013fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 22021da177e4SLinus Torvalds 22033bcb09a3SJeremy Fitzhardinge err = -EBUSY; 220442f91fa1SViresh Kumar if (!find_governor(governor->name)) { 22053bcb09a3SJeremy Fitzhardinge err = 0; 22061da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 22073bcb09a3SJeremy Fitzhardinge } 22081da177e4SLinus Torvalds 22093fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 22103bcb09a3SJeremy Fitzhardinge return err; 22111da177e4SLinus Torvalds } 22121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 22131da177e4SLinus Torvalds 22141da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 22151da177e4SLinus Torvalds { 22164573237bSViresh Kumar struct cpufreq_policy *policy; 22174573237bSViresh Kumar unsigned long flags; 221890e41bacSPrarit Bhargava 22191da177e4SLinus Torvalds if (!governor) 22201da177e4SLinus Torvalds return; 22211da177e4SLinus Torvalds 2222a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2223a7b422cdSKonrad Rzeszutek Wilk return; 2224a7b422cdSKonrad Rzeszutek Wilk 22254573237bSViresh Kumar /* clear last_governor for all inactive policies */ 22264573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 22274573237bSViresh Kumar for_each_inactive_policy(policy) { 222818bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 222918bf3a12SViresh Kumar policy->governor = NULL; 22304573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 223190e41bacSPrarit Bhargava } 223218bf3a12SViresh Kumar } 22334573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 223490e41bacSPrarit Bhargava 22353fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 22361da177e4SLinus Torvalds list_del(&governor->governor_list); 22373fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds /********************************************************************* 22431da177e4SLinus Torvalds * POLICY INTERFACE * 22441da177e4SLinus Torvalds *********************************************************************/ 22451da177e4SLinus Torvalds 22461da177e4SLinus Torvalds /** 22471da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 224829464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 224929464f28SDave Jones * is written 22501da177e4SLinus Torvalds * 22511da177e4SLinus Torvalds * Reads the current cpufreq policy. 22521da177e4SLinus Torvalds */ 22531da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 22541da177e4SLinus Torvalds { 22551da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 22561da177e4SLinus Torvalds if (!policy) 22571da177e4SLinus Torvalds return -EINVAL; 22581da177e4SLinus Torvalds 22591da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 22601da177e4SLinus Torvalds if (!cpu_policy) 22611da177e4SLinus Torvalds return -EINVAL; 22621da177e4SLinus Torvalds 2263d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 22661da177e4SLinus Torvalds return 0; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 22691da177e4SLinus Torvalds 2270a0dbb819SRafael J. Wysocki /** 2271a0dbb819SRafael J. Wysocki * cpufreq_set_policy - Modify cpufreq policy parameters. 2272a0dbb819SRafael J. Wysocki * @policy: Policy object to modify. 2273a0dbb819SRafael J. Wysocki * @new_policy: New policy data. 2274a0dbb819SRafael J. Wysocki * 2275a0dbb819SRafael J. Wysocki * Pass @new_policy to the cpufreq driver's ->verify() callback, run the 2276a0dbb819SRafael J. Wysocki * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to 2277a0dbb819SRafael J. Wysocki * the driver's ->verify() callback again and run the notifiers for it again 2278a0dbb819SRafael J. Wysocki * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters 2279a0dbb819SRafael J. Wysocki * of @new_policy to @policy and either invoke the driver's ->setpolicy() 2280a0dbb819SRafael J. Wysocki * callback (if present) or carry out a governor update for @policy. That is, 2281a0dbb819SRafael J. Wysocki * run the current governor's ->limits() callback (if the governor field in 2282a0dbb819SRafael J. Wysocki * @new_policy points to the same object as the one in @policy) or replace the 2283a0dbb819SRafael J. Wysocki * governor for @policy with the new one stored in @new_policy. 2284a0dbb819SRafael J. Wysocki * 2285a0dbb819SRafael J. Wysocki * The cpuinfo part of @policy is not updated by this function. 2286153d7f3fSArjan van de Ven */ 22879083e498SRafael J. Wysocki int cpufreq_set_policy(struct cpufreq_policy *policy, 22883a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 22891da177e4SLinus Torvalds { 2290d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2291d9a789c7SRafael J. Wysocki int ret; 22921da177e4SLinus Torvalds 2293e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2294e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 22951da177e4SLinus Torvalds 2296d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 22971da177e4SLinus Torvalds 2298fba9573bSPan Xinhui /* 2299fba9573bSPan Xinhui * This check works well when we store new min/max freq attributes, 2300fba9573bSPan Xinhui * because new_policy is a copy of policy with one field updated. 2301fba9573bSPan Xinhui */ 2302fba9573bSPan Xinhui if (new_policy->min > new_policy->max) 2303d9a789c7SRafael J. Wysocki return -EINVAL; 23049c9a43edSMattia Dongili 23051da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 23063a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 23071da177e4SLinus Torvalds if (ret) 2308d9a789c7SRafael J. Wysocki return ret; 23091da177e4SLinus Torvalds 23101da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2311e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 23123a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 23131da177e4SLinus Torvalds 2314bb176f7dSViresh Kumar /* 2315bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2316bb176f7dSViresh Kumar * different to the first one 2317bb176f7dSViresh Kumar */ 23183a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2319e041c683SAlan Stern if (ret) 2320d9a789c7SRafael J. Wysocki return ret; 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds /* notification of the new policy */ 2323e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 23243a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 23251da177e4SLinus Torvalds 23263a3e9e06SViresh Kumar policy->min = new_policy->min; 23273a3e9e06SViresh Kumar policy->max = new_policy->max; 2328601b2185SRuchi Kandoi trace_cpu_frequency_limits(policy); 23291da177e4SLinus Torvalds 2330e3c06236SSteve Muckle policy->cached_target_freq = UINT_MAX; 2331e3c06236SSteve Muckle 23322d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 23333a3e9e06SViresh Kumar policy->min, policy->max); 23341da177e4SLinus Torvalds 23351c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 23363a3e9e06SViresh Kumar policy->policy = new_policy->policy; 23372d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2338167a38dcSRafael J. Wysocki return cpufreq_driver->setpolicy(policy); 2339d9a789c7SRafael J. Wysocki } 2340d9a789c7SRafael J. Wysocki 23410a300767SRafael J. Wysocki if (new_policy->governor == policy->governor) { 23422bb4059eSRafael J. Wysocki pr_debug("governor limits update\n"); 2343a92604b4SRafael J. Wysocki cpufreq_governor_limits(policy); 2344d6ff44d6SRafael J. Wysocki return 0; 23450a300767SRafael J. Wysocki } 23461da177e4SLinus Torvalds 23472d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 23481da177e4SLinus Torvalds 2349d9a789c7SRafael J. Wysocki /* save old, working values */ 2350d9a789c7SRafael J. Wysocki old_gov = policy->governor; 23511da177e4SLinus Torvalds /* end old governor */ 2352d9a789c7SRafael J. Wysocki if (old_gov) { 235345482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 235436be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 23557bd353a9SViresh Kumar } 23561da177e4SLinus Torvalds 23571da177e4SLinus Torvalds /* start new governor */ 23583a3e9e06SViresh Kumar policy->governor = new_policy->governor; 2359a92604b4SRafael J. Wysocki ret = cpufreq_init_governor(policy); 23604bc384aeSViresh Kumar if (!ret) { 23610a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 23620a300767SRafael J. Wysocki if (!ret) { 23632bb4059eSRafael J. Wysocki pr_debug("governor change\n"); 2364531b5c9fSQuentin Perret sched_cpufreq_governor_change(policy, old_gov); 23650a300767SRafael J. Wysocki return 0; 23660a300767SRafael J. Wysocki } 2367b7898fdaSRafael J. Wysocki cpufreq_exit_governor(policy); 2368955ef483SViresh Kumar } 23697bd353a9SViresh Kumar 23701da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2371d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 23721da177e4SLinus Torvalds if (old_gov) { 23733a3e9e06SViresh Kumar policy->governor = old_gov; 2374a92604b4SRafael J. Wysocki if (cpufreq_init_governor(policy)) 23754bc384aeSViresh Kumar policy->governor = NULL; 23764bc384aeSViresh Kumar else 23770a300767SRafael J. Wysocki cpufreq_start_governor(policy); 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 23804bc384aeSViresh Kumar return ret; 23811da177e4SLinus Torvalds } 23821da177e4SLinus Torvalds 23831da177e4SLinus Torvalds /** 2384a0dbb819SRafael J. Wysocki * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. 2385a0dbb819SRafael J. Wysocki * @cpu: CPU to re-evaluate the policy for. 23861da177e4SLinus Torvalds * 2387a0dbb819SRafael J. Wysocki * Update the current frequency for the cpufreq policy of @cpu and use 2388a0dbb819SRafael J. Wysocki * cpufreq_set_policy() to re-apply the min and max limits saved in the 2389a0dbb819SRafael J. Wysocki * user_policy sub-structure of that policy, which triggers the evaluation 2390a0dbb819SRafael J. Wysocki * of policy notifiers and the cpufreq driver's ->verify() callback for the 2391a0dbb819SRafael J. Wysocki * policy in question, among other things. 23921da177e4SLinus Torvalds */ 239330248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu) 23941da177e4SLinus Torvalds { 2395540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); 23963a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 23971da177e4SLinus Torvalds 2398fefa8ff8SAaron Plattner if (!policy) 239930248fefSRafael J. Wysocki return; 24001da177e4SLinus Torvalds 2401bb176f7dSViresh Kumar /* 2402bb176f7dSViresh Kumar * BIOS might change freq behind our back 2403bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2404bb176f7dSViresh Kumar */ 2405*5ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target() && 2406348a2ec5SRafael J. Wysocki (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy)))) 2407742c87bfSRafael J. Wysocki goto unlock; 240830248fefSRafael J. Wysocki 2409348a2ec5SRafael J. Wysocki pr_debug("updating policy for CPU %u\n", cpu); 2410348a2ec5SRafael J. Wysocki memcpy(&new_policy, policy, sizeof(*policy)); 2411348a2ec5SRafael J. Wysocki new_policy.min = policy->user_policy.min; 2412348a2ec5SRafael J. Wysocki new_policy.max = policy->user_policy.max; 24130961dd0dSThomas Renninger 241430248fefSRafael J. Wysocki cpufreq_set_policy(policy, &new_policy); 24151da177e4SLinus Torvalds 2416fefa8ff8SAaron Plattner unlock: 2417540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 24201da177e4SLinus Torvalds 24215a25e3f7SRafael J. Wysocki /** 24225a25e3f7SRafael J. Wysocki * cpufreq_update_limits - Update policy limits for a given CPU. 24235a25e3f7SRafael J. Wysocki * @cpu: CPU to update the policy limits for. 24245a25e3f7SRafael J. Wysocki * 24255a25e3f7SRafael J. Wysocki * Invoke the driver's ->update_limits callback if present or call 24265a25e3f7SRafael J. Wysocki * cpufreq_update_policy() for @cpu. 24275a25e3f7SRafael J. Wysocki */ 24285a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu) 24295a25e3f7SRafael J. Wysocki { 24305a25e3f7SRafael J. Wysocki if (cpufreq_driver->update_limits) 24315a25e3f7SRafael J. Wysocki cpufreq_driver->update_limits(cpu); 24325a25e3f7SRafael J. Wysocki else 24335a25e3f7SRafael J. Wysocki cpufreq_update_policy(cpu); 24345a25e3f7SRafael J. Wysocki } 24355a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits); 24365a25e3f7SRafael J. Wysocki 24371da177e4SLinus Torvalds /********************************************************************* 24386f19efc0SLukasz Majewski * BOOST * 24396f19efc0SLukasz Majewski *********************************************************************/ 24406f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 24416f19efc0SLukasz Majewski { 24426f19efc0SLukasz Majewski struct cpufreq_policy *policy; 24436f19efc0SLukasz Majewski int ret = -EINVAL; 24446f19efc0SLukasz Majewski 2445f963735aSViresh Kumar for_each_active_policy(policy) { 2446f8bfc116SViresh Kumar if (!policy->freq_table) 2447f8bfc116SViresh Kumar continue; 2448f8bfc116SViresh Kumar 24496f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 2450f8bfc116SViresh Kumar policy->freq_table); 24516f19efc0SLukasz Majewski if (ret) { 24526f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 24536f19efc0SLukasz Majewski __func__); 24546f19efc0SLukasz Majewski break; 24556f19efc0SLukasz Majewski } 245649f18560SViresh Kumar 245749f18560SViresh Kumar down_write(&policy->rwsem); 24586f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 2459a92604b4SRafael J. Wysocki cpufreq_governor_limits(policy); 246049f18560SViresh Kumar up_write(&policy->rwsem); 24616f19efc0SLukasz Majewski } 24626f19efc0SLukasz Majewski 24636f19efc0SLukasz Majewski return ret; 24646f19efc0SLukasz Majewski } 24656f19efc0SLukasz Majewski 24666f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 24676f19efc0SLukasz Majewski { 24686f19efc0SLukasz Majewski unsigned long flags; 24696f19efc0SLukasz Majewski int ret = 0; 24706f19efc0SLukasz Majewski 24716f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 24726f19efc0SLukasz Majewski return 0; 24736f19efc0SLukasz Majewski 24746f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 24756f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 24766f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24776f19efc0SLukasz Majewski 24786f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 24796f19efc0SLukasz Majewski if (ret) { 24806f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 24816f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 24826f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24836f19efc0SLukasz Majewski 2484e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2485e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 24866f19efc0SLukasz Majewski } 24876f19efc0SLukasz Majewski 24886f19efc0SLukasz Majewski return ret; 24896f19efc0SLukasz Majewski } 24906f19efc0SLukasz Majewski 249141669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void) 24926f19efc0SLukasz Majewski { 249389f98d7eSYue Hu return cpufreq_driver->set_boost; 24946f19efc0SLukasz Majewski } 24956f19efc0SLukasz Majewski 249644139ed4SViresh Kumar static int create_boost_sysfs_file(void) 249744139ed4SViresh Kumar { 249844139ed4SViresh Kumar int ret; 249944139ed4SViresh Kumar 2500c82bd444SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 250144139ed4SViresh Kumar if (ret) 250244139ed4SViresh Kumar pr_err("%s: cannot register global BOOST sysfs file\n", 250344139ed4SViresh Kumar __func__); 250444139ed4SViresh Kumar 250544139ed4SViresh Kumar return ret; 250644139ed4SViresh Kumar } 250744139ed4SViresh Kumar 250844139ed4SViresh Kumar static void remove_boost_sysfs_file(void) 250944139ed4SViresh Kumar { 251044139ed4SViresh Kumar if (cpufreq_boost_supported()) 2511c82bd444SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 251244139ed4SViresh Kumar } 251344139ed4SViresh Kumar 251444139ed4SViresh Kumar int cpufreq_enable_boost_support(void) 251544139ed4SViresh Kumar { 251644139ed4SViresh Kumar if (!cpufreq_driver) 251744139ed4SViresh Kumar return -EINVAL; 251844139ed4SViresh Kumar 251944139ed4SViresh Kumar if (cpufreq_boost_supported()) 252044139ed4SViresh Kumar return 0; 252144139ed4SViresh Kumar 25227a6c79f2SRafael J. Wysocki cpufreq_driver->set_boost = cpufreq_boost_set_sw; 252344139ed4SViresh Kumar 252444139ed4SViresh Kumar /* This will get removed on driver unregister */ 252544139ed4SViresh Kumar return create_boost_sysfs_file(); 252644139ed4SViresh Kumar } 252744139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 252844139ed4SViresh Kumar 25296f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 25306f19efc0SLukasz Majewski { 25316f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 25326f19efc0SLukasz Majewski } 25336f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 25346f19efc0SLukasz Majewski 25356f19efc0SLukasz Majewski /********************************************************************* 25361da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 25371da177e4SLinus Torvalds *********************************************************************/ 253827622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online; 25391da177e4SLinus Torvalds 2540c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu) 2541c4a3fa26SChen Yu { 2542c4a3fa26SChen Yu cpufreq_online(cpu); 2543c4a3fa26SChen Yu 2544c4a3fa26SChen Yu return 0; 2545c4a3fa26SChen Yu } 2546c4a3fa26SChen Yu 2547c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu) 2548c4a3fa26SChen Yu { 2549c4a3fa26SChen Yu cpufreq_offline(cpu); 2550c4a3fa26SChen Yu 2551c4a3fa26SChen Yu return 0; 2552c4a3fa26SChen Yu } 2553c4a3fa26SChen Yu 25541da177e4SLinus Torvalds /** 25551da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 25561da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 25571da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 25581da177e4SLinus Torvalds * 25591da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 256063af4055SEric Biggers * returns zero on success, -EEXIST when another driver got here first 25611da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 25621da177e4SLinus Torvalds * 25631da177e4SLinus Torvalds */ 2564221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 25651da177e4SLinus Torvalds { 25661da177e4SLinus Torvalds unsigned long flags; 25671da177e4SLinus Torvalds int ret; 25681da177e4SLinus Torvalds 2569a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2570a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2571a7b422cdSKonrad Rzeszutek Wilk 25721da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 25739c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 25749832235fSRafael J. Wysocki driver_data->target) || 25759832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 25761c03a2d0SViresh Kumar driver_data->target)) || 2577a9a22b57SViresh Kumar (!driver_data->get_intermediate != !driver_data->target_intermediate) || 257891a12e91SViresh Kumar (!driver_data->online != !driver_data->offline)) 25791da177e4SLinus Torvalds return -EINVAL; 25801da177e4SLinus Torvalds 25812d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 25821da177e4SLinus Torvalds 2583fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2584a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 2585fdd320daSRafael J. Wysocki 25860d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 25871c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 25880d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2589fdd320daSRafael J. Wysocki ret = -EEXIST; 2590fdd320daSRafael J. Wysocki goto out; 25911da177e4SLinus Torvalds } 25921c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 25930d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 25941da177e4SLinus Torvalds 2595bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2596bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2597bc68b7dfSViresh Kumar 25987a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 259944139ed4SViresh Kumar ret = create_boost_sysfs_file(); 260044139ed4SViresh Kumar if (ret) 26016f19efc0SLukasz Majewski goto err_null_driver; 26027a6c79f2SRafael J. Wysocki } 26036f19efc0SLukasz Majewski 26048a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 26058f5bc2abSJiri Slaby if (ret) 26066f19efc0SLukasz Majewski goto err_boost_unreg; 26071da177e4SLinus Torvalds 2608ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2609ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 26101da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 26116c770036SDavid Arcari ret = -ENODEV; 2612ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2613e08f5f5bSGautham R Shenoy driver_data->name); 26148a25a2fdSKay Sievers goto err_if_unreg; 26151da177e4SLinus Torvalds } 26161da177e4SLinus Torvalds 2617a92551e4SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, 2618a92551e4SSebastian Andrzej Siewior "cpufreq:online", 2619c4a3fa26SChen Yu cpuhp_cpufreq_online, 2620c4a3fa26SChen Yu cpuhp_cpufreq_offline); 262127622b06SSebastian Andrzej Siewior if (ret < 0) 262227622b06SSebastian Andrzej Siewior goto err_if_unreg; 262327622b06SSebastian Andrzej Siewior hp_online = ret; 26245372e054SSebastian Andrzej Siewior ret = 0; 262527622b06SSebastian Andrzej Siewior 26262d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 26273834abb4SPankaj Gupta goto out; 2628fdd320daSRafael J. Wysocki 26298a25a2fdSKay Sievers err_if_unreg: 26308a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 26316f19efc0SLukasz Majewski err_boost_unreg: 263244139ed4SViresh Kumar remove_boost_sysfs_file(); 26338f5bc2abSJiri Slaby err_null_driver: 26340d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 26351c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 26360d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 26373834abb4SPankaj Gupta out: 2638a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 26393834abb4SPankaj Gupta return ret; 26401da177e4SLinus Torvalds } 26411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 26421da177e4SLinus Torvalds 26431da177e4SLinus Torvalds /** 26441da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 26451da177e4SLinus Torvalds * 26461da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 26471da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 26481da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 26491da177e4SLinus Torvalds * currently not initialised. 26501da177e4SLinus Torvalds */ 2651221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 26521da177e4SLinus Torvalds { 26531da177e4SLinus Torvalds unsigned long flags; 26541da177e4SLinus Torvalds 26551c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 26561da177e4SLinus Torvalds return -EINVAL; 26571da177e4SLinus Torvalds 26582d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 26591da177e4SLinus Torvalds 2660454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2661a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 26628a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 266344139ed4SViresh Kumar remove_boost_sysfs_file(); 2664a92551e4SSebastian Andrzej Siewior cpuhp_remove_state_nocalls_cpuslocked(hp_online); 26651da177e4SLinus Torvalds 26660d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 26676eed9404SViresh Kumar 26681c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 26696eed9404SViresh Kumar 26700d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2671a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 26721da177e4SLinus Torvalds 26731da177e4SLinus Torvalds return 0; 26741da177e4SLinus Torvalds } 26751da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 26765a01f2e8SVenkatesh Pallipadi 267790de2a4aSDoug Anderson /* 267890de2a4aSDoug Anderson * Stop cpufreq at shutdown to make sure it isn't holding any locks 267990de2a4aSDoug Anderson * or mutexes when secondary CPUs are halted. 268090de2a4aSDoug Anderson */ 268190de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = { 268290de2a4aSDoug Anderson .shutdown = cpufreq_suspend, 268390de2a4aSDoug Anderson }; 268490de2a4aSDoug Anderson 2685c82bd444SViresh Kumar struct kobject *cpufreq_global_kobject; 2686c82bd444SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 2687c82bd444SViresh Kumar 26885a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 26895a01f2e8SVenkatesh Pallipadi { 2690a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2691a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2692a7b422cdSKonrad Rzeszutek Wilk 26938eec1020SViresh Kumar cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); 26948aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 26958aa84ad8SThomas Renninger 269690de2a4aSDoug Anderson register_syscore_ops(&cpufreq_syscore_ops); 269790de2a4aSDoug Anderson 26985a01f2e8SVenkatesh Pallipadi return 0; 26995a01f2e8SVenkatesh Pallipadi } 2700d82f2692SLen Brown module_param(off, int, 0444); 27015a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2702