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