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 */ 36298015228SViresh Kumar if (policy->cur && policy->cur != freqs->old) { 363e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 364e4472cb3SDave Jones freqs->old, policy->cur); 365e4472cb3SDave Jones freqs->old = policy->cur; 3661da177e4SLinus Torvalds } 36720b5324dSViresh Kumar 368b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 369e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 37020b5324dSViresh Kumar 3711da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3721da177e4SLinus Torvalds break; 373e4472cb3SDave Jones 3741da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3751da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 37620b5324dSViresh Kumar pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new, 37720b5324dSViresh Kumar cpumask_pr_args(policy->cpus)); 37820b5324dSViresh Kumar 379df24014aSViresh Kumar for_each_cpu(cpu, policy->cpus) 380df24014aSViresh Kumar trace_cpu_frequency(freqs->new, cpu); 381df24014aSViresh Kumar 382b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 383e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 384bb176f7dSViresh Kumar 38520b5324dSViresh Kumar cpufreq_stats_record_transition(policy, freqs->new); 38620b5324dSViresh Kumar policy->cur = freqs->new; 38720b5324dSViresh Kumar } 388b43a7ffbSViresh Kumar } 3891da177e4SLinus Torvalds 390f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 391236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 392f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 393f7ba3b41SViresh Kumar { 394f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 395f7ba3b41SViresh Kumar if (!transition_failed) 396f7ba3b41SViresh Kumar return; 397f7ba3b41SViresh Kumar 398f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 399f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 400f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 401f7ba3b41SViresh Kumar } 402f7ba3b41SViresh Kumar 40312478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 40412478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 40512478cf0SSrivatsa S. Bhat { 406ca654dc3SSrivatsa S. Bhat 407ca654dc3SSrivatsa S. Bhat /* 408ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 409ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 410ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 411ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 412ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 413ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 414ca654dc3SSrivatsa S. Bhat */ 415ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 416ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 417ca654dc3SSrivatsa S. Bhat 41812478cf0SSrivatsa S. Bhat wait: 41912478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 42012478cf0SSrivatsa S. Bhat 42112478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 42212478cf0SSrivatsa S. Bhat 42312478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 42412478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 42512478cf0SSrivatsa S. Bhat goto wait; 42612478cf0SSrivatsa S. Bhat } 42712478cf0SSrivatsa S. Bhat 42812478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 429ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 43012478cf0SSrivatsa S. Bhat 43112478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 43212478cf0SSrivatsa S. Bhat 43312478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 43412478cf0SSrivatsa S. Bhat } 43512478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 43612478cf0SSrivatsa S. Bhat 43712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 43812478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 43912478cf0SSrivatsa S. Bhat { 4400e7ea2f3SIgor Stoppa if (WARN_ON(!policy->transition_ongoing)) 44112478cf0SSrivatsa S. Bhat return; 44212478cf0SSrivatsa S. Bhat 44312478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 44412478cf0SSrivatsa S. Bhat 44512478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 446ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 44712478cf0SSrivatsa S. Bhat 44812478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 44912478cf0SSrivatsa S. Bhat } 45012478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 45112478cf0SSrivatsa S. Bhat 452b7898fdaSRafael J. Wysocki /* 453b7898fdaSRafael J. Wysocki * Fast frequency switching status count. Positive means "enabled", negative 454b7898fdaSRafael J. Wysocki * means "disabled" and 0 means "not decided yet". 455b7898fdaSRafael J. Wysocki */ 456b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count; 457b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock); 458b7898fdaSRafael J. Wysocki 459b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void) 460b7898fdaSRafael J. Wysocki { 461b7898fdaSRafael J. Wysocki struct notifier_block *nb; 462b7898fdaSRafael J. Wysocki 463b7898fdaSRafael J. Wysocki pr_info("Registered transition notifiers:\n"); 464b7898fdaSRafael J. Wysocki 465b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_transition_notifier_list.mutex); 466b7898fdaSRafael J. Wysocki 467b7898fdaSRafael J. Wysocki for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next) 468d75f773cSSakari Ailus pr_info("%pS\n", nb->notifier_call); 469b7898fdaSRafael J. Wysocki 470b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_transition_notifier_list.mutex); 471b7898fdaSRafael J. Wysocki } 472b7898fdaSRafael J. Wysocki 473b7898fdaSRafael J. Wysocki /** 474b7898fdaSRafael J. Wysocki * cpufreq_enable_fast_switch - Enable fast frequency switching for policy. 475b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to enable fast frequency switching for. 476b7898fdaSRafael J. Wysocki * 477b7898fdaSRafael J. Wysocki * Try to enable fast frequency switching for @policy. 478b7898fdaSRafael J. Wysocki * 479b7898fdaSRafael J. Wysocki * The attempt will fail if there is at least one transition notifier registered 480b7898fdaSRafael J. Wysocki * at this point, as fast frequency switching is quite fundamentally at odds 481b7898fdaSRafael J. Wysocki * with transition notifiers. Thus if successful, it will make registration of 482b7898fdaSRafael J. Wysocki * transition notifiers fail going forward. 483b7898fdaSRafael J. Wysocki */ 484b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy) 485b7898fdaSRafael J. Wysocki { 486b7898fdaSRafael J. Wysocki lockdep_assert_held(&policy->rwsem); 487b7898fdaSRafael J. Wysocki 488b7898fdaSRafael J. Wysocki if (!policy->fast_switch_possible) 489b7898fdaSRafael J. Wysocki return; 490b7898fdaSRafael J. Wysocki 491b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 492b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count >= 0) { 493b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 494b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = true; 495b7898fdaSRafael J. Wysocki } else { 496b7898fdaSRafael J. Wysocki pr_warn("CPU%u: Fast frequency switching not enabled\n", 497b7898fdaSRafael J. Wysocki policy->cpu); 498b7898fdaSRafael J. Wysocki cpufreq_list_transition_notifiers(); 499b7898fdaSRafael J. Wysocki } 500b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 501b7898fdaSRafael J. Wysocki } 502b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch); 503b7898fdaSRafael J. Wysocki 5046c9d9c81SRafael J. Wysocki /** 5056c9d9c81SRafael J. Wysocki * cpufreq_disable_fast_switch - Disable fast frequency switching for policy. 5066c9d9c81SRafael J. Wysocki * @policy: cpufreq policy to disable fast frequency switching for. 5076c9d9c81SRafael J. Wysocki */ 5086c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy) 509b7898fdaSRafael J. Wysocki { 510b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 511b7898fdaSRafael J. Wysocki if (policy->fast_switch_enabled) { 512b7898fdaSRafael J. Wysocki policy->fast_switch_enabled = false; 513b7898fdaSRafael J. Wysocki if (!WARN_ON(cpufreq_fast_switch_count <= 0)) 514b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 515b7898fdaSRafael J. Wysocki } 516b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 517b7898fdaSRafael J. Wysocki } 5186c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch); 5191da177e4SLinus Torvalds 520e3c06236SSteve Muckle /** 521e3c06236SSteve Muckle * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported 522e3c06236SSteve Muckle * one. 523e3c06236SSteve Muckle * @target_freq: target frequency to resolve. 524e3c06236SSteve Muckle * 525e3c06236SSteve Muckle * The target to driver frequency mapping is cached in the policy. 526e3c06236SSteve Muckle * 527e3c06236SSteve Muckle * Return: Lowest driver-supported frequency greater than or equal to the 528e3c06236SSteve Muckle * given target_freq, subject to policy (min/max) and driver limitations. 529e3c06236SSteve Muckle */ 530e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy, 531e3c06236SSteve Muckle unsigned int target_freq) 532e3c06236SSteve Muckle { 533e3c06236SSteve Muckle target_freq = clamp_val(target_freq, policy->min, policy->max); 534e3c06236SSteve Muckle policy->cached_target_freq = target_freq; 535abe8bd02SViresh Kumar 536abe8bd02SViresh Kumar if (cpufreq_driver->target_index) { 537abe8bd02SViresh Kumar int idx; 538abe8bd02SViresh Kumar 539abe8bd02SViresh Kumar idx = cpufreq_frequency_table_target(policy, target_freq, 540abe8bd02SViresh Kumar CPUFREQ_RELATION_L); 541abe8bd02SViresh Kumar policy->cached_resolved_idx = idx; 542abe8bd02SViresh Kumar return policy->freq_table[idx].frequency; 543abe8bd02SViresh Kumar } 544abe8bd02SViresh Kumar 545e3c06236SSteve Muckle if (cpufreq_driver->resolve_freq) 546e3c06236SSteve Muckle return cpufreq_driver->resolve_freq(policy, target_freq); 547abe8bd02SViresh Kumar 548abe8bd02SViresh Kumar return target_freq; 549e3c06236SSteve Muckle } 550ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq); 551e3c06236SSteve Muckle 552aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy) 553aa7519afSViresh Kumar { 554aa7519afSViresh Kumar unsigned int latency; 555aa7519afSViresh Kumar 556aa7519afSViresh Kumar if (policy->transition_delay_us) 557aa7519afSViresh Kumar return policy->transition_delay_us; 558aa7519afSViresh Kumar 559aa7519afSViresh Kumar latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC; 560e948bc8fSViresh Kumar if (latency) { 561e948bc8fSViresh Kumar /* 562e948bc8fSViresh Kumar * For platforms that can change the frequency very fast (< 10 563e948bc8fSViresh Kumar * us), the above formula gives a decent transition delay. But 564e948bc8fSViresh Kumar * for platforms where transition_latency is in milliseconds, it 565e948bc8fSViresh Kumar * ends up giving unrealistic values. 566e948bc8fSViresh Kumar * 567e948bc8fSViresh Kumar * Cap the default transition delay to 10 ms, which seems to be 568e948bc8fSViresh Kumar * a reasonable amount of time after which we should reevaluate 569e948bc8fSViresh Kumar * the frequency. 570e948bc8fSViresh Kumar */ 571e948bc8fSViresh Kumar return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000); 572e948bc8fSViresh Kumar } 573aa7519afSViresh Kumar 574aa7519afSViresh Kumar return LATENCY_MULTIPLIER; 575aa7519afSViresh Kumar } 576aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us); 577aa7519afSViresh Kumar 5781da177e4SLinus Torvalds /********************************************************************* 5791da177e4SLinus Torvalds * SYSFS INTERFACE * 5801da177e4SLinus Torvalds *********************************************************************/ 5818a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 582625c85a6SViresh Kumar struct kobj_attribute *attr, char *buf) 5836f19efc0SLukasz Majewski { 5846f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 5856f19efc0SLukasz Majewski } 5866f19efc0SLukasz Majewski 587625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr, 5886f19efc0SLukasz Majewski const char *buf, size_t count) 5896f19efc0SLukasz Majewski { 5906f19efc0SLukasz Majewski int ret, enable; 5916f19efc0SLukasz Majewski 5926f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 5936f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 5946f19efc0SLukasz Majewski return -EINVAL; 5956f19efc0SLukasz Majewski 5966f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 597e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 598e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 5996f19efc0SLukasz Majewski return -EINVAL; 6006f19efc0SLukasz Majewski } 6016f19efc0SLukasz Majewski 602e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 603e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 6046f19efc0SLukasz Majewski 6056f19efc0SLukasz Majewski return count; 6066f19efc0SLukasz Majewski } 6076f19efc0SLukasz Majewski define_one_global_rw(boost); 6081da177e4SLinus Torvalds 60942f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 6103bcb09a3SJeremy Fitzhardinge { 6113bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 6123bcb09a3SJeremy Fitzhardinge 613f7b27061SViresh Kumar for_each_governor(t) 6147c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 6153bcb09a3SJeremy Fitzhardinge return t; 6163bcb09a3SJeremy Fitzhardinge 6173bcb09a3SJeremy Fitzhardinge return NULL; 6183bcb09a3SJeremy Fitzhardinge } 6193bcb09a3SJeremy Fitzhardinge 620ab05d97aSYue Hu static int cpufreq_parse_policy(char *str_governor, 621ae0ff89fSRafael J. Wysocki struct cpufreq_policy *policy) 6221da177e4SLinus Torvalds { 6237c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 624ae0ff89fSRafael J. Wysocki policy->policy = CPUFREQ_POLICY_PERFORMANCE; 625045149e6SRafael J. Wysocki return 0; 626045149e6SRafael J. Wysocki } 627045149e6SRafael J. Wysocki if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) { 628ae0ff89fSRafael J. Wysocki policy->policy = CPUFREQ_POLICY_POWERSAVE; 629045149e6SRafael J. Wysocki return 0; 6301da177e4SLinus Torvalds } 631ab05d97aSYue Hu return -EINVAL; 632ab05d97aSYue Hu } 633ab05d97aSYue Hu 634ab05d97aSYue Hu /** 6355ddc6d4eSViresh Kumar * cpufreq_parse_governor - parse a governor string only for has_target() 636ab05d97aSYue Hu */ 637ab05d97aSYue Hu static int cpufreq_parse_governor(char *str_governor, 638ab05d97aSYue Hu struct cpufreq_policy *policy) 639ab05d97aSYue Hu { 6401da177e4SLinus Torvalds struct cpufreq_governor *t; 6413bcb09a3SJeremy Fitzhardinge 6423fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 6433bcb09a3SJeremy Fitzhardinge 64442f91fa1SViresh Kumar t = find_governor(str_governor); 645045149e6SRafael J. Wysocki if (!t) { 646ea714970SJeremy Fitzhardinge int ret; 647ea714970SJeremy Fitzhardinge 648ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 649045149e6SRafael J. Wysocki 6501a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 651045149e6SRafael J. Wysocki if (ret) 652045149e6SRafael J. Wysocki return -EINVAL; 653045149e6SRafael J. Wysocki 654ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 655ea714970SJeremy Fitzhardinge 65642f91fa1SViresh Kumar t = find_governor(str_governor); 657ea714970SJeremy Fitzhardinge } 658a8b149d3SRafael J. Wysocki if (t && !try_module_get(t->owner)) 659a8b149d3SRafael J. Wysocki t = NULL; 660ea714970SJeremy Fitzhardinge 661045149e6SRafael J. Wysocki mutex_unlock(&cpufreq_governor_mutex); 662045149e6SRafael J. Wysocki 663045149e6SRafael J. Wysocki if (t) { 664ae0ff89fSRafael J. Wysocki policy->governor = t; 665045149e6SRafael J. Wysocki return 0; 666045149e6SRafael J. Wysocki } 6673bcb09a3SJeremy Fitzhardinge 668045149e6SRafael J. Wysocki return -EINVAL; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds 6711da177e4SLinus Torvalds /** 672e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 673e08f5f5bSGautham R Shenoy * print out cpufreq information 6741da177e4SLinus Torvalds * 6751da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 6761da177e4SLinus Torvalds * "unsigned int". 6771da177e4SLinus Torvalds */ 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds #define show_one(file_name, object) \ 6801da177e4SLinus Torvalds static ssize_t show_##file_name \ 6811da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 6821da177e4SLinus Torvalds { \ 6831da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds 6861da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 6871da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 688ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 6891da177e4SLinus Torvalds show_one(scaling_min_freq, min); 6901da177e4SLinus Torvalds show_one(scaling_max_freq, max); 691c034b02eSDirk Brandewie 692f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu) 693f8475cefSLen Brown { 694f8475cefSLen Brown return 0; 695f8475cefSLen Brown } 696f8475cefSLen Brown 69709347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 698c034b02eSDirk Brandewie { 699c034b02eSDirk Brandewie ssize_t ret; 700f8475cefSLen Brown unsigned int freq; 701c034b02eSDirk Brandewie 702f8475cefSLen Brown freq = arch_freq_get_on_cpu(policy->cpu); 703f8475cefSLen Brown if (freq) 704f8475cefSLen Brown ret = sprintf(buf, "%u\n", freq); 705f8475cefSLen Brown else if (cpufreq_driver && cpufreq_driver->setpolicy && 706f8475cefSLen Brown cpufreq_driver->get) 707c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 708c034b02eSDirk Brandewie else 709c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 710c034b02eSDirk Brandewie return ret; 711c034b02eSDirk Brandewie } 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds /** 7141da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 7151da177e4SLinus Torvalds */ 7161da177e4SLinus Torvalds #define store_one(file_name, object) \ 7171da177e4SLinus Torvalds static ssize_t store_##file_name \ 7181da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 7191da177e4SLinus Torvalds { \ 720619c144cSVince Hsu int ret, temp; \ 7211da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 7221da177e4SLinus Torvalds \ 7238fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); \ 724c7d1f119STao Wang new_policy.min = policy->user_policy.min; \ 725c7d1f119STao Wang new_policy.max = policy->user_policy.max; \ 7261da177e4SLinus Torvalds \ 7271da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 7281da177e4SLinus Torvalds if (ret != 1) \ 7291da177e4SLinus Torvalds return -EINVAL; \ 7301da177e4SLinus Torvalds \ 731619c144cSVince Hsu temp = new_policy.object; \ 732037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 733619c144cSVince Hsu if (!ret) \ 734619c144cSVince Hsu policy->user_policy.object = temp; \ 7351da177e4SLinus Torvalds \ 7361da177e4SLinus Torvalds return ret ? ret : count; \ 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds store_one(scaling_min_freq, min); 7401da177e4SLinus Torvalds store_one(scaling_max_freq, max); 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /** 7431da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 7441da177e4SLinus Torvalds */ 745e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 746e08f5f5bSGautham R Shenoy char *buf) 7471da177e4SLinus Torvalds { 748d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 7499b4f603eSRafael J. Wysocki 7509b4f603eSRafael J. Wysocki if (cur_freq) 7511da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 7529b4f603eSRafael J. Wysocki 7539b4f603eSRafael J. Wysocki return sprintf(buf, "<unknown>\n"); 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds /** 7571da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 7581da177e4SLinus Torvalds */ 759905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 7601da177e4SLinus Torvalds { 7611da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 7621da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 7631da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 7641da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 7651da177e4SLinus Torvalds else if (policy->governor) 7664b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 76729464f28SDave Jones policy->governor->name); 7681da177e4SLinus Torvalds return -EINVAL; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds /** 7721da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 7731da177e4SLinus Torvalds */ 7741da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 7751da177e4SLinus Torvalds const char *buf, size_t count) 7761da177e4SLinus Torvalds { 7775136fa56SSrivatsa S. Bhat int ret; 7781da177e4SLinus Torvalds char str_governor[16]; 7791da177e4SLinus Torvalds struct cpufreq_policy new_policy; 7801da177e4SLinus Torvalds 7818fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 7821da177e4SLinus Torvalds 7831da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 7841da177e4SLinus Torvalds if (ret != 1) 7851da177e4SLinus Torvalds return -EINVAL; 7861da177e4SLinus Torvalds 787ab05d97aSYue Hu if (cpufreq_driver->setpolicy) { 788ab05d97aSYue Hu if (cpufreq_parse_policy(str_governor, &new_policy)) 789ab05d97aSYue Hu return -EINVAL; 790ab05d97aSYue Hu } else { 791ae0ff89fSRafael J. Wysocki if (cpufreq_parse_governor(str_governor, &new_policy)) 7921da177e4SLinus Torvalds return -EINVAL; 793ab05d97aSYue Hu } 7941da177e4SLinus Torvalds 795037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 796a8b149d3SRafael J. Wysocki 797a8b149d3SRafael J. Wysocki if (new_policy.governor) 798a8b149d3SRafael J. Wysocki module_put(new_policy.governor->owner); 799a8b149d3SRafael J. Wysocki 80088dc4384SViresh Kumar return ret ? ret : count; 8011da177e4SLinus Torvalds } 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /** 8041da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 8051da177e4SLinus Torvalds */ 8061da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 8071da177e4SLinus Torvalds { 8081c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 8091da177e4SLinus Torvalds } 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /** 8121da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 8131da177e4SLinus Torvalds */ 8141da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 8151da177e4SLinus Torvalds char *buf) 8161da177e4SLinus Torvalds { 8171da177e4SLinus Torvalds ssize_t i = 0; 8181da177e4SLinus Torvalds struct cpufreq_governor *t; 8191da177e4SLinus Torvalds 8209c0ebcf7SViresh Kumar if (!has_target()) { 8211da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 8221da177e4SLinus Torvalds goto out; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds 825f7b27061SViresh Kumar for_each_governor(t) { 82629464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 82729464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 8281da177e4SLinus Torvalds goto out; 8294b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds out: 8321da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8331da177e4SLinus Torvalds return i; 8341da177e4SLinus Torvalds } 835e8628dd0SDarrick J. Wong 836f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 8371da177e4SLinus Torvalds { 8381da177e4SLinus Torvalds ssize_t i = 0; 8391da177e4SLinus Torvalds unsigned int cpu; 8401da177e4SLinus Torvalds 841835481d9SRusty Russell for_each_cpu(cpu, mask) { 8421da177e4SLinus Torvalds if (i) 8431da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 8441da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 8451da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 8461da177e4SLinus Torvalds break; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 8491da177e4SLinus Torvalds return i; 8501da177e4SLinus Torvalds } 851f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 8521da177e4SLinus Torvalds 853e8628dd0SDarrick J. Wong /** 854e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 855e8628dd0SDarrick J. Wong * hw coordination is in use 856e8628dd0SDarrick J. Wong */ 857e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 858e8628dd0SDarrick J. Wong { 859f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 860e8628dd0SDarrick J. Wong } 861e8628dd0SDarrick J. Wong 862e8628dd0SDarrick J. Wong /** 863e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 864e8628dd0SDarrick J. Wong */ 865e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 866e8628dd0SDarrick J. Wong { 867f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 868e8628dd0SDarrick J. Wong } 869e8628dd0SDarrick J. Wong 8709e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 8719e76988eSVenki Pallipadi const char *buf, size_t count) 8729e76988eSVenki Pallipadi { 8739e76988eSVenki Pallipadi unsigned int freq = 0; 8749e76988eSVenki Pallipadi unsigned int ret; 8759e76988eSVenki Pallipadi 876879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 8779e76988eSVenki Pallipadi return -EINVAL; 8789e76988eSVenki Pallipadi 8799e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 8809e76988eSVenki Pallipadi if (ret != 1) 8819e76988eSVenki Pallipadi return -EINVAL; 8829e76988eSVenki Pallipadi 8839e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 8849e76988eSVenki Pallipadi 8859e76988eSVenki Pallipadi return count; 8869e76988eSVenki Pallipadi } 8879e76988eSVenki Pallipadi 8889e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 8899e76988eSVenki Pallipadi { 890879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 8919e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 8929e76988eSVenki Pallipadi 8939e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 8949e76988eSVenki Pallipadi } 8951da177e4SLinus Torvalds 896e2f74f35SThomas Renninger /** 8978bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 898e2f74f35SThomas Renninger */ 899e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 900e2f74f35SThomas Renninger { 901e2f74f35SThomas Renninger unsigned int limit; 902e2f74f35SThomas Renninger int ret; 9031c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 904e2f74f35SThomas Renninger if (!ret) 905e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 906e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 907e2f74f35SThomas Renninger } 908e2f74f35SThomas Renninger 9096dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 9106dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 9116dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 9126dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 9136dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 9146dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 9156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 9166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 9176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 9186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 9196dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 9206dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 9216dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 9226dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 9251da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 9261da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 927ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 9281da177e4SLinus Torvalds &scaling_min_freq.attr, 9291da177e4SLinus Torvalds &scaling_max_freq.attr, 9301da177e4SLinus Torvalds &affected_cpus.attr, 931e8628dd0SDarrick J. Wong &related_cpus.attr, 9321da177e4SLinus Torvalds &scaling_governor.attr, 9331da177e4SLinus Torvalds &scaling_driver.attr, 9341da177e4SLinus Torvalds &scaling_available_governors.attr, 9359e76988eSVenki Pallipadi &scaling_setspeed.attr, 9361da177e4SLinus Torvalds NULL 9371da177e4SLinus Torvalds }; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 9401da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 9431da177e4SLinus Torvalds { 9441da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9451da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 9461b750e3bSViresh Kumar ssize_t ret; 9476eed9404SViresh Kumar 948ad7722daSviresh kumar down_read(&policy->rwsem); 949e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 950ad7722daSviresh kumar up_read(&policy->rwsem); 9511b750e3bSViresh Kumar 9521da177e4SLinus Torvalds return ret; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds 9551da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 9561da177e4SLinus Torvalds const char *buf, size_t count) 9571da177e4SLinus Torvalds { 9581da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9591da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 960a07530b4SDave Jones ssize_t ret = -EINVAL; 9616eed9404SViresh Kumar 9629b3d9bb3SWaiman Long /* 9639b3d9bb3SWaiman Long * cpus_read_trylock() is used here to work around a circular lock 9649b3d9bb3SWaiman Long * dependency problem with respect to the cpufreq_register_driver(). 9659b3d9bb3SWaiman Long */ 9669b3d9bb3SWaiman Long if (!cpus_read_trylock()) 9679b3d9bb3SWaiman Long return -EBUSY; 9684f750c93SSrivatsa S. Bhat 9696541aef0SRafael J. Wysocki if (cpu_online(policy->cpu)) { 970ad7722daSviresh kumar down_write(&policy->rwsem); 971e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 972ad7722daSviresh kumar up_write(&policy->rwsem); 9736541aef0SRafael J. Wysocki } 9746541aef0SRafael J. Wysocki 975a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 9764f750c93SSrivatsa S. Bhat 9771da177e4SLinus Torvalds return ret; 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 9811da177e4SLinus Torvalds { 9821da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9832d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 9841da177e4SLinus Torvalds complete(&policy->kobj_unregister); 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 98752cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 9881da177e4SLinus Torvalds .show = show, 9891da177e4SLinus Torvalds .store = store, 9901da177e4SLinus Torvalds }; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 9931da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 9941da177e4SLinus Torvalds .default_attrs = default_attrs, 9951da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 9961da177e4SLinus Torvalds }; 9971da177e4SLinus Torvalds 9982f0ba790SRafael J. Wysocki static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu) 99987549141SViresh Kumar { 10002f0ba790SRafael J. Wysocki struct device *dev = get_cpu_device(cpu); 10012f0ba790SRafael J. Wysocki 10022f0ba790SRafael J. Wysocki if (!dev) 10032f0ba790SRafael J. Wysocki return; 10042f0ba790SRafael J. Wysocki 10052f0ba790SRafael J. Wysocki if (cpumask_test_and_set_cpu(cpu, policy->real_cpus)) 10062f0ba790SRafael J. Wysocki return; 10072f0ba790SRafael J. Wysocki 100826619804SViresh Kumar dev_dbg(dev, "%s: Adding symlink\n", __func__); 10092f0ba790SRafael J. Wysocki if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq")) 10102f0ba790SRafael J. Wysocki dev_err(dev, "cpufreq symlink creation failed\n"); 101187549141SViresh Kumar } 101287549141SViresh Kumar 101326619804SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, 101426619804SViresh Kumar struct device *dev) 101587549141SViresh Kumar { 101626619804SViresh Kumar dev_dbg(dev, "%s: Removing symlink\n", __func__); 101726619804SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 101887549141SViresh Kumar } 101987549141SViresh Kumar 1020d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 1021909a694eSDave Jones { 1022909a694eSDave Jones struct freq_attr **drv_attr; 1023909a694eSDave Jones int ret = 0; 1024909a694eSDave Jones 1025909a694eSDave Jones /* set up files for this cpu device */ 10261c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 1027f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 1028909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 1029909a694eSDave Jones if (ret) 10306d4e81edSTomeu Vizoso return ret; 1031909a694eSDave Jones drv_attr++; 1032909a694eSDave Jones } 10331c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 1034909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 1035909a694eSDave Jones if (ret) 10366d4e81edSTomeu Vizoso return ret; 1037909a694eSDave Jones } 1038c034b02eSDirk Brandewie 1039909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 1040909a694eSDave Jones if (ret) 10416d4e81edSTomeu Vizoso return ret; 1042c034b02eSDirk Brandewie 10431c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 1044e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 1045e2f74f35SThomas Renninger if (ret) 10466d4e81edSTomeu Vizoso return ret; 1047e2f74f35SThomas Renninger } 1048909a694eSDave Jones 104926619804SViresh Kumar return 0; 1050e18f1682SSrivatsa S. Bhat } 1051e18f1682SSrivatsa S. Bhat 1052de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_default_governor(void) 1053de1df26bSRafael J. Wysocki { 1054de1df26bSRafael J. Wysocki return NULL; 1055de1df26bSRafael J. Wysocki } 1056de1df26bSRafael J. Wysocki 10577f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy) 1058e18f1682SSrivatsa S. Bhat { 1059ab05d97aSYue Hu struct cpufreq_governor *gov = NULL, *def_gov = NULL; 1060e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 1061e18f1682SSrivatsa S. Bhat 1062d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 1063a27a9ab7SJason Baron 1064ab05d97aSYue Hu def_gov = cpufreq_default_governor(); 1065ab05d97aSYue Hu 1066ab05d97aSYue Hu if (has_target()) { 1067ab05d97aSYue Hu /* 1068ab05d97aSYue Hu * Update governor of new_policy to the governor used before 1069ab05d97aSYue Hu * hotplug 1070ab05d97aSYue Hu */ 10714573237bSViresh Kumar gov = find_governor(policy->last_governor); 1072de1df26bSRafael J. Wysocki if (gov) { 10736e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 10746e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 1075de1df26bSRafael J. Wysocki } else { 1076ab05d97aSYue Hu if (!def_gov) 1077de1df26bSRafael J. Wysocki return -ENODATA; 1078ab05d97aSYue Hu gov = def_gov; 1079de1df26bSRafael J. Wysocki } 10806e2c89d1Sviresh kumar new_policy.governor = gov; 1081ab05d97aSYue Hu } else { 108269030dd1SSrinivas Pandruvada /* Use the default policy if there is no last_policy. */ 1083ab05d97aSYue Hu if (policy->last_policy) { 108469030dd1SSrinivas Pandruvada new_policy.policy = policy->last_policy; 1085ab05d97aSYue Hu } else { 1086ab05d97aSYue Hu if (!def_gov) 1087ab05d97aSYue Hu return -ENODATA; 1088ab05d97aSYue Hu cpufreq_parse_policy(def_gov->name, &new_policy); 108969030dd1SSrinivas Pandruvada } 1090ab05d97aSYue Hu } 1091ab05d97aSYue Hu 10927f0fa40fSViresh Kumar return cpufreq_set_policy(policy, &new_policy); 1093909a694eSDave Jones } 1094909a694eSDave Jones 1095d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1096fcf80582SViresh Kumar { 10979c0ebcf7SViresh Kumar int ret = 0; 1098fcf80582SViresh Kumar 1099bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 1100bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 1101bb29ae15SViresh Kumar return 0; 1102bb29ae15SViresh Kumar 110349f18560SViresh Kumar down_write(&policy->rwsem); 110445482c70SRafael J. Wysocki if (has_target()) 110545482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 1106fcf80582SViresh Kumar 1107fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 11082eaa3e2dSViresh Kumar 11099c0ebcf7SViresh Kumar if (has_target()) { 11100a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 111149f18560SViresh Kumar if (ret) 11123de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 1113820c6ca2SViresh Kumar } 111449f18560SViresh Kumar up_write(&policy->rwsem); 1115fcf80582SViresh Kumar return ret; 1116fcf80582SViresh Kumar } 1117fcf80582SViresh Kumar 111811eb69b9SViresh Kumar static void handle_update(struct work_struct *work) 111911eb69b9SViresh Kumar { 112011eb69b9SViresh Kumar struct cpufreq_policy *policy = 112111eb69b9SViresh Kumar container_of(work, struct cpufreq_policy, update); 112211eb69b9SViresh Kumar unsigned int cpu = policy->cpu; 112311eb69b9SViresh Kumar pr_debug("handle_update for cpu %u called\n", cpu); 112411eb69b9SViresh Kumar cpufreq_update_policy(cpu); 1125fcf80582SViresh Kumar } 11261da177e4SLinus Torvalds 1127a34e63b1SRafael J. Wysocki static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 11288414809cSSrivatsa S. Bhat { 1129e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1130edd4a893SViresh Kumar int ret; 1131e9698cc5SSrivatsa S. Bhat 1132e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1133e9698cc5SSrivatsa S. Bhat if (!policy) 1134e9698cc5SSrivatsa S. Bhat return NULL; 1135e9698cc5SSrivatsa S. Bhat 1136e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1137e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1138e9698cc5SSrivatsa S. Bhat 1139e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1140e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1141e9698cc5SSrivatsa S. Bhat 1142559ed407SRafael J. Wysocki if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 1143559ed407SRafael J. Wysocki goto err_free_rcpumask; 1144559ed407SRafael J. Wysocki 1145edd4a893SViresh Kumar ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 1146edd4a893SViresh Kumar cpufreq_global_kobject, "policy%u", cpu); 1147edd4a893SViresh Kumar if (ret) { 1148edd4a893SViresh Kumar pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); 11492acb9bdaSRafael J. Wysocki /* 11502acb9bdaSRafael J. Wysocki * The entire policy object will be freed below, but the extra 11512acb9bdaSRafael J. Wysocki * memory allocated for the kobject name needs to be freed by 11522acb9bdaSRafael J. Wysocki * releasing the kobject. 11532acb9bdaSRafael J. Wysocki */ 11544ebe36c9SViresh Kumar kobject_put(&policy->kobj); 1155edd4a893SViresh Kumar goto err_free_real_cpus; 1156edd4a893SViresh Kumar } 1157edd4a893SViresh Kumar 1158c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1159ad7722daSviresh kumar init_rwsem(&policy->rwsem); 116012478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 116112478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1162818c5712SViresh Kumar init_completion(&policy->kobj_unregister); 1163818c5712SViresh Kumar INIT_WORK(&policy->update, handle_update); 1164ad7722daSviresh kumar 1165a34e63b1SRafael J. Wysocki policy->cpu = cpu; 1166e9698cc5SSrivatsa S. Bhat return policy; 1167e9698cc5SSrivatsa S. Bhat 1168edd4a893SViresh Kumar err_free_real_cpus: 1169edd4a893SViresh Kumar free_cpumask_var(policy->real_cpus); 11702fc3384dSViresh Kumar err_free_rcpumask: 11712fc3384dSViresh Kumar free_cpumask_var(policy->related_cpus); 1172e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1173e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1174e9698cc5SSrivatsa S. Bhat err_free_policy: 1175e9698cc5SSrivatsa S. Bhat kfree(policy); 1176e9698cc5SSrivatsa S. Bhat 1177e9698cc5SSrivatsa S. Bhat return NULL; 1178e9698cc5SSrivatsa S. Bhat } 1179e9698cc5SSrivatsa S. Bhat 1180f9f41e3eSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 118142f921a6SViresh Kumar { 118242f921a6SViresh Kumar struct kobject *kobj; 118342f921a6SViresh Kumar struct completion *cmp; 118442f921a6SViresh Kumar 118587549141SViresh Kumar down_write(&policy->rwsem); 11861aefc75bSRafael J. Wysocki cpufreq_stats_free_table(policy); 118742f921a6SViresh Kumar kobj = &policy->kobj; 118842f921a6SViresh Kumar cmp = &policy->kobj_unregister; 118987549141SViresh Kumar up_write(&policy->rwsem); 119042f921a6SViresh Kumar kobject_put(kobj); 119142f921a6SViresh Kumar 119242f921a6SViresh Kumar /* 119342f921a6SViresh Kumar * We need to make sure that the underlying kobj is 119442f921a6SViresh Kumar * actually not referenced anymore by anybody before we 119542f921a6SViresh Kumar * proceed with unloading. 119642f921a6SViresh Kumar */ 119742f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 119842f921a6SViresh Kumar wait_for_completion(cmp); 119942f921a6SViresh Kumar pr_debug("wait complete\n"); 120042f921a6SViresh Kumar } 120142f921a6SViresh Kumar 1202f9f41e3eSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy) 1203e9698cc5SSrivatsa S. Bhat { 1204988bed09SViresh Kumar unsigned long flags; 1205988bed09SViresh Kumar int cpu; 1206988bed09SViresh Kumar 1207988bed09SViresh Kumar /* Remove policy from list */ 1208988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1209988bed09SViresh Kumar list_del(&policy->policy_list); 1210988bed09SViresh Kumar 1211988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1212988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1213988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1214988bed09SViresh Kumar 1215f9f41e3eSViresh Kumar cpufreq_policy_put_kobj(policy); 1216559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1217e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1218e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1219e9698cc5SSrivatsa S. Bhat kfree(policy); 1220e9698cc5SSrivatsa S. Bhat } 1221e9698cc5SSrivatsa S. Bhat 12220b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 12231da177e4SLinus Torvalds { 12247f0c020aSViresh Kumar struct cpufreq_policy *policy; 1225194d99c7SRafael J. Wysocki bool new_policy; 12261da177e4SLinus Torvalds unsigned long flags; 12270b275352SRafael J. Wysocki unsigned int j; 12280b275352SRafael J. Wysocki int ret; 1229c32b6b8eSAshok Raj 12300b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 12316eed9404SViresh Kumar 1232bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 12339104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 123411ce707eSRafael J. Wysocki if (policy) { 12359104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 123611ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1237d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 12381da177e4SLinus Torvalds 123911ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1240194d99c7SRafael J. Wysocki new_policy = false; 124111ce707eSRafael J. Wysocki down_write(&policy->rwsem); 124211ce707eSRafael J. Wysocki policy->cpu = cpu; 124311ce707eSRafael J. Wysocki policy->governor = NULL; 124411ce707eSRafael J. Wysocki up_write(&policy->rwsem); 124511ce707eSRafael J. Wysocki } else { 1246194d99c7SRafael J. Wysocki new_policy = true; 1247a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1248059019a3SDave Jones if (!policy) 1249d4d854d6SRafael J. Wysocki return -ENOMEM; 125072368d12SRafael J. Wysocki } 12510d66b91eSSrivatsa S. Bhat 125291a12e91SViresh Kumar if (!new_policy && cpufreq_driver->online) { 125391a12e91SViresh Kumar ret = cpufreq_driver->online(policy); 125491a12e91SViresh Kumar if (ret) { 125591a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 125691a12e91SViresh Kumar __LINE__); 125791a12e91SViresh Kumar goto out_exit_policy; 125891a12e91SViresh Kumar } 125991a12e91SViresh Kumar 126091a12e91SViresh Kumar /* Recover policy->cpus using related_cpus */ 126191a12e91SViresh Kumar cpumask_copy(policy->cpus, policy->related_cpus); 126291a12e91SViresh Kumar } else { 1263835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 12641da177e4SLinus Torvalds 126591a12e91SViresh Kumar /* 126691a12e91SViresh Kumar * Call driver. From then on the cpufreq must be able 126791a12e91SViresh Kumar * to accept all calls to ->verify and ->setpolicy for this CPU. 12681da177e4SLinus Torvalds */ 12691c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 12701da177e4SLinus Torvalds if (ret) { 127191a12e91SViresh Kumar pr_debug("%s: %d: initialization failed\n", __func__, 127291a12e91SViresh Kumar __LINE__); 12738101f997SViresh Kumar goto out_free_policy; 12741da177e4SLinus Torvalds } 1275643ae6e8SViresh Kumar 1276d417e069SViresh Kumar ret = cpufreq_table_validate_and_sort(policy); 1277d417e069SViresh Kumar if (ret) 1278d417e069SViresh Kumar goto out_exit_policy; 1279d417e069SViresh Kumar 12804d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 12810998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 12824d1f3a5bSRafael J. Wysocki } 1283559ed407SRafael J. Wysocki 128491a12e91SViresh Kumar down_write(&policy->rwsem); 12855a7e56a5SViresh Kumar /* 12865a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 12875a7e56a5SViresh Kumar * managing offline cpus here. 12885a7e56a5SViresh Kumar */ 12895a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 12905a7e56a5SViresh Kumar 1291194d99c7SRafael J. Wysocki if (new_policy) { 12925a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 12935a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 12946d4e81edSTomeu Vizoso 12952f0ba790SRafael J. Wysocki for_each_cpu(j, policy->related_cpus) { 1296652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 12972f0ba790SRafael J. Wysocki add_cpu_dev_symlink(policy, j); 12982f0ba790SRafael J. Wysocki } 1299ff010472SViresh Kumar } else { 1300ff010472SViresh Kumar policy->min = policy->user_policy.min; 1301ff010472SViresh Kumar policy->max = policy->user_policy.max; 1302988bed09SViresh Kumar } 1303652ed95dSViresh Kumar 13045ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target()) { 1305da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1306da60ce9fSViresh Kumar if (!policy->cur) { 1307da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1308d417e069SViresh Kumar goto out_destroy_policy; 1309da60ce9fSViresh Kumar } 1310da60ce9fSViresh Kumar } 1311da60ce9fSViresh Kumar 1312d3916691SViresh Kumar /* 1313d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1314d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1315d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1316d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1317d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1318d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1319d3916691SViresh Kumar * isn't found in freq-table. 1320d3916691SViresh Kumar * 1321d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1322d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1323d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1324d3916691SViresh Kumar * is initialized to zero). 1325d3916691SViresh Kumar * 1326d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1327d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1328d3916691SViresh Kumar * equal to target-freq. 1329d3916691SViresh Kumar */ 1330d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1331d3916691SViresh Kumar && has_target()) { 1332d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1333d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1334d3916691SViresh Kumar if (ret == -EINVAL) { 1335d3916691SViresh Kumar /* Warn user and fix it */ 1336d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1337d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1338d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1339d3916691SViresh Kumar CPUFREQ_RELATION_L); 1340d3916691SViresh Kumar 1341d3916691SViresh Kumar /* 1342d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1343d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1344d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1345d3916691SViresh Kumar */ 1346d3916691SViresh Kumar BUG_ON(ret); 1347d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1348d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1349d3916691SViresh Kumar } 1350d3916691SViresh Kumar } 1351d3916691SViresh Kumar 1352194d99c7SRafael J. Wysocki if (new_policy) { 1353d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 135419d6f7ecSDave Jones if (ret) 1355d417e069SViresh Kumar goto out_destroy_policy; 13561aefc75bSRafael J. Wysocki 13571aefc75bSRafael J. Wysocki cpufreq_stats_create_table(policy); 1358c88a1f8bSLukasz Majewski 1359c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1360c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1361c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1362988bed09SViresh Kumar } 13638ff69732SDave Jones 13647f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 13657f0fa40fSViresh Kumar if (ret) { 13667f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 13677f0fa40fSViresh Kumar __func__, cpu, ret); 1368d417e069SViresh Kumar goto out_destroy_policy; 136908fd8c1cSViresh Kumar } 1370e18f1682SSrivatsa S. Bhat 13714e97b631SViresh Kumar up_write(&policy->rwsem); 137208fd8c1cSViresh Kumar 1373038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 13747c45cf31SViresh Kumar 13757c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 13767c45cf31SViresh Kumar if (cpufreq_driver->ready) 13777c45cf31SViresh Kumar cpufreq_driver->ready(policy); 13787c45cf31SViresh Kumar 1379bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) 13805c238a8bSAmit Kucheria policy->cdev = of_cpufreq_cooling_register(policy); 13815c238a8bSAmit Kucheria 13822d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds return 0; 13851da177e4SLinus Torvalds 1386d417e069SViresh Kumar out_destroy_policy: 1387b24b6478SViresh Kumar for_each_cpu(j, policy->real_cpus) 1388b24b6478SViresh Kumar remove_cpu_dev_symlink(policy, get_cpu_device(j)); 1389b24b6478SViresh Kumar 13907106e02bSPrarit Bhargava up_write(&policy->rwsem); 13917106e02bSPrarit Bhargava 1392d417e069SViresh Kumar out_exit_policy: 1393da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1394da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 13952f0ba790SRafael J. Wysocki 13968101f997SViresh Kumar out_free_policy: 1397f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 13981da177e4SLinus Torvalds return ret; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14010b275352SRafael J. Wysocki /** 14020b275352SRafael J. Wysocki * cpufreq_add_dev - the cpufreq interface for a CPU device. 14030b275352SRafael J. Wysocki * @dev: CPU device. 14040b275352SRafael J. Wysocki * @sif: Subsystem interface structure pointer (not used) 14050b275352SRafael J. Wysocki */ 14060b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 14070b275352SRafael J. Wysocki { 1408a794d613SRafael J. Wysocki struct cpufreq_policy *policy; 14090b275352SRafael J. Wysocki unsigned cpu = dev->id; 141026619804SViresh Kumar int ret; 14110b275352SRafael J. Wysocki 14120b275352SRafael J. Wysocki dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 14130b275352SRafael J. Wysocki 141426619804SViresh Kumar if (cpu_online(cpu)) { 141526619804SViresh Kumar ret = cpufreq_online(cpu); 141626619804SViresh Kumar if (ret) 141726619804SViresh Kumar return ret; 141826619804SViresh Kumar } 1419a794d613SRafael J. Wysocki 142026619804SViresh Kumar /* Create sysfs link on CPU registration */ 1421a794d613SRafael J. Wysocki policy = per_cpu(cpufreq_cpu_data, cpu); 14222f0ba790SRafael J. Wysocki if (policy) 14232f0ba790SRafael J. Wysocki add_cpu_dev_symlink(policy, cpu); 14242f0ba790SRafael J. Wysocki 1425a794d613SRafael J. Wysocki return 0; 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds 142827622b06SSebastian Andrzej Siewior static int cpufreq_offline(unsigned int cpu) 14291da177e4SLinus Torvalds { 14303a3e9e06SViresh Kumar struct cpufreq_policy *policy; 143169cee714SViresh Kumar int ret; 14321da177e4SLinus Torvalds 1433b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 14341da177e4SLinus Torvalds 1435988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 14363a3e9e06SViresh Kumar if (!policy) { 1437b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 143827622b06SSebastian Andrzej Siewior return 0; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 144149f18560SViresh Kumar down_write(&policy->rwsem); 144245482c70SRafael J. Wysocki if (has_target()) 144345482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 14441da177e4SLinus Torvalds 14459591becbSViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 14464573237bSViresh Kumar 14479591becbSViresh Kumar if (policy_is_inactive(policy)) { 14489591becbSViresh Kumar if (has_target()) 14494573237bSViresh Kumar strncpy(policy->last_governor, policy->governor->name, 14504573237bSViresh Kumar CPUFREQ_NAME_LEN); 145169030dd1SSrinivas Pandruvada else 145269030dd1SSrinivas Pandruvada policy->last_policy = policy->policy; 14539591becbSViresh Kumar } else if (cpu == policy->cpu) { 14549591becbSViresh Kumar /* Nominate new CPU */ 14559591becbSViresh Kumar policy->cpu = cpumask_any(policy->cpus); 14569591becbSViresh Kumar } 14571da177e4SLinus Torvalds 14589591becbSViresh Kumar /* Start governor again for active policy */ 14599591becbSViresh Kumar if (!policy_is_inactive(policy)) { 14609591becbSViresh Kumar if (has_target()) { 14610a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 14629591becbSViresh Kumar if (ret) 14639591becbSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 14649591becbSViresh Kumar } 146569cee714SViresh Kumar 146649f18560SViresh Kumar goto unlock; 146769cee714SViresh Kumar } 146869cee714SViresh Kumar 1469bcc61569SDaniel Lezcano if (cpufreq_thermal_control_enabled(cpufreq_driver)) { 14705c238a8bSAmit Kucheria cpufreq_cooling_unregister(policy->cdev); 14715c238a8bSAmit Kucheria policy->cdev = NULL; 14725c238a8bSAmit Kucheria } 14735c238a8bSAmit Kucheria 147469cee714SViresh Kumar if (cpufreq_driver->stop_cpu) 1475367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 147687549141SViresh Kumar 147736be3418SRafael J. Wysocki if (has_target()) 147836be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 14792a998599SRafael J. Wysocki 14808414809cSSrivatsa S. Bhat /* 148191a12e91SViresh Kumar * Perform the ->offline() during light-weight tear-down, as 148291a12e91SViresh Kumar * that allows fast recovery when the CPU comes back. 14838414809cSSrivatsa S. Bhat */ 148491a12e91SViresh Kumar if (cpufreq_driver->offline) { 148591a12e91SViresh Kumar cpufreq_driver->offline(policy); 148691a12e91SViresh Kumar } else if (cpufreq_driver->exit) { 14873a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 148855582bccSSrinivas Pandruvada policy->freq_table = NULL; 148955582bccSSrinivas Pandruvada } 149049f18560SViresh Kumar 149149f18560SViresh Kumar unlock: 149249f18560SViresh Kumar up_write(&policy->rwsem); 149327622b06SSebastian Andrzej Siewior return 0; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 1496cedb70afSSrivatsa S. Bhat /** 149727a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1498cedb70afSSrivatsa S. Bhat * 1499cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1500cedb70afSSrivatsa S. Bhat */ 150171db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 15025a01f2e8SVenkatesh Pallipadi { 15038a25a2fdSKay Sievers unsigned int cpu = dev->id; 150487549141SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 150587549141SViresh Kumar 150687549141SViresh Kumar if (!policy) 150771db87baSViresh Kumar return; 1508ec28297aSVenki Pallipadi 150969cee714SViresh Kumar if (cpu_online(cpu)) 151069cee714SViresh Kumar cpufreq_offline(cpu); 151187549141SViresh Kumar 1512559ed407SRafael J. Wysocki cpumask_clear_cpu(cpu, policy->real_cpus); 151326619804SViresh Kumar remove_cpu_dev_symlink(policy, dev); 1514f344dae0SViresh Kumar 151591a12e91SViresh Kumar if (cpumask_empty(policy->real_cpus)) { 151691a12e91SViresh Kumar /* We did light-weight exit earlier, do full tear down now */ 151791a12e91SViresh Kumar if (cpufreq_driver->offline) 151891a12e91SViresh Kumar cpufreq_driver->exit(policy); 151991a12e91SViresh Kumar 1520f9f41e3eSViresh Kumar cpufreq_policy_free(policy); 15215a01f2e8SVenkatesh Pallipadi } 152291a12e91SViresh Kumar } 15235a01f2e8SVenkatesh Pallipadi 15241da177e4SLinus Torvalds /** 1525bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1526bb176f7dSViresh Kumar * in deep trouble. 1527a1e1dc41SViresh Kumar * @policy: policy managing CPUs 15281da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 15291da177e4SLinus Torvalds * 153029464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 153129464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 15321da177e4SLinus Torvalds */ 1533a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1534e08f5f5bSGautham R Shenoy unsigned int new_freq) 15351da177e4SLinus Torvalds { 15361da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1537b43a7ffbSViresh Kumar 1538e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1539a1e1dc41SViresh Kumar policy->cur, new_freq); 15401da177e4SLinus Torvalds 1541a1e1dc41SViresh Kumar freqs.old = policy->cur; 15421da177e4SLinus Torvalds freqs.new = new_freq; 1543b43a7ffbSViresh Kumar 15448fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 15458fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 15461da177e4SLinus Torvalds } 15471da177e4SLinus Torvalds 1548*5980752eSViresh Kumar static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update) 1549*5980752eSViresh Kumar { 1550*5980752eSViresh Kumar unsigned int new_freq; 1551*5980752eSViresh Kumar 1552*5980752eSViresh Kumar new_freq = cpufreq_driver->get(policy->cpu); 1553*5980752eSViresh Kumar if (!new_freq) 1554*5980752eSViresh Kumar return 0; 1555*5980752eSViresh Kumar 1556*5980752eSViresh Kumar /* 1557*5980752eSViresh Kumar * If fast frequency switching is used with the given policy, the check 1558*5980752eSViresh Kumar * against policy->cur is pointless, so skip it in that case. 1559*5980752eSViresh Kumar */ 1560*5980752eSViresh Kumar if (policy->fast_switch_enabled || !has_target()) 1561*5980752eSViresh Kumar return new_freq; 1562*5980752eSViresh Kumar 1563*5980752eSViresh Kumar if (policy->cur != new_freq) { 1564*5980752eSViresh Kumar cpufreq_out_of_sync(policy, new_freq); 1565*5980752eSViresh Kumar if (update) 1566*5980752eSViresh Kumar schedule_work(&policy->update); 1567*5980752eSViresh Kumar } 1568*5980752eSViresh Kumar 1569*5980752eSViresh Kumar return new_freq; 1570*5980752eSViresh Kumar } 1571*5980752eSViresh Kumar 15721da177e4SLinus Torvalds /** 15734ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 157495235ca2SVenkatesh Pallipadi * @cpu: CPU number 157595235ca2SVenkatesh Pallipadi * 157695235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 157795235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 157895235ca2SVenkatesh Pallipadi */ 157995235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 158095235ca2SVenkatesh Pallipadi { 15819e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1582e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 1583c75361c0SRichard Cochran unsigned long flags; 158495235ca2SVenkatesh Pallipadi 1585c75361c0SRichard Cochran read_lock_irqsave(&cpufreq_driver_lock, flags); 1586c75361c0SRichard Cochran 1587c75361c0SRichard Cochran if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { 1588c75361c0SRichard Cochran ret_freq = cpufreq_driver->get(cpu); 1589c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1590c75361c0SRichard Cochran return ret_freq; 1591c75361c0SRichard Cochran } 1592c75361c0SRichard Cochran 1593c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 15949e21ba8bSDirk Brandewie 15959e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 159695235ca2SVenkatesh Pallipadi if (policy) { 1597e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 159895235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 159995235ca2SVenkatesh Pallipadi } 160095235ca2SVenkatesh Pallipadi 16014d34a67dSDave Jones return ret_freq; 160295235ca2SVenkatesh Pallipadi } 160395235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 160495235ca2SVenkatesh Pallipadi 16053d737108SJesse Barnes /** 16063d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 16073d737108SJesse Barnes * @cpu: CPU number 16083d737108SJesse Barnes * 16093d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 16103d737108SJesse Barnes */ 16113d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 16123d737108SJesse Barnes { 16133d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 16143d737108SJesse Barnes unsigned int ret_freq = 0; 16153d737108SJesse Barnes 16163d737108SJesse Barnes if (policy) { 16173d737108SJesse Barnes ret_freq = policy->max; 16183d737108SJesse Barnes cpufreq_cpu_put(policy); 16193d737108SJesse Barnes } 16203d737108SJesse Barnes 16213d737108SJesse Barnes return ret_freq; 16223d737108SJesse Barnes } 16233d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 16243d737108SJesse Barnes 1625d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 16261da177e4SLinus Torvalds { 16274db7c34cSYue Hu if (unlikely(policy_is_inactive(policy))) 1628*5980752eSViresh Kumar return 0; 16291da177e4SLinus Torvalds 1630*5980752eSViresh Kumar return cpufreq_verify_current_freq(policy, true); 16315a01f2e8SVenkatesh Pallipadi } 16321da177e4SLinus Torvalds 16335a01f2e8SVenkatesh Pallipadi /** 16345a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 16355a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 16365a01f2e8SVenkatesh Pallipadi * 16375a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 16385a01f2e8SVenkatesh Pallipadi */ 16395a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 16405a01f2e8SVenkatesh Pallipadi { 1641999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 16425a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 16435a01f2e8SVenkatesh Pallipadi 1644999976e0SAaron Plattner if (policy) { 1645ad7722daSviresh kumar down_read(&policy->rwsem); 16464db7c34cSYue Hu if (cpufreq_driver->get) 1647d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1648ad7722daSviresh kumar up_read(&policy->rwsem); 1649999976e0SAaron Plattner 1650999976e0SAaron Plattner cpufreq_cpu_put(policy); 1651999976e0SAaron Plattner } 16526eed9404SViresh Kumar 16534d34a67dSDave Jones return ret_freq; 16541da177e4SLinus Torvalds } 16551da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 16561da177e4SLinus Torvalds 16578a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 16588a25a2fdSKay Sievers .name = "cpufreq", 16598a25a2fdSKay Sievers .subsys = &cpu_subsys, 16608a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 16618a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1662e00e56dfSRafael J. Wysocki }; 1663e00e56dfSRafael J. Wysocki 1664e28867eaSViresh Kumar /* 1665e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1666e28867eaSViresh Kumar * during suspend.. 166742d4dc3fSBenjamin Herrenschmidt */ 1668e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 166942d4dc3fSBenjamin Herrenschmidt { 1670e28867eaSViresh Kumar int ret; 16714bc5d341SDave Jones 1672e28867eaSViresh Kumar if (!policy->suspend_freq) { 1673201f3716SBartlomiej Zolnierkiewicz pr_debug("%s: suspend_freq not defined\n", __func__); 1674201f3716SBartlomiej Zolnierkiewicz return 0; 167542d4dc3fSBenjamin Herrenschmidt } 167642d4dc3fSBenjamin Herrenschmidt 1677e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1678e28867eaSViresh Kumar policy->suspend_freq); 1679e28867eaSViresh Kumar 1680e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1681e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1682e28867eaSViresh Kumar if (ret) 1683e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1684e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1685e28867eaSViresh Kumar 1686c9060494SDave Jones return ret; 168742d4dc3fSBenjamin Herrenschmidt } 1688e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 168942d4dc3fSBenjamin Herrenschmidt 169042d4dc3fSBenjamin Herrenschmidt /** 16912f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 16921da177e4SLinus Torvalds * 16932f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 16942f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 16952f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 16962f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 16971da177e4SLinus Torvalds */ 16982f0aea93SViresh Kumar void cpufreq_suspend(void) 16991da177e4SLinus Torvalds { 17003a3e9e06SViresh Kumar struct cpufreq_policy *policy; 17011da177e4SLinus Torvalds 17022f0aea93SViresh Kumar if (!cpufreq_driver) 1703e00e56dfSRafael J. Wysocki return; 17041da177e4SLinus Torvalds 1705ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->suspend) 1706b1b12babSViresh Kumar goto suspend; 17071da177e4SLinus Torvalds 17082f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 17092f0aea93SViresh Kumar 1710f963735aSViresh Kumar for_each_active_policy(policy) { 1711ba41e1bcSRafael J. Wysocki if (has_target()) { 171249f18560SViresh Kumar down_write(&policy->rwsem); 171345482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 171449f18560SViresh Kumar up_write(&policy->rwsem); 1715ba41e1bcSRafael J. Wysocki } 1716ba41e1bcSRafael J. Wysocki 1717ba41e1bcSRafael J. Wysocki if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy)) 17182f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 17192f0aea93SViresh Kumar policy); 17201da177e4SLinus Torvalds } 1721b1b12babSViresh Kumar 1722b1b12babSViresh Kumar suspend: 1723b1b12babSViresh Kumar cpufreq_suspended = true; 17241da177e4SLinus Torvalds } 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds /** 17272f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 17281da177e4SLinus Torvalds * 17292f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 17302f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 17311da177e4SLinus Torvalds */ 17322f0aea93SViresh Kumar void cpufreq_resume(void) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds struct cpufreq_policy *policy; 173549f18560SViresh Kumar int ret; 17361da177e4SLinus Torvalds 17372f0aea93SViresh Kumar if (!cpufreq_driver) 17381da177e4SLinus Torvalds return; 17391da177e4SLinus Torvalds 1740703cbaa6SBo Yan if (unlikely(!cpufreq_suspended)) 1741703cbaa6SBo Yan return; 1742703cbaa6SBo Yan 17438e30444eSLan Tianyu cpufreq_suspended = false; 17448e30444eSLan Tianyu 1745ba41e1bcSRafael J. Wysocki if (!has_target() && !cpufreq_driver->resume) 17462f0aea93SViresh Kumar return; 17471da177e4SLinus Torvalds 17482f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 17492f0aea93SViresh Kumar 1750f963735aSViresh Kumar for_each_active_policy(policy) { 175149f18560SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 17520c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 17530c5aa405SViresh Kumar policy); 1754ba41e1bcSRafael J. Wysocki } else if (has_target()) { 175549f18560SViresh Kumar down_write(&policy->rwsem); 17560a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 175749f18560SViresh Kumar up_write(&policy->rwsem); 175849f18560SViresh Kumar 175949f18560SViresh Kumar if (ret) 17602f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 17612f0aea93SViresh Kumar __func__, policy); 1762c75de0acSViresh Kumar } 176349f18560SViresh Kumar } 17641da177e4SLinus Torvalds } 17651da177e4SLinus Torvalds 17669d95046eSBorislav Petkov /** 17679d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17689d95046eSBorislav Petkov * 17699d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17709d95046eSBorislav Petkov * or NULL, if none. 17719d95046eSBorislav Petkov */ 17729d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17739d95046eSBorislav Petkov { 17741c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17751c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17761c3d85ddSRafael J. Wysocki 17771c3d85ddSRafael J. Wysocki return NULL; 17789d95046eSBorislav Petkov } 17799d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17801da177e4SLinus Torvalds 178151315cdfSThomas Petazzoni /** 178251315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 178351315cdfSThomas Petazzoni * 178451315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 178551315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 178651315cdfSThomas Petazzoni */ 178751315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 178851315cdfSThomas Petazzoni { 178951315cdfSThomas Petazzoni if (cpufreq_driver) 179051315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 179151315cdfSThomas Petazzoni 179251315cdfSThomas Petazzoni return NULL; 179351315cdfSThomas Petazzoni } 179451315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 179551315cdfSThomas Petazzoni 17961da177e4SLinus Torvalds /********************************************************************* 17971da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 17981da177e4SLinus Torvalds *********************************************************************/ 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds /** 18011da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 18021da177e4SLinus Torvalds * @nb: notifier function to register 18031da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 18041da177e4SLinus Torvalds * 18051da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 18061da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 18071da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 18081da177e4SLinus Torvalds * changes in cpufreq policy. 18091da177e4SLinus Torvalds * 18101da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1811e041c683SAlan Stern * blocking_notifier_chain_register. 18121da177e4SLinus Torvalds */ 18131da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 18141da177e4SLinus Torvalds { 18151da177e4SLinus Torvalds int ret; 18161da177e4SLinus Torvalds 1817d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1818d5aaffa9SDirk Brandewie return -EINVAL; 1819d5aaffa9SDirk Brandewie 18201da177e4SLinus Torvalds switch (list) { 18211da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1822b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 1823b7898fdaSRafael J. Wysocki 1824b7898fdaSRafael J. Wysocki if (cpufreq_fast_switch_count > 0) { 1825b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 1826b7898fdaSRafael J. Wysocki return -EBUSY; 1827b7898fdaSRafael J. Wysocki } 1828b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1829e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 1830b7898fdaSRafael J. Wysocki if (!ret) 1831b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count--; 1832b7898fdaSRafael J. Wysocki 1833b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 18341da177e4SLinus Torvalds break; 18351da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1836e041c683SAlan Stern ret = blocking_notifier_chain_register( 1837e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18381da177e4SLinus Torvalds break; 18391da177e4SLinus Torvalds default: 18401da177e4SLinus Torvalds ret = -EINVAL; 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds return ret; 18441da177e4SLinus Torvalds } 18451da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds /** 18481da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 18491da177e4SLinus Torvalds * @nb: notifier block to be unregistered 18501da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 18511da177e4SLinus Torvalds * 18521da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 18531da177e4SLinus Torvalds * 18541da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1855e041c683SAlan Stern * blocking_notifier_chain_unregister. 18561da177e4SLinus Torvalds */ 18571da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 18581da177e4SLinus Torvalds { 18591da177e4SLinus Torvalds int ret; 18601da177e4SLinus Torvalds 1861d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1862d5aaffa9SDirk Brandewie return -EINVAL; 1863d5aaffa9SDirk Brandewie 18641da177e4SLinus Torvalds switch (list) { 18651da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1866b7898fdaSRafael J. Wysocki mutex_lock(&cpufreq_fast_switch_lock); 1867b7898fdaSRafael J. Wysocki 1868b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1869e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 1870b7898fdaSRafael J. Wysocki if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0)) 1871b7898fdaSRafael J. Wysocki cpufreq_fast_switch_count++; 1872b7898fdaSRafael J. Wysocki 1873b7898fdaSRafael J. Wysocki mutex_unlock(&cpufreq_fast_switch_lock); 18741da177e4SLinus Torvalds break; 18751da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1876e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1877e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18781da177e4SLinus Torvalds break; 18791da177e4SLinus Torvalds default: 18801da177e4SLinus Torvalds ret = -EINVAL; 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds 18831da177e4SLinus Torvalds return ret; 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 18861da177e4SLinus Torvalds 18871da177e4SLinus Torvalds 18881da177e4SLinus Torvalds /********************************************************************* 18891da177e4SLinus Torvalds * GOVERNORS * 18901da177e4SLinus Torvalds *********************************************************************/ 18911da177e4SLinus Torvalds 1892b7898fdaSRafael J. Wysocki /** 1893b7898fdaSRafael J. Wysocki * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch. 1894b7898fdaSRafael J. Wysocki * @policy: cpufreq policy to switch the frequency for. 1895b7898fdaSRafael J. Wysocki * @target_freq: New frequency to set (may be approximate). 1896b7898fdaSRafael J. Wysocki * 1897b7898fdaSRafael J. Wysocki * Carry out a fast frequency switch without sleeping. 1898b7898fdaSRafael J. Wysocki * 1899b7898fdaSRafael J. Wysocki * The driver's ->fast_switch() callback invoked by this function must be 1900b7898fdaSRafael J. Wysocki * suitable for being called from within RCU-sched read-side critical sections 1901b7898fdaSRafael J. Wysocki * and it is expected to select the minimum available frequency greater than or 1902b7898fdaSRafael J. Wysocki * equal to @target_freq (CPUFREQ_RELATION_L). 1903b7898fdaSRafael J. Wysocki * 1904b7898fdaSRafael J. Wysocki * This function must not be called if policy->fast_switch_enabled is unset. 1905b7898fdaSRafael J. Wysocki * 1906b7898fdaSRafael J. Wysocki * Governors calling this function must guarantee that it will never be invoked 1907b7898fdaSRafael J. Wysocki * twice in parallel for the same policy and that it will never be called in 1908b7898fdaSRafael J. Wysocki * parallel with either ->target() or ->target_index() for the same policy. 1909b7898fdaSRafael J. Wysocki * 1910209887e6SViresh Kumar * Returns the actual frequency set for the CPU. 1911209887e6SViresh Kumar * 1912209887e6SViresh Kumar * If 0 is returned by the driver's ->fast_switch() callback to indicate an 1913209887e6SViresh Kumar * error condition, the hardware configuration must be preserved. 1914b7898fdaSRafael J. Wysocki */ 1915b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy, 1916b7898fdaSRafael J. Wysocki unsigned int target_freq) 1917b7898fdaSRafael J. Wysocki { 1918b9af6948SRafael J. Wysocki target_freq = clamp_val(target_freq, policy->min, policy->max); 1919b7898fdaSRafael J. Wysocki 1920b7898fdaSRafael J. Wysocki return cpufreq_driver->fast_switch(policy, target_freq); 1921b7898fdaSRafael J. Wysocki } 1922b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch); 1923b7898fdaSRafael J. Wysocki 19241c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 19251c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 19261c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 19271c03a2d0SViresh Kumar { 19281c03a2d0SViresh Kumar int ret; 19291c03a2d0SViresh Kumar 19301c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 19311c03a2d0SViresh Kumar 19321c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 19331c03a2d0SViresh Kumar if (!freqs->new) 19341c03a2d0SViresh Kumar return 0; 19351c03a2d0SViresh Kumar 19361c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 19371c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 19381c03a2d0SViresh Kumar 19391c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 19401c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 19411c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 19421c03a2d0SViresh Kumar 19431c03a2d0SViresh Kumar if (ret) 19441c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 19451c03a2d0SViresh Kumar __func__, ret); 19461c03a2d0SViresh Kumar 19471c03a2d0SViresh Kumar return ret; 19481c03a2d0SViresh Kumar } 19491c03a2d0SViresh Kumar 195023727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index) 19518d65775dSViresh Kumar { 19521c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 19531c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 195423727845SViresh Kumar unsigned int newfreq = policy->freq_table[index].frequency; 19558d65775dSViresh Kumar int retval = -EINVAL; 19568d65775dSViresh Kumar bool notify; 19578d65775dSViresh Kumar 195823727845SViresh Kumar if (newfreq == policy->cur) 195923727845SViresh Kumar return 0; 196023727845SViresh Kumar 19618d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 19628d65775dSViresh Kumar if (notify) { 19631c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 19641c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 19651c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 19661c03a2d0SViresh Kumar if (retval) 19671c03a2d0SViresh Kumar return retval; 19688d65775dSViresh Kumar 19691c03a2d0SViresh Kumar intermediate_freq = freqs.new; 19701c03a2d0SViresh Kumar /* Set old freq to intermediate */ 19711c03a2d0SViresh Kumar if (intermediate_freq) 19721c03a2d0SViresh Kumar freqs.old = freqs.new; 19731c03a2d0SViresh Kumar } 19741c03a2d0SViresh Kumar 197523727845SViresh Kumar freqs.new = newfreq; 19768d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 19778d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 19788d65775dSViresh Kumar 19798d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 19808d65775dSViresh Kumar } 19818d65775dSViresh Kumar 19828d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 19838d65775dSViresh Kumar if (retval) 19848d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 19858d65775dSViresh Kumar retval); 19868d65775dSViresh Kumar 19871c03a2d0SViresh Kumar if (notify) { 19888d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 19898d65775dSViresh Kumar 19901c03a2d0SViresh Kumar /* 19911c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 19921c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 19931c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 199458405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 19951c03a2d0SViresh Kumar */ 19961c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 19971c03a2d0SViresh Kumar freqs.old = intermediate_freq; 19981c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 19991c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 20001c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 20011c03a2d0SViresh Kumar } 20021c03a2d0SViresh Kumar } 20031c03a2d0SViresh Kumar 20048d65775dSViresh Kumar return retval; 20058d65775dSViresh Kumar } 20068d65775dSViresh Kumar 20071da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 20081da177e4SLinus Torvalds unsigned int target_freq, 20091da177e4SLinus Torvalds unsigned int relation) 20101da177e4SLinus Torvalds { 20117249924eSViresh Kumar unsigned int old_target_freq = target_freq; 2012d218ed77SViresh Kumar int index; 2013c32b6b8eSAshok Raj 2014a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2015a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2016a7b422cdSKonrad Rzeszutek Wilk 20177249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 2018910c6e88SViresh Kumar target_freq = clamp_val(target_freq, policy->min, policy->max); 20197249924eSViresh Kumar 20207249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 20217249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 20225a1c0228SViresh Kumar 20239c0ebcf7SViresh Kumar /* 20249c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 20259c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 20269c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 20279c0ebcf7SViresh Kumar * calls. 20289c0ebcf7SViresh Kumar */ 20295a1c0228SViresh Kumar if (target_freq == policy->cur) 20305a1c0228SViresh Kumar return 0; 20315a1c0228SViresh Kumar 20321c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 20331c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 20341c03a2d0SViresh Kumar 20351c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 20366019d23aSRafael J. Wysocki return cpufreq_driver->target(policy, target_freq, relation); 20376019d23aSRafael J. Wysocki 20386019d23aSRafael J. Wysocki if (!cpufreq_driver->target_index) 20396019d23aSRafael J. Wysocki return -EINVAL; 204090d45d17SAshok Raj 2041d218ed77SViresh Kumar index = cpufreq_frequency_table_target(policy, target_freq, relation); 20429c0ebcf7SViresh Kumar 204323727845SViresh Kumar return __target_index(policy, index); 20446019d23aSRafael J. Wysocki } 20451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 20461da177e4SLinus Torvalds 20471da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 20481da177e4SLinus Torvalds unsigned int target_freq, 20491da177e4SLinus Torvalds unsigned int relation) 20501da177e4SLinus Torvalds { 2051f1829e4aSJulia Lawall int ret = -EINVAL; 20521da177e4SLinus Torvalds 2053ad7722daSviresh kumar down_write(&policy->rwsem); 20541da177e4SLinus Torvalds 20551da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 20561da177e4SLinus Torvalds 2057ad7722daSviresh kumar up_write(&policy->rwsem); 20581da177e4SLinus Torvalds 20591da177e4SLinus Torvalds return ret; 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 20621da177e4SLinus Torvalds 2063de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 2064de1df26bSRafael J. Wysocki { 2065de1df26bSRafael J. Wysocki return NULL; 2066de1df26bSRafael J. Wysocki } 2067de1df26bSRafael J. Wysocki 2068a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy) 20691da177e4SLinus Torvalds { 2070cc993cabSDave Jones int ret; 20716afde10cSThomas Renninger 20722f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 20732f0aea93SViresh Kumar if (cpufreq_suspended) 20742f0aea93SViresh Kumar return 0; 2075cb57720bSEthan Zhao /* 2076cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 2077cb57720bSEthan Zhao * notification happened, so check it. 2078cb57720bSEthan Zhao */ 2079cb57720bSEthan Zhao if (!policy->governor) 2080cb57720bSEthan Zhao return -EINVAL; 20812f0aea93SViresh Kumar 2082ed4676e2SViresh Kumar /* Platform doesn't want dynamic frequency switching ? */ 2083ed4676e2SViresh Kumar if (policy->governor->dynamic_switching && 2084fc4c709fSViresh Kumar cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) { 2085de1df26bSRafael J. Wysocki struct cpufreq_governor *gov = cpufreq_fallback_governor(); 2086de1df26bSRafael J. Wysocki 2087de1df26bSRafael J. Wysocki if (gov) { 2088fe829ed8SViresh Kumar pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n", 2089e837f9b5SJoe Perches policy->governor->name, gov->name); 20901c256245SThomas Renninger policy->governor = gov; 2091de1df26bSRafael J. Wysocki } else { 2092de1df26bSRafael J. Wysocki return -EINVAL; 20931c256245SThomas Renninger } 20946afde10cSThomas Renninger } 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 20971da177e4SLinus Torvalds return -EINVAL; 20981da177e4SLinus Torvalds 2099a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 210095731ebbSXiaoguang Chen 2101e788892bSRafael J. Wysocki if (policy->governor->init) { 2102e788892bSRafael J. Wysocki ret = policy->governor->init(policy); 2103a92604b4SRafael J. Wysocki if (ret) { 21041da177e4SLinus Torvalds module_put(policy->governor->owner); 2105a92604b4SRafael J. Wysocki return ret; 21069e8c0a89SRafael J. Wysocki } 210736be3418SRafael J. Wysocki } 21081da177e4SLinus Torvalds 2109a92604b4SRafael J. Wysocki return 0; 2110a92604b4SRafael J. Wysocki } 2111a92604b4SRafael J. Wysocki 2112a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy) 2113a92604b4SRafael J. Wysocki { 2114a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2115a92604b4SRafael J. Wysocki return; 2116a92604b4SRafael J. Wysocki 2117a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2118a92604b4SRafael J. Wysocki 2119e788892bSRafael J. Wysocki if (policy->governor->exit) 2120e788892bSRafael J. Wysocki policy->governor->exit(policy); 2121a92604b4SRafael J. Wysocki 21221da177e4SLinus Torvalds module_put(policy->governor->owner); 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 21250a300767SRafael J. Wysocki static int cpufreq_start_governor(struct cpufreq_policy *policy) 21260a300767SRafael J. Wysocki { 21270a300767SRafael J. Wysocki int ret; 21280a300767SRafael J. Wysocki 2129a92604b4SRafael J. Wysocki if (cpufreq_suspended) 2130a92604b4SRafael J. Wysocki return 0; 2131a92604b4SRafael J. Wysocki 2132a92604b4SRafael J. Wysocki if (!policy->governor) 2133a92604b4SRafael J. Wysocki return -EINVAL; 2134a92604b4SRafael J. Wysocki 2135a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2136a92604b4SRafael J. Wysocki 2137407d0fffSViresh Kumar if (cpufreq_driver->get) 2138*5980752eSViresh Kumar cpufreq_verify_current_freq(policy, false); 21393bbf8fe3SRafael J. Wysocki 2140e788892bSRafael J. Wysocki if (policy->governor->start) { 2141e788892bSRafael J. Wysocki ret = policy->governor->start(policy); 2142d6ff44d6SRafael J. Wysocki if (ret) 2143d6ff44d6SRafael J. Wysocki return ret; 2144e788892bSRafael J. Wysocki } 2145d6ff44d6SRafael J. Wysocki 2146e788892bSRafael J. Wysocki if (policy->governor->limits) 2147e788892bSRafael J. Wysocki policy->governor->limits(policy); 2148e788892bSRafael J. Wysocki 2149d6ff44d6SRafael J. Wysocki return 0; 21500a300767SRafael J. Wysocki } 21510a300767SRafael J. Wysocki 2152a92604b4SRafael J. Wysocki static void cpufreq_stop_governor(struct cpufreq_policy *policy) 2153a92604b4SRafael J. Wysocki { 2154a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2155a92604b4SRafael J. Wysocki return; 2156a92604b4SRafael J. Wysocki 2157a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2158a92604b4SRafael J. Wysocki 2159e788892bSRafael J. Wysocki if (policy->governor->stop) 2160e788892bSRafael J. Wysocki policy->governor->stop(policy); 2161a92604b4SRafael J. Wysocki } 2162a92604b4SRafael J. Wysocki 2163a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy) 2164a92604b4SRafael J. Wysocki { 2165a92604b4SRafael J. Wysocki if (cpufreq_suspended || !policy->governor) 2166a92604b4SRafael J. Wysocki return; 2167a92604b4SRafael J. Wysocki 2168a92604b4SRafael J. Wysocki pr_debug("%s: for CPU %u\n", __func__, policy->cpu); 2169a92604b4SRafael J. Wysocki 2170e788892bSRafael J. Wysocki if (policy->governor->limits) 2171e788892bSRafael J. Wysocki policy->governor->limits(policy); 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 21741da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 21751da177e4SLinus Torvalds { 21763bcb09a3SJeremy Fitzhardinge int err; 21771da177e4SLinus Torvalds 21781da177e4SLinus Torvalds if (!governor) 21791da177e4SLinus Torvalds return -EINVAL; 21801da177e4SLinus Torvalds 2181a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2182a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2183a7b422cdSKonrad Rzeszutek Wilk 21843fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 21851da177e4SLinus Torvalds 21863bcb09a3SJeremy Fitzhardinge err = -EBUSY; 218742f91fa1SViresh Kumar if (!find_governor(governor->name)) { 21883bcb09a3SJeremy Fitzhardinge err = 0; 21891da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 21903bcb09a3SJeremy Fitzhardinge } 21911da177e4SLinus Torvalds 21923fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 21933bcb09a3SJeremy Fitzhardinge return err; 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 21961da177e4SLinus Torvalds 21971da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 21981da177e4SLinus Torvalds { 21994573237bSViresh Kumar struct cpufreq_policy *policy; 22004573237bSViresh Kumar unsigned long flags; 220190e41bacSPrarit Bhargava 22021da177e4SLinus Torvalds if (!governor) 22031da177e4SLinus Torvalds return; 22041da177e4SLinus Torvalds 2205a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2206a7b422cdSKonrad Rzeszutek Wilk return; 2207a7b422cdSKonrad Rzeszutek Wilk 22084573237bSViresh Kumar /* clear last_governor for all inactive policies */ 22094573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 22104573237bSViresh Kumar for_each_inactive_policy(policy) { 221118bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 221218bf3a12SViresh Kumar policy->governor = NULL; 22134573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 221490e41bacSPrarit Bhargava } 221518bf3a12SViresh Kumar } 22164573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 221790e41bacSPrarit Bhargava 22183fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 22191da177e4SLinus Torvalds list_del(&governor->governor_list); 22203fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 22211da177e4SLinus Torvalds } 22221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds 22251da177e4SLinus Torvalds /********************************************************************* 22261da177e4SLinus Torvalds * POLICY INTERFACE * 22271da177e4SLinus Torvalds *********************************************************************/ 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds /** 22301da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 223129464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 223229464f28SDave Jones * is written 22331da177e4SLinus Torvalds * 22341da177e4SLinus Torvalds * Reads the current cpufreq policy. 22351da177e4SLinus Torvalds */ 22361da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 22371da177e4SLinus Torvalds { 22381da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 22391da177e4SLinus Torvalds if (!policy) 22401da177e4SLinus Torvalds return -EINVAL; 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 22431da177e4SLinus Torvalds if (!cpu_policy) 22441da177e4SLinus Torvalds return -EINVAL; 22451da177e4SLinus Torvalds 2246d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 22491da177e4SLinus Torvalds return 0; 22501da177e4SLinus Torvalds } 22511da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 22521da177e4SLinus Torvalds 2253a0dbb819SRafael J. Wysocki /** 2254a0dbb819SRafael J. Wysocki * cpufreq_set_policy - Modify cpufreq policy parameters. 2255a0dbb819SRafael J. Wysocki * @policy: Policy object to modify. 2256a0dbb819SRafael J. Wysocki * @new_policy: New policy data. 2257a0dbb819SRafael J. Wysocki * 2258a0dbb819SRafael J. Wysocki * Pass @new_policy to the cpufreq driver's ->verify() callback, run the 2259a0dbb819SRafael J. Wysocki * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to 2260a0dbb819SRafael J. Wysocki * the driver's ->verify() callback again and run the notifiers for it again 2261a0dbb819SRafael J. Wysocki * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters 2262a0dbb819SRafael J. Wysocki * of @new_policy to @policy and either invoke the driver's ->setpolicy() 2263a0dbb819SRafael J. Wysocki * callback (if present) or carry out a governor update for @policy. That is, 2264a0dbb819SRafael J. Wysocki * run the current governor's ->limits() callback (if the governor field in 2265a0dbb819SRafael J. Wysocki * @new_policy points to the same object as the one in @policy) or replace the 2266a0dbb819SRafael J. Wysocki * governor for @policy with the new one stored in @new_policy. 2267a0dbb819SRafael J. Wysocki * 2268a0dbb819SRafael J. Wysocki * The cpuinfo part of @policy is not updated by this function. 2269153d7f3fSArjan van de Ven */ 22709083e498SRafael J. Wysocki int cpufreq_set_policy(struct cpufreq_policy *policy, 22713a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 22721da177e4SLinus Torvalds { 2273d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2274d9a789c7SRafael J. Wysocki int ret; 22751da177e4SLinus Torvalds 2276e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2277e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 22781da177e4SLinus Torvalds 2279d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 22801da177e4SLinus Torvalds 2281fba9573bSPan Xinhui /* 2282fba9573bSPan Xinhui * This check works well when we store new min/max freq attributes, 2283fba9573bSPan Xinhui * because new_policy is a copy of policy with one field updated. 2284fba9573bSPan Xinhui */ 2285fba9573bSPan Xinhui if (new_policy->min > new_policy->max) 2286d9a789c7SRafael J. Wysocki return -EINVAL; 22879c9a43edSMattia Dongili 22881da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 22893a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 22901da177e4SLinus Torvalds if (ret) 2291d9a789c7SRafael J. Wysocki return ret; 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2294e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 22953a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 22961da177e4SLinus Torvalds 2297bb176f7dSViresh Kumar /* 2298bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2299bb176f7dSViresh Kumar * different to the first one 2300bb176f7dSViresh Kumar */ 23013a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2302e041c683SAlan Stern if (ret) 2303d9a789c7SRafael J. Wysocki return ret; 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds /* notification of the new policy */ 2306e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 23073a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 23081da177e4SLinus Torvalds 23093a3e9e06SViresh Kumar policy->min = new_policy->min; 23103a3e9e06SViresh Kumar policy->max = new_policy->max; 2311601b2185SRuchi Kandoi trace_cpu_frequency_limits(policy); 23121da177e4SLinus Torvalds 2313e3c06236SSteve Muckle policy->cached_target_freq = UINT_MAX; 2314e3c06236SSteve Muckle 23152d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 23163a3e9e06SViresh Kumar policy->min, policy->max); 23171da177e4SLinus Torvalds 23181c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 23193a3e9e06SViresh Kumar policy->policy = new_policy->policy; 23202d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2321167a38dcSRafael J. Wysocki return cpufreq_driver->setpolicy(policy); 2322d9a789c7SRafael J. Wysocki } 2323d9a789c7SRafael J. Wysocki 23240a300767SRafael J. Wysocki if (new_policy->governor == policy->governor) { 23252bb4059eSRafael J. Wysocki pr_debug("governor limits update\n"); 2326a92604b4SRafael J. Wysocki cpufreq_governor_limits(policy); 2327d6ff44d6SRafael J. Wysocki return 0; 23280a300767SRafael J. Wysocki } 23291da177e4SLinus Torvalds 23302d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 23311da177e4SLinus Torvalds 2332d9a789c7SRafael J. Wysocki /* save old, working values */ 2333d9a789c7SRafael J. Wysocki old_gov = policy->governor; 23341da177e4SLinus Torvalds /* end old governor */ 2335d9a789c7SRafael J. Wysocki if (old_gov) { 233645482c70SRafael J. Wysocki cpufreq_stop_governor(policy); 233736be3418SRafael J. Wysocki cpufreq_exit_governor(policy); 23387bd353a9SViresh Kumar } 23391da177e4SLinus Torvalds 23401da177e4SLinus Torvalds /* start new governor */ 23413a3e9e06SViresh Kumar policy->governor = new_policy->governor; 2342a92604b4SRafael J. Wysocki ret = cpufreq_init_governor(policy); 23434bc384aeSViresh Kumar if (!ret) { 23440a300767SRafael J. Wysocki ret = cpufreq_start_governor(policy); 23450a300767SRafael J. Wysocki if (!ret) { 23462bb4059eSRafael J. Wysocki pr_debug("governor change\n"); 2347531b5c9fSQuentin Perret sched_cpufreq_governor_change(policy, old_gov); 23480a300767SRafael J. Wysocki return 0; 23490a300767SRafael J. Wysocki } 2350b7898fdaSRafael J. Wysocki cpufreq_exit_governor(policy); 2351955ef483SViresh Kumar } 23527bd353a9SViresh Kumar 23531da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2354d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 23551da177e4SLinus Torvalds if (old_gov) { 23563a3e9e06SViresh Kumar policy->governor = old_gov; 2357a92604b4SRafael J. Wysocki if (cpufreq_init_governor(policy)) 23584bc384aeSViresh Kumar policy->governor = NULL; 23594bc384aeSViresh Kumar else 23600a300767SRafael J. Wysocki cpufreq_start_governor(policy); 23611da177e4SLinus Torvalds } 23621da177e4SLinus Torvalds 23634bc384aeSViresh Kumar return ret; 23641da177e4SLinus Torvalds } 23651da177e4SLinus Torvalds 23661da177e4SLinus Torvalds /** 2367a0dbb819SRafael J. Wysocki * cpufreq_update_policy - Re-evaluate an existing cpufreq policy. 2368a0dbb819SRafael J. Wysocki * @cpu: CPU to re-evaluate the policy for. 23691da177e4SLinus Torvalds * 2370a0dbb819SRafael J. Wysocki * Update the current frequency for the cpufreq policy of @cpu and use 2371a0dbb819SRafael J. Wysocki * cpufreq_set_policy() to re-apply the min and max limits saved in the 2372a0dbb819SRafael J. Wysocki * user_policy sub-structure of that policy, which triggers the evaluation 2373a0dbb819SRafael J. Wysocki * of policy notifiers and the cpufreq driver's ->verify() callback for the 2374a0dbb819SRafael J. Wysocki * policy in question, among other things. 23751da177e4SLinus Torvalds */ 237630248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu) 23771da177e4SLinus Torvalds { 2378540a3758SRafael J. Wysocki struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu); 23793a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 23801da177e4SLinus Torvalds 2381fefa8ff8SAaron Plattner if (!policy) 238230248fefSRafael J. Wysocki return; 23831da177e4SLinus Torvalds 2384bb176f7dSViresh Kumar /* 2385bb176f7dSViresh Kumar * BIOS might change freq behind our back 2386bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2387bb176f7dSViresh Kumar */ 23885ddc6d4eSViresh Kumar if (cpufreq_driver->get && has_target() && 2389*5980752eSViresh Kumar (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false)))) 2390742c87bfSRafael J. Wysocki goto unlock; 239130248fefSRafael J. Wysocki 2392348a2ec5SRafael J. Wysocki pr_debug("updating policy for CPU %u\n", cpu); 2393348a2ec5SRafael J. Wysocki memcpy(&new_policy, policy, sizeof(*policy)); 2394348a2ec5SRafael J. Wysocki new_policy.min = policy->user_policy.min; 2395348a2ec5SRafael J. Wysocki new_policy.max = policy->user_policy.max; 23960961dd0dSThomas Renninger 239730248fefSRafael J. Wysocki cpufreq_set_policy(policy, &new_policy); 23981da177e4SLinus Torvalds 2399fefa8ff8SAaron Plattner unlock: 2400540a3758SRafael J. Wysocki cpufreq_cpu_release(policy); 24011da177e4SLinus Torvalds } 24021da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 24031da177e4SLinus Torvalds 24045a25e3f7SRafael J. Wysocki /** 24055a25e3f7SRafael J. Wysocki * cpufreq_update_limits - Update policy limits for a given CPU. 24065a25e3f7SRafael J. Wysocki * @cpu: CPU to update the policy limits for. 24075a25e3f7SRafael J. Wysocki * 24085a25e3f7SRafael J. Wysocki * Invoke the driver's ->update_limits callback if present or call 24095a25e3f7SRafael J. Wysocki * cpufreq_update_policy() for @cpu. 24105a25e3f7SRafael J. Wysocki */ 24115a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu) 24125a25e3f7SRafael J. Wysocki { 24135a25e3f7SRafael J. Wysocki if (cpufreq_driver->update_limits) 24145a25e3f7SRafael J. Wysocki cpufreq_driver->update_limits(cpu); 24155a25e3f7SRafael J. Wysocki else 24165a25e3f7SRafael J. Wysocki cpufreq_update_policy(cpu); 24175a25e3f7SRafael J. Wysocki } 24185a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits); 24195a25e3f7SRafael J. Wysocki 24201da177e4SLinus Torvalds /********************************************************************* 24216f19efc0SLukasz Majewski * BOOST * 24226f19efc0SLukasz Majewski *********************************************************************/ 24236f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 24246f19efc0SLukasz Majewski { 24256f19efc0SLukasz Majewski struct cpufreq_policy *policy; 24266f19efc0SLukasz Majewski int ret = -EINVAL; 24276f19efc0SLukasz Majewski 2428f963735aSViresh Kumar for_each_active_policy(policy) { 2429f8bfc116SViresh Kumar if (!policy->freq_table) 2430f8bfc116SViresh Kumar continue; 2431f8bfc116SViresh Kumar 24326f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 2433f8bfc116SViresh Kumar policy->freq_table); 24346f19efc0SLukasz Majewski if (ret) { 24356f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 24366f19efc0SLukasz Majewski __func__); 24376f19efc0SLukasz Majewski break; 24386f19efc0SLukasz Majewski } 243949f18560SViresh Kumar 244049f18560SViresh Kumar down_write(&policy->rwsem); 24416f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 2442a92604b4SRafael J. Wysocki cpufreq_governor_limits(policy); 244349f18560SViresh Kumar up_write(&policy->rwsem); 24446f19efc0SLukasz Majewski } 24456f19efc0SLukasz Majewski 24466f19efc0SLukasz Majewski return ret; 24476f19efc0SLukasz Majewski } 24486f19efc0SLukasz Majewski 24496f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 24506f19efc0SLukasz Majewski { 24516f19efc0SLukasz Majewski unsigned long flags; 24526f19efc0SLukasz Majewski int ret = 0; 24536f19efc0SLukasz Majewski 24546f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 24556f19efc0SLukasz Majewski return 0; 24566f19efc0SLukasz Majewski 24576f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 24586f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 24596f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24606f19efc0SLukasz Majewski 24616f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 24626f19efc0SLukasz Majewski if (ret) { 24636f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 24646f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 24656f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24666f19efc0SLukasz Majewski 2467e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2468e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 24696f19efc0SLukasz Majewski } 24706f19efc0SLukasz Majewski 24716f19efc0SLukasz Majewski return ret; 24726f19efc0SLukasz Majewski } 24736f19efc0SLukasz Majewski 247441669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void) 24756f19efc0SLukasz Majewski { 247689f98d7eSYue Hu return cpufreq_driver->set_boost; 24776f19efc0SLukasz Majewski } 24786f19efc0SLukasz Majewski 247944139ed4SViresh Kumar static int create_boost_sysfs_file(void) 248044139ed4SViresh Kumar { 248144139ed4SViresh Kumar int ret; 248244139ed4SViresh Kumar 2483c82bd444SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 248444139ed4SViresh Kumar if (ret) 248544139ed4SViresh Kumar pr_err("%s: cannot register global BOOST sysfs file\n", 248644139ed4SViresh Kumar __func__); 248744139ed4SViresh Kumar 248844139ed4SViresh Kumar return ret; 248944139ed4SViresh Kumar } 249044139ed4SViresh Kumar 249144139ed4SViresh Kumar static void remove_boost_sysfs_file(void) 249244139ed4SViresh Kumar { 249344139ed4SViresh Kumar if (cpufreq_boost_supported()) 2494c82bd444SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 249544139ed4SViresh Kumar } 249644139ed4SViresh Kumar 249744139ed4SViresh Kumar int cpufreq_enable_boost_support(void) 249844139ed4SViresh Kumar { 249944139ed4SViresh Kumar if (!cpufreq_driver) 250044139ed4SViresh Kumar return -EINVAL; 250144139ed4SViresh Kumar 250244139ed4SViresh Kumar if (cpufreq_boost_supported()) 250344139ed4SViresh Kumar return 0; 250444139ed4SViresh Kumar 25057a6c79f2SRafael J. Wysocki cpufreq_driver->set_boost = cpufreq_boost_set_sw; 250644139ed4SViresh Kumar 250744139ed4SViresh Kumar /* This will get removed on driver unregister */ 250844139ed4SViresh Kumar return create_boost_sysfs_file(); 250944139ed4SViresh Kumar } 251044139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 251144139ed4SViresh Kumar 25126f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 25136f19efc0SLukasz Majewski { 25146f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 25156f19efc0SLukasz Majewski } 25166f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 25176f19efc0SLukasz Majewski 25186f19efc0SLukasz Majewski /********************************************************************* 25191da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 25201da177e4SLinus Torvalds *********************************************************************/ 252127622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online; 25221da177e4SLinus Torvalds 2523c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu) 2524c4a3fa26SChen Yu { 2525c4a3fa26SChen Yu cpufreq_online(cpu); 2526c4a3fa26SChen Yu 2527c4a3fa26SChen Yu return 0; 2528c4a3fa26SChen Yu } 2529c4a3fa26SChen Yu 2530c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu) 2531c4a3fa26SChen Yu { 2532c4a3fa26SChen Yu cpufreq_offline(cpu); 2533c4a3fa26SChen Yu 2534c4a3fa26SChen Yu return 0; 2535c4a3fa26SChen Yu } 2536c4a3fa26SChen Yu 25371da177e4SLinus Torvalds /** 25381da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 25391da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 25401da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 25411da177e4SLinus Torvalds * 25421da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 254363af4055SEric Biggers * returns zero on success, -EEXIST when another driver got here first 25441da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 25451da177e4SLinus Torvalds * 25461da177e4SLinus Torvalds */ 2547221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 25481da177e4SLinus Torvalds { 25491da177e4SLinus Torvalds unsigned long flags; 25501da177e4SLinus Torvalds int ret; 25511da177e4SLinus Torvalds 2552a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2553a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2554a7b422cdSKonrad Rzeszutek Wilk 25551da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 25569c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 25579832235fSRafael J. Wysocki driver_data->target) || 25589832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 25591c03a2d0SViresh Kumar driver_data->target)) || 2560a9a22b57SViresh Kumar (!driver_data->get_intermediate != !driver_data->target_intermediate) || 256191a12e91SViresh Kumar (!driver_data->online != !driver_data->offline)) 25621da177e4SLinus Torvalds return -EINVAL; 25631da177e4SLinus Torvalds 25642d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 25651da177e4SLinus Torvalds 2566fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2567a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 2568fdd320daSRafael J. Wysocki 25690d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 25701c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 25710d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2572fdd320daSRafael J. Wysocki ret = -EEXIST; 2573fdd320daSRafael J. Wysocki goto out; 25741da177e4SLinus Torvalds } 25751c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 25760d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 25771da177e4SLinus Torvalds 2578bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2579bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2580bc68b7dfSViresh Kumar 25817a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 258244139ed4SViresh Kumar ret = create_boost_sysfs_file(); 258344139ed4SViresh Kumar if (ret) 25846f19efc0SLukasz Majewski goto err_null_driver; 25857a6c79f2SRafael J. Wysocki } 25866f19efc0SLukasz Majewski 25878a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 25888f5bc2abSJiri Slaby if (ret) 25896f19efc0SLukasz Majewski goto err_boost_unreg; 25901da177e4SLinus Torvalds 2591ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2592ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 25931da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 25946c770036SDavid Arcari ret = -ENODEV; 2595ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2596e08f5f5bSGautham R Shenoy driver_data->name); 25978a25a2fdSKay Sievers goto err_if_unreg; 25981da177e4SLinus Torvalds } 25991da177e4SLinus Torvalds 2600a92551e4SSebastian Andrzej Siewior ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, 2601a92551e4SSebastian Andrzej Siewior "cpufreq:online", 2602c4a3fa26SChen Yu cpuhp_cpufreq_online, 2603c4a3fa26SChen Yu cpuhp_cpufreq_offline); 260427622b06SSebastian Andrzej Siewior if (ret < 0) 260527622b06SSebastian Andrzej Siewior goto err_if_unreg; 260627622b06SSebastian Andrzej Siewior hp_online = ret; 26075372e054SSebastian Andrzej Siewior ret = 0; 260827622b06SSebastian Andrzej Siewior 26092d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 26103834abb4SPankaj Gupta goto out; 2611fdd320daSRafael J. Wysocki 26128a25a2fdSKay Sievers err_if_unreg: 26138a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 26146f19efc0SLukasz Majewski err_boost_unreg: 261544139ed4SViresh Kumar remove_boost_sysfs_file(); 26168f5bc2abSJiri Slaby err_null_driver: 26170d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 26181c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 26190d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 26203834abb4SPankaj Gupta out: 2621a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 26223834abb4SPankaj Gupta return ret; 26231da177e4SLinus Torvalds } 26241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds /** 26271da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 26281da177e4SLinus Torvalds * 26291da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 26301da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 26311da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 26321da177e4SLinus Torvalds * currently not initialised. 26331da177e4SLinus Torvalds */ 2634221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 26351da177e4SLinus Torvalds { 26361da177e4SLinus Torvalds unsigned long flags; 26371da177e4SLinus Torvalds 26381c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 26391da177e4SLinus Torvalds return -EINVAL; 26401da177e4SLinus Torvalds 26412d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 26421da177e4SLinus Torvalds 2643454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2644a92551e4SSebastian Andrzej Siewior cpus_read_lock(); 26458a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 264644139ed4SViresh Kumar remove_boost_sysfs_file(); 2647a92551e4SSebastian Andrzej Siewior cpuhp_remove_state_nocalls_cpuslocked(hp_online); 26481da177e4SLinus Torvalds 26490d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 26506eed9404SViresh Kumar 26511c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 26526eed9404SViresh Kumar 26530d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2654a92551e4SSebastian Andrzej Siewior cpus_read_unlock(); 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds return 0; 26571da177e4SLinus Torvalds } 26581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 26595a01f2e8SVenkatesh Pallipadi 266090de2a4aSDoug Anderson /* 266190de2a4aSDoug Anderson * Stop cpufreq at shutdown to make sure it isn't holding any locks 266290de2a4aSDoug Anderson * or mutexes when secondary CPUs are halted. 266390de2a4aSDoug Anderson */ 266490de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = { 266590de2a4aSDoug Anderson .shutdown = cpufreq_suspend, 266690de2a4aSDoug Anderson }; 266790de2a4aSDoug Anderson 2668c82bd444SViresh Kumar struct kobject *cpufreq_global_kobject; 2669c82bd444SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 2670c82bd444SViresh Kumar 26715a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 26725a01f2e8SVenkatesh Pallipadi { 2673a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2674a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2675a7b422cdSKonrad Rzeszutek Wilk 26768eec1020SViresh Kumar cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); 26778aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 26788aa84ad8SThomas Renninger 267990de2a4aSDoug Anderson register_syscore_ops(&cpufreq_syscore_ops); 268090de2a4aSDoug Anderson 26815a01f2e8SVenkatesh Pallipadi return 0; 26825a01f2e8SVenkatesh Pallipadi } 2683d82f2692SLen Brown module_param(off, int, 0444); 26845a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2685