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