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