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