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> 221da177e4SLinus Torvalds #include <linux/delay.h> 231da177e4SLinus Torvalds #include <linux/device.h> 245ff0a268SViresh Kumar #include <linux/init.h> 255ff0a268SViresh Kumar #include <linux/kernel_stat.h> 265ff0a268SViresh Kumar #include <linux/module.h> 273fc54d37Sakpm@osdl.org #include <linux/mutex.h> 285ff0a268SViresh Kumar #include <linux/slab.h> 292f0aea93SViresh Kumar #include <linux/suspend.h> 3090de2a4aSDoug Anderson #include <linux/syscore_ops.h> 315ff0a268SViresh Kumar #include <linux/tick.h> 326f4f2723SThomas Renninger #include <trace/events/power.h> 336f4f2723SThomas Renninger 34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list); 35f963735aSViresh Kumar 36f963735aSViresh Kumar static inline bool policy_is_inactive(struct cpufreq_policy *policy) 37f963735aSViresh Kumar { 38f963735aSViresh Kumar return cpumask_empty(policy->cpus); 39f963735aSViresh Kumar } 40f963735aSViresh Kumar 41f963735aSViresh Kumar static bool suitable_policy(struct cpufreq_policy *policy, bool active) 42f963735aSViresh Kumar { 43f963735aSViresh Kumar return active == !policy_is_inactive(policy); 44f963735aSViresh Kumar } 45f963735aSViresh Kumar 46f963735aSViresh Kumar /* Finds Next Acive/Inactive policy */ 47f963735aSViresh Kumar static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy, 48f963735aSViresh Kumar bool active) 49f963735aSViresh Kumar { 50f963735aSViresh Kumar do { 51f963735aSViresh Kumar /* No more policies in the list */ 522dadfd75SGautham R Shenoy if (list_is_last(&policy->policy_list, &cpufreq_policy_list)) 53f963735aSViresh Kumar return NULL; 542dadfd75SGautham R Shenoy 552dadfd75SGautham R Shenoy policy = list_next_entry(policy, policy_list); 56f963735aSViresh Kumar } while (!suitable_policy(policy, active)); 57f963735aSViresh Kumar 58f963735aSViresh Kumar return policy; 59f963735aSViresh Kumar } 60f963735aSViresh Kumar 61f963735aSViresh Kumar static struct cpufreq_policy *first_policy(bool active) 62f963735aSViresh Kumar { 63f963735aSViresh Kumar struct cpufreq_policy *policy; 64f963735aSViresh Kumar 65f963735aSViresh Kumar /* No policies in the list */ 66f963735aSViresh Kumar if (list_empty(&cpufreq_policy_list)) 67f963735aSViresh Kumar return NULL; 68f963735aSViresh Kumar 69f963735aSViresh Kumar policy = list_first_entry(&cpufreq_policy_list, typeof(*policy), 70f963735aSViresh Kumar policy_list); 71f963735aSViresh Kumar 72f963735aSViresh Kumar if (!suitable_policy(policy, active)) 73f963735aSViresh Kumar policy = next_policy(policy, active); 74f963735aSViresh Kumar 75f963735aSViresh Kumar return policy; 76f963735aSViresh Kumar } 77f963735aSViresh Kumar 78f963735aSViresh Kumar /* Macros to iterate over CPU policies */ 79f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active) \ 80f963735aSViresh Kumar for (__policy = first_policy(__active); \ 81f963735aSViresh Kumar __policy; \ 82f963735aSViresh Kumar __policy = next_policy(__policy, __active)) 83f963735aSViresh Kumar 84f963735aSViresh Kumar #define for_each_active_policy(__policy) \ 85f963735aSViresh Kumar for_each_suitable_policy(__policy, true) 86f963735aSViresh Kumar #define for_each_inactive_policy(__policy) \ 87f963735aSViresh Kumar for_each_suitable_policy(__policy, false) 88f963735aSViresh Kumar 89b4f0676fSViresh Kumar #define for_each_policy(__policy) \ 90b4f0676fSViresh Kumar list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) 91b4f0676fSViresh Kumar 92f7b27061SViresh Kumar /* Iterate over governors */ 93f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 94f7b27061SViresh Kumar #define for_each_governor(__governor) \ 95f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 96f7b27061SViresh Kumar 971da177e4SLinus Torvalds /** 98cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 991da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 1001da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 1011da177e4SLinus Torvalds */ 1021c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 1037a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 104bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 10534e2c555SRafael J. Wysocki 10634e2c555SRafael J. Wysocki static DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); 10734e2c555SRafael J. Wysocki 10834e2c555SRafael J. Wysocki /** 10934e2c555SRafael J. Wysocki * cpufreq_set_update_util_data - Populate the CPU's update_util_data pointer. 11034e2c555SRafael J. Wysocki * @cpu: The CPU to set the pointer for. 11134e2c555SRafael J. Wysocki * @data: New pointer value. 11234e2c555SRafael J. Wysocki * 11334e2c555SRafael J. Wysocki * Set and publish the update_util_data pointer for the given CPU. That pointer 11434e2c555SRafael J. Wysocki * points to a struct update_util_data object containing a callback function 11534e2c555SRafael J. Wysocki * to call from cpufreq_update_util(). That function will be called from an RCU 11634e2c555SRafael J. Wysocki * read-side critical section, so it must not sleep. 11734e2c555SRafael J. Wysocki * 11834e2c555SRafael J. Wysocki * Callers must use RCU callbacks to free any memory that might be accessed 11934e2c555SRafael J. Wysocki * via the old update_util_data pointer or invoke synchronize_rcu() right after 12034e2c555SRafael J. Wysocki * this function to avoid use-after-free. 12134e2c555SRafael J. Wysocki */ 12234e2c555SRafael J. Wysocki void cpufreq_set_update_util_data(int cpu, struct update_util_data *data) 12334e2c555SRafael J. Wysocki { 12434e2c555SRafael J. Wysocki rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data); 12534e2c555SRafael J. Wysocki } 12634e2c555SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data); 12734e2c555SRafael J. Wysocki 12834e2c555SRafael J. Wysocki /** 12934e2c555SRafael J. Wysocki * cpufreq_update_util - Take a note about CPU utilization changes. 13034e2c555SRafael J. Wysocki * @time: Current time. 13134e2c555SRafael J. Wysocki * @util: Current utilization. 13234e2c555SRafael J. Wysocki * @max: Utilization ceiling. 13334e2c555SRafael J. Wysocki * 13434e2c555SRafael J. Wysocki * This function is called by the scheduler on every invocation of 13534e2c555SRafael J. Wysocki * update_load_avg() on the CPU whose utilization is being updated. 13634e2c555SRafael J. Wysocki */ 13734e2c555SRafael J. Wysocki void cpufreq_update_util(u64 time, unsigned long util, unsigned long max) 13834e2c555SRafael J. Wysocki { 13934e2c555SRafael J. Wysocki struct update_util_data *data; 14034e2c555SRafael J. Wysocki 14134e2c555SRafael J. Wysocki rcu_read_lock(); 14234e2c555SRafael J. Wysocki 14334e2c555SRafael J. Wysocki data = rcu_dereference(*this_cpu_ptr(&cpufreq_update_util_data)); 14434e2c555SRafael J. Wysocki if (data && data->func) 14534e2c555SRafael J. Wysocki data->func(data, time, util, max); 14634e2c555SRafael J. Wysocki 14734e2c555SRafael J. Wysocki rcu_read_unlock(); 14834e2c555SRafael J. Wysocki } 14934e2c555SRafael J. Wysocki 1502f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 1512f0aea93SViresh Kumar static bool cpufreq_suspended; 1521da177e4SLinus Torvalds 1539c0ebcf7SViresh Kumar static inline bool has_target(void) 1549c0ebcf7SViresh Kumar { 1559c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 1569c0ebcf7SViresh Kumar } 1579c0ebcf7SViresh Kumar 1581da177e4SLinus Torvalds /* internal prototypes */ 159*a1317e09SViresh Kumar static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); 160d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 1611da177e4SLinus Torvalds 1621da177e4SLinus Torvalds /** 1631da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 1641da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 1651da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 1661da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 1671da177e4SLinus Torvalds * The mutex locks both lists. 1681da177e4SLinus Torvalds */ 169e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 170b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 1711da177e4SLinus Torvalds 17274212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 173b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 174b4dfdbb3SAlan Stern { 175b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 17674212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 177b4dfdbb3SAlan Stern return 0; 178b4dfdbb3SAlan Stern } 179b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1801da177e4SLinus Torvalds 181a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 182da584455SViresh Kumar static int cpufreq_disabled(void) 183a7b422cdSKonrad Rzeszutek Wilk { 184a7b422cdSKonrad Rzeszutek Wilk return off; 185a7b422cdSKonrad Rzeszutek Wilk } 186a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 187a7b422cdSKonrad Rzeszutek Wilk { 188a7b422cdSKonrad Rzeszutek Wilk off = 1; 189a7b422cdSKonrad Rzeszutek Wilk } 1903fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1911da177e4SLinus Torvalds 1924d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1934d5dcc42SViresh Kumar { 1940b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1954d5dcc42SViresh Kumar } 1963f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1974d5dcc42SViresh Kumar 198944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 199944e9a03SViresh Kumar { 200944e9a03SViresh Kumar if (have_governor_per_policy()) 201944e9a03SViresh Kumar return &policy->kobj; 202944e9a03SViresh Kumar else 203944e9a03SViresh Kumar return cpufreq_global_kobject; 204944e9a03SViresh Kumar } 205944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 206944e9a03SViresh Kumar 2075a31d594SViresh Kumar struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) 2085a31d594SViresh Kumar { 2095a31d594SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 2105a31d594SViresh Kumar 2115a31d594SViresh Kumar return policy && !policy_is_inactive(policy) ? 2125a31d594SViresh Kumar policy->freq_table : NULL; 2135a31d594SViresh Kumar } 2145a31d594SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); 2155a31d594SViresh Kumar 21672a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 21772a4ce34SViresh Kumar { 21872a4ce34SViresh Kumar u64 idle_time; 21972a4ce34SViresh Kumar u64 cur_wall_time; 22072a4ce34SViresh Kumar u64 busy_time; 22172a4ce34SViresh Kumar 22272a4ce34SViresh Kumar cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); 22372a4ce34SViresh Kumar 22472a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 22572a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 22672a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 22772a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 22872a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 22972a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 23072a4ce34SViresh Kumar 23172a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 23272a4ce34SViresh Kumar if (wall) 23372a4ce34SViresh Kumar *wall = cputime_to_usecs(cur_wall_time); 23472a4ce34SViresh Kumar 23572a4ce34SViresh Kumar return cputime_to_usecs(idle_time); 23672a4ce34SViresh Kumar } 23772a4ce34SViresh Kumar 23872a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 23972a4ce34SViresh Kumar { 24072a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 24172a4ce34SViresh Kumar 24272a4ce34SViresh Kumar if (idle_time == -1ULL) 24372a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 24472a4ce34SViresh Kumar else if (!io_busy) 24572a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 24672a4ce34SViresh Kumar 24772a4ce34SViresh Kumar return idle_time; 24872a4ce34SViresh Kumar } 24972a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 25072a4ce34SViresh Kumar 25170e9e778SViresh Kumar /* 25270e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 25370e9e778SViresh Kumar * drivers of SMP systems. It will do following: 25470e9e778SViresh Kumar * - validate & show freq table passed 25570e9e778SViresh Kumar * - set policies transition latency 25670e9e778SViresh Kumar * - policy->cpus with all possible CPUs 25770e9e778SViresh Kumar */ 25870e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy, 25970e9e778SViresh Kumar struct cpufreq_frequency_table *table, 26070e9e778SViresh Kumar unsigned int transition_latency) 26170e9e778SViresh Kumar { 26270e9e778SViresh Kumar int ret; 26370e9e778SViresh Kumar 26470e9e778SViresh Kumar ret = cpufreq_table_validate_and_show(policy, table); 26570e9e778SViresh Kumar if (ret) { 26670e9e778SViresh Kumar pr_err("%s: invalid frequency table: %d\n", __func__, ret); 26770e9e778SViresh Kumar return ret; 26870e9e778SViresh Kumar } 26970e9e778SViresh Kumar 27070e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 27170e9e778SViresh Kumar 27270e9e778SViresh Kumar /* 27358405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 27470e9e778SViresh Kumar * share the clock and voltage and clock. 27570e9e778SViresh Kumar */ 27670e9e778SViresh Kumar cpumask_setall(policy->cpus); 27770e9e778SViresh Kumar 27870e9e778SViresh Kumar return 0; 27970e9e778SViresh Kumar } 28070e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 28170e9e778SViresh Kumar 2821f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 283652ed95dSViresh Kumar { 284652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 285652ed95dSViresh Kumar 286988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 287988bed09SViresh Kumar } 2881f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); 289988bed09SViresh Kumar 290988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 291988bed09SViresh Kumar { 292988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 293988bed09SViresh Kumar 294652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 295e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 296e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 297652ed95dSViresh Kumar return 0; 298652ed95dSViresh Kumar } 299652ed95dSViresh Kumar 300652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 301652ed95dSViresh Kumar } 302652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 303652ed95dSViresh Kumar 30450e9c852SViresh Kumar /** 30550e9c852SViresh Kumar * cpufreq_cpu_get: returns policy for a cpu and marks it busy. 30650e9c852SViresh Kumar * 30750e9c852SViresh Kumar * @cpu: cpu to find policy for. 30850e9c852SViresh Kumar * 30950e9c852SViresh Kumar * This returns policy for 'cpu', returns NULL if it doesn't exist. 31050e9c852SViresh Kumar * It also increments the kobject reference count to mark it busy and so would 31150e9c852SViresh Kumar * require a corresponding call to cpufreq_cpu_put() to decrement it back. 31250e9c852SViresh Kumar * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be 31350e9c852SViresh Kumar * freed as that depends on the kobj count. 31450e9c852SViresh Kumar * 31550e9c852SViresh Kumar * Return: A valid policy on success, otherwise NULL on failure. 31650e9c852SViresh Kumar */ 3176eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 3181da177e4SLinus Torvalds { 3196eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 3201da177e4SLinus Torvalds unsigned long flags; 3211da177e4SLinus Torvalds 3221b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 3236eed9404SViresh Kumar return NULL; 3246eed9404SViresh Kumar 3251da177e4SLinus Torvalds /* get the cpufreq driver */ 3260d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 3271da177e4SLinus Torvalds 3286eed9404SViresh Kumar if (cpufreq_driver) { 3291da177e4SLinus Torvalds /* get the CPU */ 330988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 3316eed9404SViresh Kumar if (policy) 3326eed9404SViresh Kumar kobject_get(&policy->kobj); 3336eed9404SViresh Kumar } 3346eed9404SViresh Kumar 3356eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 3361da177e4SLinus Torvalds 3373a3e9e06SViresh Kumar return policy; 338a9144436SStephen Boyd } 3391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 3401da177e4SLinus Torvalds 34150e9c852SViresh Kumar /** 34250e9c852SViresh Kumar * cpufreq_cpu_put: Decrements the usage count of a policy 34350e9c852SViresh Kumar * 34450e9c852SViresh Kumar * @policy: policy earlier returned by cpufreq_cpu_get(). 34550e9c852SViresh Kumar * 34650e9c852SViresh Kumar * This decrements the kobject reference count incremented earlier by calling 34750e9c852SViresh Kumar * cpufreq_cpu_get(). 34850e9c852SViresh Kumar */ 3493a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 350a9144436SStephen Boyd { 3516eed9404SViresh Kumar kobject_put(&policy->kobj); 352a9144436SStephen Boyd } 3531da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 3541da177e4SLinus Torvalds 3551da177e4SLinus Torvalds /********************************************************************* 3561da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 3571da177e4SLinus Torvalds *********************************************************************/ 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /** 3601da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 3611da177e4SLinus Torvalds * 3621da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 3631da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 3641da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 3651da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 3661da177e4SLinus Torvalds */ 36739c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 36839c132eeSViresh Kumar { 3691da177e4SLinus Torvalds #ifndef CONFIG_SMP 3701da177e4SLinus Torvalds static unsigned long l_p_j_ref; 3711da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 3741da177e4SLinus Torvalds return; 3751da177e4SLinus Torvalds 3761da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 3771da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 3781da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 379e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 380e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 3811da177e4SLinus Torvalds } 3820b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 383e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 384e08f5f5bSGautham R Shenoy ci->new); 385e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 386e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3871da177e4SLinus Torvalds } 3881da177e4SLinus Torvalds #endif 38939c132eeSViresh Kumar } 3901da177e4SLinus Torvalds 3910956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 392b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3951da177e4SLinus Torvalds 396d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 397d5aaffa9SDirk Brandewie return; 398d5aaffa9SDirk Brandewie 3991c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 4002d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 401e4472cb3SDave Jones state, freqs->new); 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds switch (state) { 404e4472cb3SDave Jones 4051da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 406e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 407e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 408e4472cb3SDave Jones * "old frequency". 4091da177e4SLinus Torvalds */ 4101c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 411e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 412e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 413e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 414e4472cb3SDave Jones freqs->old, policy->cur); 415e4472cb3SDave Jones freqs->old = policy->cur; 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds } 418b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 419e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 4201da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 4211da177e4SLinus Torvalds break; 422e4472cb3SDave Jones 4231da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 4241da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 425e837f9b5SJoe Perches pr_debug("FREQ: %lu - CPU: %lu\n", 426e837f9b5SJoe Perches (unsigned long)freqs->new, (unsigned long)freqs->cpu); 42725e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 428b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 429e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 430e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 431e4472cb3SDave Jones policy->cur = freqs->new; 4321da177e4SLinus Torvalds break; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds } 435bb176f7dSViresh Kumar 436b43a7ffbSViresh Kumar /** 437b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 438b43a7ffbSViresh Kumar * on frequency transition. 439b43a7ffbSViresh Kumar * 440b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 441b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 442b43a7ffbSViresh Kumar * external effects. 443b43a7ffbSViresh Kumar */ 444236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 445b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 446b43a7ffbSViresh Kumar { 447b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 448b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 449b43a7ffbSViresh Kumar } 4501da177e4SLinus Torvalds 451f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 452236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 453f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 454f7ba3b41SViresh Kumar { 455f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 456f7ba3b41SViresh Kumar if (!transition_failed) 457f7ba3b41SViresh Kumar return; 458f7ba3b41SViresh Kumar 459f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 460f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 461f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 462f7ba3b41SViresh Kumar } 463f7ba3b41SViresh Kumar 46412478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 46512478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 46612478cf0SSrivatsa S. Bhat { 467ca654dc3SSrivatsa S. Bhat 468ca654dc3SSrivatsa S. Bhat /* 469ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 470ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 471ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 472ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 473ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 474ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 475ca654dc3SSrivatsa S. Bhat */ 476ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 477ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 478ca654dc3SSrivatsa S. Bhat 47912478cf0SSrivatsa S. Bhat wait: 48012478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 48112478cf0SSrivatsa S. Bhat 48212478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 48312478cf0SSrivatsa S. Bhat 48412478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 48512478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 48612478cf0SSrivatsa S. Bhat goto wait; 48712478cf0SSrivatsa S. Bhat } 48812478cf0SSrivatsa S. Bhat 48912478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 490ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 49112478cf0SSrivatsa S. Bhat 49212478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 49312478cf0SSrivatsa S. Bhat 49412478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 49512478cf0SSrivatsa S. Bhat } 49612478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 49712478cf0SSrivatsa S. Bhat 49812478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 49912478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 50012478cf0SSrivatsa S. Bhat { 50112478cf0SSrivatsa S. Bhat if (unlikely(WARN_ON(!policy->transition_ongoing))) 50212478cf0SSrivatsa S. Bhat return; 50312478cf0SSrivatsa S. Bhat 50412478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 50512478cf0SSrivatsa S. Bhat 50612478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 507ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 50812478cf0SSrivatsa S. Bhat 50912478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 51012478cf0SSrivatsa S. Bhat } 51112478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 51212478cf0SSrivatsa S. Bhat 5131da177e4SLinus Torvalds 5141da177e4SLinus Torvalds /********************************************************************* 5151da177e4SLinus Torvalds * SYSFS INTERFACE * 5161da177e4SLinus Torvalds *********************************************************************/ 5178a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 5186f19efc0SLukasz Majewski struct attribute *attr, char *buf) 5196f19efc0SLukasz Majewski { 5206f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 5216f19efc0SLukasz Majewski } 5226f19efc0SLukasz Majewski 5236f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, 5246f19efc0SLukasz Majewski const char *buf, size_t count) 5256f19efc0SLukasz Majewski { 5266f19efc0SLukasz Majewski int ret, enable; 5276f19efc0SLukasz Majewski 5286f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 5296f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 5306f19efc0SLukasz Majewski return -EINVAL; 5316f19efc0SLukasz Majewski 5326f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 533e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 534e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 5356f19efc0SLukasz Majewski return -EINVAL; 5366f19efc0SLukasz Majewski } 5376f19efc0SLukasz Majewski 538e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 539e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 5406f19efc0SLukasz Majewski 5416f19efc0SLukasz Majewski return count; 5426f19efc0SLukasz Majewski } 5436f19efc0SLukasz Majewski define_one_global_rw(boost); 5441da177e4SLinus Torvalds 54542f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 5463bcb09a3SJeremy Fitzhardinge { 5473bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 5483bcb09a3SJeremy Fitzhardinge 549f7b27061SViresh Kumar for_each_governor(t) 5507c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 5513bcb09a3SJeremy Fitzhardinge return t; 5523bcb09a3SJeremy Fitzhardinge 5533bcb09a3SJeremy Fitzhardinge return NULL; 5543bcb09a3SJeremy Fitzhardinge } 5553bcb09a3SJeremy Fitzhardinge 5561da177e4SLinus Torvalds /** 5571da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 5581da177e4SLinus Torvalds */ 5591da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 5601da177e4SLinus Torvalds struct cpufreq_governor **governor) 5611da177e4SLinus Torvalds { 5623bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 5633bcb09a3SJeremy Fitzhardinge 5641c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 5657c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 5661da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 5673bcb09a3SJeremy Fitzhardinge err = 0; 5687c4f4539SRasmus Villemoes } else if (!strncasecmp(str_governor, "powersave", 569e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 5701da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 5713bcb09a3SJeremy Fitzhardinge err = 0; 5721da177e4SLinus Torvalds } 5732e1cc3a5SViresh Kumar } else { 5741da177e4SLinus Torvalds struct cpufreq_governor *t; 5753bcb09a3SJeremy Fitzhardinge 5763fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 5773bcb09a3SJeremy Fitzhardinge 57842f91fa1SViresh Kumar t = find_governor(str_governor); 5793bcb09a3SJeremy Fitzhardinge 580ea714970SJeremy Fitzhardinge if (t == NULL) { 581ea714970SJeremy Fitzhardinge int ret; 582ea714970SJeremy Fitzhardinge 583ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5841a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 585ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 586ea714970SJeremy Fitzhardinge 587ea714970SJeremy Fitzhardinge if (ret == 0) 58842f91fa1SViresh Kumar t = find_governor(str_governor); 589ea714970SJeremy Fitzhardinge } 590ea714970SJeremy Fitzhardinge 5913bcb09a3SJeremy Fitzhardinge if (t != NULL) { 5921da177e4SLinus Torvalds *governor = t; 5933bcb09a3SJeremy Fitzhardinge err = 0; 5941da177e4SLinus Torvalds } 5953bcb09a3SJeremy Fitzhardinge 5963bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5971da177e4SLinus Torvalds } 5983bcb09a3SJeremy Fitzhardinge return err; 5991da177e4SLinus Torvalds } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /** 602e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 603e08f5f5bSGautham R Shenoy * print out cpufreq information 6041da177e4SLinus Torvalds * 6051da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 6061da177e4SLinus Torvalds * "unsigned int". 6071da177e4SLinus Torvalds */ 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds #define show_one(file_name, object) \ 6101da177e4SLinus Torvalds static ssize_t show_##file_name \ 6111da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 6121da177e4SLinus Torvalds { \ 6131da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 6141da177e4SLinus Torvalds } 6151da177e4SLinus Torvalds 6161da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 6171da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 618ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 6191da177e4SLinus Torvalds show_one(scaling_min_freq, min); 6201da177e4SLinus Torvalds show_one(scaling_max_freq, max); 621c034b02eSDirk Brandewie 62209347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 623c034b02eSDirk Brandewie { 624c034b02eSDirk Brandewie ssize_t ret; 625c034b02eSDirk Brandewie 626c034b02eSDirk Brandewie if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 627c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 628c034b02eSDirk Brandewie else 629c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 630c034b02eSDirk Brandewie return ret; 631c034b02eSDirk Brandewie } 6321da177e4SLinus Torvalds 633037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 6343a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 6357970e08bSThomas Renninger 6361da177e4SLinus Torvalds /** 6371da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 6381da177e4SLinus Torvalds */ 6391da177e4SLinus Torvalds #define store_one(file_name, object) \ 6401da177e4SLinus Torvalds static ssize_t store_##file_name \ 6411da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 6421da177e4SLinus Torvalds { \ 643619c144cSVince Hsu int ret, temp; \ 6441da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 6451da177e4SLinus Torvalds \ 6468fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); \ 6471da177e4SLinus Torvalds \ 6481da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 6491da177e4SLinus Torvalds if (ret != 1) \ 6501da177e4SLinus Torvalds return -EINVAL; \ 6511da177e4SLinus Torvalds \ 652619c144cSVince Hsu temp = new_policy.object; \ 653037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 654619c144cSVince Hsu if (!ret) \ 655619c144cSVince Hsu policy->user_policy.object = temp; \ 6561da177e4SLinus Torvalds \ 6571da177e4SLinus Torvalds return ret ? ret : count; \ 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds store_one(scaling_min_freq, min); 6611da177e4SLinus Torvalds store_one(scaling_max_freq, max); 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds /** 6641da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 6651da177e4SLinus Torvalds */ 666e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 667e08f5f5bSGautham R Shenoy char *buf) 6681da177e4SLinus Torvalds { 669d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 6701da177e4SLinus Torvalds if (!cur_freq) 6711da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 6721da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds /** 6761da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 6771da177e4SLinus Torvalds */ 678905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 6791da177e4SLinus Torvalds { 6801da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 6811da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 6821da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 6831da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 6841da177e4SLinus Torvalds else if (policy->governor) 6854b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 68629464f28SDave Jones policy->governor->name); 6871da177e4SLinus Torvalds return -EINVAL; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds /** 6911da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 6921da177e4SLinus Torvalds */ 6931da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 6941da177e4SLinus Torvalds const char *buf, size_t count) 6951da177e4SLinus Torvalds { 6965136fa56SSrivatsa S. Bhat int ret; 6971da177e4SLinus Torvalds char str_governor[16]; 6981da177e4SLinus Torvalds struct cpufreq_policy new_policy; 6991da177e4SLinus Torvalds 7008fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 7031da177e4SLinus Torvalds if (ret != 1) 7041da177e4SLinus Torvalds return -EINVAL; 7051da177e4SLinus Torvalds 706e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 707e08f5f5bSGautham R Shenoy &new_policy.governor)) 7081da177e4SLinus Torvalds return -EINVAL; 7091da177e4SLinus Torvalds 710037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 71188dc4384SViresh Kumar return ret ? ret : count; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds /** 7151da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 7161da177e4SLinus Torvalds */ 7171da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 7181da177e4SLinus Torvalds { 7191c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 7201da177e4SLinus Torvalds } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds /** 7231da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 7241da177e4SLinus Torvalds */ 7251da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 7261da177e4SLinus Torvalds char *buf) 7271da177e4SLinus Torvalds { 7281da177e4SLinus Torvalds ssize_t i = 0; 7291da177e4SLinus Torvalds struct cpufreq_governor *t; 7301da177e4SLinus Torvalds 7319c0ebcf7SViresh Kumar if (!has_target()) { 7321da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 7331da177e4SLinus Torvalds goto out; 7341da177e4SLinus Torvalds } 7351da177e4SLinus Torvalds 736f7b27061SViresh Kumar for_each_governor(t) { 73729464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 73829464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 7391da177e4SLinus Torvalds goto out; 7404b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds out: 7431da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 7441da177e4SLinus Torvalds return i; 7451da177e4SLinus Torvalds } 746e8628dd0SDarrick J. Wong 747f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 7481da177e4SLinus Torvalds { 7491da177e4SLinus Torvalds ssize_t i = 0; 7501da177e4SLinus Torvalds unsigned int cpu; 7511da177e4SLinus Torvalds 752835481d9SRusty Russell for_each_cpu(cpu, mask) { 7531da177e4SLinus Torvalds if (i) 7541da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 7551da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 7561da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 7571da177e4SLinus Torvalds break; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 7601da177e4SLinus Torvalds return i; 7611da177e4SLinus Torvalds } 762f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 7631da177e4SLinus Torvalds 764e8628dd0SDarrick J. Wong /** 765e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 766e8628dd0SDarrick J. Wong * hw coordination is in use 767e8628dd0SDarrick J. Wong */ 768e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 769e8628dd0SDarrick J. Wong { 770f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 771e8628dd0SDarrick J. Wong } 772e8628dd0SDarrick J. Wong 773e8628dd0SDarrick J. Wong /** 774e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 775e8628dd0SDarrick J. Wong */ 776e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 777e8628dd0SDarrick J. Wong { 778f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 779e8628dd0SDarrick J. Wong } 780e8628dd0SDarrick J. Wong 7819e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 7829e76988eSVenki Pallipadi const char *buf, size_t count) 7839e76988eSVenki Pallipadi { 7849e76988eSVenki Pallipadi unsigned int freq = 0; 7859e76988eSVenki Pallipadi unsigned int ret; 7869e76988eSVenki Pallipadi 787879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 7889e76988eSVenki Pallipadi return -EINVAL; 7899e76988eSVenki Pallipadi 7909e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 7919e76988eSVenki Pallipadi if (ret != 1) 7929e76988eSVenki Pallipadi return -EINVAL; 7939e76988eSVenki Pallipadi 7949e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 7959e76988eSVenki Pallipadi 7969e76988eSVenki Pallipadi return count; 7979e76988eSVenki Pallipadi } 7989e76988eSVenki Pallipadi 7999e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 8009e76988eSVenki Pallipadi { 801879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 8029e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 8039e76988eSVenki Pallipadi 8049e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 8059e76988eSVenki Pallipadi } 8061da177e4SLinus Torvalds 807e2f74f35SThomas Renninger /** 8088bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 809e2f74f35SThomas Renninger */ 810e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 811e2f74f35SThomas Renninger { 812e2f74f35SThomas Renninger unsigned int limit; 813e2f74f35SThomas Renninger int ret; 8141c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 8151c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 816e2f74f35SThomas Renninger if (!ret) 817e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 818e2f74f35SThomas Renninger } 819e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 820e2f74f35SThomas Renninger } 821e2f74f35SThomas Renninger 8226dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 8236dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 8246dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 8256dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 8266dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 8276dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 8286dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 8296dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 8306dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 8316dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 8326dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 8336dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 8346dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 8356dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 8381da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 8391da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 840ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 8411da177e4SLinus Torvalds &scaling_min_freq.attr, 8421da177e4SLinus Torvalds &scaling_max_freq.attr, 8431da177e4SLinus Torvalds &affected_cpus.attr, 844e8628dd0SDarrick J. Wong &related_cpus.attr, 8451da177e4SLinus Torvalds &scaling_governor.attr, 8461da177e4SLinus Torvalds &scaling_driver.attr, 8471da177e4SLinus Torvalds &scaling_available_governors.attr, 8489e76988eSVenki Pallipadi &scaling_setspeed.attr, 8491da177e4SLinus Torvalds NULL 8501da177e4SLinus Torvalds }; 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 8531da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 8561da177e4SLinus Torvalds { 8571da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8581da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 8591b750e3bSViresh Kumar ssize_t ret; 8606eed9404SViresh Kumar 861ad7722daSviresh kumar down_read(&policy->rwsem); 8625a01f2e8SVenkatesh Pallipadi 863e08f5f5bSGautham R Shenoy if (fattr->show) 864e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 865e08f5f5bSGautham R Shenoy else 866e08f5f5bSGautham R Shenoy ret = -EIO; 867e08f5f5bSGautham R Shenoy 868ad7722daSviresh kumar up_read(&policy->rwsem); 8691b750e3bSViresh Kumar 8701da177e4SLinus Torvalds return ret; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 8741da177e4SLinus Torvalds const char *buf, size_t count) 8751da177e4SLinus Torvalds { 8761da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8771da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 878a07530b4SDave Jones ssize_t ret = -EINVAL; 8796eed9404SViresh Kumar 8804f750c93SSrivatsa S. Bhat get_online_cpus(); 8814f750c93SSrivatsa S. Bhat 8824f750c93SSrivatsa S. Bhat if (!cpu_online(policy->cpu)) 8834f750c93SSrivatsa S. Bhat goto unlock; 8844f750c93SSrivatsa S. Bhat 885ad7722daSviresh kumar down_write(&policy->rwsem); 8865a01f2e8SVenkatesh Pallipadi 887e08f5f5bSGautham R Shenoy if (fattr->store) 888e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 889e08f5f5bSGautham R Shenoy else 890e08f5f5bSGautham R Shenoy ret = -EIO; 891e08f5f5bSGautham R Shenoy 892ad7722daSviresh kumar up_write(&policy->rwsem); 8934f750c93SSrivatsa S. Bhat unlock: 8944f750c93SSrivatsa S. Bhat put_online_cpus(); 8954f750c93SSrivatsa S. Bhat 8961da177e4SLinus Torvalds return ret; 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 9001da177e4SLinus Torvalds { 9011da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 9022d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 9031da177e4SLinus Torvalds complete(&policy->kobj_unregister); 9041da177e4SLinus Torvalds } 9051da177e4SLinus Torvalds 90652cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 9071da177e4SLinus Torvalds .show = show, 9081da177e4SLinus Torvalds .store = store, 9091da177e4SLinus Torvalds }; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 9121da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 9131da177e4SLinus Torvalds .default_attrs = default_attrs, 9141da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 9151da177e4SLinus Torvalds }; 9161da177e4SLinus Torvalds 91787549141SViresh Kumar static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu) 91887549141SViresh Kumar { 91987549141SViresh Kumar struct device *cpu_dev; 92087549141SViresh Kumar 92187549141SViresh Kumar pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu); 92287549141SViresh Kumar 92387549141SViresh Kumar if (!policy) 92487549141SViresh Kumar return 0; 92587549141SViresh Kumar 92687549141SViresh Kumar cpu_dev = get_cpu_device(cpu); 92787549141SViresh Kumar if (WARN_ON(!cpu_dev)) 92887549141SViresh Kumar return 0; 92987549141SViresh Kumar 93087549141SViresh Kumar return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq"); 93187549141SViresh Kumar } 93287549141SViresh Kumar 93387549141SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu) 93487549141SViresh Kumar { 93587549141SViresh Kumar struct device *cpu_dev; 93687549141SViresh Kumar 93787549141SViresh Kumar pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu); 93887549141SViresh Kumar 93987549141SViresh Kumar cpu_dev = get_cpu_device(cpu); 94087549141SViresh Kumar if (WARN_ON(!cpu_dev)) 94187549141SViresh Kumar return; 94287549141SViresh Kumar 94387549141SViresh Kumar sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 94487549141SViresh Kumar } 94587549141SViresh Kumar 94687549141SViresh Kumar /* Add/remove symlinks for all related CPUs */ 947308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 94819d6f7ecSDave Jones { 94919d6f7ecSDave Jones unsigned int j; 95019d6f7ecSDave Jones int ret = 0; 95119d6f7ecSDave Jones 95287549141SViresh Kumar /* Some related CPUs might not be present (physically hotplugged) */ 953559ed407SRafael J. Wysocki for_each_cpu(j, policy->real_cpus) { 95487549141SViresh Kumar ret = add_cpu_dev_symlink(policy, j); 95571c3461eSRafael J. Wysocki if (ret) 95671c3461eSRafael J. Wysocki break; 95719d6f7ecSDave Jones } 95887549141SViresh Kumar 95919d6f7ecSDave Jones return ret; 96019d6f7ecSDave Jones } 96119d6f7ecSDave Jones 96287549141SViresh Kumar static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy) 96387549141SViresh Kumar { 96487549141SViresh Kumar unsigned int j; 96587549141SViresh Kumar 96687549141SViresh Kumar /* Some related CPUs might not be present (physically hotplugged) */ 96796bdda61SViresh Kumar for_each_cpu(j, policy->real_cpus) 96887549141SViresh Kumar remove_cpu_dev_symlink(policy, j); 96987549141SViresh Kumar } 97087549141SViresh Kumar 971d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 972909a694eSDave Jones { 973909a694eSDave Jones struct freq_attr **drv_attr; 974909a694eSDave Jones int ret = 0; 975909a694eSDave Jones 976909a694eSDave Jones /* set up files for this cpu device */ 9771c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 978f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 979909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 980909a694eSDave Jones if (ret) 9816d4e81edSTomeu Vizoso return ret; 982909a694eSDave Jones drv_attr++; 983909a694eSDave Jones } 9841c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 985909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 986909a694eSDave Jones if (ret) 9876d4e81edSTomeu Vizoso return ret; 988909a694eSDave Jones } 989c034b02eSDirk Brandewie 990909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 991909a694eSDave Jones if (ret) 9926d4e81edSTomeu Vizoso return ret; 993c034b02eSDirk Brandewie 9941c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 995e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 996e2f74f35SThomas Renninger if (ret) 9976d4e81edSTomeu Vizoso return ret; 998e2f74f35SThomas Renninger } 999909a694eSDave Jones 10006d4e81edSTomeu Vizoso return cpufreq_add_dev_symlink(policy); 1001e18f1682SSrivatsa S. Bhat } 1002e18f1682SSrivatsa S. Bhat 1003de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_default_governor(void) 1004de1df26bSRafael J. Wysocki { 1005de1df26bSRafael J. Wysocki return NULL; 1006de1df26bSRafael J. Wysocki } 1007de1df26bSRafael J. Wysocki 10087f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy) 1009e18f1682SSrivatsa S. Bhat { 10106e2c89d1Sviresh kumar struct cpufreq_governor *gov = NULL; 1011e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 1012e18f1682SSrivatsa S. Bhat 1013d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 1014a27a9ab7SJason Baron 10156e2c89d1Sviresh kumar /* Update governor of new_policy to the governor used before hotplug */ 10164573237bSViresh Kumar gov = find_governor(policy->last_governor); 1017de1df26bSRafael J. Wysocki if (gov) { 10186e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 10196e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 1020de1df26bSRafael J. Wysocki } else { 1021de1df26bSRafael J. Wysocki gov = cpufreq_default_governor(); 1022de1df26bSRafael J. Wysocki if (!gov) 1023de1df26bSRafael J. Wysocki return -ENODATA; 1024de1df26bSRafael J. Wysocki } 10256e2c89d1Sviresh kumar 10266e2c89d1Sviresh kumar new_policy.governor = gov; 10276e2c89d1Sviresh kumar 102869030dd1SSrinivas Pandruvada /* Use the default policy if there is no last_policy. */ 102969030dd1SSrinivas Pandruvada if (cpufreq_driver->setpolicy) { 103069030dd1SSrinivas Pandruvada if (policy->last_policy) 103169030dd1SSrinivas Pandruvada new_policy.policy = policy->last_policy; 103269030dd1SSrinivas Pandruvada else 103369030dd1SSrinivas Pandruvada cpufreq_parse_governor(gov->name, &new_policy.policy, 103469030dd1SSrinivas Pandruvada NULL); 103569030dd1SSrinivas Pandruvada } 1036ecf7e461SDave Jones /* set default policy */ 10377f0fa40fSViresh Kumar return cpufreq_set_policy(policy, &new_policy); 1038909a694eSDave Jones } 1039909a694eSDave Jones 1040d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1041fcf80582SViresh Kumar { 10429c0ebcf7SViresh Kumar int ret = 0; 1043fcf80582SViresh Kumar 1044bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 1045bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 1046bb29ae15SViresh Kumar return 0; 1047bb29ae15SViresh Kumar 104849f18560SViresh Kumar down_write(&policy->rwsem); 10499c0ebcf7SViresh Kumar if (has_target()) { 1050*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 10513de9bdebSViresh Kumar if (ret) { 10523de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 105349f18560SViresh Kumar goto unlock; 10543de9bdebSViresh Kumar } 10553de9bdebSViresh Kumar } 1056fcf80582SViresh Kumar 1057fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 10582eaa3e2dSViresh Kumar 10599c0ebcf7SViresh Kumar if (has_target()) { 1060*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 1061e5c87b76SStratos Karafotis if (!ret) 1062*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1063e5c87b76SStratos Karafotis 106449f18560SViresh Kumar if (ret) 10653de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 1066820c6ca2SViresh Kumar } 1067fcf80582SViresh Kumar 106849f18560SViresh Kumar unlock: 106949f18560SViresh Kumar up_write(&policy->rwsem); 107049f18560SViresh Kumar return ret; 1071fcf80582SViresh Kumar } 10721da177e4SLinus Torvalds 107311eb69b9SViresh Kumar static void handle_update(struct work_struct *work) 107411eb69b9SViresh Kumar { 107511eb69b9SViresh Kumar struct cpufreq_policy *policy = 107611eb69b9SViresh Kumar container_of(work, struct cpufreq_policy, update); 107711eb69b9SViresh Kumar unsigned int cpu = policy->cpu; 107811eb69b9SViresh Kumar pr_debug("handle_update for cpu %u called\n", cpu); 107911eb69b9SViresh Kumar cpufreq_update_policy(cpu); 108011eb69b9SViresh Kumar } 108111eb69b9SViresh Kumar 1082a34e63b1SRafael J. Wysocki static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 10838414809cSSrivatsa S. Bhat { 1084a34e63b1SRafael J. Wysocki struct device *dev = get_cpu_device(cpu); 1085e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1086e9698cc5SSrivatsa S. Bhat 1087a34e63b1SRafael J. Wysocki if (WARN_ON(!dev)) 1088a34e63b1SRafael J. Wysocki return NULL; 1089a34e63b1SRafael J. Wysocki 1090e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1091e9698cc5SSrivatsa S. Bhat if (!policy) 1092e9698cc5SSrivatsa S. Bhat return NULL; 1093e9698cc5SSrivatsa S. Bhat 1094e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1095e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1096e9698cc5SSrivatsa S. Bhat 1097e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1098e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1099e9698cc5SSrivatsa S. Bhat 1100559ed407SRafael J. Wysocki if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 1101559ed407SRafael J. Wysocki goto err_free_rcpumask; 1102559ed407SRafael J. Wysocki 11033510fac4SViresh Kumar kobject_init(&policy->kobj, &ktype_cpufreq); 1104c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1105ad7722daSviresh kumar init_rwsem(&policy->rwsem); 110612478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 110712478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1108818c5712SViresh Kumar init_completion(&policy->kobj_unregister); 1109818c5712SViresh Kumar INIT_WORK(&policy->update, handle_update); 1110ad7722daSviresh kumar 1111a34e63b1SRafael J. Wysocki policy->cpu = cpu; 1112e9698cc5SSrivatsa S. Bhat return policy; 1113e9698cc5SSrivatsa S. Bhat 11142fc3384dSViresh Kumar err_free_rcpumask: 11152fc3384dSViresh Kumar free_cpumask_var(policy->related_cpus); 1116e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1117e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1118e9698cc5SSrivatsa S. Bhat err_free_policy: 1119e9698cc5SSrivatsa S. Bhat kfree(policy); 1120e9698cc5SSrivatsa S. Bhat 1121e9698cc5SSrivatsa S. Bhat return NULL; 1122e9698cc5SSrivatsa S. Bhat } 1123e9698cc5SSrivatsa S. Bhat 11242fc3384dSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify) 112542f921a6SViresh Kumar { 112642f921a6SViresh Kumar struct kobject *kobj; 112742f921a6SViresh Kumar struct completion *cmp; 112842f921a6SViresh Kumar 11292fc3384dSViresh Kumar if (notify) 1130fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1131fcd7af91SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 1132fcd7af91SViresh Kumar 113387549141SViresh Kumar down_write(&policy->rwsem); 113487549141SViresh Kumar cpufreq_remove_dev_symlink(policy); 113542f921a6SViresh Kumar kobj = &policy->kobj; 113642f921a6SViresh Kumar cmp = &policy->kobj_unregister; 113787549141SViresh Kumar up_write(&policy->rwsem); 113842f921a6SViresh Kumar kobject_put(kobj); 113942f921a6SViresh Kumar 114042f921a6SViresh Kumar /* 114142f921a6SViresh Kumar * We need to make sure that the underlying kobj is 114242f921a6SViresh Kumar * actually not referenced anymore by anybody before we 114342f921a6SViresh Kumar * proceed with unloading. 114442f921a6SViresh Kumar */ 114542f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 114642f921a6SViresh Kumar wait_for_completion(cmp); 114742f921a6SViresh Kumar pr_debug("wait complete\n"); 114842f921a6SViresh Kumar } 114942f921a6SViresh Kumar 11503654c5ccSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify) 1151e9698cc5SSrivatsa S. Bhat { 1152988bed09SViresh Kumar unsigned long flags; 1153988bed09SViresh Kumar int cpu; 1154988bed09SViresh Kumar 1155988bed09SViresh Kumar /* Remove policy from list */ 1156988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1157988bed09SViresh Kumar list_del(&policy->policy_list); 1158988bed09SViresh Kumar 1159988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1160988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1161988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1162988bed09SViresh Kumar 11633654c5ccSViresh Kumar cpufreq_policy_put_kobj(policy, notify); 1164559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1165e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1166e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1167e9698cc5SSrivatsa S. Bhat kfree(policy); 1168e9698cc5SSrivatsa S. Bhat } 1169e9698cc5SSrivatsa S. Bhat 11700b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 11711da177e4SLinus Torvalds { 11727f0c020aSViresh Kumar struct cpufreq_policy *policy; 1173194d99c7SRafael J. Wysocki bool new_policy; 11741da177e4SLinus Torvalds unsigned long flags; 11750b275352SRafael J. Wysocki unsigned int j; 11760b275352SRafael J. Wysocki int ret; 1177c32b6b8eSAshok Raj 11780b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 11796eed9404SViresh Kumar 1180bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 11819104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 118211ce707eSRafael J. Wysocki if (policy) { 11839104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 118411ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1185d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 11861da177e4SLinus Torvalds 118711ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1188194d99c7SRafael J. Wysocki new_policy = false; 118911ce707eSRafael J. Wysocki down_write(&policy->rwsem); 119011ce707eSRafael J. Wysocki policy->cpu = cpu; 119111ce707eSRafael J. Wysocki policy->governor = NULL; 119211ce707eSRafael J. Wysocki up_write(&policy->rwsem); 119311ce707eSRafael J. Wysocki } else { 1194194d99c7SRafael J. Wysocki new_policy = true; 1195a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1196059019a3SDave Jones if (!policy) 1197d4d854d6SRafael J. Wysocki return -ENOMEM; 119872368d12SRafael J. Wysocki } 11990d66b91eSSrivatsa S. Bhat 1200835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 12031da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 12041da177e4SLinus Torvalds */ 12051c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 12061da177e4SLinus Torvalds if (ret) { 12072d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 12088101f997SViresh Kumar goto out_free_policy; 12091da177e4SLinus Torvalds } 1210643ae6e8SViresh Kumar 12116d4e81edSTomeu Vizoso down_write(&policy->rwsem); 12126d4e81edSTomeu Vizoso 1213194d99c7SRafael J. Wysocki if (new_policy) { 12144d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 12150998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 12164d1f3a5bSRafael J. Wysocki /* Remember CPUs present at the policy creation time. */ 1217559ed407SRafael J. Wysocki cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask); 12183510fac4SViresh Kumar 12193510fac4SViresh Kumar /* Name and add the kobject */ 12203510fac4SViresh Kumar ret = kobject_add(&policy->kobj, cpufreq_global_kobject, 12213510fac4SViresh Kumar "policy%u", 12223510fac4SViresh Kumar cpumask_first(policy->related_cpus)); 12233510fac4SViresh Kumar if (ret) { 12243510fac4SViresh Kumar pr_err("%s: failed to add policy->kobj: %d\n", __func__, 12253510fac4SViresh Kumar ret); 12263510fac4SViresh Kumar goto out_exit_policy; 12273510fac4SViresh Kumar } 12284d1f3a5bSRafael J. Wysocki } 1229559ed407SRafael J. Wysocki 12305a7e56a5SViresh Kumar /* 12315a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 12325a7e56a5SViresh Kumar * managing offline cpus here. 12335a7e56a5SViresh Kumar */ 12345a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 12355a7e56a5SViresh Kumar 1236194d99c7SRafael J. Wysocki if (new_policy) { 12375a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 12385a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 12396d4e81edSTomeu Vizoso 1240652ed95dSViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1241988bed09SViresh Kumar for_each_cpu(j, policy->related_cpus) 1242652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 1243652ed95dSViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1244988bed09SViresh Kumar } 1245652ed95dSViresh Kumar 12462ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 1247da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1248da60ce9fSViresh Kumar if (!policy->cur) { 1249da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 12508101f997SViresh Kumar goto out_exit_policy; 1251da60ce9fSViresh Kumar } 1252da60ce9fSViresh Kumar } 1253da60ce9fSViresh Kumar 1254d3916691SViresh Kumar /* 1255d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1256d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1257d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1258d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1259d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1260d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1261d3916691SViresh Kumar * isn't found in freq-table. 1262d3916691SViresh Kumar * 1263d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1264d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1265d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1266d3916691SViresh Kumar * is initialized to zero). 1267d3916691SViresh Kumar * 1268d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1269d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1270d3916691SViresh Kumar * equal to target-freq. 1271d3916691SViresh Kumar */ 1272d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1273d3916691SViresh Kumar && has_target()) { 1274d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1275d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1276d3916691SViresh Kumar if (ret == -EINVAL) { 1277d3916691SViresh Kumar /* Warn user and fix it */ 1278d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1279d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1280d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1281d3916691SViresh Kumar CPUFREQ_RELATION_L); 1282d3916691SViresh Kumar 1283d3916691SViresh Kumar /* 1284d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1285d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1286d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1287d3916691SViresh Kumar */ 1288d3916691SViresh Kumar BUG_ON(ret); 1289d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1290d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1291d3916691SViresh Kumar } 1292d3916691SViresh Kumar } 1293d3916691SViresh Kumar 1294a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1295a1531acdSThomas Renninger CPUFREQ_START, policy); 1296a1531acdSThomas Renninger 1297194d99c7SRafael J. Wysocki if (new_policy) { 1298d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 129919d6f7ecSDave Jones if (ret) 13008101f997SViresh Kumar goto out_exit_policy; 1301fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1302fcd7af91SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 1303c88a1f8bSLukasz Majewski 1304c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1305c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1306c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1307988bed09SViresh Kumar } 13088ff69732SDave Jones 13097f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 13107f0fa40fSViresh Kumar if (ret) { 13117f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 13127f0fa40fSViresh Kumar __func__, cpu, ret); 1313194d99c7SRafael J. Wysocki /* cpufreq_policy_free() will notify based on this */ 1314194d99c7SRafael J. Wysocki new_policy = false; 1315194d99c7SRafael J. Wysocki goto out_exit_policy; 131608fd8c1cSViresh Kumar } 1317e18f1682SSrivatsa S. Bhat 13184e97b631SViresh Kumar up_write(&policy->rwsem); 131908fd8c1cSViresh Kumar 1320038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 13217c45cf31SViresh Kumar 13227c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 13237c45cf31SViresh Kumar if (cpufreq_driver->ready) 13247c45cf31SViresh Kumar cpufreq_driver->ready(policy); 13257c45cf31SViresh Kumar 13262d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds return 0; 13291da177e4SLinus Torvalds 13308101f997SViresh Kumar out_exit_policy: 13317106e02bSPrarit Bhargava up_write(&policy->rwsem); 13327106e02bSPrarit Bhargava 1333da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1334da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 13358101f997SViresh Kumar out_free_policy: 1336194d99c7SRafael J. Wysocki cpufreq_policy_free(policy, !new_policy); 13371da177e4SLinus Torvalds return ret; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13400b275352SRafael J. Wysocki /** 13410b275352SRafael J. Wysocki * cpufreq_add_dev - the cpufreq interface for a CPU device. 13420b275352SRafael J. Wysocki * @dev: CPU device. 13430b275352SRafael J. Wysocki * @sif: Subsystem interface structure pointer (not used) 13440b275352SRafael J. Wysocki */ 13450b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 13460b275352SRafael J. Wysocki { 13470b275352SRafael J. Wysocki unsigned cpu = dev->id; 13480b275352SRafael J. Wysocki int ret; 13490b275352SRafael J. Wysocki 13500b275352SRafael J. Wysocki dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 13510b275352SRafael J. Wysocki 13520b275352SRafael J. Wysocki if (cpu_online(cpu)) { 13530b275352SRafael J. Wysocki ret = cpufreq_online(cpu); 13540b275352SRafael J. Wysocki } else { 13550b275352SRafael J. Wysocki /* 13560b275352SRafael J. Wysocki * A hotplug notifier will follow and we will handle it as CPU 13570b275352SRafael J. Wysocki * online then. For now, just create the sysfs link, unless 13580b275352SRafael J. Wysocki * there is no policy or the link is already present. 13590b275352SRafael J. Wysocki */ 13600b275352SRafael J. Wysocki struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 13610b275352SRafael J. Wysocki 13620b275352SRafael J. Wysocki ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus) 13630b275352SRafael J. Wysocki ? add_cpu_dev_symlink(policy, cpu) : 0; 13640b275352SRafael J. Wysocki } 13651da177e4SLinus Torvalds 13661da177e4SLinus Torvalds return ret; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 136969cee714SViresh Kumar static void cpufreq_offline(unsigned int cpu) 13701da177e4SLinus Torvalds { 13713a3e9e06SViresh Kumar struct cpufreq_policy *policy; 137269cee714SViresh Kumar int ret; 13731da177e4SLinus Torvalds 1374b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 13751da177e4SLinus Torvalds 1376988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 13773a3e9e06SViresh Kumar if (!policy) { 1378b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 137915c0b4d2SRafael J. Wysocki return; 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds 138249f18560SViresh Kumar down_write(&policy->rwsem); 13839c0ebcf7SViresh Kumar if (has_target()) { 1384*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 1385559ed407SRafael J. Wysocki if (ret) 13863de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 1387db5f2995SViresh Kumar } 13881da177e4SLinus Torvalds 13899591becbSViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 13904573237bSViresh Kumar 13919591becbSViresh Kumar if (policy_is_inactive(policy)) { 13929591becbSViresh Kumar if (has_target()) 13934573237bSViresh Kumar strncpy(policy->last_governor, policy->governor->name, 13944573237bSViresh Kumar CPUFREQ_NAME_LEN); 139569030dd1SSrinivas Pandruvada else 139669030dd1SSrinivas Pandruvada policy->last_policy = policy->policy; 13979591becbSViresh Kumar } else if (cpu == policy->cpu) { 13989591becbSViresh Kumar /* Nominate new CPU */ 13999591becbSViresh Kumar policy->cpu = cpumask_any(policy->cpus); 14009591becbSViresh Kumar } 14011da177e4SLinus Torvalds 14029591becbSViresh Kumar /* Start governor again for active policy */ 14039591becbSViresh Kumar if (!policy_is_inactive(policy)) { 14049591becbSViresh Kumar if (has_target()) { 1405*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 14069591becbSViresh Kumar if (!ret) 1407*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 140887549141SViresh Kumar 14099591becbSViresh Kumar if (ret) 14109591becbSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 14119591becbSViresh Kumar } 141269cee714SViresh Kumar 141349f18560SViresh Kumar goto unlock; 141469cee714SViresh Kumar } 141569cee714SViresh Kumar 141669cee714SViresh Kumar if (cpufreq_driver->stop_cpu) 1417367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 141887549141SViresh Kumar 141987549141SViresh Kumar /* If cpu is last user of policy, free policy */ 142087549141SViresh Kumar if (has_target()) { 1421*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 1422559ed407SRafael J. Wysocki if (ret) 142387549141SViresh Kumar pr_err("%s: Failed to exit governor\n", __func__); 14243de9bdebSViresh Kumar } 14252a998599SRafael J. Wysocki 14268414809cSSrivatsa S. Bhat /* 14278414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 14288414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 14298414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 14308414809cSSrivatsa S. Bhat */ 143155582bccSSrinivas Pandruvada if (cpufreq_driver->exit) { 14323a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 143355582bccSSrinivas Pandruvada policy->freq_table = NULL; 143455582bccSSrinivas Pandruvada } 143549f18560SViresh Kumar 143649f18560SViresh Kumar unlock: 143749f18560SViresh Kumar up_write(&policy->rwsem); 14381da177e4SLinus Torvalds } 14391da177e4SLinus Torvalds 1440cedb70afSSrivatsa S. Bhat /** 144127a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1442cedb70afSSrivatsa S. Bhat * 1443cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1444cedb70afSSrivatsa S. Bhat */ 144571db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 14465a01f2e8SVenkatesh Pallipadi { 14478a25a2fdSKay Sievers unsigned int cpu = dev->id; 144887549141SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 144987549141SViresh Kumar 145087549141SViresh Kumar if (!policy) 145171db87baSViresh Kumar return; 1452ec28297aSVenki Pallipadi 145369cee714SViresh Kumar if (cpu_online(cpu)) 145469cee714SViresh Kumar cpufreq_offline(cpu); 145587549141SViresh Kumar 1456559ed407SRafael J. Wysocki cpumask_clear_cpu(cpu, policy->real_cpus); 1457559ed407SRafael J. Wysocki remove_cpu_dev_symlink(policy, cpu); 1458f344dae0SViresh Kumar 1459f344dae0SViresh Kumar if (cpumask_empty(policy->real_cpus)) 1460f344dae0SViresh Kumar cpufreq_policy_free(policy, true); 14615a01f2e8SVenkatesh Pallipadi } 14625a01f2e8SVenkatesh Pallipadi 14631da177e4SLinus Torvalds /** 1464bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1465bb176f7dSViresh Kumar * in deep trouble. 1466a1e1dc41SViresh Kumar * @policy: policy managing CPUs 14671da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 14681da177e4SLinus Torvalds * 146929464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 147029464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 14711da177e4SLinus Torvalds */ 1472a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1473e08f5f5bSGautham R Shenoy unsigned int new_freq) 14741da177e4SLinus Torvalds { 14751da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1476b43a7ffbSViresh Kumar 1477e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1478a1e1dc41SViresh Kumar policy->cur, new_freq); 14791da177e4SLinus Torvalds 1480a1e1dc41SViresh Kumar freqs.old = policy->cur; 14811da177e4SLinus Torvalds freqs.new = new_freq; 1482b43a7ffbSViresh Kumar 14838fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 14848fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 14851da177e4SLinus Torvalds } 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds /** 14884ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 148995235ca2SVenkatesh Pallipadi * @cpu: CPU number 149095235ca2SVenkatesh Pallipadi * 149195235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 149295235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 149395235ca2SVenkatesh Pallipadi */ 149495235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 149595235ca2SVenkatesh Pallipadi { 14969e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1497e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 149895235ca2SVenkatesh Pallipadi 14991c3d85ddSRafael J. Wysocki if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 15001c3d85ddSRafael J. Wysocki return cpufreq_driver->get(cpu); 15019e21ba8bSDirk Brandewie 15029e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 150395235ca2SVenkatesh Pallipadi if (policy) { 1504e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 150595235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 150695235ca2SVenkatesh Pallipadi } 150795235ca2SVenkatesh Pallipadi 15084d34a67dSDave Jones return ret_freq; 150995235ca2SVenkatesh Pallipadi } 151095235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 151195235ca2SVenkatesh Pallipadi 15123d737108SJesse Barnes /** 15133d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 15143d737108SJesse Barnes * @cpu: CPU number 15153d737108SJesse Barnes * 15163d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 15173d737108SJesse Barnes */ 15183d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 15193d737108SJesse Barnes { 15203d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15213d737108SJesse Barnes unsigned int ret_freq = 0; 15223d737108SJesse Barnes 15233d737108SJesse Barnes if (policy) { 15243d737108SJesse Barnes ret_freq = policy->max; 15253d737108SJesse Barnes cpufreq_cpu_put(policy); 15263d737108SJesse Barnes } 15273d737108SJesse Barnes 15283d737108SJesse Barnes return ret_freq; 15293d737108SJesse Barnes } 15303d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 15313d737108SJesse Barnes 1532d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 15331da177e4SLinus Torvalds { 1534e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 15351da177e4SLinus Torvalds 15361c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 15374d34a67dSDave Jones return ret_freq; 15381da177e4SLinus Torvalds 1539d92d50a4SViresh Kumar ret_freq = cpufreq_driver->get(policy->cpu); 15401da177e4SLinus Torvalds 154111e584cfSViresh Kumar /* Updating inactive policies is invalid, so avoid doing that. */ 154211e584cfSViresh Kumar if (unlikely(policy_is_inactive(policy))) 154311e584cfSViresh Kumar return ret_freq; 154411e584cfSViresh Kumar 1545e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 15461c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1547e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1548e08f5f5bSGautham R Shenoy saved value exists */ 1549e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1550a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, ret_freq); 15511da177e4SLinus Torvalds schedule_work(&policy->update); 15521da177e4SLinus Torvalds } 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 15554d34a67dSDave Jones return ret_freq; 15565a01f2e8SVenkatesh Pallipadi } 15571da177e4SLinus Torvalds 15585a01f2e8SVenkatesh Pallipadi /** 15595a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 15605a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 15615a01f2e8SVenkatesh Pallipadi * 15625a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 15635a01f2e8SVenkatesh Pallipadi */ 15645a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 15655a01f2e8SVenkatesh Pallipadi { 1566999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15675a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 15685a01f2e8SVenkatesh Pallipadi 1569999976e0SAaron Plattner if (policy) { 1570ad7722daSviresh kumar down_read(&policy->rwsem); 1571d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1572ad7722daSviresh kumar up_read(&policy->rwsem); 1573999976e0SAaron Plattner 1574999976e0SAaron Plattner cpufreq_cpu_put(policy); 1575999976e0SAaron Plattner } 15766eed9404SViresh Kumar 15774d34a67dSDave Jones return ret_freq; 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 15801da177e4SLinus Torvalds 15818a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 15828a25a2fdSKay Sievers .name = "cpufreq", 15838a25a2fdSKay Sievers .subsys = &cpu_subsys, 15848a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 15858a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1586e00e56dfSRafael J. Wysocki }; 1587e00e56dfSRafael J. Wysocki 1588e28867eaSViresh Kumar /* 1589e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1590e28867eaSViresh Kumar * during suspend.. 159142d4dc3fSBenjamin Herrenschmidt */ 1592e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 159342d4dc3fSBenjamin Herrenschmidt { 1594e28867eaSViresh Kumar int ret; 15954bc5d341SDave Jones 1596e28867eaSViresh Kumar if (!policy->suspend_freq) { 1597201f3716SBartlomiej Zolnierkiewicz pr_debug("%s: suspend_freq not defined\n", __func__); 1598201f3716SBartlomiej Zolnierkiewicz return 0; 159942d4dc3fSBenjamin Herrenschmidt } 160042d4dc3fSBenjamin Herrenschmidt 1601e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1602e28867eaSViresh Kumar policy->suspend_freq); 1603e28867eaSViresh Kumar 1604e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1605e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1606e28867eaSViresh Kumar if (ret) 1607e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1608e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1609e28867eaSViresh Kumar 1610c9060494SDave Jones return ret; 161142d4dc3fSBenjamin Herrenschmidt } 1612e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 161342d4dc3fSBenjamin Herrenschmidt 161442d4dc3fSBenjamin Herrenschmidt /** 16152f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 16161da177e4SLinus Torvalds * 16172f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 16182f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 16192f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 16202f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 16211da177e4SLinus Torvalds */ 16222f0aea93SViresh Kumar void cpufreq_suspend(void) 16231da177e4SLinus Torvalds { 16243a3e9e06SViresh Kumar struct cpufreq_policy *policy; 162549f18560SViresh Kumar int ret; 16261da177e4SLinus Torvalds 16272f0aea93SViresh Kumar if (!cpufreq_driver) 1628e00e56dfSRafael J. Wysocki return; 16291da177e4SLinus Torvalds 16302f0aea93SViresh Kumar if (!has_target()) 1631b1b12babSViresh Kumar goto suspend; 16321da177e4SLinus Torvalds 16332f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 16342f0aea93SViresh Kumar 1635f963735aSViresh Kumar for_each_active_policy(policy) { 163649f18560SViresh Kumar down_write(&policy->rwsem); 1637*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 163849f18560SViresh Kumar up_write(&policy->rwsem); 163949f18560SViresh Kumar 164049f18560SViresh Kumar if (ret) 16412f0aea93SViresh Kumar pr_err("%s: Failed to stop governor for policy: %p\n", 16422f0aea93SViresh Kumar __func__, policy); 16432f0aea93SViresh Kumar else if (cpufreq_driver->suspend 16442f0aea93SViresh Kumar && cpufreq_driver->suspend(policy)) 16452f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 16462f0aea93SViresh Kumar policy); 16471da177e4SLinus Torvalds } 1648b1b12babSViresh Kumar 1649b1b12babSViresh Kumar suspend: 1650b1b12babSViresh Kumar cpufreq_suspended = true; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds /** 16542f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 16551da177e4SLinus Torvalds * 16562f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 16572f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 16581da177e4SLinus Torvalds */ 16592f0aea93SViresh Kumar void cpufreq_resume(void) 16601da177e4SLinus Torvalds { 16611da177e4SLinus Torvalds struct cpufreq_policy *policy; 166249f18560SViresh Kumar int ret; 16631da177e4SLinus Torvalds 16642f0aea93SViresh Kumar if (!cpufreq_driver) 16651da177e4SLinus Torvalds return; 16661da177e4SLinus Torvalds 16678e30444eSLan Tianyu cpufreq_suspended = false; 16688e30444eSLan Tianyu 16692f0aea93SViresh Kumar if (!has_target()) 16702f0aea93SViresh Kumar return; 16711da177e4SLinus Torvalds 16722f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 16732f0aea93SViresh Kumar 1674f963735aSViresh Kumar for_each_active_policy(policy) { 167549f18560SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 16760c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 16770c5aa405SViresh Kumar policy); 167849f18560SViresh Kumar } else { 167949f18560SViresh Kumar down_write(&policy->rwsem); 1680*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 168149f18560SViresh Kumar if (!ret) 1682*a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 168349f18560SViresh Kumar up_write(&policy->rwsem); 168449f18560SViresh Kumar 168549f18560SViresh Kumar if (ret) 16862f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 16872f0aea93SViresh Kumar __func__, policy); 1688c75de0acSViresh Kumar } 168949f18560SViresh Kumar } 16902f0aea93SViresh Kumar 16912f0aea93SViresh Kumar /* 1692c75de0acSViresh Kumar * schedule call cpufreq_update_policy() for first-online CPU, as that 1693c75de0acSViresh Kumar * wouldn't be hotplugged-out on suspend. It will verify that the 1694c75de0acSViresh Kumar * current freq is in sync with what we believe it to be. 16952f0aea93SViresh Kumar */ 1696c75de0acSViresh Kumar policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask)); 1697c75de0acSViresh Kumar if (WARN_ON(!policy)) 1698c75de0acSViresh Kumar return; 1699c75de0acSViresh Kumar 17003a3e9e06SViresh Kumar schedule_work(&policy->update); 17011da177e4SLinus Torvalds } 17021da177e4SLinus Torvalds 17039d95046eSBorislav Petkov /** 17049d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17059d95046eSBorislav Petkov * 17069d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17079d95046eSBorislav Petkov * or NULL, if none. 17089d95046eSBorislav Petkov */ 17099d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17109d95046eSBorislav Petkov { 17111c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17121c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17131c3d85ddSRafael J. Wysocki 17141c3d85ddSRafael J. Wysocki return NULL; 17159d95046eSBorislav Petkov } 17169d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17171da177e4SLinus Torvalds 171851315cdfSThomas Petazzoni /** 171951315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 172051315cdfSThomas Petazzoni * 172151315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 172251315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 172351315cdfSThomas Petazzoni */ 172451315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 172551315cdfSThomas Petazzoni { 172651315cdfSThomas Petazzoni if (cpufreq_driver) 172751315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 172851315cdfSThomas Petazzoni 172951315cdfSThomas Petazzoni return NULL; 173051315cdfSThomas Petazzoni } 173151315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 173251315cdfSThomas Petazzoni 17331da177e4SLinus Torvalds /********************************************************************* 17341da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 17351da177e4SLinus Torvalds *********************************************************************/ 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds /** 17381da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 17391da177e4SLinus Torvalds * @nb: notifier function to register 17401da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17411da177e4SLinus Torvalds * 17421da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 17431da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 17441da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 17451da177e4SLinus Torvalds * changes in cpufreq policy. 17461da177e4SLinus Torvalds * 17471da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1748e041c683SAlan Stern * blocking_notifier_chain_register. 17491da177e4SLinus Torvalds */ 17501da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 17511da177e4SLinus Torvalds { 17521da177e4SLinus Torvalds int ret; 17531da177e4SLinus Torvalds 1754d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1755d5aaffa9SDirk Brandewie return -EINVAL; 1756d5aaffa9SDirk Brandewie 175774212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 175874212ca4SCesar Eduardo Barros 17591da177e4SLinus Torvalds switch (list) { 17601da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1761b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1762e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17631da177e4SLinus Torvalds break; 17641da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1765e041c683SAlan Stern ret = blocking_notifier_chain_register( 1766e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 17671da177e4SLinus Torvalds break; 17681da177e4SLinus Torvalds default: 17691da177e4SLinus Torvalds ret = -EINVAL; 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds return ret; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds /** 17771da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 17781da177e4SLinus Torvalds * @nb: notifier block to be unregistered 17791da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17801da177e4SLinus Torvalds * 17811da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 17821da177e4SLinus Torvalds * 17831da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1784e041c683SAlan Stern * blocking_notifier_chain_unregister. 17851da177e4SLinus Torvalds */ 17861da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 17871da177e4SLinus Torvalds { 17881da177e4SLinus Torvalds int ret; 17891da177e4SLinus Torvalds 1790d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1791d5aaffa9SDirk Brandewie return -EINVAL; 1792d5aaffa9SDirk Brandewie 17931da177e4SLinus Torvalds switch (list) { 17941da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1795b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1796e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17971da177e4SLinus Torvalds break; 17981da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1799e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1800e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18011da177e4SLinus Torvalds break; 18021da177e4SLinus Torvalds default: 18031da177e4SLinus Torvalds ret = -EINVAL; 18041da177e4SLinus Torvalds } 18051da177e4SLinus Torvalds 18061da177e4SLinus Torvalds return ret; 18071da177e4SLinus Torvalds } 18081da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds 18111da177e4SLinus Torvalds /********************************************************************* 18121da177e4SLinus Torvalds * GOVERNORS * 18131da177e4SLinus Torvalds *********************************************************************/ 18141da177e4SLinus Torvalds 18151c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 18161c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 18171c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 18181c03a2d0SViresh Kumar { 18191c03a2d0SViresh Kumar int ret; 18201c03a2d0SViresh Kumar 18211c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 18221c03a2d0SViresh Kumar 18231c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 18241c03a2d0SViresh Kumar if (!freqs->new) 18251c03a2d0SViresh Kumar return 0; 18261c03a2d0SViresh Kumar 18271c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 18281c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 18291c03a2d0SViresh Kumar 18301c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 18311c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 18321c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 18331c03a2d0SViresh Kumar 18341c03a2d0SViresh Kumar if (ret) 18351c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 18361c03a2d0SViresh Kumar __func__, ret); 18371c03a2d0SViresh Kumar 18381c03a2d0SViresh Kumar return ret; 18391c03a2d0SViresh Kumar } 18401c03a2d0SViresh Kumar 18418d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy, 18428d65775dSViresh Kumar struct cpufreq_frequency_table *freq_table, int index) 18438d65775dSViresh Kumar { 18441c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 18451c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 18468d65775dSViresh Kumar int retval = -EINVAL; 18478d65775dSViresh Kumar bool notify; 18488d65775dSViresh Kumar 18498d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 18508d65775dSViresh Kumar if (notify) { 18511c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 18521c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 18531c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 18541c03a2d0SViresh Kumar if (retval) 18551c03a2d0SViresh Kumar return retval; 18568d65775dSViresh Kumar 18571c03a2d0SViresh Kumar intermediate_freq = freqs.new; 18581c03a2d0SViresh Kumar /* Set old freq to intermediate */ 18591c03a2d0SViresh Kumar if (intermediate_freq) 18601c03a2d0SViresh Kumar freqs.old = freqs.new; 18611c03a2d0SViresh Kumar } 18621c03a2d0SViresh Kumar 18631c03a2d0SViresh Kumar freqs.new = freq_table[index].frequency; 18648d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 18658d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 18668d65775dSViresh Kumar 18678d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 18688d65775dSViresh Kumar } 18698d65775dSViresh Kumar 18708d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 18718d65775dSViresh Kumar if (retval) 18728d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 18738d65775dSViresh Kumar retval); 18748d65775dSViresh Kumar 18751c03a2d0SViresh Kumar if (notify) { 18768d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 18778d65775dSViresh Kumar 18781c03a2d0SViresh Kumar /* 18791c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 18801c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 18811c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 188258405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 18831c03a2d0SViresh Kumar */ 18841c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 18851c03a2d0SViresh Kumar freqs.old = intermediate_freq; 18861c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 18871c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 18881c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 18891c03a2d0SViresh Kumar } 18901c03a2d0SViresh Kumar } 18911c03a2d0SViresh Kumar 18928d65775dSViresh Kumar return retval; 18938d65775dSViresh Kumar } 18948d65775dSViresh Kumar 18951da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 18961da177e4SLinus Torvalds unsigned int target_freq, 18971da177e4SLinus Torvalds unsigned int relation) 18981da177e4SLinus Torvalds { 18997249924eSViresh Kumar unsigned int old_target_freq = target_freq; 19008d65775dSViresh Kumar int retval = -EINVAL; 1901c32b6b8eSAshok Raj 1902a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1903a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1904a7b422cdSKonrad Rzeszutek Wilk 19057249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 19067249924eSViresh Kumar if (target_freq > policy->max) 19077249924eSViresh Kumar target_freq = policy->max; 19087249924eSViresh Kumar if (target_freq < policy->min) 19097249924eSViresh Kumar target_freq = policy->min; 19107249924eSViresh Kumar 19117249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 19127249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 19135a1c0228SViresh Kumar 19149c0ebcf7SViresh Kumar /* 19159c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 19169c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 19179c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 19189c0ebcf7SViresh Kumar * calls. 19199c0ebcf7SViresh Kumar */ 19205a1c0228SViresh Kumar if (target_freq == policy->cur) 19215a1c0228SViresh Kumar return 0; 19225a1c0228SViresh Kumar 19231c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 19241c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 19251c03a2d0SViresh Kumar 19261c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 19271c3d85ddSRafael J. Wysocki retval = cpufreq_driver->target(policy, target_freq, relation); 19289c0ebcf7SViresh Kumar else if (cpufreq_driver->target_index) { 19299c0ebcf7SViresh Kumar struct cpufreq_frequency_table *freq_table; 19309c0ebcf7SViresh Kumar int index; 193190d45d17SAshok Raj 19329c0ebcf7SViresh Kumar freq_table = cpufreq_frequency_get_table(policy->cpu); 19339c0ebcf7SViresh Kumar if (unlikely(!freq_table)) { 19349c0ebcf7SViresh Kumar pr_err("%s: Unable to find freq_table\n", __func__); 19359c0ebcf7SViresh Kumar goto out; 19369c0ebcf7SViresh Kumar } 19379c0ebcf7SViresh Kumar 19389c0ebcf7SViresh Kumar retval = cpufreq_frequency_table_target(policy, freq_table, 19399c0ebcf7SViresh Kumar target_freq, relation, &index); 19409c0ebcf7SViresh Kumar if (unlikely(retval)) { 19419c0ebcf7SViresh Kumar pr_err("%s: Unable to find matching freq\n", __func__); 19429c0ebcf7SViresh Kumar goto out; 19439c0ebcf7SViresh Kumar } 19449c0ebcf7SViresh Kumar 1945d4019f0aSViresh Kumar if (freq_table[index].frequency == policy->cur) { 19469c0ebcf7SViresh Kumar retval = 0; 1947d4019f0aSViresh Kumar goto out; 1948d4019f0aSViresh Kumar } 1949d4019f0aSViresh Kumar 19508d65775dSViresh Kumar retval = __target_index(policy, freq_table, index); 19519c0ebcf7SViresh Kumar } 19529c0ebcf7SViresh Kumar 19539c0ebcf7SViresh Kumar out: 19541da177e4SLinus Torvalds return retval; 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 19571da177e4SLinus Torvalds 19581da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 19591da177e4SLinus Torvalds unsigned int target_freq, 19601da177e4SLinus Torvalds unsigned int relation) 19611da177e4SLinus Torvalds { 1962f1829e4aSJulia Lawall int ret = -EINVAL; 19631da177e4SLinus Torvalds 1964ad7722daSviresh kumar down_write(&policy->rwsem); 19651da177e4SLinus Torvalds 19661da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 19671da177e4SLinus Torvalds 1968ad7722daSviresh kumar up_write(&policy->rwsem); 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds return ret; 19711da177e4SLinus Torvalds } 19721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 19731da177e4SLinus Torvalds 1974de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 1975de1df26bSRafael J. Wysocki { 1976de1df26bSRafael J. Wysocki return NULL; 1977de1df26bSRafael J. Wysocki } 1978de1df26bSRafael J. Wysocki 1979*a1317e09SViresh Kumar static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event) 19801da177e4SLinus Torvalds { 1981cc993cabSDave Jones int ret; 19826afde10cSThomas Renninger 19832f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 19842f0aea93SViresh Kumar if (cpufreq_suspended) 19852f0aea93SViresh Kumar return 0; 1986cb57720bSEthan Zhao /* 1987cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 1988cb57720bSEthan Zhao * notification happened, so check it. 1989cb57720bSEthan Zhao */ 1990cb57720bSEthan Zhao if (!policy->governor) 1991cb57720bSEthan Zhao return -EINVAL; 19922f0aea93SViresh Kumar 19931c256245SThomas Renninger if (policy->governor->max_transition_latency && 19941c256245SThomas Renninger policy->cpuinfo.transition_latency > 19951c256245SThomas Renninger policy->governor->max_transition_latency) { 1996de1df26bSRafael J. Wysocki struct cpufreq_governor *gov = cpufreq_fallback_governor(); 1997de1df26bSRafael J. Wysocki 1998de1df26bSRafael J. Wysocki if (gov) { 1999e837f9b5SJoe Perches pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", 2000e837f9b5SJoe Perches policy->governor->name, gov->name); 20011c256245SThomas Renninger policy->governor = gov; 2002de1df26bSRafael J. Wysocki } else { 2003de1df26bSRafael J. Wysocki return -EINVAL; 20041c256245SThomas Renninger } 20056afde10cSThomas Renninger } 20061da177e4SLinus Torvalds 2007fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 20081da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 20091da177e4SLinus Torvalds return -EINVAL; 20101da177e4SLinus Torvalds 201163431f78SViresh Kumar pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event); 201295731ebbSXiaoguang Chen 201356d07db2SSrivatsa S. Bhat if ((policy->governor_enabled && event == CPUFREQ_GOV_START) 2014f73d3933SViresh Kumar || (!policy->governor_enabled 2015f73d3933SViresh Kumar && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) { 201695731ebbSXiaoguang Chen return -EBUSY; 201795731ebbSXiaoguang Chen } 201895731ebbSXiaoguang Chen 201995731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 202095731ebbSXiaoguang Chen policy->governor_enabled = false; 202195731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 202295731ebbSXiaoguang Chen policy->governor_enabled = true; 202395731ebbSXiaoguang Chen 20241da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 20251da177e4SLinus Torvalds 20264d5dcc42SViresh Kumar if (!ret) { 20274d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 20288e53695fSViresh Kumar policy->governor->initialized++; 20294d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 20308e53695fSViresh Kumar policy->governor->initialized--; 203195731ebbSXiaoguang Chen } else { 203295731ebbSXiaoguang Chen /* Restore original values */ 203395731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 203495731ebbSXiaoguang Chen policy->governor_enabled = true; 203595731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 203695731ebbSXiaoguang Chen policy->governor_enabled = false; 20374d5dcc42SViresh Kumar } 2038b394058fSViresh Kumar 2039fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 2040fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 20411da177e4SLinus Torvalds module_put(policy->governor->owner); 20421da177e4SLinus Torvalds 20431da177e4SLinus Torvalds return ret; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 20471da177e4SLinus Torvalds { 20483bcb09a3SJeremy Fitzhardinge int err; 20491da177e4SLinus Torvalds 20501da177e4SLinus Torvalds if (!governor) 20511da177e4SLinus Torvalds return -EINVAL; 20521da177e4SLinus Torvalds 2053a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2054a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2055a7b422cdSKonrad Rzeszutek Wilk 20563fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 20571da177e4SLinus Torvalds 2058b394058fSViresh Kumar governor->initialized = 0; 20593bcb09a3SJeremy Fitzhardinge err = -EBUSY; 206042f91fa1SViresh Kumar if (!find_governor(governor->name)) { 20613bcb09a3SJeremy Fitzhardinge err = 0; 20621da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 20633bcb09a3SJeremy Fitzhardinge } 20641da177e4SLinus Torvalds 20653fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20663bcb09a3SJeremy Fitzhardinge return err; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 20711da177e4SLinus Torvalds { 20724573237bSViresh Kumar struct cpufreq_policy *policy; 20734573237bSViresh Kumar unsigned long flags; 207490e41bacSPrarit Bhargava 20751da177e4SLinus Torvalds if (!governor) 20761da177e4SLinus Torvalds return; 20771da177e4SLinus Torvalds 2078a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2079a7b422cdSKonrad Rzeszutek Wilk return; 2080a7b422cdSKonrad Rzeszutek Wilk 20814573237bSViresh Kumar /* clear last_governor for all inactive policies */ 20824573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 20834573237bSViresh Kumar for_each_inactive_policy(policy) { 208418bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 208518bf3a12SViresh Kumar policy->governor = NULL; 20864573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 208790e41bacSPrarit Bhargava } 208818bf3a12SViresh Kumar } 20894573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 209090e41bacSPrarit Bhargava 20913fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 20921da177e4SLinus Torvalds list_del(&governor->governor_list); 20933fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 20941da177e4SLinus Torvalds return; 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 20971da177e4SLinus Torvalds 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds /********************************************************************* 21001da177e4SLinus Torvalds * POLICY INTERFACE * 21011da177e4SLinus Torvalds *********************************************************************/ 21021da177e4SLinus Torvalds 21031da177e4SLinus Torvalds /** 21041da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 210529464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 210629464f28SDave Jones * is written 21071da177e4SLinus Torvalds * 21081da177e4SLinus Torvalds * Reads the current cpufreq policy. 21091da177e4SLinus Torvalds */ 21101da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 21111da177e4SLinus Torvalds { 21121da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 21131da177e4SLinus Torvalds if (!policy) 21141da177e4SLinus Torvalds return -EINVAL; 21151da177e4SLinus Torvalds 21161da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 21171da177e4SLinus Torvalds if (!cpu_policy) 21181da177e4SLinus Torvalds return -EINVAL; 21191da177e4SLinus Torvalds 2120d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 21211da177e4SLinus Torvalds 21221da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 21231da177e4SLinus Torvalds return 0; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 21261da177e4SLinus Torvalds 2127153d7f3fSArjan van de Ven /* 2128037ce839SViresh Kumar * policy : current policy. 2129037ce839SViresh Kumar * new_policy: policy to be set. 2130153d7f3fSArjan van de Ven */ 2131037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 21323a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 21331da177e4SLinus Torvalds { 2134d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2135d9a789c7SRafael J. Wysocki int ret; 21361da177e4SLinus Torvalds 2137e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2138e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 21391da177e4SLinus Torvalds 2140d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 21411da177e4SLinus Torvalds 2142fba9573bSPan Xinhui /* 2143fba9573bSPan Xinhui * This check works well when we store new min/max freq attributes, 2144fba9573bSPan Xinhui * because new_policy is a copy of policy with one field updated. 2145fba9573bSPan Xinhui */ 2146fba9573bSPan Xinhui if (new_policy->min > new_policy->max) 2147d9a789c7SRafael J. Wysocki return -EINVAL; 21489c9a43edSMattia Dongili 21491da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 21503a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 21511da177e4SLinus Torvalds if (ret) 2152d9a789c7SRafael J. Wysocki return ret; 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2155e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21563a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 21571da177e4SLinus Torvalds 2158bb176f7dSViresh Kumar /* 2159bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2160bb176f7dSViresh Kumar * different to the first one 2161bb176f7dSViresh Kumar */ 21623a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2163e041c683SAlan Stern if (ret) 2164d9a789c7SRafael J. Wysocki return ret; 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds /* notification of the new policy */ 2167e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 21683a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 21691da177e4SLinus Torvalds 21703a3e9e06SViresh Kumar policy->min = new_policy->min; 21713a3e9e06SViresh Kumar policy->max = new_policy->max; 21721da177e4SLinus Torvalds 21732d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 21743a3e9e06SViresh Kumar policy->min, policy->max); 21751da177e4SLinus Torvalds 21761c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 21773a3e9e06SViresh Kumar policy->policy = new_policy->policy; 21782d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2179d9a789c7SRafael J. Wysocki return cpufreq_driver->setpolicy(new_policy); 2180d9a789c7SRafael J. Wysocki } 2181d9a789c7SRafael J. Wysocki 2182d9a789c7SRafael J. Wysocki if (new_policy->governor == policy->governor) 2183d9a789c7SRafael J. Wysocki goto out; 21841da177e4SLinus Torvalds 21852d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 21861da177e4SLinus Torvalds 2187d9a789c7SRafael J. Wysocki /* save old, working values */ 2188d9a789c7SRafael J. Wysocki old_gov = policy->governor; 21891da177e4SLinus Torvalds /* end old governor */ 2190d9a789c7SRafael J. Wysocki if (old_gov) { 2191*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 21924bc384aeSViresh Kumar if (ret) { 21934bc384aeSViresh Kumar /* This can happen due to race with other operations */ 21944bc384aeSViresh Kumar pr_debug("%s: Failed to Stop Governor: %s (%d)\n", 21954bc384aeSViresh Kumar __func__, old_gov->name, ret); 21964bc384aeSViresh Kumar return ret; 21974bc384aeSViresh Kumar } 21984bc384aeSViresh Kumar 2199*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 22004bc384aeSViresh Kumar if (ret) { 22014bc384aeSViresh Kumar pr_err("%s: Failed to Exit Governor: %s (%d)\n", 22024bc384aeSViresh Kumar __func__, old_gov->name, ret); 22034bc384aeSViresh Kumar return ret; 22044bc384aeSViresh Kumar } 22057bd353a9SViresh Kumar } 22061da177e4SLinus Torvalds 22071da177e4SLinus Torvalds /* start new governor */ 22083a3e9e06SViresh Kumar policy->governor = new_policy->governor; 2209*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); 22104bc384aeSViresh Kumar if (!ret) { 2211*a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 22124bc384aeSViresh Kumar if (!ret) 2213d9a789c7SRafael J. Wysocki goto out; 2214d9a789c7SRafael J. Wysocki 2215*a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2216955ef483SViresh Kumar } 22177bd353a9SViresh Kumar 22181da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2219d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 22201da177e4SLinus Torvalds if (old_gov) { 22213a3e9e06SViresh Kumar policy->governor = old_gov; 2222*a1317e09SViresh Kumar if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) 22234bc384aeSViresh Kumar policy->governor = NULL; 22244bc384aeSViresh Kumar else 2225*a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_START); 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds 22284bc384aeSViresh Kumar return ret; 2229d9a789c7SRafael J. Wysocki 2230d9a789c7SRafael J. Wysocki out: 2231d9a789c7SRafael J. Wysocki pr_debug("governor: change or update limits\n"); 2232*a1317e09SViresh Kumar return cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 22331da177e4SLinus Torvalds } 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds /** 22361da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 22371da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 22381da177e4SLinus Torvalds * 223925985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 22401da177e4SLinus Torvalds * at different times. 22411da177e4SLinus Torvalds */ 22421da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 22431da177e4SLinus Torvalds { 22443a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 22453a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 2246f1829e4aSJulia Lawall int ret; 22471da177e4SLinus Torvalds 2248fefa8ff8SAaron Plattner if (!policy) 2249fefa8ff8SAaron Plattner return -ENODEV; 22501da177e4SLinus Torvalds 2251ad7722daSviresh kumar down_write(&policy->rwsem); 22521da177e4SLinus Torvalds 22532d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 2254d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 22553a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 22563a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 22571da177e4SLinus Torvalds 2258bb176f7dSViresh Kumar /* 2259bb176f7dSViresh Kumar * BIOS might change freq behind our back 2260bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2261bb176f7dSViresh Kumar */ 22622ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 22633a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 2264bd0fa9bbSViresh Kumar if (WARN_ON(!new_policy.cur)) { 2265bd0fa9bbSViresh Kumar ret = -EIO; 2266fefa8ff8SAaron Plattner goto unlock; 2267bd0fa9bbSViresh Kumar } 2268bd0fa9bbSViresh Kumar 22693a3e9e06SViresh Kumar if (!policy->cur) { 2270e837f9b5SJoe Perches pr_debug("Driver did not initialize current freq\n"); 22713a3e9e06SViresh Kumar policy->cur = new_policy.cur; 2272a85f7bd3SThomas Renninger } else { 22739c0ebcf7SViresh Kumar if (policy->cur != new_policy.cur && has_target()) 2274a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, new_policy.cur); 22750961dd0dSThomas Renninger } 2276a85f7bd3SThomas Renninger } 22770961dd0dSThomas Renninger 2278037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 22791da177e4SLinus Torvalds 2280fefa8ff8SAaron Plattner unlock: 2281ad7722daSviresh kumar up_write(&policy->rwsem); 22825a01f2e8SVenkatesh Pallipadi 22833a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 22841da177e4SLinus Torvalds return ret; 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 22871da177e4SLinus Torvalds 22882760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 2289c32b6b8eSAshok Raj unsigned long action, void *hcpu) 2290c32b6b8eSAshok Raj { 2291c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 2292c32b6b8eSAshok Raj 22935302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 2294c32b6b8eSAshok Raj case CPU_ONLINE: 22950b275352SRafael J. Wysocki cpufreq_online(cpu); 2296c32b6b8eSAshok Raj break; 22975302c3fbSSrivatsa S. Bhat 2298c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 229969cee714SViresh Kumar cpufreq_offline(cpu); 2300c32b6b8eSAshok Raj break; 23015302c3fbSSrivatsa S. Bhat 23025a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 23030b275352SRafael J. Wysocki cpufreq_online(cpu); 2304c32b6b8eSAshok Raj break; 2305c32b6b8eSAshok Raj } 2306c32b6b8eSAshok Raj return NOTIFY_OK; 2307c32b6b8eSAshok Raj } 2308c32b6b8eSAshok Raj 23099c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2310c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2311c32b6b8eSAshok Raj }; 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds /********************************************************************* 23146f19efc0SLukasz Majewski * BOOST * 23156f19efc0SLukasz Majewski *********************************************************************/ 23166f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 23176f19efc0SLukasz Majewski { 23186f19efc0SLukasz Majewski struct cpufreq_frequency_table *freq_table; 23196f19efc0SLukasz Majewski struct cpufreq_policy *policy; 23206f19efc0SLukasz Majewski int ret = -EINVAL; 23216f19efc0SLukasz Majewski 2322f963735aSViresh Kumar for_each_active_policy(policy) { 23236f19efc0SLukasz Majewski freq_table = cpufreq_frequency_get_table(policy->cpu); 23246f19efc0SLukasz Majewski if (freq_table) { 23256f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 23266f19efc0SLukasz Majewski freq_table); 23276f19efc0SLukasz Majewski if (ret) { 23286f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 23296f19efc0SLukasz Majewski __func__); 23306f19efc0SLukasz Majewski break; 23316f19efc0SLukasz Majewski } 233249f18560SViresh Kumar 233349f18560SViresh Kumar down_write(&policy->rwsem); 23346f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 2335*a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 233649f18560SViresh Kumar up_write(&policy->rwsem); 23376f19efc0SLukasz Majewski } 23386f19efc0SLukasz Majewski } 23396f19efc0SLukasz Majewski 23406f19efc0SLukasz Majewski return ret; 23416f19efc0SLukasz Majewski } 23426f19efc0SLukasz Majewski 23436f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 23446f19efc0SLukasz Majewski { 23456f19efc0SLukasz Majewski unsigned long flags; 23466f19efc0SLukasz Majewski int ret = 0; 23476f19efc0SLukasz Majewski 23486f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 23496f19efc0SLukasz Majewski return 0; 23506f19efc0SLukasz Majewski 23516f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 23526f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 23536f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23546f19efc0SLukasz Majewski 23556f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 23566f19efc0SLukasz Majewski if (ret) { 23576f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 23586f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 23596f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23606f19efc0SLukasz Majewski 2361e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2362e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 23636f19efc0SLukasz Majewski } 23646f19efc0SLukasz Majewski 23656f19efc0SLukasz Majewski return ret; 23666f19efc0SLukasz Majewski } 23676f19efc0SLukasz Majewski 236841669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void) 23696f19efc0SLukasz Majewski { 23707a6c79f2SRafael J. Wysocki return likely(cpufreq_driver) && cpufreq_driver->set_boost; 23716f19efc0SLukasz Majewski } 23726f19efc0SLukasz Majewski 237344139ed4SViresh Kumar static int create_boost_sysfs_file(void) 237444139ed4SViresh Kumar { 237544139ed4SViresh Kumar int ret; 237644139ed4SViresh Kumar 2377c82bd444SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 237844139ed4SViresh Kumar if (ret) 237944139ed4SViresh Kumar pr_err("%s: cannot register global BOOST sysfs file\n", 238044139ed4SViresh Kumar __func__); 238144139ed4SViresh Kumar 238244139ed4SViresh Kumar return ret; 238344139ed4SViresh Kumar } 238444139ed4SViresh Kumar 238544139ed4SViresh Kumar static void remove_boost_sysfs_file(void) 238644139ed4SViresh Kumar { 238744139ed4SViresh Kumar if (cpufreq_boost_supported()) 2388c82bd444SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 238944139ed4SViresh Kumar } 239044139ed4SViresh Kumar 239144139ed4SViresh Kumar int cpufreq_enable_boost_support(void) 239244139ed4SViresh Kumar { 239344139ed4SViresh Kumar if (!cpufreq_driver) 239444139ed4SViresh Kumar return -EINVAL; 239544139ed4SViresh Kumar 239644139ed4SViresh Kumar if (cpufreq_boost_supported()) 239744139ed4SViresh Kumar return 0; 239844139ed4SViresh Kumar 23997a6c79f2SRafael J. Wysocki cpufreq_driver->set_boost = cpufreq_boost_set_sw; 240044139ed4SViresh Kumar 240144139ed4SViresh Kumar /* This will get removed on driver unregister */ 240244139ed4SViresh Kumar return create_boost_sysfs_file(); 240344139ed4SViresh Kumar } 240444139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 240544139ed4SViresh Kumar 24066f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 24076f19efc0SLukasz Majewski { 24086f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 24096f19efc0SLukasz Majewski } 24106f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 24116f19efc0SLukasz Majewski 24126f19efc0SLukasz Majewski /********************************************************************* 24131da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 24141da177e4SLinus Torvalds *********************************************************************/ 24151da177e4SLinus Torvalds 24161da177e4SLinus Torvalds /** 24171da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 24181da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 24191da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 24201da177e4SLinus Torvalds * 24211da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 24221da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 24231da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 24241da177e4SLinus Torvalds * 24251da177e4SLinus Torvalds */ 2426221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 24271da177e4SLinus Torvalds { 24281da177e4SLinus Torvalds unsigned long flags; 24291da177e4SLinus Torvalds int ret; 24301da177e4SLinus Torvalds 2431a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2432a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2433a7b422cdSKonrad Rzeszutek Wilk 24341da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 24359c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 24369832235fSRafael J. Wysocki driver_data->target) || 24379832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 24381c03a2d0SViresh Kumar driver_data->target)) || 24391c03a2d0SViresh Kumar (!!driver_data->get_intermediate != !!driver_data->target_intermediate)) 24401da177e4SLinus Torvalds return -EINVAL; 24411da177e4SLinus Torvalds 24422d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 24431da177e4SLinus Torvalds 2444fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2445fdd320daSRafael J. Wysocki get_online_cpus(); 2446fdd320daSRafael J. Wysocki 24470d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24481c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 24490d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2450fdd320daSRafael J. Wysocki ret = -EEXIST; 2451fdd320daSRafael J. Wysocki goto out; 24521da177e4SLinus Torvalds } 24531c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 24540d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24551da177e4SLinus Torvalds 2456bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2457bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2458bc68b7dfSViresh Kumar 24597a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 246044139ed4SViresh Kumar ret = create_boost_sysfs_file(); 246144139ed4SViresh Kumar if (ret) 24626f19efc0SLukasz Majewski goto err_null_driver; 24637a6c79f2SRafael J. Wysocki } 24646f19efc0SLukasz Majewski 24658a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 24668f5bc2abSJiri Slaby if (ret) 24676f19efc0SLukasz Majewski goto err_boost_unreg; 24681da177e4SLinus Torvalds 2469ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2470ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 24711da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 2472ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2473e08f5f5bSGautham R Shenoy driver_data->name); 24748a25a2fdSKay Sievers goto err_if_unreg; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 247765edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 24782d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 24791da177e4SLinus Torvalds 2480fdd320daSRafael J. Wysocki out: 2481fdd320daSRafael J. Wysocki put_online_cpus(); 2482fdd320daSRafael J. Wysocki return ret; 2483fdd320daSRafael J. Wysocki 24848a25a2fdSKay Sievers err_if_unreg: 24858a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 24866f19efc0SLukasz Majewski err_boost_unreg: 248744139ed4SViresh Kumar remove_boost_sysfs_file(); 24888f5bc2abSJiri Slaby err_null_driver: 24890d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24901c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24910d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2492fdd320daSRafael J. Wysocki goto out; 24931da177e4SLinus Torvalds } 24941da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 24951da177e4SLinus Torvalds 24961da177e4SLinus Torvalds /** 24971da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 24981da177e4SLinus Torvalds * 24991da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 25001da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 25011da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 25021da177e4SLinus Torvalds * currently not initialised. 25031da177e4SLinus Torvalds */ 2504221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 25051da177e4SLinus Torvalds { 25061da177e4SLinus Torvalds unsigned long flags; 25071da177e4SLinus Torvalds 25081c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 25091da177e4SLinus Torvalds return -EINVAL; 25101da177e4SLinus Torvalds 25112d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 25121da177e4SLinus Torvalds 2513454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2514454d3a25SSebastian Andrzej Siewior get_online_cpus(); 25158a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 251644139ed4SViresh Kumar remove_boost_sysfs_file(); 251765edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 25181da177e4SLinus Torvalds 25190d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 25206eed9404SViresh Kumar 25211c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 25226eed9404SViresh Kumar 25230d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2524454d3a25SSebastian Andrzej Siewior put_online_cpus(); 25251da177e4SLinus Torvalds 25261da177e4SLinus Torvalds return 0; 25271da177e4SLinus Torvalds } 25281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 25295a01f2e8SVenkatesh Pallipadi 253090de2a4aSDoug Anderson /* 253190de2a4aSDoug Anderson * Stop cpufreq at shutdown to make sure it isn't holding any locks 253290de2a4aSDoug Anderson * or mutexes when secondary CPUs are halted. 253390de2a4aSDoug Anderson */ 253490de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = { 253590de2a4aSDoug Anderson .shutdown = cpufreq_suspend, 253690de2a4aSDoug Anderson }; 253790de2a4aSDoug Anderson 2538c82bd444SViresh Kumar struct kobject *cpufreq_global_kobject; 2539c82bd444SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 2540c82bd444SViresh Kumar 25415a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 25425a01f2e8SVenkatesh Pallipadi { 2543a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2544a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2545a7b422cdSKonrad Rzeszutek Wilk 25468eec1020SViresh Kumar cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); 25478aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 25488aa84ad8SThomas Renninger 254990de2a4aSDoug Anderson register_syscore_ops(&cpufreq_syscore_ops); 255090de2a4aSDoug Anderson 25515a01f2e8SVenkatesh Pallipadi return 0; 25525a01f2e8SVenkatesh Pallipadi } 25535a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2554