blob: d98ff688b2f110b8249c6be6c019a632f0c65d43 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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 Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080029#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080030#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053031#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020032#include <trace/events/power.h>
33
Viresh Kumarb4f06762015-01-27 14:06:08 +053034static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053035
36static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37{
38 return cpumask_empty(policy->cpus);
39}
40
Viresh Kumarf9637352015-05-12 12:20:11 +053041/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060042#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 Kumarf9637352015-05-12 12:20:11 +053045
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 Kumarb4f06762015-01-27 14:06:08 +053052 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
53
Viresh Kumarf7b27062015-01-27 14:06:09 +053054/* Iterate over governors */
55static LIST_HEAD(cpufreq_governor_list);
56#define for_each_governor(__governor) \
57 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**
Dave Jonescd878472006-08-11 17:59:28 -040060 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * level driver of CPUFreq support, and its spinlock. This lock
62 * also protects the cpufreq_cpu_data array.
63 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020064static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070065static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053066static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053067
Viresh Kumar2f0aea92014-03-04 11:00:26 +080068/* Flag to suspend/resume CPUFreq governors */
69static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053071static inline bool has_target(void)
72{
73 return cpufreq_driver->target_index || cpufreq_driver->target;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053077static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020078static int cpufreq_init_governor(struct cpufreq_policy *policy);
79static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010080static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020081static void cpufreq_stop_governor(struct cpufreq_policy *policy);
82static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
Dave Jones32ee8c32006-02-28 00:43:23 -050085 * Two notifier lists: the "policy" list is involved in the
86 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * "transition" list for kernel code that needs to handle
88 * changes to devices when the CPU clock speed changes.
89 * The mutex locks both lists.
90 */
Alan Sterne041c682006-03-27 01:16:30 -080091static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070092static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020094static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070095static int __init init_cpufreq_transition_notifier_list(void)
96{
97 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020098 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070099 return 0;
100}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800101pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400103static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200104static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400105{
106 return off;
107}
108void disable_cpufreq(void)
109{
110 off = 1;
111}
Dave Jones29464f22009-01-18 01:37:11 -0500112static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000114bool have_governor_per_policy(void)
115{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530116 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000117}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000118EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000119
Viresh Kumar944e9a02013-05-16 05:09:57 +0000120struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
121{
122 if (have_governor_per_policy())
123 return &policy->kobj;
124 else
125 return cpufreq_global_kobject;
126}
127EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
128
Viresh Kumar5a31d592015-07-10 01:43:27 +0200129struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
130{
131 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
132
133 return policy && !policy_is_inactive(policy) ?
134 policy->freq_table : NULL;
135}
136EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
137
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000138static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
139{
140 u64 idle_time;
141 u64 cur_wall_time;
142 u64 busy_time;
143
144 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
145
146 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
147 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
148 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
149 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
150 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
151 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
152
153 idle_time = cur_wall_time - busy_time;
154 if (wall)
155 *wall = cputime_to_usecs(cur_wall_time);
156
157 return cputime_to_usecs(idle_time);
158}
159
160u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
161{
162 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
163
164 if (idle_time == -1ULL)
165 return get_cpu_idle_time_jiffy(cpu, wall);
166 else if (!io_busy)
167 idle_time += get_cpu_iowait_time_us(cpu, wall);
168
169 return idle_time;
170}
171EXPORT_SYMBOL_GPL(get_cpu_idle_time);
172
Viresh Kumar70e9e772013-10-03 20:29:07 +0530173/*
174 * This is a generic cpufreq init() routine which can be used by cpufreq
175 * drivers of SMP systems. It will do following:
176 * - validate & show freq table passed
177 * - set policies transition latency
178 * - policy->cpus with all possible CPUs
179 */
180int cpufreq_generic_init(struct cpufreq_policy *policy,
181 struct cpufreq_frequency_table *table,
182 unsigned int transition_latency)
183{
184 int ret;
185
186 ret = cpufreq_table_validate_and_show(policy, table);
187 if (ret) {
188 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
189 return ret;
190 }
191
192 policy->cpuinfo.transition_latency = transition_latency;
193
194 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530195 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530196 * share the clock and voltage and clock.
197 */
198 cpumask_setall(policy->cpus);
199
200 return 0;
201}
202EXPORT_SYMBOL_GPL(cpufreq_generic_init);
203
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200204struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530205{
206 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
207
Viresh Kumar988bed02015-05-08 11:53:45 +0530208 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
209}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200210EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530211
212unsigned int cpufreq_generic_get(unsigned int cpu)
213{
214 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
215
Viresh Kumar652ed952014-01-09 20:38:43 +0530216 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700217 pr_err("%s: No %s associated to cpu: %d\n",
218 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530219 return 0;
220 }
221
222 return clk_get_rate(policy->clk) / 1000;
223}
224EXPORT_SYMBOL_GPL(cpufreq_generic_get);
225
Viresh Kumar50e9c852015-02-19 17:02:03 +0530226/**
227 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
228 *
229 * @cpu: cpu to find policy for.
230 *
231 * This returns policy for 'cpu', returns NULL if it doesn't exist.
232 * It also increments the kobject reference count to mark it busy and so would
233 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
234 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
235 * freed as that depends on the kobj count.
236 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530237 * Return: A valid policy on success, otherwise NULL on failure.
238 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530239struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530241 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 unsigned long flags;
243
Viresh Kumar1b947c92015-02-19 17:02:05 +0530244 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530245 return NULL;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000248 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Viresh Kumar6eed9402013-08-06 22:53:11 +0530250 if (cpufreq_driver) {
251 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530252 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530253 if (policy)
254 kobject_get(&policy->kobj);
255 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200256
Viresh Kumar6eed9402013-08-06 22:53:11 +0530257 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530259 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
262
Viresh Kumar50e9c852015-02-19 17:02:03 +0530263/**
264 * cpufreq_cpu_put: Decrements the usage count of a policy
265 *
266 * @policy: policy earlier returned by cpufreq_cpu_get().
267 *
268 * This decrements the kobject reference count incremented earlier by calling
269 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530270 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530271void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530273 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
279 *********************************************************************/
280
281/**
282 * adjust_jiffies - adjust the system "loops_per_jiffy"
283 *
284 * This function alters the system "loops_per_jiffy" for the clock
285 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500286 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * per-CPU loops_per_jiffy value wherever possible.
288 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800289static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530291#ifndef CONFIG_SMP
292 static unsigned long l_p_j_ref;
293 static unsigned int l_p_j_ref_freq;
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (ci->flags & CPUFREQ_CONST_LOOPS)
296 return;
297
298 if (!l_p_j_ref_freq) {
299 l_p_j_ref = loops_per_jiffy;
300 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700301 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
302 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530304 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530305 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
306 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700307 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
308 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530313static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530314 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
316 BUG_ON(irqs_disabled());
317
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000318 if (cpufreq_disabled())
319 return;
320
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200321 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200322 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700323 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500328 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800329 * which is not equal to what the cpufreq core thinks is
330 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200332 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800333 if ((policy) && (policy->cpu == freqs->cpu) &&
334 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700335 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
336 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800337 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700340 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800341 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
343 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 case CPUFREQ_POSTCHANGE:
346 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700347 pr_debug("FREQ: %lu - CPU: %lu\n",
348 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100349 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700350 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800351 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800352 if (likely(policy) && likely(policy->cpu == freqs->cpu))
353 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530357
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530358/**
359 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
360 * on frequency transition.
361 *
362 * This function calls the transition notifiers and the "adjust_jiffies"
363 * function. It is called twice on all CPU frequency changes that have
364 * external effects.
365 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530366static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530367 struct cpufreq_freqs *freqs, unsigned int state)
368{
369 for_each_cpu(freqs->cpu, policy->cpus)
370 __cpufreq_notify_transition(policy, freqs, state);
371}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530373/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530374static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530375 struct cpufreq_freqs *freqs, int transition_failed)
376{
377 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
378 if (!transition_failed)
379 return;
380
381 swap(freqs->old, freqs->new);
382 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
383 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
384}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530385
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530386void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
387 struct cpufreq_freqs *freqs)
388{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530389
390 /*
391 * Catch double invocations of _begin() which lead to self-deadlock.
392 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
393 * doesn't invoke _begin() on their behalf, and hence the chances of
394 * double invocations are very low. Moreover, there are scenarios
395 * where these checks can emit false-positive warnings in these
396 * drivers; so we avoid that by skipping them altogether.
397 */
398 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
399 && current == policy->transition_task);
400
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530401wait:
402 wait_event(policy->transition_wait, !policy->transition_ongoing);
403
404 spin_lock(&policy->transition_lock);
405
406 if (unlikely(policy->transition_ongoing)) {
407 spin_unlock(&policy->transition_lock);
408 goto wait;
409 }
410
411 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530412 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530413
414 spin_unlock(&policy->transition_lock);
415
416 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
417}
418EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
419
420void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
421 struct cpufreq_freqs *freqs, int transition_failed)
422{
423 if (unlikely(WARN_ON(!policy->transition_ongoing)))
424 return;
425
426 cpufreq_notify_post_transition(policy, freqs, transition_failed);
427
428 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530429 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530430
431 wake_up(&policy->transition_wait);
432}
433EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
434
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200435/*
436 * Fast frequency switching status count. Positive means "enabled", negative
437 * means "disabled" and 0 means "not decided yet".
438 */
439static int cpufreq_fast_switch_count;
440static DEFINE_MUTEX(cpufreq_fast_switch_lock);
441
442static void cpufreq_list_transition_notifiers(void)
443{
444 struct notifier_block *nb;
445
446 pr_info("Registered transition notifiers:\n");
447
448 mutex_lock(&cpufreq_transition_notifier_list.mutex);
449
450 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
451 pr_info("%pF\n", nb->notifier_call);
452
453 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
454}
455
456/**
457 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
458 * @policy: cpufreq policy to enable fast frequency switching for.
459 *
460 * Try to enable fast frequency switching for @policy.
461 *
462 * The attempt will fail if there is at least one transition notifier registered
463 * at this point, as fast frequency switching is quite fundamentally at odds
464 * with transition notifiers. Thus if successful, it will make registration of
465 * transition notifiers fail going forward.
466 */
467void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
468{
469 lockdep_assert_held(&policy->rwsem);
470
471 if (!policy->fast_switch_possible)
472 return;
473
474 mutex_lock(&cpufreq_fast_switch_lock);
475 if (cpufreq_fast_switch_count >= 0) {
476 cpufreq_fast_switch_count++;
477 policy->fast_switch_enabled = true;
478 } else {
479 pr_warn("CPU%u: Fast frequency switching not enabled\n",
480 policy->cpu);
481 cpufreq_list_transition_notifiers();
482 }
483 mutex_unlock(&cpufreq_fast_switch_lock);
484}
485EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
486
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200487/**
488 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
489 * @policy: cpufreq policy to disable fast frequency switching for.
490 */
491void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200492{
493 mutex_lock(&cpufreq_fast_switch_lock);
494 if (policy->fast_switch_enabled) {
495 policy->fast_switch_enabled = false;
496 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
497 cpufreq_fast_switch_count--;
498 }
499 mutex_unlock(&cpufreq_fast_switch_lock);
500}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200501EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/*********************************************************************
504 * SYSFS INTERFACE *
505 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530506static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100507 struct attribute *attr, char *buf)
508{
509 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
510}
511
512static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
513 const char *buf, size_t count)
514{
515 int ret, enable;
516
517 ret = sscanf(buf, "%d", &enable);
518 if (ret != 1 || enable < 0 || enable > 1)
519 return -EINVAL;
520
521 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700522 pr_err("%s: Cannot %s BOOST!\n",
523 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100524 return -EINVAL;
525 }
526
Joe Perchese837f9b2014-03-11 10:03:00 -0700527 pr_debug("%s: cpufreq BOOST %s\n",
528 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100529
530 return count;
531}
532define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530534static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700535{
536 struct cpufreq_governor *t;
537
Viresh Kumarf7b27062015-01-27 14:06:09 +0530538 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200539 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700540 return t;
541
542 return NULL;
543}
544
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545/**
546 * cpufreq_parse_governor - parse a governor string
547 */
Dave Jones905d77c2008-03-05 14:28:32 -0500548static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 struct cpufreq_governor **governor)
550{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700551 int err = -EINVAL;
552
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200553 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200554 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700556 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200557 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530558 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700560 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530562 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700564
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800565 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700566
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530567 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700568
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700569 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700570 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700571
Kees Cook1a8e1462011-05-04 08:38:56 -0700572 mutex_unlock(&cpufreq_governor_mutex);
573 ret = request_module("cpufreq_%s", str_governor);
574 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700575
Kees Cook1a8e1462011-05-04 08:38:56 -0700576 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530577 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700578 }
579
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700580 if (t != NULL) {
581 *governor = t;
582 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700584
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800585 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530591 * cpufreq_per_cpu_attr_read() / show_##file_name() -
592 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
594 * Write out information from cpufreq_driver->policy[cpu]; object must be
595 * "unsigned int".
596 */
597
Dave Jones32ee8c32006-02-28 00:43:23 -0500598#define show_one(file_name, object) \
599static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500600(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500601{ \
Dave Jones29464f22009-01-18 01:37:11 -0500602 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605show_one(cpuinfo_min_freq, cpuinfo.min_freq);
606show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100607show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608show_one(scaling_min_freq, min);
609show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700610
Viresh Kumar09347b22015-01-02 12:34:24 +0530611static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700612{
613 ssize_t ret;
614
615 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
616 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
617 else
618 ret = sprintf(buf, "%u\n", policy->cur);
619 return ret;
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Viresh Kumar037ce832013-10-02 14:13:16 +0530622static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530623 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/**
626 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
627 */
628#define store_one(file_name, object) \
629static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500630(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800632 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct cpufreq_policy new_policy; \
634 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530635 memcpy(&new_policy, policy, sizeof(*policy)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 \
Dave Jones29464f22009-01-18 01:37:11 -0500637 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 if (ret != 1) \
639 return -EINVAL; \
640 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800641 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530642 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800643 if (!ret) \
644 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 \
646 return ret ? ret : count; \
647}
648
Dave Jones29464f22009-01-18 01:37:11 -0500649store_one(scaling_min_freq, min);
650store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652/**
653 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
654 */
Dave Jones905d77c2008-03-05 14:28:32 -0500655static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
656 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530658 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (!cur_freq)
660 return sprintf(buf, "<unknown>");
661 return sprintf(buf, "%u\n", cur_freq);
662}
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/**
665 * show_scaling_governor - show the current policy for the specified CPU
666 */
Dave Jones905d77c2008-03-05 14:28:32 -0500667static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Dave Jones29464f22009-01-18 01:37:11 -0500669 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 return sprintf(buf, "powersave\n");
671 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
672 return sprintf(buf, "performance\n");
673 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200674 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500675 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EINVAL;
677}
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679/**
680 * store_scaling_governor - store policy for the specified CPU
681 */
Dave Jones905d77c2008-03-05 14:28:32 -0500682static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
683 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530685 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 char str_governor[16];
687 struct cpufreq_policy new_policy;
688
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530689 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dave Jones29464f22009-01-18 01:37:11 -0500691 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 if (ret != 1)
693 return -EINVAL;
694
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530695 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
696 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -EINVAL;
698
Viresh Kumar037ce832013-10-02 14:13:16 +0530699 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530700 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703/**
704 * show_scaling_driver - show the cpufreq driver currently loaded
705 */
Dave Jones905d77c2008-03-05 14:28:32 -0500706static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200708 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711/**
712 * show_scaling_available_governors - show the available CPUfreq governors
713 */
Dave Jones905d77c2008-03-05 14:28:32 -0500714static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
715 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 ssize_t i = 0;
718 struct cpufreq_governor *t;
719
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530720 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 i += sprintf(buf, "performance powersave");
722 goto out;
723 }
724
Viresh Kumarf7b27062015-01-27 14:06:09 +0530725 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500726 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
727 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200729 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500731out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 i += sprintf(&buf[i], "\n");
733 return i;
734}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700735
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800736ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 ssize_t i = 0;
739 unsigned int cpu;
740
Rusty Russell835481d2009-01-04 05:18:06 -0800741 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (i)
743 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
744 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
745 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 i += sprintf(&buf[i], "\n");
749 return i;
750}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800751EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700753/**
754 * show_related_cpus - show the CPUs affected by each transition even if
755 * hw coordination is in use
756 */
757static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
758{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800759 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700760}
761
762/**
763 * show_affected_cpus - show the CPUs affected by each transition
764 */
765static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
766{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800767 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700768}
769
Venki Pallipadi9e769882007-10-26 10:18:21 -0700770static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500771 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700772{
773 unsigned int freq = 0;
774 unsigned int ret;
775
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700776 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700777 return -EINVAL;
778
779 ret = sscanf(buf, "%u", &freq);
780 if (ret != 1)
781 return -EINVAL;
782
783 policy->governor->store_setspeed(policy, freq);
784
785 return count;
786}
787
788static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
789{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700790 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700791 return sprintf(buf, "<unsupported>\n");
792
793 return policy->governor->show_setspeed(policy, buf);
794}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Thomas Renningere2f74f32009-11-19 12:31:01 +0100796/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200797 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100798 */
799static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
800{
801 unsigned int limit;
802 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200803 if (cpufreq_driver->bios_limit) {
804 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100805 if (!ret)
806 return sprintf(buf, "%u\n", limit);
807 }
808 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
809}
810
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200811cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
812cpufreq_freq_attr_ro(cpuinfo_min_freq);
813cpufreq_freq_attr_ro(cpuinfo_max_freq);
814cpufreq_freq_attr_ro(cpuinfo_transition_latency);
815cpufreq_freq_attr_ro(scaling_available_governors);
816cpufreq_freq_attr_ro(scaling_driver);
817cpufreq_freq_attr_ro(scaling_cur_freq);
818cpufreq_freq_attr_ro(bios_limit);
819cpufreq_freq_attr_ro(related_cpus);
820cpufreq_freq_attr_ro(affected_cpus);
821cpufreq_freq_attr_rw(scaling_min_freq);
822cpufreq_freq_attr_rw(scaling_max_freq);
823cpufreq_freq_attr_rw(scaling_governor);
824cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Dave Jones905d77c2008-03-05 14:28:32 -0500826static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 &cpuinfo_min_freq.attr,
828 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100829 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 &scaling_min_freq.attr,
831 &scaling_max_freq.attr,
832 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700833 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 &scaling_governor.attr,
835 &scaling_driver.attr,
836 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700837 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 NULL
839};
840
Dave Jones29464f22009-01-18 01:37:11 -0500841#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
842#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Dave Jones29464f22009-01-18 01:37:11 -0500844static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Dave Jones905d77c2008-03-05 14:28:32 -0500846 struct cpufreq_policy *policy = to_policy(kobj);
847 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530848 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530849
viresh kumarad7722d2013-10-18 19:10:15 +0530850 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100851 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530852 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return ret;
855}
856
Dave Jones905d77c2008-03-05 14:28:32 -0500857static ssize_t store(struct kobject *kobj, struct attribute *attr,
858 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Dave Jones905d77c2008-03-05 14:28:32 -0500860 struct cpufreq_policy *policy = to_policy(kobj);
861 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500862 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530863
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530864 get_online_cpus();
865
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100866 if (cpu_online(policy->cpu)) {
867 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530868 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100869 up_write(&policy->rwsem);
870 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530871
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530872 put_online_cpus();
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return ret;
875}
876
Dave Jones905d77c2008-03-05 14:28:32 -0500877static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Dave Jones905d77c2008-03-05 14:28:32 -0500879 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200880 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 complete(&policy->kobj_unregister);
882}
883
Emese Revfy52cf25d2010-01-19 02:58:23 +0100884static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 .show = show,
886 .store = store,
887};
888
889static struct kobj_type ktype_cpufreq = {
890 .sysfs_ops = &sysfs_ops,
891 .default_attrs = default_attrs,
892 .release = cpufreq_sysfs_release,
893};
894
Viresh Kumar87549142015-06-10 02:13:21 +0200895static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
896{
897 struct device *cpu_dev;
898
899 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
900
901 if (!policy)
902 return 0;
903
904 cpu_dev = get_cpu_device(cpu);
905 if (WARN_ON(!cpu_dev))
906 return 0;
907
908 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
909}
910
911static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
912{
913 struct device *cpu_dev;
914
915 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
916
917 cpu_dev = get_cpu_device(cpu);
918 if (WARN_ON(!cpu_dev))
919 return;
920
921 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
922}
923
924/* Add/remove symlinks for all related CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200925static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400926{
927 unsigned int j;
928 int ret = 0;
929
Viresh Kumar87549142015-06-10 02:13:21 +0200930 /* Some related CPUs might not be present (physically hotplugged) */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +0200931 for_each_cpu(j, policy->real_cpus) {
Viresh Kumar87549142015-06-10 02:13:21 +0200932 ret = add_cpu_dev_symlink(policy, j);
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200933 if (ret)
934 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400935 }
Viresh Kumar87549142015-06-10 02:13:21 +0200936
Dave Jones19d6f7e2009-07-08 17:35:39 -0400937 return ret;
938}
939
Viresh Kumar87549142015-06-10 02:13:21 +0200940static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
941{
942 unsigned int j;
943
944 /* Some related CPUs might not be present (physically hotplugged) */
Viresh Kumar96bdda62015-10-15 21:35:24 +0530945 for_each_cpu(j, policy->real_cpus)
Viresh Kumar87549142015-06-10 02:13:21 +0200946 remove_cpu_dev_symlink(policy, j);
Viresh Kumar87549142015-06-10 02:13:21 +0200947}
948
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200949static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400950{
951 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400952 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400953
Dave Jones909a6942009-07-08 18:05:42 -0400954 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200955 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530956 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400957 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
958 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100959 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400960 drv_attr++;
961 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200962 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400963 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
964 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100965 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400966 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700967
968 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
969 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100970 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700971
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200972 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100973 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
974 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100975 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100976 }
Dave Jones909a6942009-07-08 18:05:42 -0400977
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100978 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530979}
980
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100981__weak struct cpufreq_governor *cpufreq_default_governor(void)
982{
983 return NULL;
984}
985
Viresh Kumar7f0fa402015-07-08 15:12:16 +0530986static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530987{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800988 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530989 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530990
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530991 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000992
viresh kumar6e2c89d2014-03-04 11:43:59 +0800993 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +0530994 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100995 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +0800996 pr_debug("Restoring governor %s for cpu %d\n",
997 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100998 } else {
999 gov = cpufreq_default_governor();
1000 if (!gov)
1001 return -ENODATA;
1002 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001003
1004 new_policy.governor = gov;
1005
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001006 /* Use the default policy if there is no last_policy. */
1007 if (cpufreq_driver->setpolicy) {
1008 if (policy->last_policy)
1009 new_policy.policy = policy->last_policy;
1010 else
1011 cpufreq_parse_governor(gov->name, &new_policy.policy,
1012 NULL);
1013 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001014 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301015 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001016}
1017
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001018static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001019{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301020 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001021
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301022 /* Has this CPU been taken care of already? */
1023 if (cpumask_test_cpu(cpu, policy->cpus))
1024 return 0;
1025
Viresh Kumar49f18562016-02-11 17:31:12 +05301026 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001027 if (has_target())
1028 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001029
Viresh Kumarfcf80582013-01-29 14:39:08 +00001030 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301031
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301032 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001033 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301034 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301035 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001036 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301037 up_write(&policy->rwsem);
1038 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001039}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301041static void handle_update(struct work_struct *work)
1042{
1043 struct cpufreq_policy *policy =
1044 container_of(work, struct cpufreq_policy, update);
1045 unsigned int cpu = policy->cpu;
1046 pr_debug("handle_update for cpu %u called\n", cpu);
1047 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001050static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301051{
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001052 struct device *dev = get_cpu_device(cpu);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301053 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301054 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301055
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001056 if (WARN_ON(!dev))
1057 return NULL;
1058
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301059 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1060 if (!policy)
1061 return NULL;
1062
1063 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1064 goto err_free_policy;
1065
1066 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1067 goto err_free_cpumask;
1068
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001069 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1070 goto err_free_rcpumask;
1071
Viresh Kumaredd4a892016-03-03 14:51:33 +05301072 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1073 cpufreq_global_kobject, "policy%u", cpu);
1074 if (ret) {
1075 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1076 goto err_free_real_cpus;
1077 }
1078
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301079 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301080 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301081 spin_lock_init(&policy->transition_lock);
1082 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301083 init_completion(&policy->kobj_unregister);
1084 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301085
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001086 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301087 return policy;
1088
Viresh Kumaredd4a892016-03-03 14:51:33 +05301089err_free_real_cpus:
1090 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301091err_free_rcpumask:
1092 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301093err_free_cpumask:
1094 free_cpumask_var(policy->cpus);
1095err_free_policy:
1096 kfree(policy);
1097
1098 return NULL;
1099}
1100
Viresh Kumar2fc33842015-06-08 18:25:29 +05301101static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301102{
1103 struct kobject *kobj;
1104 struct completion *cmp;
1105
Viresh Kumar2fc33842015-06-08 18:25:29 +05301106 if (notify)
1107 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1108 CPUFREQ_REMOVE_POLICY, policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301109
Viresh Kumar87549142015-06-10 02:13:21 +02001110 down_write(&policy->rwsem);
1111 cpufreq_remove_dev_symlink(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301112 kobj = &policy->kobj;
1113 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001114 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301115 kobject_put(kobj);
1116
1117 /*
1118 * We need to make sure that the underlying kobj is
1119 * actually not referenced anymore by anybody before we
1120 * proceed with unloading.
1121 */
1122 pr_debug("waiting for dropping of refcount\n");
1123 wait_for_completion(cmp);
1124 pr_debug("wait complete\n");
1125}
1126
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301127static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301128{
Viresh Kumar988bed02015-05-08 11:53:45 +05301129 unsigned long flags;
1130 int cpu;
1131
1132 /* Remove policy from list */
1133 write_lock_irqsave(&cpufreq_driver_lock, flags);
1134 list_del(&policy->policy_list);
1135
1136 for_each_cpu(cpu, policy->related_cpus)
1137 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1138 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1139
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301140 cpufreq_policy_put_kobj(policy, notify);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001141 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301142 free_cpumask_var(policy->related_cpus);
1143 free_cpumask_var(policy->cpus);
1144 kfree(policy);
1145}
1146
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001147static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301149 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001150 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001152 unsigned int j;
1153 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001154
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001155 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301156
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301157 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301158 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001159 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301160 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001161 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001162 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001164 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001165 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001166 down_write(&policy->rwsem);
1167 policy->cpu = cpu;
1168 policy->governor = NULL;
1169 up_write(&policy->rwsem);
1170 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001171 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001172 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001173 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001174 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001175 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301176
Rusty Russell835481d2009-01-04 05:18:06 -08001177 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* call driver. From then on the cpufreq must be able
1180 * to accept all calls to ->verify and ->setpolicy for this CPU
1181 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001182 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001184 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301185 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001187
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001188 down_write(&policy->rwsem);
1189
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001190 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001191 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301192 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001193 /* Remember CPUs present at the policy creation time. */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001194 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001195 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001196
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001197 /*
1198 * affected cpus must always be the one, which are online. We aren't
1199 * managing offline cpus here.
1200 */
1201 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1202
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001203 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001204 policy->user_policy.min = policy->min;
1205 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001206
Viresh Kumar988bed02015-05-08 11:53:45 +05301207 write_lock_irqsave(&cpufreq_driver_lock, flags);
1208 for_each_cpu(j, policy->related_cpus)
1209 per_cpu(cpufreq_cpu_data, j) = policy;
1210 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1211 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301212
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001213 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301214 policy->cur = cpufreq_driver->get(policy->cpu);
1215 if (!policy->cur) {
1216 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301217 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301218 }
1219 }
1220
Viresh Kumard3916692013-12-03 11:20:46 +05301221 /*
1222 * Sometimes boot loaders set CPU frequency to a value outside of
1223 * frequency table present with cpufreq core. In such cases CPU might be
1224 * unstable if it has to run on that frequency for long duration of time
1225 * and so its better to set it to a frequency which is specified in
1226 * freq-table. This also makes cpufreq stats inconsistent as
1227 * cpufreq-stats would fail to register because current frequency of CPU
1228 * isn't found in freq-table.
1229 *
1230 * Because we don't want this change to effect boot process badly, we go
1231 * for the next freq which is >= policy->cur ('cur' must be set by now,
1232 * otherwise we will end up setting freq to lowest of the table as 'cur'
1233 * is initialized to zero).
1234 *
1235 * We are passing target-freq as "policy->cur - 1" otherwise
1236 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1237 * equal to target-freq.
1238 */
1239 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1240 && has_target()) {
1241 /* Are we running at unknown frequency ? */
1242 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1243 if (ret == -EINVAL) {
1244 /* Warn user and fix it */
1245 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1246 __func__, policy->cpu, policy->cur);
1247 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1248 CPUFREQ_RELATION_L);
1249
1250 /*
1251 * Reaching here after boot in a few seconds may not
1252 * mean that system will remain stable at "unknown"
1253 * frequency for longer duration. Hence, a BUG_ON().
1254 */
1255 BUG_ON(ret);
1256 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1257 __func__, policy->cpu, policy->cur);
1258 }
1259 }
1260
Thomas Renningera1531ac2008-07-29 22:32:58 -07001261 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1262 CPUFREQ_START, policy);
1263
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001264 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001265 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301266 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301267 goto out_exit_policy;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301268 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1269 CPUFREQ_CREATE_POLICY, policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001270
Viresh Kumar988bed02015-05-08 11:53:45 +05301271 write_lock_irqsave(&cpufreq_driver_lock, flags);
1272 list_add(&policy->policy_list, &cpufreq_policy_list);
1273 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1274 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301275
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301276 ret = cpufreq_init_policy(policy);
1277 if (ret) {
1278 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1279 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001280 /* cpufreq_policy_free() will notify based on this */
1281 new_policy = false;
1282 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301283 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301284
Viresh Kumar4e97b632014-03-04 11:44:01 +08001285 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301286
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001287 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301288
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301289 /* Callback for handling stuff after policy is ready */
1290 if (cpufreq_driver->ready)
1291 cpufreq_driver->ready(policy);
1292
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001293 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return 0;
1296
Viresh Kumar8101f992015-07-08 15:12:15 +05301297out_exit_policy:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001298 up_write(&policy->rwsem);
1299
Viresh Kumarda60ce92013-10-03 20:28:30 +05301300 if (cpufreq_driver->exit)
1301 cpufreq_driver->exit(policy);
Viresh Kumar8101f992015-07-08 15:12:15 +05301302out_free_policy:
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001303 cpufreq_policy_free(policy, !new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 return ret;
1305}
1306
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001307/**
1308 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1309 * @dev: CPU device.
1310 * @sif: Subsystem interface structure pointer (not used)
1311 */
1312static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1313{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001314 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001315 unsigned cpu = dev->id;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001316
1317 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1318
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001319 if (cpu_online(cpu))
1320 return cpufreq_online(cpu);
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001321
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001322 /*
1323 * A hotplug notifier will follow and we will handle it as CPU online
1324 * then. For now, just create the sysfs link, unless there is no policy
1325 * or the link is already present.
1326 */
1327 policy = per_cpu(cpufreq_cpu_data, cpu);
1328 if (!policy || cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001331 return add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Viresh Kumar69cee712016-02-11 17:31:11 +05301334static void cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301336 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301337 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001339 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Viresh Kumar988bed02015-05-08 11:53:45 +05301341 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301342 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001343 pr_debug("%s: No cpu_data found\n", __func__);
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Viresh Kumar49f18562016-02-11 17:31:12 +05301347 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001348 if (has_target())
1349 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001350
Viresh Kumar9591bec2015-06-10 02:20:23 +02001351 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301352
Viresh Kumar9591bec2015-06-10 02:20:23 +02001353 if (policy_is_inactive(policy)) {
1354 if (has_target())
1355 strncpy(policy->last_governor, policy->governor->name,
1356 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001357 else
1358 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001359 } else if (cpu == policy->cpu) {
1360 /* Nominate new CPU */
1361 policy->cpu = cpumask_any(policy->cpus);
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Viresh Kumar9591bec2015-06-10 02:20:23 +02001364 /* Start governor again for active policy */
1365 if (!policy_is_inactive(policy)) {
1366 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001367 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001368 if (ret)
1369 pr_err("%s: Failed to start governor\n", __func__);
1370 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301371
Viresh Kumar49f18562016-02-11 17:31:12 +05301372 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301373 }
1374
1375 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001376 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001378 if (has_target())
1379 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001380
Viresh Kumar87549142015-06-10 02:13:21 +02001381 /*
1382 * Perform the ->exit() even during light-weight tear-down,
1383 * since this is a core component, and is essential for the
1384 * subsequent light-weight ->init() to succeed.
1385 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001386 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001387 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001388 policy->freq_table = NULL;
1389 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301390
1391unlock:
1392 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
1394
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301395/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301396 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301397 *
1398 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301399 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301400static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001401{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001402 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001403 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001404
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001405 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001406 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001407
Viresh Kumar69cee712016-02-11 17:31:11 +05301408 if (cpu_online(cpu))
1409 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001410
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001411 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar96bdda62015-10-15 21:35:24 +05301412 remove_cpu_dev_symlink(policy, cpu);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301413
1414 if (cpumask_empty(policy->real_cpus))
1415 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001416}
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301419 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1420 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301421 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * @new_freq: CPU frequency the CPU actually runs at
1423 *
Dave Jones29464f22009-01-18 01:37:11 -05001424 * We adjust to current frequency first, and need to clean up later.
1425 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301427static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301428 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
1430 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301431
Joe Perchese837f9b2014-03-11 10:03:00 -07001432 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301433 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301435 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301437
Viresh Kumar8fec0512014-03-24 13:35:45 +05301438 cpufreq_freq_transition_begin(policy, &freqs);
1439 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
Dave Jones32ee8c32006-02-28 00:43:23 -05001442/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301443 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001444 * @cpu: CPU number
1445 *
1446 * This is the last known freq, without actually getting it from the driver.
1447 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1448 */
1449unsigned int cpufreq_quick_get(unsigned int cpu)
1450{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001451 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301452 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001453 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001454
Richard Cochranc75361c2016-03-11 09:43:07 +01001455 read_lock_irqsave(&cpufreq_driver_lock, flags);
1456
1457 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1458 ret_freq = cpufreq_driver->get(cpu);
1459 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1460 return ret_freq;
1461 }
1462
1463 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001464
1465 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001466 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301467 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001468 cpufreq_cpu_put(policy);
1469 }
1470
Dave Jones4d34a672008-02-07 16:33:49 -05001471 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001472}
1473EXPORT_SYMBOL(cpufreq_quick_get);
1474
Jesse Barnes3d737102011-06-28 10:59:12 -07001475/**
1476 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1477 * @cpu: CPU number
1478 *
1479 * Just return the max possible frequency for a given CPU.
1480 */
1481unsigned int cpufreq_quick_get_max(unsigned int cpu)
1482{
1483 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1484 unsigned int ret_freq = 0;
1485
1486 if (policy) {
1487 ret_freq = policy->max;
1488 cpufreq_cpu_put(policy);
1489 }
1490
1491 return ret_freq;
1492}
1493EXPORT_SYMBOL(cpufreq_quick_get_max);
1494
Viresh Kumard92d50a2015-01-02 12:34:29 +05301495static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301497 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001499 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001500 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Viresh Kumard92d50a2015-01-02 12:34:29 +05301502 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001504 /*
1505 * Updating inactive policies is invalid, so avoid doing that. Also
1506 * if fast frequency switching is used with the given policy, the check
1507 * against policy->cur is pointless, so skip it in that case too.
1508 */
1509 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001510 return ret_freq;
1511
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301512 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001513 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301514 /* verify no discrepancy between actual and
1515 saved value exists */
1516 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301517 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 schedule_work(&policy->update);
1519 }
1520 }
1521
Dave Jones4d34a672008-02-07 16:33:49 -05001522 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001523}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001525/**
1526 * cpufreq_get - get the current CPU frequency (in kHz)
1527 * @cpu: CPU number
1528 *
1529 * Get the CPU current (static) CPU frequency
1530 */
1531unsigned int cpufreq_get(unsigned int cpu)
1532{
Aaron Plattner999976e2014-03-04 12:42:15 -08001533 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001534 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001535
Aaron Plattner999976e2014-03-04 12:42:15 -08001536 if (policy) {
1537 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301538 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001539 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301540
Aaron Plattner999976e2014-03-04 12:42:15 -08001541 cpufreq_cpu_put(policy);
1542 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301543
Dave Jones4d34a672008-02-07 16:33:49 -05001544 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546EXPORT_SYMBOL(cpufreq_get);
1547
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001548static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1549{
1550 unsigned int new_freq;
1551
Rafael J. Wysockic9d9c922016-04-10 05:59:33 +02001552 if (cpufreq_suspended)
1553 return 0;
1554
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001555 new_freq = cpufreq_driver->get(policy->cpu);
1556 if (!new_freq)
1557 return 0;
1558
1559 if (!policy->cur) {
1560 pr_debug("cpufreq: Driver did not initialize current freq\n");
1561 policy->cur = new_freq;
1562 } else if (policy->cur != new_freq && has_target()) {
1563 cpufreq_out_of_sync(policy, new_freq);
1564 }
1565
1566 return new_freq;
1567}
1568
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001569static struct subsys_interface cpufreq_interface = {
1570 .name = "cpufreq",
1571 .subsys = &cpu_subsys,
1572 .add_dev = cpufreq_add_dev,
1573 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001574};
1575
Viresh Kumare28867e2014-03-04 11:00:27 +08001576/*
1577 * In case platform wants some specific frequency to be configured
1578 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001579 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001580int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001581{
Viresh Kumare28867e2014-03-04 11:00:27 +08001582 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001583
Viresh Kumare28867e2014-03-04 11:00:27 +08001584 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001585 pr_debug("%s: suspend_freq not defined\n", __func__);
1586 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001587 }
1588
Viresh Kumare28867e2014-03-04 11:00:27 +08001589 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1590 policy->suspend_freq);
1591
1592 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1593 CPUFREQ_RELATION_H);
1594 if (ret)
1595 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1596 __func__, policy->suspend_freq, ret);
1597
Dave Jonesc9060492008-02-07 16:32:18 -05001598 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001599}
Viresh Kumare28867e2014-03-04 11:00:27 +08001600EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001601
1602/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001603 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001605 * Called during system wide Suspend/Hibernate cycles for suspending governors
1606 * as some platforms can't change frequency after this point in suspend cycle.
1607 * Because some of the devices (like: i2c, regulators, etc) they use for
1608 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001610void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301612 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001614 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001615 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001617 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301618 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001620 pr_debug("%s: Suspending Governors\n", __func__);
1621
Viresh Kumarf9637352015-05-12 12:20:11 +05301622 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001623 if (has_target()) {
1624 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001625 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001626 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001627 }
1628
1629 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001630 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1631 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301633
1634suspend:
1635 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001639 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001641 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1642 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001644void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001649 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return;
1651
Lan Tianyu8e304442014-09-18 15:03:07 +08001652 cpufreq_suspended = false;
1653
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001654 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001655 return;
1656
1657 pr_debug("%s: Resuming Governors\n", __func__);
1658
Viresh Kumarf9637352015-05-12 12:20:11 +05301659 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301660 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301661 pr_err("%s: Failed to resume driver: %p\n", __func__,
1662 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001663 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301664 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001665 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301666 up_write(&policy->rwsem);
1667
1668 if (ret)
1669 pr_err("%s: Failed to start governor for policy: %p\n",
1670 __func__, policy);
1671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Borislav Petkov9d950462013-01-20 10:24:28 +00001675/**
1676 * cpufreq_get_current_driver - return current driver's name
1677 *
1678 * Return the name string of the currently loaded cpufreq driver
1679 * or NULL, if none.
1680 */
1681const char *cpufreq_get_current_driver(void)
1682{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001683 if (cpufreq_driver)
1684 return cpufreq_driver->name;
1685
1686 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001687}
1688EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001690/**
1691 * cpufreq_get_driver_data - return current driver data
1692 *
1693 * Return the private data of the currently loaded cpufreq
1694 * driver, or NULL if no cpufreq driver is loaded.
1695 */
1696void *cpufreq_get_driver_data(void)
1697{
1698 if (cpufreq_driver)
1699 return cpufreq_driver->driver_data;
1700
1701 return NULL;
1702}
1703EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705/*********************************************************************
1706 * NOTIFIER LISTS INTERFACE *
1707 *********************************************************************/
1708
1709/**
1710 * cpufreq_register_notifier - register a driver with cpufreq
1711 * @nb: notifier function to register
1712 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1713 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001714 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 * are notified about clock rate changes (once before and once after
1716 * the transition), or a list of drivers that are notified about
1717 * changes in cpufreq policy.
1718 *
1719 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001720 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
1722int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1723{
1724 int ret;
1725
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001726 if (cpufreq_disabled())
1727 return -EINVAL;
1728
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001729 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 switch (list) {
1732 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001733 mutex_lock(&cpufreq_fast_switch_lock);
1734
1735 if (cpufreq_fast_switch_count > 0) {
1736 mutex_unlock(&cpufreq_fast_switch_lock);
1737 return -EBUSY;
1738 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001739 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001740 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001741 if (!ret)
1742 cpufreq_fast_switch_count--;
1743
1744 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 break;
1746 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001747 ret = blocking_notifier_chain_register(
1748 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 break;
1750 default:
1751 ret = -EINVAL;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 return ret;
1755}
1756EXPORT_SYMBOL(cpufreq_register_notifier);
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/**
1759 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1760 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301761 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *
1763 * Remove a driver from the CPU frequency notifier list.
1764 *
1765 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001766 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
1768int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1769{
1770 int ret;
1771
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001772 if (cpufreq_disabled())
1773 return -EINVAL;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 switch (list) {
1776 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001777 mutex_lock(&cpufreq_fast_switch_lock);
1778
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001779 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001780 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001781 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1782 cpufreq_fast_switch_count++;
1783
1784 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 break;
1786 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001787 ret = blocking_notifier_chain_unregister(
1788 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 break;
1790 default:
1791 ret = -EINVAL;
1792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 return ret;
1795}
1796EXPORT_SYMBOL(cpufreq_unregister_notifier);
1797
1798
1799/*********************************************************************
1800 * GOVERNORS *
1801 *********************************************************************/
1802
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001803/**
1804 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1805 * @policy: cpufreq policy to switch the frequency for.
1806 * @target_freq: New frequency to set (may be approximate).
1807 *
1808 * Carry out a fast frequency switch without sleeping.
1809 *
1810 * The driver's ->fast_switch() callback invoked by this function must be
1811 * suitable for being called from within RCU-sched read-side critical sections
1812 * and it is expected to select the minimum available frequency greater than or
1813 * equal to @target_freq (CPUFREQ_RELATION_L).
1814 *
1815 * This function must not be called if policy->fast_switch_enabled is unset.
1816 *
1817 * Governors calling this function must guarantee that it will never be invoked
1818 * twice in parallel for the same policy and that it will never be called in
1819 * parallel with either ->target() or ->target_index() for the same policy.
1820 *
1821 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1822 * callback to indicate an error condition, the hardware configuration must be
1823 * preserved.
1824 */
1825unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1826 unsigned int target_freq)
1827{
1828 clamp_val(target_freq, policy->min, policy->max);
1829
1830 return cpufreq_driver->fast_switch(policy, target_freq);
1831}
1832EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1833
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301834/* Must set freqs->new to intermediate frequency */
1835static int __target_intermediate(struct cpufreq_policy *policy,
1836 struct cpufreq_freqs *freqs, int index)
1837{
1838 int ret;
1839
1840 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1841
1842 /* We don't need to switch to intermediate freq */
1843 if (!freqs->new)
1844 return 0;
1845
1846 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1847 __func__, policy->cpu, freqs->old, freqs->new);
1848
1849 cpufreq_freq_transition_begin(policy, freqs);
1850 ret = cpufreq_driver->target_intermediate(policy, index);
1851 cpufreq_freq_transition_end(policy, freqs, ret);
1852
1853 if (ret)
1854 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1855 __func__, ret);
1856
1857 return ret;
1858}
1859
Viresh Kumar8d657752014-05-21 14:29:29 +05301860static int __target_index(struct cpufreq_policy *policy,
1861 struct cpufreq_frequency_table *freq_table, int index)
1862{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301863 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1864 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301865 int retval = -EINVAL;
1866 bool notify;
1867
1868 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301869 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301870 /* Handle switching to intermediate frequency */
1871 if (cpufreq_driver->get_intermediate) {
1872 retval = __target_intermediate(policy, &freqs, index);
1873 if (retval)
1874 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301875
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301876 intermediate_freq = freqs.new;
1877 /* Set old freq to intermediate */
1878 if (intermediate_freq)
1879 freqs.old = freqs.new;
1880 }
1881
1882 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301883 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1884 __func__, policy->cpu, freqs.old, freqs.new);
1885
1886 cpufreq_freq_transition_begin(policy, &freqs);
1887 }
1888
1889 retval = cpufreq_driver->target_index(policy, index);
1890 if (retval)
1891 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1892 retval);
1893
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301894 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301895 cpufreq_freq_transition_end(policy, &freqs, retval);
1896
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301897 /*
1898 * Failed after setting to intermediate freq? Driver should have
1899 * reverted back to initial frequency and so should we. Check
1900 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301901 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301902 */
1903 if (unlikely(retval && intermediate_freq)) {
1904 freqs.old = intermediate_freq;
1905 freqs.new = policy->restore_freq;
1906 cpufreq_freq_transition_begin(policy, &freqs);
1907 cpufreq_freq_transition_end(policy, &freqs, 0);
1908 }
1909 }
1910
Viresh Kumar8d657752014-05-21 14:29:29 +05301911 return retval;
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914int __cpufreq_driver_target(struct cpufreq_policy *policy,
1915 unsigned int target_freq,
1916 unsigned int relation)
1917{
Viresh Kumar72499242012-10-31 01:28:21 +01001918 unsigned int old_target_freq = target_freq;
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001919 struct cpufreq_frequency_table *freq_table;
1920 int index, retval;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001921
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001922 if (cpufreq_disabled())
1923 return -ENODEV;
1924
Viresh Kumar72499242012-10-31 01:28:21 +01001925 /* Make sure that target_freq is within supported range */
1926 if (target_freq > policy->max)
1927 target_freq = policy->max;
1928 if (target_freq < policy->min)
1929 target_freq = policy->min;
1930
1931 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001932 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001933
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301934 /*
1935 * This might look like a redundant call as we are checking it again
1936 * after finding index. But it is left intentionally for cases where
1937 * exactly same freq is called again and so we can save on few function
1938 * calls.
1939 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001940 if (target_freq == policy->cur)
1941 return 0;
1942
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301943 /* Save last value to restore later on errors */
1944 policy->restore_freq = policy->cur;
1945
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001946 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001947 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001948
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001949 if (!cpufreq_driver->target_index)
1950 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301951
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001952 freq_table = cpufreq_frequency_get_table(policy->cpu);
1953 if (unlikely(!freq_table)) {
1954 pr_err("%s: Unable to find freq_table\n", __func__);
1955 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301956 }
1957
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001958 retval = cpufreq_frequency_table_target(policy, freq_table, target_freq,
1959 relation, &index);
1960 if (unlikely(retval)) {
1961 pr_err("%s: Unable to find matching freq\n", __func__);
1962 return retval;
1963 }
1964
1965 if (freq_table[index].frequency == policy->cur)
1966 return 0;
1967
1968 return __target_index(policy, freq_table, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972int cpufreq_driver_target(struct cpufreq_policy *policy,
1973 unsigned int target_freq,
1974 unsigned int relation)
1975{
Julia Lawallf1829e42008-07-25 22:44:53 +02001976 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
viresh kumarad7722d2013-10-18 19:10:15 +05301978 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 ret = __cpufreq_driver_target(policy, target_freq, relation);
1981
viresh kumarad7722d2013-10-18 19:10:15 +05301982 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 return ret;
1985}
1986EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1987
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001988__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1989{
1990 return NULL;
1991}
1992
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02001993static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994{
Dave Jonescc993ca2005-07-28 09:43:56 -07001995 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001996
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001997 /* Don't start any governor operations if we are entering suspend */
1998 if (cpufreq_suspended)
1999 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09002000 /*
2001 * Governor might not be initiated here if ACPI _PPC changed
2002 * notification happened, so check it.
2003 */
2004 if (!policy->governor)
2005 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002006
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002007 if (policy->governor->max_transition_latency &&
2008 policy->cpuinfo.transition_latency >
2009 policy->governor->max_transition_latency) {
2010 struct cpufreq_governor *gov = cpufreq_fallback_governor();
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002011
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002012 if (gov) {
2013 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2014 policy->governor->name, gov->name);
2015 policy->governor = gov;
2016 } else {
Viresh Kumarfe492f32013-08-06 22:53:10 +05302017 return -EINVAL;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002018 }
Rafael J. Wysocki9e8c0a82016-05-14 01:00:28 +02002019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002021 if (!try_module_get(policy->governor->owner))
2022 return -EINVAL;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002023
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002024 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002026 if (policy->governor->init) {
2027 ret = policy->governor->init(policy);
2028 if (ret) {
2029 module_put(policy->governor->owner);
2030 return ret;
2031 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002034 policy->governor->initialized++;
2035 return 0;
2036}
2037
2038static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2039{
2040 if (cpufreq_suspended || !policy->governor)
2041 return;
2042
2043 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2044
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002045 if (policy->governor->exit)
2046 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002047
2048 policy->governor->initialized--;
2049 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002052static int cpufreq_start_governor(struct cpufreq_policy *policy)
2053{
2054 int ret;
2055
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002056 if (cpufreq_suspended)
2057 return 0;
2058
2059 if (!policy->governor)
2060 return -EINVAL;
2061
2062 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2063
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002064 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2065 cpufreq_update_current_freq(policy);
2066
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002067 if (policy->governor->start) {
2068 ret = policy->governor->start(policy);
2069 if (ret)
2070 return ret;
2071 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002072
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002073 if (policy->governor->limits)
2074 policy->governor->limits(policy);
2075
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002076 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002077}
2078
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002079static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2080{
2081 if (cpufreq_suspended || !policy->governor)
2082 return;
2083
2084 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2085
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002086 if (policy->governor->stop)
2087 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002088}
2089
2090static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2091{
2092 if (cpufreq_suspended || !policy->governor)
2093 return;
2094
2095 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2096
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002097 if (policy->governor->limits)
2098 policy->governor->limits(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101int cpufreq_register_governor(struct cpufreq_governor *governor)
2102{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002103 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 if (!governor)
2106 return -EINVAL;
2107
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002108 if (cpufreq_disabled())
2109 return -ENODEV;
2110
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002111 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002112
Viresh Kumarb3940582013-02-01 05:42:58 +00002113 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002114 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302115 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002116 err = 0;
2117 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Dave Jones32ee8c32006-02-28 00:43:23 -05002120 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002121 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2126{
Viresh Kumar45732372015-05-12 12:22:34 +05302127 struct cpufreq_policy *policy;
2128 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (!governor)
2131 return;
2132
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002133 if (cpufreq_disabled())
2134 return;
2135
Viresh Kumar45732372015-05-12 12:22:34 +05302136 /* clear last_governor for all inactive policies */
2137 read_lock_irqsave(&cpufreq_driver_lock, flags);
2138 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302139 if (!strcmp(policy->last_governor, governor->name)) {
2140 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302141 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302142 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002143 }
Viresh Kumar45732372015-05-12 12:22:34 +05302144 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002145
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002146 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002148 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return;
2150}
2151EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2152
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/*********************************************************************
2155 * POLICY INTERFACE *
2156 *********************************************************************/
2157
2158/**
2159 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002160 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2161 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 *
2163 * Reads the current cpufreq policy.
2164 */
2165int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2166{
2167 struct cpufreq_policy *cpu_policy;
2168 if (!policy)
2169 return -EINVAL;
2170
2171 cpu_policy = cpufreq_cpu_get(cpu);
2172 if (!cpu_policy)
2173 return -EINVAL;
2174
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302175 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 return 0;
2179}
2180EXPORT_SYMBOL(cpufreq_get_policy);
2181
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002182/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302183 * policy : current policy.
2184 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002185 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302186static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302187 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002189 struct cpufreq_governor *old_gov;
2190 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Joe Perchese837f9b2014-03-11 10:03:00 -07002192 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2193 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302195 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Pan Xinhuifba95732015-07-30 18:10:40 +08002197 /*
2198 * This check works well when we store new min/max freq attributes,
2199 * because new_policy is a copy of policy with one field updated.
2200 */
2201 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002202 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302205 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002207 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002210 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302211 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Viresh Kumarbb176f72013-06-19 14:19:33 +05302213 /*
2214 * verify the cpu speed can be set within this limit, which might be
2215 * different to the first one
2216 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302217 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002218 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002219 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002222 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302223 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302225 policy->min = new_policy->min;
2226 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002228 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002229 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002231 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302232 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002233 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002234 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002237 if (new_policy->governor == policy->governor) {
2238 pr_debug("cpufreq: governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002239 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002240 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002241 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002242
2243 pr_debug("governor switch\n");
2244
2245 /* save old, working values */
2246 old_gov = policy->governor;
2247 /* end old governor */
2248 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002249 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002250 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002251 }
2252
2253 /* start new governor */
2254 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002255 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302256 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002257 ret = cpufreq_start_governor(policy);
2258 if (!ret) {
2259 pr_debug("cpufreq: governor change\n");
2260 return 0;
2261 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002262 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002263 }
2264
2265 /* new governor failed, so re-start old one */
2266 pr_debug("starting governor %s failed\n", policy->governor->name);
2267 if (old_gov) {
2268 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002269 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302270 policy->governor = NULL;
2271 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002272 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002273 }
2274
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276}
2277
2278/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2280 * @cpu: CPU which shall be re-evaluated
2281 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002282 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * at different times.
2284 */
2285int cpufreq_update_policy(unsigned int cpu)
2286{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302287 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2288 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002289 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002291 if (!policy)
2292 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
viresh kumarad7722d2013-10-18 19:10:15 +05302294 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002296 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302297 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302298 new_policy.min = policy->user_policy.min;
2299 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Viresh Kumarbb176f72013-06-19 14:19:33 +05302301 /*
2302 * BIOS might change freq behind our back
2303 * -> ask driver for current freq and notify governors about a change
2304 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002305 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002306 new_policy.cur = cpufreq_update_current_freq(policy);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302307 if (WARN_ON(!new_policy.cur)) {
2308 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002309 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302310 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002311 }
2312
Viresh Kumar037ce832013-10-02 14:13:16 +05302313 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002315unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302316 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002317
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302318 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return ret;
2320}
2321EXPORT_SYMBOL(cpufreq_update_policy);
2322
Paul Gortmaker27609842013-06-19 13:54:04 -04002323static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002324 unsigned long action, void *hcpu)
2325{
2326 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002327
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002328 switch (action & ~CPU_TASKS_FROZEN) {
2329 case CPU_ONLINE:
Rafael J. Wysockicd73e9b2016-04-07 03:30:40 +02002330 case CPU_DOWN_FAILED:
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002331 cpufreq_online(cpu);
2332 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302333
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002334 case CPU_DOWN_PREPARE:
Viresh Kumar69cee712016-02-11 17:31:11 +05302335 cpufreq_offline(cpu);
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002336 break;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002337 }
2338 return NOTIFY_OK;
2339}
2340
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002341static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302342 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002343};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002346 * BOOST *
2347 *********************************************************************/
2348static int cpufreq_boost_set_sw(int state)
2349{
2350 struct cpufreq_frequency_table *freq_table;
2351 struct cpufreq_policy *policy;
2352 int ret = -EINVAL;
2353
Viresh Kumarf9637352015-05-12 12:20:11 +05302354 for_each_active_policy(policy) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002355 freq_table = cpufreq_frequency_get_table(policy->cpu);
2356 if (freq_table) {
2357 ret = cpufreq_frequency_table_cpuinfo(policy,
2358 freq_table);
2359 if (ret) {
2360 pr_err("%s: Policy frequency update failed\n",
2361 __func__);
2362 break;
2363 }
Viresh Kumar49f18562016-02-11 17:31:12 +05302364
2365 down_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002366 policy->user_policy.max = policy->max;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002367 cpufreq_governor_limits(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05302368 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002369 }
2370 }
2371
2372 return ret;
2373}
2374
2375int cpufreq_boost_trigger_state(int state)
2376{
2377 unsigned long flags;
2378 int ret = 0;
2379
2380 if (cpufreq_driver->boost_enabled == state)
2381 return 0;
2382
2383 write_lock_irqsave(&cpufreq_driver_lock, flags);
2384 cpufreq_driver->boost_enabled = state;
2385 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2386
2387 ret = cpufreq_driver->set_boost(state);
2388 if (ret) {
2389 write_lock_irqsave(&cpufreq_driver_lock, flags);
2390 cpufreq_driver->boost_enabled = !state;
2391 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2392
Joe Perchese837f9b2014-03-11 10:03:00 -07002393 pr_err("%s: Cannot %s BOOST\n",
2394 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002395 }
2396
2397 return ret;
2398}
2399
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002400static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002401{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002402 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002403}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002404
Viresh Kumar44139ed2015-07-29 16:23:09 +05302405static int create_boost_sysfs_file(void)
2406{
2407 int ret;
2408
Viresh Kumarc82bd442015-10-15 21:35:23 +05302409 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302410 if (ret)
2411 pr_err("%s: cannot register global BOOST sysfs file\n",
2412 __func__);
2413
2414 return ret;
2415}
2416
2417static void remove_boost_sysfs_file(void)
2418{
2419 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302420 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302421}
2422
2423int cpufreq_enable_boost_support(void)
2424{
2425 if (!cpufreq_driver)
2426 return -EINVAL;
2427
2428 if (cpufreq_boost_supported())
2429 return 0;
2430
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002431 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302432
2433 /* This will get removed on driver unregister */
2434 return create_boost_sysfs_file();
2435}
2436EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2437
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002438int cpufreq_boost_enabled(void)
2439{
2440 return cpufreq_driver->boost_enabled;
2441}
2442EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2443
2444/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2446 *********************************************************************/
2447
2448/**
2449 * cpufreq_register_driver - register a CPU Frequency driver
2450 * @driver_data: A struct cpufreq_driver containing the values#
2451 * submitted by the CPU Frequency driver.
2452 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302453 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002454 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002455 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
2457 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002458int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
2460 unsigned long flags;
2461 int ret;
2462
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002463 if (cpufreq_disabled())
2464 return -ENODEV;
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302467 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002468 driver_data->target) ||
2469 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302470 driver_data->target)) ||
2471 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 return -EINVAL;
2473
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002474 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002476 /* Protect against concurrent CPU online/offline. */
2477 get_online_cpus();
2478
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002479 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002480 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002481 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002482 ret = -EEXIST;
2483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002485 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002486 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302488 if (driver_data->setpolicy)
2489 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2490
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002491 if (cpufreq_boost_supported()) {
2492 ret = create_boost_sysfs_file();
2493 if (ret)
2494 goto err_null_driver;
2495 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002496
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002497 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002498 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002499 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302501 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2502 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302504 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2505 driver_data->name);
2506 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 }
2508
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002509 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002510 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002511 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002512
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002513err_if_unreg:
2514 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002515err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302516 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002517err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002518 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002519 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002520 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002521out:
2522 put_online_cpus();
2523 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527/**
2528 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2529 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302530 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 * the right to do so, i.e. if you have succeeded in initialising before!
2532 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2533 * currently not initialised.
2534 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002535int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
2537 unsigned long flags;
2538
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002539 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002542 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002544 /* Protect against concurrent cpu hotplug */
2545 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002546 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302547 remove_boost_sysfs_file();
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002548 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002550 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302551
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002552 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302553
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002554 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002555 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 return 0;
2558}
2559EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002560
Doug Anderson90de2a42014-12-23 22:09:48 -08002561/*
2562 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2563 * or mutexes when secondary CPUs are halted.
2564 */
2565static struct syscore_ops cpufreq_syscore_ops = {
2566 .shutdown = cpufreq_suspend,
2567};
2568
Viresh Kumarc82bd442015-10-15 21:35:23 +05302569struct kobject *cpufreq_global_kobject;
2570EXPORT_SYMBOL(cpufreq_global_kobject);
2571
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002572static int __init cpufreq_core_init(void)
2573{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002574 if (cpufreq_disabled())
2575 return -ENODEV;
2576
Viresh Kumar8eec1022015-10-15 21:35:22 +05302577 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002578 BUG_ON(!cpufreq_global_kobject);
2579
Doug Anderson90de2a42014-12-23 22:09:48 -08002580 register_syscore_ops(&cpufreq_syscore_ops);
2581
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002582 return 0;
2583}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002584core_initcall(cpufreq_core_init);