Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/cpufreq/cpufreq.c |
| 3 | * |
| 4 | * Copyright (C) 2001 Russell King |
| 5 | * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 6 | * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 8 | * Oct 2005 - Ashok Raj <ashok.raj@intel.com> |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 9 | * Added handling for CPU hotplug |
Dave Jones | 8ff6973 | 2006-03-05 03:37:23 -0500 | [diff] [blame] | 10 | * Feb 2006 - Jacob Shin <jacob.shin@amd.com> |
| 11 | * Fix handling for CPU hotplug -- affected CPUs |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | */ |
| 17 | |
Viresh Kumar | db70115 | 2012-10-23 01:29:03 +0200 | [diff] [blame] | 18 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 19 | |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 20 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/cpufreq.h> |
| 22 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/device.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 24 | #include <linux/init.h> |
| 25 | #include <linux/kernel_stat.h> |
| 26 | #include <linux/module.h> |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 27 | #include <linux/mutex.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 28 | #include <linux/slab.h> |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 29 | #include <linux/suspend.h> |
Doug Anderson | 90de2a4 | 2014-12-23 22:09:48 -0800 | [diff] [blame] | 30 | #include <linux/syscore_ops.h> |
Viresh Kumar | 5ff0a26 | 2013-08-06 22:53:03 +0530 | [diff] [blame] | 31 | #include <linux/tick.h> |
Thomas Renninger | 6f4f272 | 2010-04-20 13:17:36 +0200 | [diff] [blame] | 32 | #include <trace/events/power.h> |
| 33 | |
Viresh Kumar | b4f0676 | 2015-01-27 14:06:08 +0530 | [diff] [blame] | 34 | static LIST_HEAD(cpufreq_policy_list); |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 35 | |
| 36 | static inline bool policy_is_inactive(struct cpufreq_policy *policy) |
| 37 | { |
| 38 | return cpumask_empty(policy->cpus); |
| 39 | } |
| 40 | |
| 41 | static bool suitable_policy(struct cpufreq_policy *policy, bool active) |
| 42 | { |
| 43 | return active == !policy_is_inactive(policy); |
| 44 | } |
| 45 | |
| 46 | /* Finds Next Acive/Inactive policy */ |
| 47 | static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy, |
| 48 | bool active) |
| 49 | { |
| 50 | do { |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 51 | /* No more policies in the list */ |
Gautham R Shenoy | 2dadfd7 | 2016-01-27 12:02:26 +0530 | [diff] [blame] | 52 | if (list_is_last(&policy->policy_list, &cpufreq_policy_list)) |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 53 | return NULL; |
Gautham R Shenoy | 2dadfd7 | 2016-01-27 12:02:26 +0530 | [diff] [blame] | 54 | |
| 55 | policy = list_next_entry(policy, policy_list); |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 56 | } while (!suitable_policy(policy, active)); |
| 57 | |
| 58 | return policy; |
| 59 | } |
| 60 | |
| 61 | static struct cpufreq_policy *first_policy(bool active) |
| 62 | { |
| 63 | struct cpufreq_policy *policy; |
| 64 | |
| 65 | /* No policies in the list */ |
| 66 | if (list_empty(&cpufreq_policy_list)) |
| 67 | return NULL; |
| 68 | |
| 69 | policy = list_first_entry(&cpufreq_policy_list, typeof(*policy), |
| 70 | policy_list); |
| 71 | |
| 72 | if (!suitable_policy(policy, active)) |
| 73 | policy = next_policy(policy, active); |
| 74 | |
| 75 | return policy; |
| 76 | } |
| 77 | |
| 78 | /* Macros to iterate over CPU policies */ |
| 79 | #define for_each_suitable_policy(__policy, __active) \ |
| 80 | for (__policy = first_policy(__active); \ |
| 81 | __policy; \ |
| 82 | __policy = next_policy(__policy, __active)) |
| 83 | |
| 84 | #define for_each_active_policy(__policy) \ |
| 85 | for_each_suitable_policy(__policy, true) |
| 86 | #define for_each_inactive_policy(__policy) \ |
| 87 | for_each_suitable_policy(__policy, false) |
| 88 | |
| 89 | #define for_each_policy(__policy) \ |
Viresh Kumar | b4f0676 | 2015-01-27 14:06:08 +0530 | [diff] [blame] | 90 | list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) |
| 91 | |
Viresh Kumar | f7b2706 | 2015-01-27 14:06:09 +0530 | [diff] [blame] | 92 | /* Iterate over governors */ |
| 93 | static LIST_HEAD(cpufreq_governor_list); |
| 94 | #define for_each_governor(__governor) \ |
| 95 | list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) |
| 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /** |
Dave Jones | cd87847 | 2006-08-11 17:59:28 -0400 | [diff] [blame] | 98 | * The "cpufreq driver" - the arch- or hardware-dependent low |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * level driver of CPUFreq support, and its spinlock. This lock |
| 100 | * also protects the cpufreq_cpu_data array. |
| 101 | */ |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 102 | static struct cpufreq_driver *cpufreq_driver; |
Mike Travis | 7a6aedf | 2008-03-25 15:06:53 -0700 | [diff] [blame] | 103 | static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 104 | static DEFINE_RWLOCK(cpufreq_driver_lock); |
Rafael J. Wysocki | 34e2c55 | 2016-02-15 20:20:42 +0100 | [diff] [blame] | 105 | |
| 106 | static DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data); |
| 107 | |
| 108 | /** |
| 109 | * cpufreq_set_update_util_data - Populate the CPU's update_util_data pointer. |
| 110 | * @cpu: The CPU to set the pointer for. |
| 111 | * @data: New pointer value. |
| 112 | * |
| 113 | * Set and publish the update_util_data pointer for the given CPU. That pointer |
| 114 | * points to a struct update_util_data object containing a callback function |
| 115 | * to call from cpufreq_update_util(). That function will be called from an RCU |
| 116 | * read-side critical section, so it must not sleep. |
| 117 | * |
| 118 | * Callers must use RCU callbacks to free any memory that might be accessed |
| 119 | * via the old update_util_data pointer or invoke synchronize_rcu() right after |
| 120 | * this function to avoid use-after-free. |
| 121 | */ |
| 122 | void cpufreq_set_update_util_data(int cpu, struct update_util_data *data) |
| 123 | { |
| 124 | rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data); |
| 125 | } |
| 126 | EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data); |
| 127 | |
| 128 | /** |
| 129 | * cpufreq_update_util - Take a note about CPU utilization changes. |
| 130 | * @time: Current time. |
| 131 | * @util: Current utilization. |
| 132 | * @max: Utilization ceiling. |
| 133 | * |
| 134 | * This function is called by the scheduler on every invocation of |
| 135 | * update_load_avg() on the CPU whose utilization is being updated. |
| 136 | */ |
| 137 | void cpufreq_update_util(u64 time, unsigned long util, unsigned long max) |
| 138 | { |
| 139 | struct update_util_data *data; |
| 140 | |
| 141 | rcu_read_lock(); |
| 142 | |
| 143 | data = rcu_dereference(*this_cpu_ptr(&cpufreq_update_util_data)); |
| 144 | if (data && data->func) |
| 145 | data->func(data, time, util, max); |
| 146 | |
| 147 | rcu_read_unlock(); |
| 148 | } |
| 149 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 150 | /* Flag to suspend/resume CPUFreq governors */ |
| 151 | static bool cpufreq_suspended; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 153 | static inline bool has_target(void) |
| 154 | { |
| 155 | return cpufreq_driver->target_index || cpufreq_driver->target; |
| 156 | } |
| 157 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | /* internal prototypes */ |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 159 | static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 160 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 163 | * Two notifier lists: the "policy" list is involved in the |
| 164 | * validation process for a new CPU frequency policy; the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | * "transition" list for kernel code that needs to handle |
| 166 | * changes to devices when the CPU clock speed changes. |
| 167 | * The mutex locks both lists. |
| 168 | */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 169 | static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 170 | static struct srcu_notifier_head cpufreq_transition_notifier_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Cesar Eduardo Barros | 74212ca | 2008-02-16 08:41:24 -0200 | [diff] [blame] | 172 | static bool init_cpufreq_transition_notifier_list_called; |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 173 | static int __init init_cpufreq_transition_notifier_list(void) |
| 174 | { |
| 175 | srcu_init_notifier_head(&cpufreq_transition_notifier_list); |
Cesar Eduardo Barros | 74212ca | 2008-02-16 08:41:24 -0200 | [diff] [blame] | 176 | init_cpufreq_transition_notifier_list_called = true; |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 177 | return 0; |
| 178 | } |
Linus Torvalds | b3438f8 | 2006-11-20 11:47:18 -0800 | [diff] [blame] | 179 | pure_initcall(init_cpufreq_transition_notifier_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 181 | static int off __read_mostly; |
Viresh Kumar | da58445 | 2012-10-26 00:51:32 +0200 | [diff] [blame] | 182 | static int cpufreq_disabled(void) |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 183 | { |
| 184 | return off; |
| 185 | } |
| 186 | void disable_cpufreq(void) |
| 187 | { |
| 188 | off = 1; |
| 189 | } |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 190 | static DEFINE_MUTEX(cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 192 | bool have_governor_per_policy(void) |
| 193 | { |
Viresh Kumar | 0b981e7 | 2013-10-02 14:13:18 +0530 | [diff] [blame] | 194 | return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 195 | } |
Viresh Kumar | 3f869d6 | 2013-05-16 05:09:56 +0000 | [diff] [blame] | 196 | EXPORT_SYMBOL_GPL(have_governor_per_policy); |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 197 | |
Viresh Kumar | 944e9a0 | 2013-05-16 05:09:57 +0000 | [diff] [blame] | 198 | struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) |
| 199 | { |
| 200 | if (have_governor_per_policy()) |
| 201 | return &policy->kobj; |
| 202 | else |
| 203 | return cpufreq_global_kobject; |
| 204 | } |
| 205 | EXPORT_SYMBOL_GPL(get_governor_parent_kobj); |
| 206 | |
Viresh Kumar | 5a31d59 | 2015-07-10 01:43:27 +0200 | [diff] [blame] | 207 | struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) |
| 208 | { |
| 209 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
| 210 | |
| 211 | return policy && !policy_is_inactive(policy) ? |
| 212 | policy->freq_table : NULL; |
| 213 | } |
| 214 | EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); |
| 215 | |
Viresh Kumar | 72a4ce3 | 2013-05-17 11:26:32 +0000 | [diff] [blame] | 216 | static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) |
| 217 | { |
| 218 | u64 idle_time; |
| 219 | u64 cur_wall_time; |
| 220 | u64 busy_time; |
| 221 | |
| 222 | cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); |
| 223 | |
| 224 | busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; |
| 225 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; |
| 226 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; |
| 227 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; |
| 228 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; |
| 229 | busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; |
| 230 | |
| 231 | idle_time = cur_wall_time - busy_time; |
| 232 | if (wall) |
| 233 | *wall = cputime_to_usecs(cur_wall_time); |
| 234 | |
| 235 | return cputime_to_usecs(idle_time); |
| 236 | } |
| 237 | |
| 238 | u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) |
| 239 | { |
| 240 | u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); |
| 241 | |
| 242 | if (idle_time == -1ULL) |
| 243 | return get_cpu_idle_time_jiffy(cpu, wall); |
| 244 | else if (!io_busy) |
| 245 | idle_time += get_cpu_iowait_time_us(cpu, wall); |
| 246 | |
| 247 | return idle_time; |
| 248 | } |
| 249 | EXPORT_SYMBOL_GPL(get_cpu_idle_time); |
| 250 | |
Viresh Kumar | 70e9e77 | 2013-10-03 20:29:07 +0530 | [diff] [blame] | 251 | /* |
| 252 | * This is a generic cpufreq init() routine which can be used by cpufreq |
| 253 | * drivers of SMP systems. It will do following: |
| 254 | * - validate & show freq table passed |
| 255 | * - set policies transition latency |
| 256 | * - policy->cpus with all possible CPUs |
| 257 | */ |
| 258 | int cpufreq_generic_init(struct cpufreq_policy *policy, |
| 259 | struct cpufreq_frequency_table *table, |
| 260 | unsigned int transition_latency) |
| 261 | { |
| 262 | int ret; |
| 263 | |
| 264 | ret = cpufreq_table_validate_and_show(policy, table); |
| 265 | if (ret) { |
| 266 | pr_err("%s: invalid frequency table: %d\n", __func__, ret); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | policy->cpuinfo.transition_latency = transition_latency; |
| 271 | |
| 272 | /* |
Shailendra Verma | 58405af | 2015-05-22 22:48:22 +0530 | [diff] [blame] | 273 | * The driver only supports the SMP configuration where all processors |
Viresh Kumar | 70e9e77 | 2013-10-03 20:29:07 +0530 | [diff] [blame] | 274 | * share the clock and voltage and clock. |
| 275 | */ |
| 276 | cpumask_setall(policy->cpus); |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | EXPORT_SYMBOL_GPL(cpufreq_generic_init); |
| 281 | |
Rafael J. Wysocki | 1f0bd44 | 2015-09-16 02:17:49 +0200 | [diff] [blame] | 282 | struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 283 | { |
| 284 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
| 285 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 286 | return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; |
| 287 | } |
Rafael J. Wysocki | 1f0bd44 | 2015-09-16 02:17:49 +0200 | [diff] [blame] | 288 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 289 | |
| 290 | unsigned int cpufreq_generic_get(unsigned int cpu) |
| 291 | { |
| 292 | struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); |
| 293 | |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 294 | if (!policy || IS_ERR(policy->clk)) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 295 | pr_err("%s: No %s associated to cpu: %d\n", |
| 296 | __func__, policy ? "clk" : "policy", cpu); |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | return clk_get_rate(policy->clk) / 1000; |
| 301 | } |
| 302 | EXPORT_SYMBOL_GPL(cpufreq_generic_get); |
| 303 | |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 304 | /** |
| 305 | * cpufreq_cpu_get: returns policy for a cpu and marks it busy. |
| 306 | * |
| 307 | * @cpu: cpu to find policy for. |
| 308 | * |
| 309 | * This returns policy for 'cpu', returns NULL if it doesn't exist. |
| 310 | * It also increments the kobject reference count to mark it busy and so would |
| 311 | * require a corresponding call to cpufreq_cpu_put() to decrement it back. |
| 312 | * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be |
| 313 | * freed as that depends on the kobj count. |
| 314 | * |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 315 | * Return: A valid policy on success, otherwise NULL on failure. |
| 316 | */ |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 317 | struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 319 | struct cpufreq_policy *policy = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | unsigned long flags; |
| 321 | |
Viresh Kumar | 1b947c9 | 2015-02-19 17:02:05 +0530 | [diff] [blame] | 322 | if (WARN_ON(cpu >= nr_cpu_ids)) |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 323 | return NULL; |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* get the cpufreq driver */ |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 326 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 328 | if (cpufreq_driver) { |
| 329 | /* get the CPU */ |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 330 | policy = cpufreq_cpu_get_raw(cpu); |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 331 | if (policy) |
| 332 | kobject_get(&policy->kobj); |
| 333 | } |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 334 | |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 335 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 337 | return policy; |
Stephen Boyd | a914443 | 2012-07-20 18:14:38 +0000 | [diff] [blame] | 338 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | EXPORT_SYMBOL_GPL(cpufreq_cpu_get); |
| 340 | |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 341 | /** |
| 342 | * cpufreq_cpu_put: Decrements the usage count of a policy |
| 343 | * |
| 344 | * @policy: policy earlier returned by cpufreq_cpu_get(). |
| 345 | * |
| 346 | * This decrements the kobject reference count incremented earlier by calling |
| 347 | * cpufreq_cpu_get(). |
Viresh Kumar | 50e9c85 | 2015-02-19 17:02:03 +0530 | [diff] [blame] | 348 | */ |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 349 | void cpufreq_cpu_put(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | { |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 351 | kobject_put(&policy->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | EXPORT_SYMBOL_GPL(cpufreq_cpu_put); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | /********************************************************************* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | * EXTERNALLY AFFECTING FREQUENCY CHANGES * |
| 357 | *********************************************************************/ |
| 358 | |
| 359 | /** |
| 360 | * adjust_jiffies - adjust the system "loops_per_jiffy" |
| 361 | * |
| 362 | * This function alters the system "loops_per_jiffy" for the clock |
| 363 | * speed change. Note that loops_per_jiffy cannot be updated on SMP |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 364 | * systems as each CPU might be scaled differently. So, use the arch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | * per-CPU loops_per_jiffy value wherever possible. |
| 366 | */ |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 367 | static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | { |
Viresh Kumar | 39c132e | 2015-01-02 12:34:34 +0530 | [diff] [blame] | 369 | #ifndef CONFIG_SMP |
| 370 | static unsigned long l_p_j_ref; |
| 371 | static unsigned int l_p_j_ref_freq; |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | if (ci->flags & CPUFREQ_CONST_LOOPS) |
| 374 | return; |
| 375 | |
| 376 | if (!l_p_j_ref_freq) { |
| 377 | l_p_j_ref = loops_per_jiffy; |
| 378 | l_p_j_ref_freq = ci->old; |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 379 | pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", |
| 380 | l_p_j_ref, l_p_j_ref_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
Viresh Kumar | 0b443ea | 2014-03-19 11:24:58 +0530 | [diff] [blame] | 382 | if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 383 | loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, |
| 384 | ci->new); |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 385 | pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", |
| 386 | loops_per_jiffy, ci->new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | #endif |
Viresh Kumar | 39c132e | 2015-01-02 12:34:34 +0530 | [diff] [blame] | 389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Viresh Kumar | 0956df9c | 2013-06-19 14:19:34 +0530 | [diff] [blame] | 391 | static void __cpufreq_notify_transition(struct cpufreq_policy *policy, |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 392 | struct cpufreq_freqs *freqs, unsigned int state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | { |
| 394 | BUG_ON(irqs_disabled()); |
| 395 | |
Dirk Brandewie | d5aaffa | 2013-01-17 16:22:21 +0000 | [diff] [blame] | 396 | if (cpufreq_disabled()) |
| 397 | return; |
| 398 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 399 | freqs->flags = cpufreq_driver->flags; |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 400 | pr_debug("notification %u of frequency transition to %u kHz\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 401 | state, freqs->new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | switch (state) { |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | case CPUFREQ_PRECHANGE: |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 406 | /* detect if the driver reported a value as "old frequency" |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 407 | * which is not equal to what the cpufreq core thinks is |
| 408 | * "old frequency". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | */ |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 410 | if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 411 | if ((policy) && (policy->cpu == freqs->cpu) && |
| 412 | (policy->cur) && (policy->cur != freqs->old)) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 413 | pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", |
| 414 | freqs->old, policy->cur); |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 415 | freqs->old = policy->cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | } |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 418 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 419 | CPUFREQ_PRECHANGE, freqs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | adjust_jiffies(CPUFREQ_PRECHANGE, freqs); |
| 421 | break; |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 422 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | case CPUFREQ_POSTCHANGE: |
| 424 | adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 425 | pr_debug("FREQ: %lu - CPU: %lu\n", |
| 426 | (unsigned long)freqs->new, (unsigned long)freqs->cpu); |
Thomas Renninger | 25e4193 | 2011-01-03 17:50:44 +0100 | [diff] [blame] | 427 | trace_cpu_frequency(freqs->new, freqs->cpu); |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 428 | srcu_notifier_call_chain(&cpufreq_transition_notifier_list, |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 429 | CPUFREQ_POSTCHANGE, freqs); |
Dave Jones | e4472cb | 2006-01-31 15:53:55 -0800 | [diff] [blame] | 430 | if (likely(policy) && likely(policy->cpu == freqs->cpu)) |
| 431 | policy->cur = freqs->new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | break; |
| 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | } |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 435 | |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 436 | /** |
| 437 | * cpufreq_notify_transition - call notifier chain and adjust_jiffies |
| 438 | * on frequency transition. |
| 439 | * |
| 440 | * This function calls the transition notifiers and the "adjust_jiffies" |
| 441 | * function. It is called twice on all CPU frequency changes that have |
| 442 | * external effects. |
| 443 | */ |
Viresh Kumar | 236a980 | 2014-03-24 13:35:46 +0530 | [diff] [blame] | 444 | static void cpufreq_notify_transition(struct cpufreq_policy *policy, |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 445 | struct cpufreq_freqs *freqs, unsigned int state) |
| 446 | { |
| 447 | for_each_cpu(freqs->cpu, policy->cpus) |
| 448 | __cpufreq_notify_transition(policy, freqs, state); |
| 449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Viresh Kumar | f7ba3b4 | 2013-12-02 11:04:12 +0530 | [diff] [blame] | 451 | /* Do post notifications when there are chances that transition has failed */ |
Viresh Kumar | 236a980 | 2014-03-24 13:35:46 +0530 | [diff] [blame] | 452 | static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, |
Viresh Kumar | f7ba3b4 | 2013-12-02 11:04:12 +0530 | [diff] [blame] | 453 | struct cpufreq_freqs *freqs, int transition_failed) |
| 454 | { |
| 455 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); |
| 456 | if (!transition_failed) |
| 457 | return; |
| 458 | |
| 459 | swap(freqs->old, freqs->new); |
| 460 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); |
| 461 | cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); |
| 462 | } |
Viresh Kumar | f7ba3b4 | 2013-12-02 11:04:12 +0530 | [diff] [blame] | 463 | |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 464 | void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, |
| 465 | struct cpufreq_freqs *freqs) |
| 466 | { |
Srivatsa S. Bhat | ca654dc | 2014-05-05 12:52:39 +0530 | [diff] [blame] | 467 | |
| 468 | /* |
| 469 | * Catch double invocations of _begin() which lead to self-deadlock. |
| 470 | * ASYNC_NOTIFICATION drivers are left out because the cpufreq core |
| 471 | * doesn't invoke _begin() on their behalf, and hence the chances of |
| 472 | * double invocations are very low. Moreover, there are scenarios |
| 473 | * where these checks can emit false-positive warnings in these |
| 474 | * drivers; so we avoid that by skipping them altogether. |
| 475 | */ |
| 476 | WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) |
| 477 | && current == policy->transition_task); |
| 478 | |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 479 | wait: |
| 480 | wait_event(policy->transition_wait, !policy->transition_ongoing); |
| 481 | |
| 482 | spin_lock(&policy->transition_lock); |
| 483 | |
| 484 | if (unlikely(policy->transition_ongoing)) { |
| 485 | spin_unlock(&policy->transition_lock); |
| 486 | goto wait; |
| 487 | } |
| 488 | |
| 489 | policy->transition_ongoing = true; |
Srivatsa S. Bhat | ca654dc | 2014-05-05 12:52:39 +0530 | [diff] [blame] | 490 | policy->transition_task = current; |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 491 | |
| 492 | spin_unlock(&policy->transition_lock); |
| 493 | |
| 494 | cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); |
| 495 | } |
| 496 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); |
| 497 | |
| 498 | void cpufreq_freq_transition_end(struct cpufreq_policy *policy, |
| 499 | struct cpufreq_freqs *freqs, int transition_failed) |
| 500 | { |
| 501 | if (unlikely(WARN_ON(!policy->transition_ongoing))) |
| 502 | return; |
| 503 | |
| 504 | cpufreq_notify_post_transition(policy, freqs, transition_failed); |
| 505 | |
| 506 | policy->transition_ongoing = false; |
Srivatsa S. Bhat | ca654dc | 2014-05-05 12:52:39 +0530 | [diff] [blame] | 507 | policy->transition_task = NULL; |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 508 | |
| 509 | wake_up(&policy->transition_wait); |
| 510 | } |
| 511 | EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | /********************************************************************* |
| 515 | * SYSFS INTERFACE * |
| 516 | *********************************************************************/ |
Rashika Kheria | 8a5c74a | 2014-02-26 22:12:42 +0530 | [diff] [blame] | 517 | static ssize_t show_boost(struct kobject *kobj, |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 518 | struct attribute *attr, char *buf) |
| 519 | { |
| 520 | return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); |
| 521 | } |
| 522 | |
| 523 | static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, |
| 524 | const char *buf, size_t count) |
| 525 | { |
| 526 | int ret, enable; |
| 527 | |
| 528 | ret = sscanf(buf, "%d", &enable); |
| 529 | if (ret != 1 || enable < 0 || enable > 1) |
| 530 | return -EINVAL; |
| 531 | |
| 532 | if (cpufreq_boost_trigger_state(enable)) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 533 | pr_err("%s: Cannot %s BOOST!\n", |
| 534 | __func__, enable ? "enable" : "disable"); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 535 | return -EINVAL; |
| 536 | } |
| 537 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 538 | pr_debug("%s: cpufreq BOOST %s\n", |
| 539 | __func__, enable ? "enabled" : "disabled"); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 540 | |
| 541 | return count; |
| 542 | } |
| 543 | define_one_global_rw(boost); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 545 | static struct cpufreq_governor *find_governor(const char *str_governor) |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 546 | { |
| 547 | struct cpufreq_governor *t; |
| 548 | |
Viresh Kumar | f7b2706 | 2015-01-27 14:06:09 +0530 | [diff] [blame] | 549 | for_each_governor(t) |
Rasmus Villemoes | 7c4f453 | 2014-09-29 15:50:11 +0200 | [diff] [blame] | 550 | if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 551 | return t; |
| 552 | |
| 553 | return NULL; |
| 554 | } |
| 555 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | /** |
| 557 | * cpufreq_parse_governor - parse a governor string |
| 558 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 559 | static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | struct cpufreq_governor **governor) |
| 561 | { |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 562 | int err = -EINVAL; |
| 563 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 564 | if (cpufreq_driver->setpolicy) { |
Rasmus Villemoes | 7c4f453 | 2014-09-29 15:50:11 +0200 | [diff] [blame] | 565 | if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | *policy = CPUFREQ_POLICY_PERFORMANCE; |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 567 | err = 0; |
Rasmus Villemoes | 7c4f453 | 2014-09-29 15:50:11 +0200 | [diff] [blame] | 568 | } else if (!strncasecmp(str_governor, "powersave", |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 569 | CPUFREQ_NAME_LEN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | *policy = CPUFREQ_POLICY_POWERSAVE; |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 571 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
Viresh Kumar | 2e1cc3a | 2015-01-02 12:34:27 +0530 | [diff] [blame] | 573 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | struct cpufreq_governor *t; |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 575 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 576 | mutex_lock(&cpufreq_governor_mutex); |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 577 | |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 578 | t = find_governor(str_governor); |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 579 | |
Jeremy Fitzhardinge | ea71497 | 2006-07-06 12:32:01 -0700 | [diff] [blame] | 580 | if (t == NULL) { |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 581 | int ret; |
Jeremy Fitzhardinge | ea71497 | 2006-07-06 12:32:01 -0700 | [diff] [blame] | 582 | |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 583 | mutex_unlock(&cpufreq_governor_mutex); |
| 584 | ret = request_module("cpufreq_%s", str_governor); |
| 585 | mutex_lock(&cpufreq_governor_mutex); |
Jeremy Fitzhardinge | ea71497 | 2006-07-06 12:32:01 -0700 | [diff] [blame] | 586 | |
Kees Cook | 1a8e146 | 2011-05-04 08:38:56 -0700 | [diff] [blame] | 587 | if (ret == 0) |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 588 | t = find_governor(str_governor); |
Jeremy Fitzhardinge | ea71497 | 2006-07-06 12:32:01 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 591 | if (t != NULL) { |
| 592 | *governor = t; |
| 593 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 595 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 596 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | } |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 598 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | /** |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 602 | * cpufreq_per_cpu_attr_read() / show_##file_name() - |
| 603 | * print out cpufreq information |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | * |
| 605 | * Write out information from cpufreq_driver->policy[cpu]; object must be |
| 606 | * "unsigned int". |
| 607 | */ |
| 608 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 609 | #define show_one(file_name, object) \ |
| 610 | static ssize_t show_##file_name \ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 611 | (struct cpufreq_policy *policy, char *buf) \ |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 612 | { \ |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 613 | return sprintf(buf, "%u\n", policy->object); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | show_one(cpuinfo_min_freq, cpuinfo.min_freq); |
| 617 | show_one(cpuinfo_max_freq, cpuinfo.max_freq); |
Thomas Renninger | ed12978 | 2009-02-04 01:17:41 +0100 | [diff] [blame] | 618 | show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | show_one(scaling_min_freq, min); |
| 620 | show_one(scaling_max_freq, max); |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 621 | |
Viresh Kumar | 09347b2 | 2015-01-02 12:34:24 +0530 | [diff] [blame] | 622 | static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 623 | { |
| 624 | ssize_t ret; |
| 625 | |
| 626 | if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) |
| 627 | ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); |
| 628 | else |
| 629 | ret = sprintf(buf, "%u\n", policy->cur); |
| 630 | return ret; |
| 631 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 633 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 634 | struct cpufreq_policy *new_policy); |
Thomas Renninger | 7970e08 | 2006-04-13 15:14:04 +0200 | [diff] [blame] | 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | /** |
| 637 | * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access |
| 638 | */ |
| 639 | #define store_one(file_name, object) \ |
| 640 | static ssize_t store_##file_name \ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 641 | (struct cpufreq_policy *policy, const char *buf, size_t count) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | { \ |
Vince Hsu | 619c144c | 2014-11-10 14:14:50 +0800 | [diff] [blame] | 643 | int ret, temp; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | struct cpufreq_policy new_policy; \ |
| 645 | \ |
Viresh Kumar | 8fa5b63 | 2015-08-03 08:36:15 +0530 | [diff] [blame] | 646 | memcpy(&new_policy, policy, sizeof(*policy)); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | \ |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 648 | ret = sscanf(buf, "%u", &new_policy.object); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | if (ret != 1) \ |
| 650 | return -EINVAL; \ |
| 651 | \ |
Vince Hsu | 619c144c | 2014-11-10 14:14:50 +0800 | [diff] [blame] | 652 | temp = new_policy.object; \ |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 653 | ret = cpufreq_set_policy(policy, &new_policy); \ |
Vince Hsu | 619c144c | 2014-11-10 14:14:50 +0800 | [diff] [blame] | 654 | if (!ret) \ |
| 655 | policy->user_policy.object = temp; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | \ |
| 657 | return ret ? ret : count; \ |
| 658 | } |
| 659 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 660 | store_one(scaling_min_freq, min); |
| 661 | store_one(scaling_max_freq, max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | |
| 663 | /** |
| 664 | * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware |
| 665 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 666 | static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, |
| 667 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | { |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 669 | unsigned int cur_freq = __cpufreq_get(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | if (!cur_freq) |
| 671 | return sprintf(buf, "<unknown>"); |
| 672 | return sprintf(buf, "%u\n", cur_freq); |
| 673 | } |
| 674 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | /** |
| 676 | * show_scaling_governor - show the current policy for the specified CPU |
| 677 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 678 | static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | { |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 680 | if (policy->policy == CPUFREQ_POLICY_POWERSAVE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | return sprintf(buf, "powersave\n"); |
| 682 | else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) |
| 683 | return sprintf(buf, "performance\n"); |
| 684 | else if (policy->governor) |
viresh kumar | 4b972f0 | 2012-10-23 01:23:43 +0200 | [diff] [blame] | 685 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 686 | policy->governor->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | return -EINVAL; |
| 688 | } |
| 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | /** |
| 691 | * store_scaling_governor - store policy for the specified CPU |
| 692 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 693 | static ssize_t store_scaling_governor(struct cpufreq_policy *policy, |
| 694 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | { |
Srivatsa S. Bhat | 5136fa5 | 2013-09-07 01:24:06 +0530 | [diff] [blame] | 696 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | char str_governor[16]; |
| 698 | struct cpufreq_policy new_policy; |
| 699 | |
Viresh Kumar | 8fa5b63 | 2015-08-03 08:36:15 +0530 | [diff] [blame] | 700 | memcpy(&new_policy, policy, sizeof(*policy)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 702 | ret = sscanf(buf, "%15s", str_governor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | if (ret != 1) |
| 704 | return -EINVAL; |
| 705 | |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 706 | if (cpufreq_parse_governor(str_governor, &new_policy.policy, |
| 707 | &new_policy.governor)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | return -EINVAL; |
| 709 | |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 710 | ret = cpufreq_set_policy(policy, &new_policy); |
Viresh Kumar | 88dc438 | 2015-08-03 08:36:18 +0530 | [diff] [blame] | 711 | return ret ? ret : count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /** |
| 715 | * show_scaling_driver - show the cpufreq driver currently loaded |
| 716 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 717 | static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 719 | return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /** |
| 723 | * show_scaling_available_governors - show the available CPUfreq governors |
| 724 | */ |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 725 | static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, |
| 726 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
| 728 | ssize_t i = 0; |
| 729 | struct cpufreq_governor *t; |
| 730 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 731 | if (!has_target()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | i += sprintf(buf, "performance powersave"); |
| 733 | goto out; |
| 734 | } |
| 735 | |
Viresh Kumar | f7b2706 | 2015-01-27 14:06:09 +0530 | [diff] [blame] | 736 | for_each_governor(t) { |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 737 | if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) |
| 738 | - (CPUFREQ_NAME_LEN + 2))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | goto out; |
viresh kumar | 4b972f0 | 2012-10-23 01:23:43 +0200 | [diff] [blame] | 740 | i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
Dave Jones | 7d5e350 | 2006-02-02 17:03:42 -0500 | [diff] [blame] | 742 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | i += sprintf(&buf[i], "\n"); |
| 744 | return i; |
| 745 | } |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 746 | |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 747 | ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | { |
| 749 | ssize_t i = 0; |
| 750 | unsigned int cpu; |
| 751 | |
Rusty Russell | 835481d | 2009-01-04 05:18:06 -0800 | [diff] [blame] | 752 | for_each_cpu(cpu, mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | if (i) |
| 754 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); |
| 755 | i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); |
| 756 | if (i >= (PAGE_SIZE - 5)) |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 757 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | } |
| 759 | i += sprintf(&buf[i], "\n"); |
| 760 | return i; |
| 761 | } |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 762 | EXPORT_SYMBOL_GPL(cpufreq_show_cpus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 764 | /** |
| 765 | * show_related_cpus - show the CPUs affected by each transition even if |
| 766 | * hw coordination is in use |
| 767 | */ |
| 768 | static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) |
| 769 | { |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 770 | return cpufreq_show_cpus(policy->related_cpus, buf); |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | /** |
| 774 | * show_affected_cpus - show the CPUs affected by each transition |
| 775 | */ |
| 776 | static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) |
| 777 | { |
Lan Tianyu | f4fd379 | 2013-06-27 15:08:54 +0800 | [diff] [blame] | 778 | return cpufreq_show_cpus(policy->cpus, buf); |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 781 | static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 782 | const char *buf, size_t count) |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 783 | { |
| 784 | unsigned int freq = 0; |
| 785 | unsigned int ret; |
| 786 | |
CHIKAMA masaki | 879000f | 2008-06-05 22:46:33 -0700 | [diff] [blame] | 787 | if (!policy->governor || !policy->governor->store_setspeed) |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 788 | return -EINVAL; |
| 789 | |
| 790 | ret = sscanf(buf, "%u", &freq); |
| 791 | if (ret != 1) |
| 792 | return -EINVAL; |
| 793 | |
| 794 | policy->governor->store_setspeed(policy, freq); |
| 795 | |
| 796 | return count; |
| 797 | } |
| 798 | |
| 799 | static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) |
| 800 | { |
CHIKAMA masaki | 879000f | 2008-06-05 22:46:33 -0700 | [diff] [blame] | 801 | if (!policy->governor || !policy->governor->show_setspeed) |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 802 | return sprintf(buf, "<unsupported>\n"); |
| 803 | |
| 804 | return policy->governor->show_setspeed(policy, buf); |
| 805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 807 | /** |
viresh kumar | 8bf1ac72 | 2012-10-23 01:23:33 +0200 | [diff] [blame] | 808 | * show_bios_limit - show the current cpufreq HW/BIOS limitation |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 809 | */ |
| 810 | static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) |
| 811 | { |
| 812 | unsigned int limit; |
| 813 | int ret; |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 814 | if (cpufreq_driver->bios_limit) { |
| 815 | ret = cpufreq_driver->bios_limit(policy->cpu, &limit); |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 816 | if (!ret) |
| 817 | return sprintf(buf, "%u\n", limit); |
| 818 | } |
| 819 | return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); |
| 820 | } |
| 821 | |
Borislav Petkov | 6dad2a2 | 2010-03-31 21:56:46 +0200 | [diff] [blame] | 822 | cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); |
| 823 | cpufreq_freq_attr_ro(cpuinfo_min_freq); |
| 824 | cpufreq_freq_attr_ro(cpuinfo_max_freq); |
| 825 | cpufreq_freq_attr_ro(cpuinfo_transition_latency); |
| 826 | cpufreq_freq_attr_ro(scaling_available_governors); |
| 827 | cpufreq_freq_attr_ro(scaling_driver); |
| 828 | cpufreq_freq_attr_ro(scaling_cur_freq); |
| 829 | cpufreq_freq_attr_ro(bios_limit); |
| 830 | cpufreq_freq_attr_ro(related_cpus); |
| 831 | cpufreq_freq_attr_ro(affected_cpus); |
| 832 | cpufreq_freq_attr_rw(scaling_min_freq); |
| 833 | cpufreq_freq_attr_rw(scaling_max_freq); |
| 834 | cpufreq_freq_attr_rw(scaling_governor); |
| 835 | cpufreq_freq_attr_rw(scaling_setspeed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 837 | static struct attribute *default_attrs[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | &cpuinfo_min_freq.attr, |
| 839 | &cpuinfo_max_freq.attr, |
Thomas Renninger | ed12978 | 2009-02-04 01:17:41 +0100 | [diff] [blame] | 840 | &cpuinfo_transition_latency.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | &scaling_min_freq.attr, |
| 842 | &scaling_max_freq.attr, |
| 843 | &affected_cpus.attr, |
Darrick J. Wong | e8628dd | 2008-04-18 13:31:12 -0700 | [diff] [blame] | 844 | &related_cpus.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | &scaling_governor.attr, |
| 846 | &scaling_driver.attr, |
| 847 | &scaling_available_governors.attr, |
Venki Pallipadi | 9e76988 | 2007-10-26 10:18:21 -0700 | [diff] [blame] | 848 | &scaling_setspeed.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | NULL |
| 850 | }; |
| 851 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 852 | #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) |
| 853 | #define to_attr(a) container_of(a, struct freq_attr, attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 855 | static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | { |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 857 | struct cpufreq_policy *policy = to_policy(kobj); |
| 858 | struct freq_attr *fattr = to_attr(attr); |
Viresh Kumar | 1b750e3 | 2013-10-02 14:13:09 +0530 | [diff] [blame] | 859 | ssize_t ret; |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 860 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 861 | down_read(&policy->rwsem); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 862 | |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 863 | if (fattr->show) |
| 864 | ret = fattr->show(policy, buf); |
| 865 | else |
| 866 | ret = -EIO; |
| 867 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 868 | up_read(&policy->rwsem); |
Viresh Kumar | 1b750e3 | 2013-10-02 14:13:09 +0530 | [diff] [blame] | 869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | return ret; |
| 871 | } |
| 872 | |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 873 | static ssize_t store(struct kobject *kobj, struct attribute *attr, |
| 874 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | { |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 876 | struct cpufreq_policy *policy = to_policy(kobj); |
| 877 | struct freq_attr *fattr = to_attr(attr); |
Dave Jones | a07530b | 2008-03-05 14:22:25 -0500 | [diff] [blame] | 878 | ssize_t ret = -EINVAL; |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 879 | |
Srivatsa S. Bhat | 4f750c9 | 2013-09-07 01:23:43 +0530 | [diff] [blame] | 880 | get_online_cpus(); |
| 881 | |
| 882 | if (!cpu_online(policy->cpu)) |
| 883 | goto unlock; |
| 884 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 885 | down_write(&policy->rwsem); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 886 | |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 887 | if (fattr->store) |
| 888 | ret = fattr->store(policy, buf, count); |
| 889 | else |
| 890 | ret = -EIO; |
| 891 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 892 | up_write(&policy->rwsem); |
Srivatsa S. Bhat | 4f750c9 | 2013-09-07 01:23:43 +0530 | [diff] [blame] | 893 | unlock: |
| 894 | put_online_cpus(); |
| 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | return ret; |
| 897 | } |
| 898 | |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 899 | static void cpufreq_sysfs_release(struct kobject *kobj) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
Dave Jones | 905d77c | 2008-03-05 14:28:32 -0500 | [diff] [blame] | 901 | struct cpufreq_policy *policy = to_policy(kobj); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 902 | pr_debug("last reference is dropped\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | complete(&policy->kobj_unregister); |
| 904 | } |
| 905 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 906 | static const struct sysfs_ops sysfs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | .show = show, |
| 908 | .store = store, |
| 909 | }; |
| 910 | |
| 911 | static struct kobj_type ktype_cpufreq = { |
| 912 | .sysfs_ops = &sysfs_ops, |
| 913 | .default_attrs = default_attrs, |
| 914 | .release = cpufreq_sysfs_release, |
| 915 | }; |
| 916 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 917 | static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu) |
| 918 | { |
| 919 | struct device *cpu_dev; |
| 920 | |
| 921 | pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu); |
| 922 | |
| 923 | if (!policy) |
| 924 | return 0; |
| 925 | |
| 926 | cpu_dev = get_cpu_device(cpu); |
| 927 | if (WARN_ON(!cpu_dev)) |
| 928 | return 0; |
| 929 | |
| 930 | return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq"); |
| 931 | } |
| 932 | |
| 933 | static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu) |
| 934 | { |
| 935 | struct device *cpu_dev; |
| 936 | |
| 937 | pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu); |
| 938 | |
| 939 | cpu_dev = get_cpu_device(cpu); |
| 940 | if (WARN_ON(!cpu_dev)) |
| 941 | return; |
| 942 | |
| 943 | sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); |
| 944 | } |
| 945 | |
| 946 | /* Add/remove symlinks for all related CPUs */ |
Viresh Kumar | 308b60e | 2013-07-31 14:35:14 +0200 | [diff] [blame] | 947 | static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) |
Dave Jones | 19d6f7e | 2009-07-08 17:35:39 -0400 | [diff] [blame] | 948 | { |
| 949 | unsigned int j; |
| 950 | int ret = 0; |
| 951 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 952 | /* Some related CPUs might not be present (physically hotplugged) */ |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 953 | for_each_cpu(j, policy->real_cpus) { |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 954 | ret = add_cpu_dev_symlink(policy, j); |
Rafael J. Wysocki | 71c3461 | 2013-08-04 01:19:34 +0200 | [diff] [blame] | 955 | if (ret) |
| 956 | break; |
Dave Jones | 19d6f7e | 2009-07-08 17:35:39 -0400 | [diff] [blame] | 957 | } |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 958 | |
Dave Jones | 19d6f7e | 2009-07-08 17:35:39 -0400 | [diff] [blame] | 959 | return ret; |
| 960 | } |
| 961 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 962 | static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy) |
| 963 | { |
| 964 | unsigned int j; |
| 965 | |
| 966 | /* Some related CPUs might not be present (physically hotplugged) */ |
Viresh Kumar | 96bdda6 | 2015-10-15 21:35:24 +0530 | [diff] [blame] | 967 | for_each_cpu(j, policy->real_cpus) |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 968 | remove_cpu_dev_symlink(policy, j); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 969 | } |
| 970 | |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 971 | static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 972 | { |
| 973 | struct freq_attr **drv_attr; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 974 | int ret = 0; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 975 | |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 976 | /* set up files for this cpu device */ |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 977 | drv_attr = cpufreq_driver->attr; |
Viresh Kumar | f13f118 | 2015-01-02 12:34:23 +0530 | [diff] [blame] | 978 | while (drv_attr && *drv_attr) { |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 979 | ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); |
| 980 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 981 | return ret; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 982 | drv_attr++; |
| 983 | } |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 984 | if (cpufreq_driver->get) { |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 985 | ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); |
| 986 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 987 | return ret; |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 988 | } |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 989 | |
| 990 | ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); |
| 991 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 992 | return ret; |
Dirk Brandewie | c034b02 | 2014-10-13 08:37:40 -0700 | [diff] [blame] | 993 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 994 | if (cpufreq_driver->bios_limit) { |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 995 | ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); |
| 996 | if (ret) |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 997 | return ret; |
Thomas Renninger | e2f74f3 | 2009-11-19 12:31:01 +0100 | [diff] [blame] | 998 | } |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 999 | |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1000 | return cpufreq_add_dev_symlink(policy); |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1001 | } |
| 1002 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1003 | __weak struct cpufreq_governor *cpufreq_default_governor(void) |
| 1004 | { |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
Viresh Kumar | 7f0fa40 | 2015-07-08 15:12:16 +0530 | [diff] [blame] | 1008 | static int cpufreq_init_policy(struct cpufreq_policy *policy) |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1009 | { |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1010 | struct cpufreq_governor *gov = NULL; |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1011 | struct cpufreq_policy new_policy; |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1012 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 1013 | memcpy(&new_policy, policy, sizeof(*policy)); |
Jason Baron | a27a9ab | 2013-12-19 22:50:50 +0000 | [diff] [blame] | 1014 | |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1015 | /* Update governor of new_policy to the governor used before hotplug */ |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 1016 | gov = find_governor(policy->last_governor); |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1017 | if (gov) { |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1018 | pr_debug("Restoring governor %s for cpu %d\n", |
| 1019 | policy->governor->name, policy->cpu); |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1020 | } else { |
| 1021 | gov = cpufreq_default_governor(); |
| 1022 | if (!gov) |
| 1023 | return -ENODATA; |
| 1024 | } |
viresh kumar | 6e2c89d | 2014-03-04 11:43:59 +0800 | [diff] [blame] | 1025 | |
| 1026 | new_policy.governor = gov; |
| 1027 | |
Srinivas Pandruvada | 69030dd | 2015-12-01 16:52:14 -0800 | [diff] [blame] | 1028 | /* Use the default policy if there is no last_policy. */ |
| 1029 | if (cpufreq_driver->setpolicy) { |
| 1030 | if (policy->last_policy) |
| 1031 | new_policy.policy = policy->last_policy; |
| 1032 | else |
| 1033 | cpufreq_parse_governor(gov->name, &new_policy.policy, |
| 1034 | NULL); |
| 1035 | } |
Dave Jones | ecf7e46 | 2009-07-08 18:48:47 -0400 | [diff] [blame] | 1036 | /* set default policy */ |
Viresh Kumar | 7f0fa40 | 2015-07-08 15:12:16 +0530 | [diff] [blame] | 1037 | return cpufreq_set_policy(policy, &new_policy); |
Dave Jones | 909a694 | 2009-07-08 18:05:42 -0400 | [diff] [blame] | 1038 | } |
| 1039 | |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1040 | static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1041 | { |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1042 | int ret = 0; |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1043 | |
Viresh Kumar | bb29ae1 | 2015-02-19 17:02:06 +0530 | [diff] [blame] | 1044 | /* Has this CPU been taken care of already? */ |
| 1045 | if (cpumask_test_cpu(cpu, policy->cpus)) |
| 1046 | return 0; |
| 1047 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1048 | down_write(&policy->rwsem); |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1049 | if (has_target()) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1050 | ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); |
Viresh Kumar | 3de9bde | 2013-08-06 22:53:13 +0530 | [diff] [blame] | 1051 | if (ret) { |
| 1052 | pr_err("%s: Failed to stop governor\n", __func__); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1053 | goto unlock; |
Viresh Kumar | 3de9bde | 2013-08-06 22:53:13 +0530 | [diff] [blame] | 1054 | } |
| 1055 | } |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1056 | |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1057 | cpumask_set_cpu(cpu, policy->cpus); |
Viresh Kumar | 2eaa3e2 | 2013-02-07 10:55:00 +0530 | [diff] [blame] | 1058 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1059 | if (has_target()) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1060 | ret = cpufreq_governor(policy, CPUFREQ_GOV_START); |
Stratos Karafotis | e5c87b7 | 2014-03-19 23:29:17 +0200 | [diff] [blame] | 1061 | if (!ret) |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1062 | ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); |
Stratos Karafotis | e5c87b7 | 2014-03-19 23:29:17 +0200 | [diff] [blame] | 1063 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1064 | if (ret) |
Viresh Kumar | 3de9bde | 2013-08-06 22:53:13 +0530 | [diff] [blame] | 1065 | pr_err("%s: Failed to start governor\n", __func__); |
Viresh Kumar | 820c6ca | 2013-04-22 00:48:03 +0200 | [diff] [blame] | 1066 | } |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1067 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1068 | unlock: |
| 1069 | up_write(&policy->rwsem); |
| 1070 | return ret; |
Viresh Kumar | fcf8058 | 2013-01-29 14:39:08 +0000 | [diff] [blame] | 1071 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Viresh Kumar | 11eb69b | 2016-02-22 16:36:42 +0530 | [diff] [blame] | 1073 | static void handle_update(struct work_struct *work) |
| 1074 | { |
| 1075 | struct cpufreq_policy *policy = |
| 1076 | container_of(work, struct cpufreq_policy, update); |
| 1077 | unsigned int cpu = policy->cpu; |
| 1078 | pr_debug("handle_update for cpu %u called\n", cpu); |
| 1079 | cpufreq_update_policy(cpu); |
| 1080 | } |
| 1081 | |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1082 | static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) |
Srivatsa S. Bhat | 8414809 | 2013-07-30 04:25:10 +0530 | [diff] [blame] | 1083 | { |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1084 | struct device *dev = get_cpu_device(cpu); |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1085 | struct cpufreq_policy *policy; |
| 1086 | |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1087 | if (WARN_ON(!dev)) |
| 1088 | return NULL; |
| 1089 | |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1090 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
| 1091 | if (!policy) |
| 1092 | return NULL; |
| 1093 | |
| 1094 | if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) |
| 1095 | goto err_free_policy; |
| 1096 | |
| 1097 | if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) |
| 1098 | goto err_free_cpumask; |
| 1099 | |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1100 | if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) |
| 1101 | goto err_free_rcpumask; |
| 1102 | |
Viresh Kumar | 3510fac | 2015-10-16 12:41:12 +0530 | [diff] [blame] | 1103 | kobject_init(&policy->kobj, &ktype_cpufreq); |
Lukasz Majewski | c88a1f8 | 2013-08-06 22:53:08 +0530 | [diff] [blame] | 1104 | INIT_LIST_HEAD(&policy->policy_list); |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 1105 | init_rwsem(&policy->rwsem); |
Srivatsa S. Bhat | 12478cf | 2014-03-24 13:35:44 +0530 | [diff] [blame] | 1106 | spin_lock_init(&policy->transition_lock); |
| 1107 | init_waitqueue_head(&policy->transition_wait); |
Viresh Kumar | 818c571 | 2015-01-02 12:34:38 +0530 | [diff] [blame] | 1108 | init_completion(&policy->kobj_unregister); |
| 1109 | INIT_WORK(&policy->update, handle_update); |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 1110 | |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1111 | policy->cpu = cpu; |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1112 | return policy; |
| 1113 | |
Viresh Kumar | 2fc3384 | 2015-06-08 18:25:29 +0530 | [diff] [blame] | 1114 | err_free_rcpumask: |
| 1115 | free_cpumask_var(policy->related_cpus); |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1116 | err_free_cpumask: |
| 1117 | free_cpumask_var(policy->cpus); |
| 1118 | err_free_policy: |
| 1119 | kfree(policy); |
| 1120 | |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
Viresh Kumar | 2fc3384 | 2015-06-08 18:25:29 +0530 | [diff] [blame] | 1124 | static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify) |
Viresh Kumar | 42f921a | 2013-12-20 21:26:02 +0530 | [diff] [blame] | 1125 | { |
| 1126 | struct kobject *kobj; |
| 1127 | struct completion *cmp; |
| 1128 | |
Viresh Kumar | 2fc3384 | 2015-06-08 18:25:29 +0530 | [diff] [blame] | 1129 | if (notify) |
| 1130 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
| 1131 | CPUFREQ_REMOVE_POLICY, policy); |
Viresh Kumar | fcd7af9 | 2014-01-07 07:10:10 +0530 | [diff] [blame] | 1132 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1133 | down_write(&policy->rwsem); |
| 1134 | cpufreq_remove_dev_symlink(policy); |
Viresh Kumar | 42f921a | 2013-12-20 21:26:02 +0530 | [diff] [blame] | 1135 | kobj = &policy->kobj; |
| 1136 | cmp = &policy->kobj_unregister; |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1137 | up_write(&policy->rwsem); |
Viresh Kumar | 42f921a | 2013-12-20 21:26:02 +0530 | [diff] [blame] | 1138 | kobject_put(kobj); |
| 1139 | |
| 1140 | /* |
| 1141 | * We need to make sure that the underlying kobj is |
| 1142 | * actually not referenced anymore by anybody before we |
| 1143 | * proceed with unloading. |
| 1144 | */ |
| 1145 | pr_debug("waiting for dropping of refcount\n"); |
| 1146 | wait_for_completion(cmp); |
| 1147 | pr_debug("wait complete\n"); |
| 1148 | } |
| 1149 | |
Viresh Kumar | 3654c5c | 2015-06-08 18:25:30 +0530 | [diff] [blame] | 1150 | static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify) |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1151 | { |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1152 | unsigned long flags; |
| 1153 | int cpu; |
| 1154 | |
| 1155 | /* Remove policy from list */ |
| 1156 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1157 | list_del(&policy->policy_list); |
| 1158 | |
| 1159 | for_each_cpu(cpu, policy->related_cpus) |
| 1160 | per_cpu(cpufreq_cpu_data, cpu) = NULL; |
| 1161 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1162 | |
Viresh Kumar | 3654c5c | 2015-06-08 18:25:30 +0530 | [diff] [blame] | 1163 | cpufreq_policy_put_kobj(policy, notify); |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1164 | free_cpumask_var(policy->real_cpus); |
Srivatsa S. Bhat | e9698cc | 2013-07-30 04:24:11 +0530 | [diff] [blame] | 1165 | free_cpumask_var(policy->related_cpus); |
| 1166 | free_cpumask_var(policy->cpus); |
| 1167 | kfree(policy); |
| 1168 | } |
| 1169 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1170 | static int cpufreq_online(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | { |
Viresh Kumar | 7f0c020 | 2015-01-02 12:34:32 +0530 | [diff] [blame] | 1172 | struct cpufreq_policy *policy; |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1173 | bool new_policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | unsigned long flags; |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1175 | unsigned int j; |
| 1176 | int ret; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1177 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1178 | pr_debug("%s: bringing CPU%u online\n", __func__, cpu); |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 1179 | |
Viresh Kumar | bb29ae1 | 2015-02-19 17:02:06 +0530 | [diff] [blame] | 1180 | /* Check if this CPU already has a policy to manage it */ |
Viresh Kumar | 9104bb2 | 2015-05-12 12:22:12 +0530 | [diff] [blame] | 1181 | policy = per_cpu(cpufreq_cpu_data, cpu); |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1182 | if (policy) { |
Viresh Kumar | 9104bb2 | 2015-05-12 12:22:12 +0530 | [diff] [blame] | 1183 | WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1184 | if (!policy_is_inactive(policy)) |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1185 | return cpufreq_add_policy_cpu(policy, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1187 | /* This is the only online CPU for the policy. Start over. */ |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1188 | new_policy = false; |
Rafael J. Wysocki | 11ce707 | 2015-07-27 23:11:21 +0200 | [diff] [blame] | 1189 | down_write(&policy->rwsem); |
| 1190 | policy->cpu = cpu; |
| 1191 | policy->governor = NULL; |
| 1192 | up_write(&policy->rwsem); |
| 1193 | } else { |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1194 | new_policy = true; |
Rafael J. Wysocki | a34e63b | 2015-07-27 23:11:50 +0200 | [diff] [blame] | 1195 | policy = cpufreq_policy_alloc(cpu); |
Rafael J. Wysocki | 72368d1 | 2013-12-27 01:07:11 +0100 | [diff] [blame] | 1196 | if (!policy) |
Rafael J. Wysocki | d4d854d | 2015-07-27 23:11:30 +0200 | [diff] [blame] | 1197 | return -ENOMEM; |
Rafael J. Wysocki | 72368d1 | 2013-12-27 01:07:11 +0100 | [diff] [blame] | 1198 | } |
Srivatsa S. Bhat | 0d66b91 | 2013-09-12 01:42:59 +0530 | [diff] [blame] | 1199 | |
Rusty Russell | 835481d | 2009-01-04 05:18:06 -0800 | [diff] [blame] | 1200 | cpumask_copy(policy->cpus, cpumask_of(cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | /* call driver. From then on the cpufreq must be able |
| 1203 | * to accept all calls to ->verify and ->setpolicy for this CPU |
| 1204 | */ |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1205 | ret = cpufreq_driver->init(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | if (ret) { |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 1207 | pr_debug("initialization failed\n"); |
Viresh Kumar | 8101f99 | 2015-07-08 15:12:15 +0530 | [diff] [blame] | 1208 | goto out_free_policy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | } |
Viresh Kumar | 643ae6e | 2013-01-12 05:14:38 +0000 | [diff] [blame] | 1210 | |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1211 | down_write(&policy->rwsem); |
| 1212 | |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1213 | if (new_policy) { |
Rafael J. Wysocki | 4d1f3a5 | 2015-07-27 23:11:44 +0200 | [diff] [blame] | 1214 | /* related_cpus should at least include policy->cpus. */ |
Viresh Kumar | 0998a03 | 2015-10-15 21:35:21 +0530 | [diff] [blame] | 1215 | cpumask_copy(policy->related_cpus, policy->cpus); |
Rafael J. Wysocki | 4d1f3a5 | 2015-07-27 23:11:44 +0200 | [diff] [blame] | 1216 | /* Remember CPUs present at the policy creation time. */ |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1217 | cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask); |
Viresh Kumar | 3510fac | 2015-10-16 12:41:12 +0530 | [diff] [blame] | 1218 | |
| 1219 | /* Name and add the kobject */ |
| 1220 | ret = kobject_add(&policy->kobj, cpufreq_global_kobject, |
| 1221 | "policy%u", |
| 1222 | cpumask_first(policy->related_cpus)); |
| 1223 | if (ret) { |
| 1224 | pr_err("%s: failed to add policy->kobj: %d\n", __func__, |
| 1225 | ret); |
| 1226 | goto out_exit_policy; |
| 1227 | } |
Rafael J. Wysocki | 4d1f3a5 | 2015-07-27 23:11:44 +0200 | [diff] [blame] | 1228 | } |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1229 | |
Viresh Kumar | 5a7e56a | 2014-03-04 11:44:00 +0800 | [diff] [blame] | 1230 | /* |
| 1231 | * affected cpus must always be the one, which are online. We aren't |
| 1232 | * managing offline cpus here. |
| 1233 | */ |
| 1234 | cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); |
| 1235 | |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1236 | if (new_policy) { |
Viresh Kumar | 5a7e56a | 2014-03-04 11:44:00 +0800 | [diff] [blame] | 1237 | policy->user_policy.min = policy->min; |
| 1238 | policy->user_policy.max = policy->max; |
Tomeu Vizoso | 6d4e81e | 2014-11-24 10:08:03 +0100 | [diff] [blame] | 1239 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1240 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1241 | for_each_cpu(j, policy->related_cpus) |
| 1242 | per_cpu(cpufreq_cpu_data, j) = policy; |
| 1243 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1244 | } |
Viresh Kumar | 652ed95 | 2014-01-09 20:38:43 +0530 | [diff] [blame] | 1245 | |
Rafael J. Wysocki | 2ed99e3 | 2014-03-12 21:49:33 +0100 | [diff] [blame] | 1246 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { |
Viresh Kumar | da60ce9 | 2013-10-03 20:28:30 +0530 | [diff] [blame] | 1247 | policy->cur = cpufreq_driver->get(policy->cpu); |
| 1248 | if (!policy->cur) { |
| 1249 | pr_err("%s: ->get() failed\n", __func__); |
Viresh Kumar | 8101f99 | 2015-07-08 15:12:15 +0530 | [diff] [blame] | 1250 | goto out_exit_policy; |
Viresh Kumar | da60ce9 | 2013-10-03 20:28:30 +0530 | [diff] [blame] | 1251 | } |
| 1252 | } |
| 1253 | |
Viresh Kumar | d391669 | 2013-12-03 11:20:46 +0530 | [diff] [blame] | 1254 | /* |
| 1255 | * Sometimes boot loaders set CPU frequency to a value outside of |
| 1256 | * frequency table present with cpufreq core. In such cases CPU might be |
| 1257 | * unstable if it has to run on that frequency for long duration of time |
| 1258 | * and so its better to set it to a frequency which is specified in |
| 1259 | * freq-table. This also makes cpufreq stats inconsistent as |
| 1260 | * cpufreq-stats would fail to register because current frequency of CPU |
| 1261 | * isn't found in freq-table. |
| 1262 | * |
| 1263 | * Because we don't want this change to effect boot process badly, we go |
| 1264 | * for the next freq which is >= policy->cur ('cur' must be set by now, |
| 1265 | * otherwise we will end up setting freq to lowest of the table as 'cur' |
| 1266 | * is initialized to zero). |
| 1267 | * |
| 1268 | * We are passing target-freq as "policy->cur - 1" otherwise |
| 1269 | * __cpufreq_driver_target() would simply fail, as policy->cur will be |
| 1270 | * equal to target-freq. |
| 1271 | */ |
| 1272 | if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) |
| 1273 | && has_target()) { |
| 1274 | /* Are we running at unknown frequency ? */ |
| 1275 | ret = cpufreq_frequency_table_get_index(policy, policy->cur); |
| 1276 | if (ret == -EINVAL) { |
| 1277 | /* Warn user and fix it */ |
| 1278 | pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", |
| 1279 | __func__, policy->cpu, policy->cur); |
| 1280 | ret = __cpufreq_driver_target(policy, policy->cur - 1, |
| 1281 | CPUFREQ_RELATION_L); |
| 1282 | |
| 1283 | /* |
| 1284 | * Reaching here after boot in a few seconds may not |
| 1285 | * mean that system will remain stable at "unknown" |
| 1286 | * frequency for longer duration. Hence, a BUG_ON(). |
| 1287 | */ |
| 1288 | BUG_ON(ret); |
| 1289 | pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", |
| 1290 | __func__, policy->cpu, policy->cur); |
| 1291 | } |
| 1292 | } |
| 1293 | |
Thomas Renninger | a1531ac | 2008-07-29 22:32:58 -0700 | [diff] [blame] | 1294 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
| 1295 | CPUFREQ_START, policy); |
| 1296 | |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1297 | if (new_policy) { |
Rafael J. Wysocki | d9612a4 | 2015-07-27 23:11:37 +0200 | [diff] [blame] | 1298 | ret = cpufreq_add_dev_interface(policy); |
Srivatsa S. Bhat | a82fab2 | 2013-07-30 04:24:49 +0530 | [diff] [blame] | 1299 | if (ret) |
Viresh Kumar | 8101f99 | 2015-07-08 15:12:15 +0530 | [diff] [blame] | 1300 | goto out_exit_policy; |
Viresh Kumar | fcd7af9 | 2014-01-07 07:10:10 +0530 | [diff] [blame] | 1301 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
| 1302 | CPUFREQ_CREATE_POLICY, policy); |
Dave Jones | 8ff6973 | 2006-03-05 03:37:23 -0500 | [diff] [blame] | 1303 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1304 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 1305 | list_add(&policy->policy_list, &cpufreq_policy_list); |
| 1306 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 1307 | } |
Viresh Kumar | 9515f4d | 2013-08-20 12:08:23 +0530 | [diff] [blame] | 1308 | |
Viresh Kumar | 7f0fa40 | 2015-07-08 15:12:16 +0530 | [diff] [blame] | 1309 | ret = cpufreq_init_policy(policy); |
| 1310 | if (ret) { |
| 1311 | pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", |
| 1312 | __func__, cpu, ret); |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1313 | /* cpufreq_policy_free() will notify based on this */ |
| 1314 | new_policy = false; |
| 1315 | goto out_exit_policy; |
Viresh Kumar | 08fd8c1c | 2013-12-24 07:11:01 +0530 | [diff] [blame] | 1316 | } |
Srivatsa S. Bhat | e18f168 | 2013-07-30 04:24:23 +0530 | [diff] [blame] | 1317 | |
Viresh Kumar | 4e97b63 | 2014-03-04 11:44:01 +0800 | [diff] [blame] | 1318 | up_write(&policy->rwsem); |
Viresh Kumar | 08fd8c1c | 2013-12-24 07:11:01 +0530 | [diff] [blame] | 1319 | |
Greg Kroah-Hartman | 038c5b3 | 2007-12-17 15:54:39 -0400 | [diff] [blame] | 1320 | kobject_uevent(&policy->kobj, KOBJ_ADD); |
Viresh Kumar | 7c45cf3 | 2014-11-27 06:07:51 +0530 | [diff] [blame] | 1321 | |
Viresh Kumar | 7c45cf3 | 2014-11-27 06:07:51 +0530 | [diff] [blame] | 1322 | /* Callback for handling stuff after policy is ready */ |
| 1323 | if (cpufreq_driver->ready) |
| 1324 | cpufreq_driver->ready(policy); |
| 1325 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 1326 | pr_debug("initialization complete\n"); |
Dave Jones | 87c3227 | 2006-03-29 01:48:37 -0500 | [diff] [blame] | 1327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | return 0; |
| 1329 | |
Viresh Kumar | 8101f99 | 2015-07-08 15:12:15 +0530 | [diff] [blame] | 1330 | out_exit_policy: |
Prarit Bhargava | 7106e02 | 2014-09-10 10:12:08 -0400 | [diff] [blame] | 1331 | up_write(&policy->rwsem); |
| 1332 | |
Viresh Kumar | da60ce9 | 2013-10-03 20:28:30 +0530 | [diff] [blame] | 1333 | if (cpufreq_driver->exit) |
| 1334 | cpufreq_driver->exit(policy); |
Viresh Kumar | 8101f99 | 2015-07-08 15:12:15 +0530 | [diff] [blame] | 1335 | out_free_policy: |
Rafael J. Wysocki | 194d99c | 2015-07-29 03:08:57 +0200 | [diff] [blame] | 1336 | cpufreq_policy_free(policy, !new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | return ret; |
| 1338 | } |
| 1339 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 1340 | /** |
| 1341 | * cpufreq_add_dev - the cpufreq interface for a CPU device. |
| 1342 | * @dev: CPU device. |
| 1343 | * @sif: Subsystem interface structure pointer (not used) |
| 1344 | */ |
| 1345 | static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) |
| 1346 | { |
| 1347 | unsigned cpu = dev->id; |
| 1348 | int ret; |
| 1349 | |
| 1350 | dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); |
| 1351 | |
| 1352 | if (cpu_online(cpu)) { |
| 1353 | ret = cpufreq_online(cpu); |
| 1354 | } else { |
| 1355 | /* |
| 1356 | * A hotplug notifier will follow and we will handle it as CPU |
| 1357 | * online then. For now, just create the sysfs link, unless |
| 1358 | * there is no policy or the link is already present. |
| 1359 | */ |
| 1360 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
| 1361 | |
| 1362 | ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus) |
| 1363 | ? add_cpu_dev_symlink(policy, cpu) : 0; |
| 1364 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | |
| 1366 | return ret; |
| 1367 | } |
| 1368 | |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1369 | static void cpufreq_offline(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 1371 | struct cpufreq_policy *policy; |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1372 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 1374 | pr_debug("%s: unregistering CPU %u\n", __func__, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
Viresh Kumar | 988bed0 | 2015-05-08 11:53:45 +0530 | [diff] [blame] | 1376 | policy = cpufreq_cpu_get_raw(cpu); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 1377 | if (!policy) { |
Viresh Kumar | b8eed8a | 2013-01-14 13:23:03 +0000 | [diff] [blame] | 1378 | pr_debug("%s: No cpu_data found\n", __func__); |
Rafael J. Wysocki | 15c0b4d | 2015-07-27 23:11:09 +0200 | [diff] [blame] | 1379 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1382 | down_write(&policy->rwsem); |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1383 | if (has_target()) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1384 | ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1385 | if (ret) |
Viresh Kumar | 3de9bde | 2013-08-06 22:53:13 +0530 | [diff] [blame] | 1386 | pr_err("%s: Failed to stop governor\n", __func__); |
Viresh Kumar | db5f299 | 2015-01-02 12:34:25 +0530 | [diff] [blame] | 1387 | } |
Jacob Shin | 27ecddc | 2011-04-27 13:32:11 -0500 | [diff] [blame] | 1388 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1389 | cpumask_clear_cpu(cpu, policy->cpus); |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 1390 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1391 | if (policy_is_inactive(policy)) { |
| 1392 | if (has_target()) |
| 1393 | strncpy(policy->last_governor, policy->governor->name, |
| 1394 | CPUFREQ_NAME_LEN); |
Srinivas Pandruvada | 69030dd | 2015-12-01 16:52:14 -0800 | [diff] [blame] | 1395 | else |
| 1396 | policy->last_policy = policy->policy; |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1397 | } else if (cpu == policy->cpu) { |
| 1398 | /* Nominate new CPU */ |
| 1399 | policy->cpu = cpumask_any(policy->cpus); |
| 1400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1402 | /* Start governor again for active policy */ |
| 1403 | if (!policy_is_inactive(policy)) { |
| 1404 | if (has_target()) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1405 | ret = cpufreq_governor(policy, CPUFREQ_GOV_START); |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1406 | if (!ret) |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1407 | ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1408 | |
Viresh Kumar | 9591bec | 2015-06-10 02:20:23 +0200 | [diff] [blame] | 1409 | if (ret) |
| 1410 | pr_err("%s: Failed to start governor\n", __func__); |
| 1411 | } |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1412 | |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1413 | goto unlock; |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | if (cpufreq_driver->stop_cpu) |
Dirk Brandewie | 367dc4a | 2014-03-19 08:45:53 -0700 | [diff] [blame] | 1417 | cpufreq_driver->stop_cpu(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1419 | /* If cpu is last user of policy, free policy */ |
| 1420 | if (has_target()) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1421 | ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1422 | if (ret) |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1423 | pr_err("%s: Failed to exit governor\n", __func__); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1424 | } |
| 1425 | |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1426 | /* |
| 1427 | * Perform the ->exit() even during light-weight tear-down, |
| 1428 | * since this is a core component, and is essential for the |
| 1429 | * subsequent light-weight ->init() to succeed. |
| 1430 | */ |
Srinivas Pandruvada | 55582bc | 2015-10-07 13:50:44 -0700 | [diff] [blame] | 1431 | if (cpufreq_driver->exit) { |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1432 | cpufreq_driver->exit(policy); |
Srinivas Pandruvada | 55582bc | 2015-10-07 13:50:44 -0700 | [diff] [blame] | 1433 | policy->freq_table = NULL; |
| 1434 | } |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1435 | |
| 1436 | unlock: |
| 1437 | up_write(&policy->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Srivatsa S. Bhat | cedb70a | 2013-09-07 01:23:09 +0530 | [diff] [blame] | 1440 | /** |
Viresh Kumar | 27a862e | 2013-10-02 14:13:14 +0530 | [diff] [blame] | 1441 | * cpufreq_remove_dev - remove a CPU device |
Srivatsa S. Bhat | cedb70a | 2013-09-07 01:23:09 +0530 | [diff] [blame] | 1442 | * |
| 1443 | * Removes the cpufreq interface for a CPU device. |
Srivatsa S. Bhat | cedb70a | 2013-09-07 01:23:09 +0530 | [diff] [blame] | 1444 | */ |
Viresh Kumar | 71db87b | 2015-07-30 15:04:01 +0530 | [diff] [blame] | 1445 | static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1446 | { |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 1447 | unsigned int cpu = dev->id; |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1448 | struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); |
Venki Pallipadi | ec28297 | 2007-03-26 12:03:19 -0700 | [diff] [blame] | 1449 | |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1450 | if (!policy) |
Linus Torvalds | 1af115d | 2015-08-31 08:47:40 -0700 | [diff] [blame] | 1451 | return; |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1452 | |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 1453 | if (cpu_online(cpu)) |
| 1454 | cpufreq_offline(cpu); |
Viresh Kumar | 8754914 | 2015-06-10 02:13:21 +0200 | [diff] [blame] | 1455 | |
Rafael J. Wysocki | 559ed40 | 2015-07-26 02:07:47 +0200 | [diff] [blame] | 1456 | cpumask_clear_cpu(cpu, policy->real_cpus); |
Viresh Kumar | 96bdda6 | 2015-10-15 21:35:24 +0530 | [diff] [blame] | 1457 | remove_cpu_dev_symlink(policy, cpu); |
Viresh Kumar | f344dae | 2015-11-21 09:06:49 +0530 | [diff] [blame] | 1458 | |
| 1459 | if (cpumask_empty(policy->real_cpus)) |
| 1460 | cpufreq_policy_free(policy, true); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1461 | } |
| 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | /** |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 1464 | * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're |
| 1465 | * in deep trouble. |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1466 | * @policy: policy managing CPUs |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | * @new_freq: CPU frequency the CPU actually runs at |
| 1468 | * |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 1469 | * We adjust to current frequency first, and need to clean up later. |
| 1470 | * So either call to cpufreq_update_policy() or schedule handle_update()). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | */ |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1472 | static void cpufreq_out_of_sync(struct cpufreq_policy *policy, |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1473 | unsigned int new_freq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | { |
| 1475 | struct cpufreq_freqs freqs; |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 1476 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 1477 | pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1478 | policy->cur, new_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1480 | freqs.old = policy->cur; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | freqs.new = new_freq; |
Viresh Kumar | b43a7ff | 2013-03-24 11:56:43 +0530 | [diff] [blame] | 1482 | |
Viresh Kumar | 8fec051 | 2014-03-24 13:35:45 +0530 | [diff] [blame] | 1483 | cpufreq_freq_transition_begin(policy, &freqs); |
| 1484 | cpufreq_freq_transition_end(policy, &freqs, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 1487 | /** |
Dhaval Giani | 4ab70df | 2006-12-13 14:49:15 +0530 | [diff] [blame] | 1488 | * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1489 | * @cpu: CPU number |
| 1490 | * |
| 1491 | * This is the last known freq, without actually getting it from the driver. |
| 1492 | * Return value will be same as what is shown in scaling_cur_freq in sysfs. |
| 1493 | */ |
| 1494 | unsigned int cpufreq_quick_get(unsigned int cpu) |
| 1495 | { |
Dirk Brandewie | 9e21ba8 | 2013-02-06 09:02:08 -0800 | [diff] [blame] | 1496 | struct cpufreq_policy *policy; |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1497 | unsigned int ret_freq = 0; |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1498 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1499 | if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) |
| 1500 | return cpufreq_driver->get(cpu); |
Dirk Brandewie | 9e21ba8 | 2013-02-06 09:02:08 -0800 | [diff] [blame] | 1501 | |
| 1502 | policy = cpufreq_cpu_get(cpu); |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1503 | if (policy) { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1504 | ret_freq = policy->cur; |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1505 | cpufreq_cpu_put(policy); |
| 1506 | } |
| 1507 | |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1508 | return ret_freq; |
Venkatesh Pallipadi | 95235ca | 2005-12-02 10:43:20 -0800 | [diff] [blame] | 1509 | } |
| 1510 | EXPORT_SYMBOL(cpufreq_quick_get); |
| 1511 | |
Jesse Barnes | 3d73710 | 2011-06-28 10:59:12 -0700 | [diff] [blame] | 1512 | /** |
| 1513 | * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU |
| 1514 | * @cpu: CPU number |
| 1515 | * |
| 1516 | * Just return the max possible frequency for a given CPU. |
| 1517 | */ |
| 1518 | unsigned int cpufreq_quick_get_max(unsigned int cpu) |
| 1519 | { |
| 1520 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 1521 | unsigned int ret_freq = 0; |
| 1522 | |
| 1523 | if (policy) { |
| 1524 | ret_freq = policy->max; |
| 1525 | cpufreq_cpu_put(policy); |
| 1526 | } |
| 1527 | |
| 1528 | return ret_freq; |
| 1529 | } |
| 1530 | EXPORT_SYMBOL(cpufreq_quick_get_max); |
| 1531 | |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 1532 | static unsigned int __cpufreq_get(struct cpufreq_policy *policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1534 | unsigned int ret_freq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1536 | if (!cpufreq_driver->get) |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1537 | return ret_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 1539 | ret_freq = cpufreq_driver->get(policy->cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
Viresh Kumar | 11e584c | 2015-06-10 02:11:45 +0200 | [diff] [blame] | 1541 | /* Updating inactive policies is invalid, so avoid doing that. */ |
| 1542 | if (unlikely(policy_is_inactive(policy))) |
| 1543 | return ret_freq; |
| 1544 | |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1545 | if (ret_freq && policy->cur && |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1546 | !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { |
Gautham R Shenoy | e08f5f5 | 2006-10-26 16:20:58 +0530 | [diff] [blame] | 1547 | /* verify no discrepancy between actual and |
| 1548 | saved value exists */ |
| 1549 | if (unlikely(ret_freq != policy->cur)) { |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 1550 | cpufreq_out_of_sync(policy, ret_freq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | schedule_work(&policy->update); |
| 1552 | } |
| 1553 | } |
| 1554 | |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1555 | return ret_freq; |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1558 | /** |
| 1559 | * cpufreq_get - get the current CPU frequency (in kHz) |
| 1560 | * @cpu: CPU number |
| 1561 | * |
| 1562 | * Get the CPU current (static) CPU frequency |
| 1563 | */ |
| 1564 | unsigned int cpufreq_get(unsigned int cpu) |
| 1565 | { |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1566 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1567 | unsigned int ret_freq = 0; |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 1568 | |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1569 | if (policy) { |
| 1570 | down_read(&policy->rwsem); |
Viresh Kumar | d92d50a | 2015-01-02 12:34:29 +0530 | [diff] [blame] | 1571 | ret_freq = __cpufreq_get(policy); |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1572 | up_read(&policy->rwsem); |
Viresh Kumar | 26ca869 | 2013-09-20 22:37:31 +0530 | [diff] [blame] | 1573 | |
Aaron Plattner | 999976e | 2014-03-04 12:42:15 -0800 | [diff] [blame] | 1574 | cpufreq_cpu_put(policy); |
| 1575 | } |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 1576 | |
Dave Jones | 4d34a67 | 2008-02-07 16:33:49 -0500 | [diff] [blame] | 1577 | return ret_freq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | } |
| 1579 | EXPORT_SYMBOL(cpufreq_get); |
| 1580 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 1581 | static struct subsys_interface cpufreq_interface = { |
| 1582 | .name = "cpufreq", |
| 1583 | .subsys = &cpu_subsys, |
| 1584 | .add_dev = cpufreq_add_dev, |
| 1585 | .remove_dev = cpufreq_remove_dev, |
Rafael J. Wysocki | e00e56d | 2011-03-23 22:16:32 +0100 | [diff] [blame] | 1586 | }; |
| 1587 | |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1588 | /* |
| 1589 | * In case platform wants some specific frequency to be configured |
| 1590 | * during suspend.. |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1591 | */ |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1592 | int cpufreq_generic_suspend(struct cpufreq_policy *policy) |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1593 | { |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1594 | int ret; |
Dave Jones | 4bc5d34 | 2009-08-04 14:03:25 -0400 | [diff] [blame] | 1595 | |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1596 | if (!policy->suspend_freq) { |
Bartlomiej Zolnierkiewicz | 201f371 | 2015-09-08 18:41:02 +0200 | [diff] [blame] | 1597 | pr_debug("%s: suspend_freq not defined\n", __func__); |
| 1598 | return 0; |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1601 | pr_debug("%s: Setting suspend-freq: %u\n", __func__, |
| 1602 | policy->suspend_freq); |
| 1603 | |
| 1604 | ret = __cpufreq_driver_target(policy, policy->suspend_freq, |
| 1605 | CPUFREQ_RELATION_H); |
| 1606 | if (ret) |
| 1607 | pr_err("%s: unable to set suspend-freq: %u. err: %d\n", |
| 1608 | __func__, policy->suspend_freq, ret); |
| 1609 | |
Dave Jones | c906049 | 2008-02-07 16:32:18 -0500 | [diff] [blame] | 1610 | return ret; |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1611 | } |
Viresh Kumar | e28867e | 2014-03-04 11:00:27 +0800 | [diff] [blame] | 1612 | EXPORT_SYMBOL(cpufreq_generic_suspend); |
Benjamin Herrenschmidt | 42d4dc3 | 2005-04-29 07:40:12 -0700 | [diff] [blame] | 1613 | |
| 1614 | /** |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1615 | * cpufreq_suspend() - Suspend CPUFreq governors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | * |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1617 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
| 1618 | * as some platforms can't change frequency after this point in suspend cycle. |
| 1619 | * Because some of the devices (like: i2c, regulators, etc) they use for |
| 1620 | * changing frequency are suspended quickly after this point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | */ |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1622 | void cpufreq_suspend(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 1624 | struct cpufreq_policy *policy; |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1625 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1627 | if (!cpufreq_driver) |
Rafael J. Wysocki | e00e56d | 2011-03-23 22:16:32 +0100 | [diff] [blame] | 1628 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1630 | if (!has_target()) |
Viresh Kumar | b1b12ba | 2014-09-30 09:33:17 +0530 | [diff] [blame] | 1631 | goto suspend; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1633 | pr_debug("%s: Suspending Governors\n", __func__); |
| 1634 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 1635 | for_each_active_policy(policy) { |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1636 | down_write(&policy->rwsem); |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1637 | ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1638 | up_write(&policy->rwsem); |
| 1639 | |
| 1640 | if (ret) |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1641 | pr_err("%s: Failed to stop governor for policy: %p\n", |
| 1642 | __func__, policy); |
| 1643 | else if (cpufreq_driver->suspend |
| 1644 | && cpufreq_driver->suspend(policy)) |
| 1645 | pr_err("%s: Failed to suspend driver: %p\n", __func__, |
| 1646 | policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | } |
Viresh Kumar | b1b12ba | 2014-09-30 09:33:17 +0530 | [diff] [blame] | 1648 | |
| 1649 | suspend: |
| 1650 | cpufreq_suspended = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1653 | /** |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1654 | * cpufreq_resume() - Resume CPUFreq governors |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1655 | * |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1656 | * Called during system wide Suspend/Hibernate cycle for resuming governors that |
| 1657 | * are suspended with cpufreq_suspend(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | */ |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1659 | void cpufreq_resume(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | struct cpufreq_policy *policy; |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1662 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1664 | if (!cpufreq_driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | return; |
| 1666 | |
Lan Tianyu | 8e30444 | 2014-09-18 15:03:07 +0800 | [diff] [blame] | 1667 | cpufreq_suspended = false; |
| 1668 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1669 | if (!has_target()) |
| 1670 | return; |
| 1671 | |
| 1672 | pr_debug("%s: Resuming Governors\n", __func__); |
| 1673 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 1674 | for_each_active_policy(policy) { |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1675 | if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { |
Viresh Kumar | 0c5aa40 | 2014-03-24 12:30:29 +0530 | [diff] [blame] | 1676 | pr_err("%s: Failed to resume driver: %p\n", __func__, |
| 1677 | policy); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1678 | } else { |
| 1679 | down_write(&policy->rwsem); |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1680 | ret = cpufreq_governor(policy, CPUFREQ_GOV_START); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1681 | if (!ret) |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1682 | cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 1683 | up_write(&policy->rwsem); |
| 1684 | |
| 1685 | if (ret) |
| 1686 | pr_err("%s: Failed to start governor for policy: %p\n", |
| 1687 | __func__, policy); |
| 1688 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | } |
Viresh Kumar | c75de0a | 2015-04-02 10:21:33 +0530 | [diff] [blame] | 1690 | |
| 1691 | /* |
| 1692 | * schedule call cpufreq_update_policy() for first-online CPU, as that |
| 1693 | * wouldn't be hotplugged-out on suspend. It will verify that the |
| 1694 | * current freq is in sync with what we believe it to be. |
| 1695 | */ |
| 1696 | policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask)); |
| 1697 | if (WARN_ON(!policy)) |
| 1698 | return; |
| 1699 | |
| 1700 | schedule_work(&policy->update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | |
Borislav Petkov | 9d95046 | 2013-01-20 10:24:28 +0000 | [diff] [blame] | 1703 | /** |
| 1704 | * cpufreq_get_current_driver - return current driver's name |
| 1705 | * |
| 1706 | * Return the name string of the currently loaded cpufreq driver |
| 1707 | * or NULL, if none. |
| 1708 | */ |
| 1709 | const char *cpufreq_get_current_driver(void) |
| 1710 | { |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1711 | if (cpufreq_driver) |
| 1712 | return cpufreq_driver->name; |
| 1713 | |
| 1714 | return NULL; |
Borislav Petkov | 9d95046 | 2013-01-20 10:24:28 +0000 | [diff] [blame] | 1715 | } |
| 1716 | EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | |
Thomas Petazzoni | 51315cd | 2014-10-19 11:30:27 +0200 | [diff] [blame] | 1718 | /** |
| 1719 | * cpufreq_get_driver_data - return current driver data |
| 1720 | * |
| 1721 | * Return the private data of the currently loaded cpufreq |
| 1722 | * driver, or NULL if no cpufreq driver is loaded. |
| 1723 | */ |
| 1724 | void *cpufreq_get_driver_data(void) |
| 1725 | { |
| 1726 | if (cpufreq_driver) |
| 1727 | return cpufreq_driver->driver_data; |
| 1728 | |
| 1729 | return NULL; |
| 1730 | } |
| 1731 | EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); |
| 1732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | /********************************************************************* |
| 1734 | * NOTIFIER LISTS INTERFACE * |
| 1735 | *********************************************************************/ |
| 1736 | |
| 1737 | /** |
| 1738 | * cpufreq_register_notifier - register a driver with cpufreq |
| 1739 | * @nb: notifier function to register |
| 1740 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
| 1741 | * |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 1742 | * Add a driver to one of two lists: either a list of drivers that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | * are notified about clock rate changes (once before and once after |
| 1744 | * the transition), or a list of drivers that are notified about |
| 1745 | * changes in cpufreq policy. |
| 1746 | * |
| 1747 | * This function may sleep, and has the same return conditions as |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1748 | * blocking_notifier_chain_register. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | */ |
| 1750 | int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) |
| 1751 | { |
| 1752 | int ret; |
| 1753 | |
Dirk Brandewie | d5aaffa | 2013-01-17 16:22:21 +0000 | [diff] [blame] | 1754 | if (cpufreq_disabled()) |
| 1755 | return -EINVAL; |
| 1756 | |
Cesar Eduardo Barros | 74212ca | 2008-02-16 08:41:24 -0200 | [diff] [blame] | 1757 | WARN_ON(!init_cpufreq_transition_notifier_list_called); |
| 1758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | switch (list) { |
| 1760 | case CPUFREQ_TRANSITION_NOTIFIER: |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 1761 | ret = srcu_notifier_chain_register( |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1762 | &cpufreq_transition_notifier_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | break; |
| 1764 | case CPUFREQ_POLICY_NOTIFIER: |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1765 | ret = blocking_notifier_chain_register( |
| 1766 | &cpufreq_policy_notifier_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | break; |
| 1768 | default: |
| 1769 | ret = -EINVAL; |
| 1770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | |
| 1772 | return ret; |
| 1773 | } |
| 1774 | EXPORT_SYMBOL(cpufreq_register_notifier); |
| 1775 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | /** |
| 1777 | * cpufreq_unregister_notifier - unregister a driver with cpufreq |
| 1778 | * @nb: notifier block to be unregistered |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 1779 | * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | * |
| 1781 | * Remove a driver from the CPU frequency notifier list. |
| 1782 | * |
| 1783 | * This function may sleep, and has the same return conditions as |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1784 | * blocking_notifier_chain_unregister. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | */ |
| 1786 | int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) |
| 1787 | { |
| 1788 | int ret; |
| 1789 | |
Dirk Brandewie | d5aaffa | 2013-01-17 16:22:21 +0000 | [diff] [blame] | 1790 | if (cpufreq_disabled()) |
| 1791 | return -EINVAL; |
| 1792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | switch (list) { |
| 1794 | case CPUFREQ_TRANSITION_NOTIFIER: |
Alan Stern | b4dfdbb | 2006-10-04 02:17:06 -0700 | [diff] [blame] | 1795 | ret = srcu_notifier_chain_unregister( |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1796 | &cpufreq_transition_notifier_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | break; |
| 1798 | case CPUFREQ_POLICY_NOTIFIER: |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1799 | ret = blocking_notifier_chain_unregister( |
| 1800 | &cpufreq_policy_notifier_list, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | break; |
| 1802 | default: |
| 1803 | ret = -EINVAL; |
| 1804 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | |
| 1806 | return ret; |
| 1807 | } |
| 1808 | EXPORT_SYMBOL(cpufreq_unregister_notifier); |
| 1809 | |
| 1810 | |
| 1811 | /********************************************************************* |
| 1812 | * GOVERNORS * |
| 1813 | *********************************************************************/ |
| 1814 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1815 | /* Must set freqs->new to intermediate frequency */ |
| 1816 | static int __target_intermediate(struct cpufreq_policy *policy, |
| 1817 | struct cpufreq_freqs *freqs, int index) |
| 1818 | { |
| 1819 | int ret; |
| 1820 | |
| 1821 | freqs->new = cpufreq_driver->get_intermediate(policy, index); |
| 1822 | |
| 1823 | /* We don't need to switch to intermediate freq */ |
| 1824 | if (!freqs->new) |
| 1825 | return 0; |
| 1826 | |
| 1827 | pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", |
| 1828 | __func__, policy->cpu, freqs->old, freqs->new); |
| 1829 | |
| 1830 | cpufreq_freq_transition_begin(policy, freqs); |
| 1831 | ret = cpufreq_driver->target_intermediate(policy, index); |
| 1832 | cpufreq_freq_transition_end(policy, freqs, ret); |
| 1833 | |
| 1834 | if (ret) |
| 1835 | pr_err("%s: Failed to change to intermediate frequency: %d\n", |
| 1836 | __func__, ret); |
| 1837 | |
| 1838 | return ret; |
| 1839 | } |
| 1840 | |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1841 | static int __target_index(struct cpufreq_policy *policy, |
| 1842 | struct cpufreq_frequency_table *freq_table, int index) |
| 1843 | { |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1844 | struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; |
| 1845 | unsigned int intermediate_freq = 0; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1846 | int retval = -EINVAL; |
| 1847 | bool notify; |
| 1848 | |
| 1849 | notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1850 | if (notify) { |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1851 | /* Handle switching to intermediate frequency */ |
| 1852 | if (cpufreq_driver->get_intermediate) { |
| 1853 | retval = __target_intermediate(policy, &freqs, index); |
| 1854 | if (retval) |
| 1855 | return retval; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1856 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1857 | intermediate_freq = freqs.new; |
| 1858 | /* Set old freq to intermediate */ |
| 1859 | if (intermediate_freq) |
| 1860 | freqs.old = freqs.new; |
| 1861 | } |
| 1862 | |
| 1863 | freqs.new = freq_table[index].frequency; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1864 | pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", |
| 1865 | __func__, policy->cpu, freqs.old, freqs.new); |
| 1866 | |
| 1867 | cpufreq_freq_transition_begin(policy, &freqs); |
| 1868 | } |
| 1869 | |
| 1870 | retval = cpufreq_driver->target_index(policy, index); |
| 1871 | if (retval) |
| 1872 | pr_err("%s: Failed to change cpu frequency: %d\n", __func__, |
| 1873 | retval); |
| 1874 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1875 | if (notify) { |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1876 | cpufreq_freq_transition_end(policy, &freqs, retval); |
| 1877 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1878 | /* |
| 1879 | * Failed after setting to intermediate freq? Driver should have |
| 1880 | * reverted back to initial frequency and so should we. Check |
| 1881 | * here for intermediate_freq instead of get_intermediate, in |
Shailendra Verma | 58405af | 2015-05-22 22:48:22 +0530 | [diff] [blame] | 1882 | * case we haven't switched to intermediate freq at all. |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1883 | */ |
| 1884 | if (unlikely(retval && intermediate_freq)) { |
| 1885 | freqs.old = intermediate_freq; |
| 1886 | freqs.new = policy->restore_freq; |
| 1887 | cpufreq_freq_transition_begin(policy, &freqs); |
| 1888 | cpufreq_freq_transition_end(policy, &freqs, 0); |
| 1889 | } |
| 1890 | } |
| 1891 | |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1892 | return retval; |
| 1893 | } |
| 1894 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | int __cpufreq_driver_target(struct cpufreq_policy *policy, |
| 1896 | unsigned int target_freq, |
| 1897 | unsigned int relation) |
| 1898 | { |
Viresh Kumar | 7249924 | 2012-10-31 01:28:21 +0100 | [diff] [blame] | 1899 | unsigned int old_target_freq = target_freq; |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1900 | int retval = -EINVAL; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 1901 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 1902 | if (cpufreq_disabled()) |
| 1903 | return -ENODEV; |
| 1904 | |
Viresh Kumar | 7249924 | 2012-10-31 01:28:21 +0100 | [diff] [blame] | 1905 | /* Make sure that target_freq is within supported range */ |
| 1906 | if (target_freq > policy->max) |
| 1907 | target_freq = policy->max; |
| 1908 | if (target_freq < policy->min) |
| 1909 | target_freq = policy->min; |
| 1910 | |
| 1911 | pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 1912 | policy->cpu, target_freq, relation, old_target_freq); |
Viresh Kumar | 5a1c022 | 2012-10-31 01:28:15 +0100 | [diff] [blame] | 1913 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1914 | /* |
| 1915 | * This might look like a redundant call as we are checking it again |
| 1916 | * after finding index. But it is left intentionally for cases where |
| 1917 | * exactly same freq is called again and so we can save on few function |
| 1918 | * calls. |
| 1919 | */ |
Viresh Kumar | 5a1c022 | 2012-10-31 01:28:15 +0100 | [diff] [blame] | 1920 | if (target_freq == policy->cur) |
| 1921 | return 0; |
| 1922 | |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 1923 | /* Save last value to restore later on errors */ |
| 1924 | policy->restore_freq = policy->cur; |
| 1925 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 1926 | if (cpufreq_driver->target) |
| 1927 | retval = cpufreq_driver->target(policy, target_freq, relation); |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1928 | else if (cpufreq_driver->target_index) { |
| 1929 | struct cpufreq_frequency_table *freq_table; |
| 1930 | int index; |
Ashok Raj | 90d45d1 | 2005-11-08 21:34:24 -0800 | [diff] [blame] | 1931 | |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1932 | freq_table = cpufreq_frequency_get_table(policy->cpu); |
| 1933 | if (unlikely(!freq_table)) { |
| 1934 | pr_err("%s: Unable to find freq_table\n", __func__); |
| 1935 | goto out; |
| 1936 | } |
| 1937 | |
| 1938 | retval = cpufreq_frequency_table_target(policy, freq_table, |
| 1939 | target_freq, relation, &index); |
| 1940 | if (unlikely(retval)) { |
| 1941 | pr_err("%s: Unable to find matching freq\n", __func__); |
| 1942 | goto out; |
| 1943 | } |
| 1944 | |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 1945 | if (freq_table[index].frequency == policy->cur) { |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1946 | retval = 0; |
Viresh Kumar | d4019f0 | 2013-08-14 19:38:24 +0530 | [diff] [blame] | 1947 | goto out; |
| 1948 | } |
| 1949 | |
Viresh Kumar | 8d65775 | 2014-05-21 14:29:29 +0530 | [diff] [blame] | 1950 | retval = __target_index(policy, freq_table, index); |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | return retval; |
| 1955 | } |
| 1956 | EXPORT_SYMBOL_GPL(__cpufreq_driver_target); |
| 1957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | int cpufreq_driver_target(struct cpufreq_policy *policy, |
| 1959 | unsigned int target_freq, |
| 1960 | unsigned int relation) |
| 1961 | { |
Julia Lawall | f1829e4 | 2008-07-25 22:44:53 +0200 | [diff] [blame] | 1962 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 1964 | down_write(&policy->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | |
| 1966 | ret = __cpufreq_driver_target(policy, target_freq, relation); |
| 1967 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 1968 | up_write(&policy->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | return ret; |
| 1971 | } |
| 1972 | EXPORT_SYMBOL_GPL(cpufreq_driver_target); |
| 1973 | |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1974 | __weak struct cpufreq_governor *cpufreq_fallback_governor(void) |
| 1975 | { |
| 1976 | return NULL; |
| 1977 | } |
| 1978 | |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 1979 | static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | { |
Dave Jones | cc993ca | 2005-07-28 09:43:56 -0700 | [diff] [blame] | 1981 | int ret; |
Thomas Renninger | 6afde10 | 2007-10-02 13:28:13 -0700 | [diff] [blame] | 1982 | |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1983 | /* Don't start any governor operations if we are entering suspend */ |
| 1984 | if (cpufreq_suspended) |
| 1985 | return 0; |
Ethan Zhao | cb57720b | 2014-12-18 15:28:19 +0900 | [diff] [blame] | 1986 | /* |
| 1987 | * Governor might not be initiated here if ACPI _PPC changed |
| 1988 | * notification happened, so check it. |
| 1989 | */ |
| 1990 | if (!policy->governor) |
| 1991 | return -EINVAL; |
Viresh Kumar | 2f0aea9 | 2014-03-04 11:00:26 +0800 | [diff] [blame] | 1992 | |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame] | 1993 | if (policy->governor->max_transition_latency && |
| 1994 | policy->cpuinfo.transition_latency > |
| 1995 | policy->governor->max_transition_latency) { |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 1996 | struct cpufreq_governor *gov = cpufreq_fallback_governor(); |
| 1997 | |
| 1998 | if (gov) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 1999 | pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", |
| 2000 | policy->governor->name, gov->name); |
Thomas Renninger | 6afde10 | 2007-10-02 13:28:13 -0700 | [diff] [blame] | 2001 | policy->governor = gov; |
Rafael J. Wysocki | de1df26 | 2016-02-05 02:37:42 +0100 | [diff] [blame] | 2002 | } else { |
| 2003 | return -EINVAL; |
Thomas Renninger | 6afde10 | 2007-10-02 13:28:13 -0700 | [diff] [blame] | 2004 | } |
Thomas Renninger | 1c25624 | 2007-10-02 13:28:12 -0700 | [diff] [blame] | 2005 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
Viresh Kumar | fe492f3 | 2013-08-06 22:53:10 +0530 | [diff] [blame] | 2007 | if (event == CPUFREQ_GOV_POLICY_INIT) |
| 2008 | if (!try_module_get(policy->governor->owner)) |
| 2009 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | |
Viresh Kumar | 63431f7 | 2015-07-27 17:58:06 +0530 | [diff] [blame] | 2011 | pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event); |
Xiaoguang Chen | 95731eb | 2013-06-19 15:00:07 +0800 | [diff] [blame] | 2012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | ret = policy->governor->governor(policy, event); |
| 2014 | |
Viresh Kumar | 4d5dcc4 | 2013-03-27 15:58:58 +0000 | [diff] [blame] | 2015 | if (!ret) { |
| 2016 | if (event == CPUFREQ_GOV_POLICY_INIT) |
| 2017 | policy->governor->initialized++; |
| 2018 | else if (event == CPUFREQ_GOV_POLICY_EXIT) |
| 2019 | policy->governor->initialized--; |
| 2020 | } |
Viresh Kumar | b394058 | 2013-02-01 05:42:58 +0000 | [diff] [blame] | 2021 | |
Viresh Kumar | fe492f3 | 2013-08-06 22:53:10 +0530 | [diff] [blame] | 2022 | if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || |
| 2023 | ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2024 | module_put(policy->governor->owner); |
| 2025 | |
| 2026 | return ret; |
| 2027 | } |
| 2028 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | int cpufreq_register_governor(struct cpufreq_governor *governor) |
| 2030 | { |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2031 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
| 2033 | if (!governor) |
| 2034 | return -EINVAL; |
| 2035 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2036 | if (cpufreq_disabled()) |
| 2037 | return -ENODEV; |
| 2038 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 2039 | mutex_lock(&cpufreq_governor_mutex); |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2040 | |
Viresh Kumar | b394058 | 2013-02-01 05:42:58 +0000 | [diff] [blame] | 2041 | governor->initialized = 0; |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2042 | err = -EBUSY; |
Viresh Kumar | 42f91fa | 2015-01-02 12:34:26 +0530 | [diff] [blame] | 2043 | if (!find_governor(governor->name)) { |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2044 | err = 0; |
| 2045 | list_add(&governor->governor_list, &cpufreq_governor_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2048 | mutex_unlock(&cpufreq_governor_mutex); |
Jeremy Fitzhardinge | 3bcb09a | 2006-07-06 12:30:26 -0700 | [diff] [blame] | 2049 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | } |
| 2051 | EXPORT_SYMBOL_GPL(cpufreq_register_governor); |
| 2052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | void cpufreq_unregister_governor(struct cpufreq_governor *governor) |
| 2054 | { |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2055 | struct cpufreq_policy *policy; |
| 2056 | unsigned long flags; |
Prarit Bhargava | 90e41ba | 2009-11-12 09:18:46 -0500 | [diff] [blame] | 2057 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | if (!governor) |
| 2059 | return; |
| 2060 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2061 | if (cpufreq_disabled()) |
| 2062 | return; |
| 2063 | |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2064 | /* clear last_governor for all inactive policies */ |
| 2065 | read_lock_irqsave(&cpufreq_driver_lock, flags); |
| 2066 | for_each_inactive_policy(policy) { |
Viresh Kumar | 18bf3a1 | 2015-05-12 12:22:51 +0530 | [diff] [blame] | 2067 | if (!strcmp(policy->last_governor, governor->name)) { |
| 2068 | policy->governor = NULL; |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2069 | strcpy(policy->last_governor, "\0"); |
Viresh Kumar | 18bf3a1 | 2015-05-12 12:22:51 +0530 | [diff] [blame] | 2070 | } |
Prarit Bhargava | 90e41ba | 2009-11-12 09:18:46 -0500 | [diff] [blame] | 2071 | } |
Viresh Kumar | 4573237 | 2015-05-12 12:22:34 +0530 | [diff] [blame] | 2072 | read_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Prarit Bhargava | 90e41ba | 2009-11-12 09:18:46 -0500 | [diff] [blame] | 2073 | |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 2074 | mutex_lock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | list_del(&governor->governor_list); |
akpm@osdl.org | 3fc54d3 | 2006-01-13 15:54:22 -0800 | [diff] [blame] | 2076 | mutex_unlock(&cpufreq_governor_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | return; |
| 2078 | } |
| 2079 | EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); |
| 2080 | |
| 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | /********************************************************************* |
| 2083 | * POLICY INTERFACE * |
| 2084 | *********************************************************************/ |
| 2085 | |
| 2086 | /** |
| 2087 | * cpufreq_get_policy - get the current cpufreq_policy |
Dave Jones | 29464f2 | 2009-01-18 01:37:11 -0500 | [diff] [blame] | 2088 | * @policy: struct cpufreq_policy into which the current cpufreq_policy |
| 2089 | * is written |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | * |
| 2091 | * Reads the current cpufreq policy. |
| 2092 | */ |
| 2093 | int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) |
| 2094 | { |
| 2095 | struct cpufreq_policy *cpu_policy; |
| 2096 | if (!policy) |
| 2097 | return -EINVAL; |
| 2098 | |
| 2099 | cpu_policy = cpufreq_cpu_get(cpu); |
| 2100 | if (!cpu_policy) |
| 2101 | return -EINVAL; |
| 2102 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 2103 | memcpy(policy, cpu_policy, sizeof(*policy)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | |
| 2105 | cpufreq_cpu_put(cpu_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | return 0; |
| 2107 | } |
| 2108 | EXPORT_SYMBOL(cpufreq_get_policy); |
| 2109 | |
Arjan van de Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 2110 | /* |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 2111 | * policy : current policy. |
| 2112 | * new_policy: policy to be set. |
Arjan van de Ven | 153d7f3 | 2006-07-26 15:40:07 +0200 | [diff] [blame] | 2113 | */ |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 2114 | static int cpufreq_set_policy(struct cpufreq_policy *policy, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2115 | struct cpufreq_policy *new_policy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | { |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2117 | struct cpufreq_governor *old_gov; |
| 2118 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2120 | pr_debug("setting new policy for CPU %u: %u - %u kHz\n", |
| 2121 | new_policy->cpu, new_policy->min, new_policy->max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2122 | |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 2123 | memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | |
Pan Xinhui | fba9573 | 2015-07-30 18:10:40 +0800 | [diff] [blame] | 2125 | /* |
| 2126 | * This check works well when we store new min/max freq attributes, |
| 2127 | * because new_policy is a copy of policy with one field updated. |
| 2128 | */ |
| 2129 | if (new_policy->min > new_policy->max) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2130 | return -EINVAL; |
Mattia Dongili | 9c9a43e | 2006-07-05 23:12:20 +0200 | [diff] [blame] | 2131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | /* verify the cpu speed can be set within this limit */ |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2133 | ret = cpufreq_driver->verify(new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | if (ret) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2135 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | /* adjust if necessary - all reasons */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2138 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2139 | CPUFREQ_ADJUST, new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2141 | /* |
| 2142 | * verify the cpu speed can be set within this limit, which might be |
| 2143 | * different to the first one |
| 2144 | */ |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2145 | ret = cpufreq_driver->verify(new_policy); |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2146 | if (ret) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2147 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | |
| 2149 | /* notification of the new policy */ |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2150 | blocking_notifier_call_chain(&cpufreq_policy_notifier_list, |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2151 | CPUFREQ_NOTIFY, new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2153 | policy->min = new_policy->min; |
| 2154 | policy->max = new_policy->max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2156 | pr_debug("new min and max freqs are %u - %u kHz\n", |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2157 | policy->min, policy->max); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2159 | if (cpufreq_driver->setpolicy) { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2160 | policy->policy = new_policy->policy; |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2161 | pr_debug("setting range\n"); |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2162 | return cpufreq_driver->setpolicy(new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | } |
| 2164 | |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2165 | if (new_policy->governor == policy->governor) |
| 2166 | goto out; |
| 2167 | |
| 2168 | pr_debug("governor switch\n"); |
| 2169 | |
| 2170 | /* save old, working values */ |
| 2171 | old_gov = policy->governor; |
| 2172 | /* end old governor */ |
| 2173 | if (old_gov) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2174 | ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2175 | if (ret) { |
| 2176 | /* This can happen due to race with other operations */ |
| 2177 | pr_debug("%s: Failed to Stop Governor: %s (%d)\n", |
| 2178 | __func__, old_gov->name, ret); |
| 2179 | return ret; |
| 2180 | } |
| 2181 | |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2182 | ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2183 | if (ret) { |
| 2184 | pr_err("%s: Failed to Exit Governor: %s (%d)\n", |
| 2185 | __func__, old_gov->name, ret); |
| 2186 | return ret; |
| 2187 | } |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /* start new governor */ |
| 2191 | policy->governor = new_policy->governor; |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2192 | ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2193 | if (!ret) { |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2194 | ret = cpufreq_governor(policy, CPUFREQ_GOV_START); |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2195 | if (!ret) |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2196 | goto out; |
| 2197 | |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2198 | cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2199 | } |
| 2200 | |
| 2201 | /* new governor failed, so re-start old one */ |
| 2202 | pr_debug("starting governor %s failed\n", policy->governor->name); |
| 2203 | if (old_gov) { |
| 2204 | policy->governor = old_gov; |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2205 | if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2206 | policy->governor = NULL; |
| 2207 | else |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2208 | cpufreq_governor(policy, CPUFREQ_GOV_START); |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2209 | } |
| 2210 | |
Viresh Kumar | 4bc384a | 2015-07-18 11:31:03 +0530 | [diff] [blame] | 2211 | return ret; |
Rafael J. Wysocki | d9a789c | 2014-02-17 22:56:35 +0100 | [diff] [blame] | 2212 | |
| 2213 | out: |
| 2214 | pr_debug("governor: change or update limits\n"); |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2215 | return cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | * cpufreq_update_policy - re-evaluate an existing cpufreq policy |
| 2220 | * @cpu: CPU which shall be re-evaluated |
| 2221 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2222 | * Useful for policy notifiers which have different necessities |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | * at different times. |
| 2224 | */ |
| 2225 | int cpufreq_update_policy(unsigned int cpu) |
| 2226 | { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2227 | struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); |
| 2228 | struct cpufreq_policy new_policy; |
Julia Lawall | f1829e4 | 2008-07-25 22:44:53 +0200 | [diff] [blame] | 2229 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | |
Aaron Plattner | fefa8ff | 2014-06-18 11:27:32 -0700 | [diff] [blame] | 2231 | if (!policy) |
| 2232 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 2234 | down_write(&policy->rwsem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2236 | pr_debug("updating policy for CPU %u\n", cpu); |
Viresh Kumar | d5b73cd | 2013-08-06 22:53:06 +0530 | [diff] [blame] | 2237 | memcpy(&new_policy, policy, sizeof(*policy)); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2238 | new_policy.min = policy->user_policy.min; |
| 2239 | new_policy.max = policy->user_policy.max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2241 | /* |
| 2242 | * BIOS might change freq behind our back |
| 2243 | * -> ask driver for current freq and notify governors about a change |
| 2244 | */ |
Rafael J. Wysocki | 2ed99e3 | 2014-03-12 21:49:33 +0100 | [diff] [blame] | 2245 | if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2246 | new_policy.cur = cpufreq_driver->get(cpu); |
Viresh Kumar | bd0fa9b | 2014-02-25 14:29:44 +0530 | [diff] [blame] | 2247 | if (WARN_ON(!new_policy.cur)) { |
| 2248 | ret = -EIO; |
Aaron Plattner | fefa8ff | 2014-06-18 11:27:32 -0700 | [diff] [blame] | 2249 | goto unlock; |
Viresh Kumar | bd0fa9b | 2014-02-25 14:29:44 +0530 | [diff] [blame] | 2250 | } |
| 2251 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2252 | if (!policy->cur) { |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2253 | pr_debug("Driver did not initialize current freq\n"); |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2254 | policy->cur = new_policy.cur; |
Thomas Renninger | a85f7bd | 2006-02-01 11:36:04 +0100 | [diff] [blame] | 2255 | } else { |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 2256 | if (policy->cur != new_policy.cur && has_target()) |
Viresh Kumar | a1e1dc4 | 2015-01-02 12:34:28 +0530 | [diff] [blame] | 2257 | cpufreq_out_of_sync(policy, new_policy.cur); |
Thomas Renninger | a85f7bd | 2006-02-01 11:36:04 +0100 | [diff] [blame] | 2258 | } |
Thomas Renninger | 0961dd0 | 2006-01-26 18:46:33 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
Viresh Kumar | 037ce83 | 2013-10-02 14:13:16 +0530 | [diff] [blame] | 2261 | ret = cpufreq_set_policy(policy, &new_policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | |
Aaron Plattner | fefa8ff | 2014-06-18 11:27:32 -0700 | [diff] [blame] | 2263 | unlock: |
viresh kumar | ad7722d | 2013-10-18 19:10:15 +0530 | [diff] [blame] | 2264 | up_write(&policy->rwsem); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2265 | |
Viresh Kumar | 3a3e9e0 | 2013-08-06 22:53:05 +0530 | [diff] [blame] | 2266 | cpufreq_cpu_put(policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | return ret; |
| 2268 | } |
| 2269 | EXPORT_SYMBOL(cpufreq_update_policy); |
| 2270 | |
Paul Gortmaker | 2760984 | 2013-06-19 13:54:04 -0400 | [diff] [blame] | 2271 | static int cpufreq_cpu_callback(struct notifier_block *nfb, |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 2272 | unsigned long action, void *hcpu) |
| 2273 | { |
| 2274 | unsigned int cpu = (unsigned long)hcpu; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 2275 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 2276 | switch (action & ~CPU_TASKS_FROZEN) { |
| 2277 | case CPU_ONLINE: |
| 2278 | cpufreq_online(cpu); |
| 2279 | break; |
Srivatsa S. Bhat | 5302c3f | 2013-07-30 04:25:25 +0530 | [diff] [blame] | 2280 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 2281 | case CPU_DOWN_PREPARE: |
Viresh Kumar | 69cee71 | 2016-02-11 17:31:11 +0530 | [diff] [blame] | 2282 | cpufreq_offline(cpu); |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 2283 | break; |
Srivatsa S. Bhat | 5302c3f | 2013-07-30 04:25:25 +0530 | [diff] [blame] | 2284 | |
Rafael J. Wysocki | 0b27535 | 2015-07-29 03:03:44 +0200 | [diff] [blame] | 2285 | case CPU_DOWN_FAILED: |
| 2286 | cpufreq_online(cpu); |
| 2287 | break; |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 2288 | } |
| 2289 | return NOTIFY_OK; |
| 2290 | } |
| 2291 | |
Neal Buckendahl | 9c36f74 | 2010-06-22 22:02:44 -0500 | [diff] [blame] | 2292 | static struct notifier_block __refdata cpufreq_cpu_notifier = { |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2293 | .notifier_call = cpufreq_cpu_callback, |
Ashok Raj | c32b6b8 | 2005-10-30 14:59:54 -0800 | [diff] [blame] | 2294 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | |
| 2296 | /********************************************************************* |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2297 | * BOOST * |
| 2298 | *********************************************************************/ |
| 2299 | static int cpufreq_boost_set_sw(int state) |
| 2300 | { |
| 2301 | struct cpufreq_frequency_table *freq_table; |
| 2302 | struct cpufreq_policy *policy; |
| 2303 | int ret = -EINVAL; |
| 2304 | |
Viresh Kumar | f963735 | 2015-05-12 12:20:11 +0530 | [diff] [blame] | 2305 | for_each_active_policy(policy) { |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2306 | freq_table = cpufreq_frequency_get_table(policy->cpu); |
| 2307 | if (freq_table) { |
| 2308 | ret = cpufreq_frequency_table_cpuinfo(policy, |
| 2309 | freq_table); |
| 2310 | if (ret) { |
| 2311 | pr_err("%s: Policy frequency update failed\n", |
| 2312 | __func__); |
| 2313 | break; |
| 2314 | } |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 2315 | |
| 2316 | down_write(&policy->rwsem); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2317 | policy->user_policy.max = policy->max; |
Viresh Kumar | a1317e0 | 2016-02-22 16:36:43 +0530 | [diff] [blame] | 2318 | cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); |
Viresh Kumar | 49f1856 | 2016-02-11 17:31:12 +0530 | [diff] [blame] | 2319 | up_write(&policy->rwsem); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | return ret; |
| 2324 | } |
| 2325 | |
| 2326 | int cpufreq_boost_trigger_state(int state) |
| 2327 | { |
| 2328 | unsigned long flags; |
| 2329 | int ret = 0; |
| 2330 | |
| 2331 | if (cpufreq_driver->boost_enabled == state) |
| 2332 | return 0; |
| 2333 | |
| 2334 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 2335 | cpufreq_driver->boost_enabled = state; |
| 2336 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 2337 | |
| 2338 | ret = cpufreq_driver->set_boost(state); |
| 2339 | if (ret) { |
| 2340 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
| 2341 | cpufreq_driver->boost_enabled = !state; |
| 2342 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
| 2343 | |
Joe Perches | e837f9b | 2014-03-11 10:03:00 -0700 | [diff] [blame] | 2344 | pr_err("%s: Cannot %s BOOST\n", |
| 2345 | __func__, state ? "enable" : "disable"); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2346 | } |
| 2347 | |
| 2348 | return ret; |
| 2349 | } |
| 2350 | |
Rafael J. Wysocki | 41669da | 2015-12-27 00:23:48 +0100 | [diff] [blame] | 2351 | static bool cpufreq_boost_supported(void) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2352 | { |
Rafael J. Wysocki | 7a6c79f | 2015-12-27 00:27:38 +0100 | [diff] [blame] | 2353 | return likely(cpufreq_driver) && cpufreq_driver->set_boost; |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2354 | } |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2355 | |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2356 | static int create_boost_sysfs_file(void) |
| 2357 | { |
| 2358 | int ret; |
| 2359 | |
Viresh Kumar | c82bd44 | 2015-10-15 21:35:23 +0530 | [diff] [blame] | 2360 | ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2361 | if (ret) |
| 2362 | pr_err("%s: cannot register global BOOST sysfs file\n", |
| 2363 | __func__); |
| 2364 | |
| 2365 | return ret; |
| 2366 | } |
| 2367 | |
| 2368 | static void remove_boost_sysfs_file(void) |
| 2369 | { |
| 2370 | if (cpufreq_boost_supported()) |
Viresh Kumar | c82bd44 | 2015-10-15 21:35:23 +0530 | [diff] [blame] | 2371 | sysfs_remove_file(cpufreq_global_kobject, &boost.attr); |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | int cpufreq_enable_boost_support(void) |
| 2375 | { |
| 2376 | if (!cpufreq_driver) |
| 2377 | return -EINVAL; |
| 2378 | |
| 2379 | if (cpufreq_boost_supported()) |
| 2380 | return 0; |
| 2381 | |
Rafael J. Wysocki | 7a6c79f | 2015-12-27 00:27:38 +0100 | [diff] [blame] | 2382 | cpufreq_driver->set_boost = cpufreq_boost_set_sw; |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2383 | |
| 2384 | /* This will get removed on driver unregister */ |
| 2385 | return create_boost_sysfs_file(); |
| 2386 | } |
| 2387 | EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); |
| 2388 | |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2389 | int cpufreq_boost_enabled(void) |
| 2390 | { |
| 2391 | return cpufreq_driver->boost_enabled; |
| 2392 | } |
| 2393 | EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); |
| 2394 | |
| 2395 | /********************************************************************* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | * REGISTER / UNREGISTER CPUFREQ DRIVER * |
| 2397 | *********************************************************************/ |
| 2398 | |
| 2399 | /** |
| 2400 | * cpufreq_register_driver - register a CPU Frequency driver |
| 2401 | * @driver_data: A struct cpufreq_driver containing the values# |
| 2402 | * submitted by the CPU Frequency driver. |
| 2403 | * |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2404 | * Registers a CPU Frequency driver to this core code. This code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | * returns zero on success, -EBUSY when another driver got here first |
Dave Jones | 32ee8c3 | 2006-02-28 00:43:23 -0500 | [diff] [blame] | 2406 | * (and isn't unregistered in the meantime). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | * |
| 2408 | */ |
Linus Torvalds | 221dee2 | 2007-02-26 14:55:48 -0800 | [diff] [blame] | 2409 | int cpufreq_register_driver(struct cpufreq_driver *driver_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | { |
| 2411 | unsigned long flags; |
| 2412 | int ret; |
| 2413 | |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2414 | if (cpufreq_disabled()) |
| 2415 | return -ENODEV; |
| 2416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | if (!driver_data || !driver_data->verify || !driver_data->init || |
Viresh Kumar | 9c0ebcf | 2013-10-25 19:45:48 +0530 | [diff] [blame] | 2418 | !(driver_data->setpolicy || driver_data->target_index || |
Rafael J. Wysocki | 9832235 | 2014-03-19 12:48:30 +0100 | [diff] [blame] | 2419 | driver_data->target) || |
| 2420 | (driver_data->setpolicy && (driver_data->target_index || |
Viresh Kumar | 1c03a2d | 2014-06-02 22:49:28 +0530 | [diff] [blame] | 2421 | driver_data->target)) || |
| 2422 | (!!driver_data->get_intermediate != !!driver_data->target_intermediate)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | return -EINVAL; |
| 2424 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2425 | pr_debug("trying to register driver %s\n", driver_data->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2427 | /* Protect against concurrent CPU online/offline. */ |
| 2428 | get_online_cpus(); |
| 2429 | |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2430 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2431 | if (cpufreq_driver) { |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2432 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2433 | ret = -EEXIST; |
| 2434 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | } |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2436 | cpufreq_driver = driver_data; |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2437 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | |
Viresh Kumar | bc68b7d | 2015-01-02 12:34:30 +0530 | [diff] [blame] | 2439 | if (driver_data->setpolicy) |
| 2440 | driver_data->flags |= CPUFREQ_CONST_LOOPS; |
| 2441 | |
Rafael J. Wysocki | 7a6c79f | 2015-12-27 00:27:38 +0100 | [diff] [blame] | 2442 | if (cpufreq_boost_supported()) { |
| 2443 | ret = create_boost_sysfs_file(); |
| 2444 | if (ret) |
| 2445 | goto err_null_driver; |
| 2446 | } |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2447 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 2448 | ret = subsys_interface_register(&cpufreq_interface); |
Jiri Slaby | 8f5bc2a | 2011-03-01 17:41:10 +0100 | [diff] [blame] | 2449 | if (ret) |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2450 | goto err_boost_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | |
Viresh Kumar | ce1bcfe | 2015-01-02 12:34:35 +0530 | [diff] [blame] | 2452 | if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && |
| 2453 | list_empty(&cpufreq_policy_list)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | /* if all ->init() calls failed, unregister */ |
Viresh Kumar | ce1bcfe | 2015-01-02 12:34:35 +0530 | [diff] [blame] | 2455 | pr_debug("%s: No CPU initialized for driver %s\n", __func__, |
| 2456 | driver_data->name); |
| 2457 | goto err_if_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2458 | } |
| 2459 | |
Jiri Slaby | 8f5bc2a | 2011-03-01 17:41:10 +0100 | [diff] [blame] | 2460 | register_hotcpu_notifier(&cpufreq_cpu_notifier); |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2461 | pr_debug("driver %s up and running\n", driver_data->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2463 | out: |
| 2464 | put_online_cpus(); |
| 2465 | return ret; |
| 2466 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 2467 | err_if_unreg: |
| 2468 | subsys_interface_unregister(&cpufreq_interface); |
Lukasz Majewski | 6f19efc | 2013-12-20 15:24:49 +0100 | [diff] [blame] | 2469 | err_boost_unreg: |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2470 | remove_boost_sysfs_file(); |
Jiri Slaby | 8f5bc2a | 2011-03-01 17:41:10 +0100 | [diff] [blame] | 2471 | err_null_driver: |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2472 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2473 | cpufreq_driver = NULL; |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2474 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Rafael J. Wysocki | fdd320d | 2015-07-30 01:45:07 +0200 | [diff] [blame] | 2475 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | } |
| 2477 | EXPORT_SYMBOL_GPL(cpufreq_register_driver); |
| 2478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | /** |
| 2480 | * cpufreq_unregister_driver - unregister the current CPUFreq driver |
| 2481 | * |
Viresh Kumar | bb176f7 | 2013-06-19 14:19:33 +0530 | [diff] [blame] | 2482 | * Unregister the current CPUFreq driver. Only call this if you have |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | * the right to do so, i.e. if you have succeeded in initialising before! |
| 2484 | * Returns zero if successful, and -EINVAL if the cpufreq_driver is |
| 2485 | * currently not initialised. |
| 2486 | */ |
Linus Torvalds | 221dee2 | 2007-02-26 14:55:48 -0800 | [diff] [blame] | 2487 | int cpufreq_unregister_driver(struct cpufreq_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | { |
| 2489 | unsigned long flags; |
| 2490 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2491 | if (!cpufreq_driver || (driver != cpufreq_driver)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
Dominik Brodowski | 2d06d8c | 2011-03-27 15:04:46 +0200 | [diff] [blame] | 2494 | pr_debug("unregistering driver %s\n", driver->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
Sebastian Andrzej Siewior | 454d3a2 | 2015-07-22 17:59:11 +0200 | [diff] [blame] | 2496 | /* Protect against concurrent cpu hotplug */ |
| 2497 | get_online_cpus(); |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 2498 | subsys_interface_unregister(&cpufreq_interface); |
Viresh Kumar | 44139ed | 2015-07-29 16:23:09 +0530 | [diff] [blame] | 2499 | remove_boost_sysfs_file(); |
Chandra Seetharaman | 65edc68 | 2006-06-27 02:54:08 -0700 | [diff] [blame] | 2500 | unregister_hotcpu_notifier(&cpufreq_cpu_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2502 | write_lock_irqsave(&cpufreq_driver_lock, flags); |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 2503 | |
Rafael J. Wysocki | 1c3d85d | 2013-04-29 00:08:16 +0200 | [diff] [blame] | 2504 | cpufreq_driver = NULL; |
Viresh Kumar | 6eed940 | 2013-08-06 22:53:11 +0530 | [diff] [blame] | 2505 | |
Nathan Zimmer | 0d1857a | 2013-02-22 16:24:34 +0000 | [diff] [blame] | 2506 | write_unlock_irqrestore(&cpufreq_driver_lock, flags); |
Sebastian Andrzej Siewior | 454d3a2 | 2015-07-22 17:59:11 +0200 | [diff] [blame] | 2507 | put_online_cpus(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | |
| 2509 | return 0; |
| 2510 | } |
| 2511 | EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2512 | |
Doug Anderson | 90de2a4 | 2014-12-23 22:09:48 -0800 | [diff] [blame] | 2513 | /* |
| 2514 | * Stop cpufreq at shutdown to make sure it isn't holding any locks |
| 2515 | * or mutexes when secondary CPUs are halted. |
| 2516 | */ |
| 2517 | static struct syscore_ops cpufreq_syscore_ops = { |
| 2518 | .shutdown = cpufreq_suspend, |
| 2519 | }; |
| 2520 | |
Viresh Kumar | c82bd44 | 2015-10-15 21:35:23 +0530 | [diff] [blame] | 2521 | struct kobject *cpufreq_global_kobject; |
| 2522 | EXPORT_SYMBOL(cpufreq_global_kobject); |
| 2523 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2524 | static int __init cpufreq_core_init(void) |
| 2525 | { |
Konrad Rzeszutek Wilk | a7b422c | 2012-03-13 19:18:39 -0400 | [diff] [blame] | 2526 | if (cpufreq_disabled()) |
| 2527 | return -ENODEV; |
| 2528 | |
Viresh Kumar | 8eec102 | 2015-10-15 21:35:22 +0530 | [diff] [blame] | 2529 | cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); |
Thomas Renninger | 8aa84ad | 2009-07-24 15:25:05 +0200 | [diff] [blame] | 2530 | BUG_ON(!cpufreq_global_kobject); |
| 2531 | |
Doug Anderson | 90de2a4 | 2014-12-23 22:09:48 -0800 | [diff] [blame] | 2532 | register_syscore_ops(&cpufreq_syscore_ops); |
| 2533 | |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2534 | return 0; |
| 2535 | } |
Venkatesh Pallipadi | 5a01f2e | 2007-02-05 16:12:44 -0800 | [diff] [blame] | 2536 | core_initcall(cpufreq_core_init); |