blob: a5b7d77d48168704181e4fc4fbac9c778d3b1447 [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 Kumara1317e02016-02-22 16:36:43 +053077static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
Viresh Kumard92d50a2015-01-02 12:34:29 +053078static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010079static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +020080static int cpufreq_exit_governor(struct cpufreq_policy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/**
Dave Jones32ee8c32006-02-28 00:43:23 -050083 * Two notifier lists: the "policy" list is involved in the
84 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 * "transition" list for kernel code that needs to handle
86 * changes to devices when the CPU clock speed changes.
87 * The mutex locks both lists.
88 */
Alan Sterne041c682006-03-27 01:16:30 -080089static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070090static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020092static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070093static int __init init_cpufreq_transition_notifier_list(void)
94{
95 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020096 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070097 return 0;
98}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080099pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400101static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200102static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400103{
104 return off;
105}
106void disable_cpufreq(void)
107{
108 off = 1;
109}
Dave Jones29464f22009-01-18 01:37:11 -0500110static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000112bool have_governor_per_policy(void)
113{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530114 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000115}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000116EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000117
Viresh Kumar944e9a02013-05-16 05:09:57 +0000118struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
119{
120 if (have_governor_per_policy())
121 return &policy->kobj;
122 else
123 return cpufreq_global_kobject;
124}
125EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
126
Viresh Kumar5a31d592015-07-10 01:43:27 +0200127struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
128{
129 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
130
131 return policy && !policy_is_inactive(policy) ?
132 policy->freq_table : NULL;
133}
134EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
135
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000136static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
137{
138 u64 idle_time;
139 u64 cur_wall_time;
140 u64 busy_time;
141
142 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
143
144 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
145 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
146 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
147 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
148 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
149 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
150
151 idle_time = cur_wall_time - busy_time;
152 if (wall)
153 *wall = cputime_to_usecs(cur_wall_time);
154
155 return cputime_to_usecs(idle_time);
156}
157
158u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
159{
160 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
161
162 if (idle_time == -1ULL)
163 return get_cpu_idle_time_jiffy(cpu, wall);
164 else if (!io_busy)
165 idle_time += get_cpu_iowait_time_us(cpu, wall);
166
167 return idle_time;
168}
169EXPORT_SYMBOL_GPL(get_cpu_idle_time);
170
Viresh Kumar70e9e772013-10-03 20:29:07 +0530171/*
172 * This is a generic cpufreq init() routine which can be used by cpufreq
173 * drivers of SMP systems. It will do following:
174 * - validate & show freq table passed
175 * - set policies transition latency
176 * - policy->cpus with all possible CPUs
177 */
178int cpufreq_generic_init(struct cpufreq_policy *policy,
179 struct cpufreq_frequency_table *table,
180 unsigned int transition_latency)
181{
182 int ret;
183
184 ret = cpufreq_table_validate_and_show(policy, table);
185 if (ret) {
186 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
187 return ret;
188 }
189
190 policy->cpuinfo.transition_latency = transition_latency;
191
192 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530193 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530194 * share the clock and voltage and clock.
195 */
196 cpumask_setall(policy->cpus);
197
198 return 0;
199}
200EXPORT_SYMBOL_GPL(cpufreq_generic_init);
201
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200202struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530203{
204 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
205
Viresh Kumar988bed02015-05-08 11:53:45 +0530206 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
207}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200208EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530209
210unsigned int cpufreq_generic_get(unsigned int cpu)
211{
212 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
213
Viresh Kumar652ed952014-01-09 20:38:43 +0530214 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700215 pr_err("%s: No %s associated to cpu: %d\n",
216 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530217 return 0;
218 }
219
220 return clk_get_rate(policy->clk) / 1000;
221}
222EXPORT_SYMBOL_GPL(cpufreq_generic_get);
223
Viresh Kumar50e9c852015-02-19 17:02:03 +0530224/**
225 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
226 *
227 * @cpu: cpu to find policy for.
228 *
229 * This returns policy for 'cpu', returns NULL if it doesn't exist.
230 * It also increments the kobject reference count to mark it busy and so would
231 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
232 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
233 * freed as that depends on the kobj count.
234 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530235 * Return: A valid policy on success, otherwise NULL on failure.
236 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530237struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530239 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 unsigned long flags;
241
Viresh Kumar1b947c902015-02-19 17:02:05 +0530242 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530243 return NULL;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000246 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Viresh Kumar6eed9402013-08-06 22:53:11 +0530248 if (cpufreq_driver) {
249 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530250 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530251 if (policy)
252 kobject_get(&policy->kobj);
253 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200254
Viresh Kumar6eed9402013-08-06 22:53:11 +0530255 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530257 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
260
Viresh Kumar50e9c852015-02-19 17:02:03 +0530261/**
262 * cpufreq_cpu_put: Decrements the usage count of a policy
263 *
264 * @policy: policy earlier returned by cpufreq_cpu_get().
265 *
266 * This decrements the kobject reference count incremented earlier by calling
267 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530268 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530269void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530271 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
277 *********************************************************************/
278
279/**
280 * adjust_jiffies - adjust the system "loops_per_jiffy"
281 *
282 * This function alters the system "loops_per_jiffy" for the clock
283 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500284 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 * per-CPU loops_per_jiffy value wherever possible.
286 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800287static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530289#ifndef CONFIG_SMP
290 static unsigned long l_p_j_ref;
291 static unsigned int l_p_j_ref_freq;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (ci->flags & CPUFREQ_CONST_LOOPS)
294 return;
295
296 if (!l_p_j_ref_freq) {
297 l_p_j_ref = loops_per_jiffy;
298 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700299 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
300 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530302 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530303 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
304 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700305 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
306 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530311static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530312 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 BUG_ON(irqs_disabled());
315
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000316 if (cpufreq_disabled())
317 return;
318
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200319 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200320 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700321 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500326 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800327 * which is not equal to what the cpufreq core thinks is
328 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200330 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800331 if ((policy) && (policy->cpu == freqs->cpu) &&
332 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700333 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
334 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800335 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700338 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800339 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
341 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 case CPUFREQ_POSTCHANGE:
344 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700345 pr_debug("FREQ: %lu - CPU: %lu\n",
346 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100347 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800349 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800350 if (likely(policy) && likely(policy->cpu == freqs->cpu))
351 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530355
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530356/**
357 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
358 * on frequency transition.
359 *
360 * This function calls the transition notifiers and the "adjust_jiffies"
361 * function. It is called twice on all CPU frequency changes that have
362 * external effects.
363 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530364static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530365 struct cpufreq_freqs *freqs, unsigned int state)
366{
367 for_each_cpu(freqs->cpu, policy->cpus)
368 __cpufreq_notify_transition(policy, freqs, state);
369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530371/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530372static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530373 struct cpufreq_freqs *freqs, int transition_failed)
374{
375 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
376 if (!transition_failed)
377 return;
378
379 swap(freqs->old, freqs->new);
380 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
381 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
382}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530383
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530384void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
385 struct cpufreq_freqs *freqs)
386{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530387
388 /*
389 * Catch double invocations of _begin() which lead to self-deadlock.
390 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
391 * doesn't invoke _begin() on their behalf, and hence the chances of
392 * double invocations are very low. Moreover, there are scenarios
393 * where these checks can emit false-positive warnings in these
394 * drivers; so we avoid that by skipping them altogether.
395 */
396 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
397 && current == policy->transition_task);
398
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530399wait:
400 wait_event(policy->transition_wait, !policy->transition_ongoing);
401
402 spin_lock(&policy->transition_lock);
403
404 if (unlikely(policy->transition_ongoing)) {
405 spin_unlock(&policy->transition_lock);
406 goto wait;
407 }
408
409 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530410 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530411
412 spin_unlock(&policy->transition_lock);
413
414 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
415}
416EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
417
418void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
419 struct cpufreq_freqs *freqs, int transition_failed)
420{
421 if (unlikely(WARN_ON(!policy->transition_ongoing)))
422 return;
423
424 cpufreq_notify_post_transition(policy, freqs, transition_failed);
425
426 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530427 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530428
429 wake_up(&policy->transition_wait);
430}
431EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
432
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200433/*
434 * Fast frequency switching status count. Positive means "enabled", negative
435 * means "disabled" and 0 means "not decided yet".
436 */
437static int cpufreq_fast_switch_count;
438static DEFINE_MUTEX(cpufreq_fast_switch_lock);
439
440static void cpufreq_list_transition_notifiers(void)
441{
442 struct notifier_block *nb;
443
444 pr_info("Registered transition notifiers:\n");
445
446 mutex_lock(&cpufreq_transition_notifier_list.mutex);
447
448 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
449 pr_info("%pF\n", nb->notifier_call);
450
451 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
452}
453
454/**
455 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
456 * @policy: cpufreq policy to enable fast frequency switching for.
457 *
458 * Try to enable fast frequency switching for @policy.
459 *
460 * The attempt will fail if there is at least one transition notifier registered
461 * at this point, as fast frequency switching is quite fundamentally at odds
462 * with transition notifiers. Thus if successful, it will make registration of
463 * transition notifiers fail going forward.
464 */
465void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
466{
467 lockdep_assert_held(&policy->rwsem);
468
469 if (!policy->fast_switch_possible)
470 return;
471
472 mutex_lock(&cpufreq_fast_switch_lock);
473 if (cpufreq_fast_switch_count >= 0) {
474 cpufreq_fast_switch_count++;
475 policy->fast_switch_enabled = true;
476 } else {
477 pr_warn("CPU%u: Fast frequency switching not enabled\n",
478 policy->cpu);
479 cpufreq_list_transition_notifiers();
480 }
481 mutex_unlock(&cpufreq_fast_switch_lock);
482}
483EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
484
485static void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
486{
487 mutex_lock(&cpufreq_fast_switch_lock);
488 if (policy->fast_switch_enabled) {
489 policy->fast_switch_enabled = false;
490 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
491 cpufreq_fast_switch_count--;
492 }
493 mutex_unlock(&cpufreq_fast_switch_lock);
494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*********************************************************************
497 * SYSFS INTERFACE *
498 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530499static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100500 struct attribute *attr, char *buf)
501{
502 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
503}
504
505static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
506 const char *buf, size_t count)
507{
508 int ret, enable;
509
510 ret = sscanf(buf, "%d", &enable);
511 if (ret != 1 || enable < 0 || enable > 1)
512 return -EINVAL;
513
514 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700515 pr_err("%s: Cannot %s BOOST!\n",
516 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100517 return -EINVAL;
518 }
519
Joe Perchese837f9b2014-03-11 10:03:00 -0700520 pr_debug("%s: cpufreq BOOST %s\n",
521 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100522
523 return count;
524}
525define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530527static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700528{
529 struct cpufreq_governor *t;
530
Viresh Kumarf7b27062015-01-27 14:06:09 +0530531 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200532 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700533 return t;
534
535 return NULL;
536}
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538/**
539 * cpufreq_parse_governor - parse a governor string
540 */
Dave Jones905d77c2008-03-05 14:28:32 -0500541static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct cpufreq_governor **governor)
543{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700544 int err = -EINVAL;
545
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200546 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200547 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700549 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200550 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530551 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700553 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530555 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700557
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800558 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700559
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530560 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700561
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700562 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700563 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700564
Kees Cook1a8e1462011-05-04 08:38:56 -0700565 mutex_unlock(&cpufreq_governor_mutex);
566 ret = request_module("cpufreq_%s", str_governor);
567 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700568
Kees Cook1a8e1462011-05-04 08:38:56 -0700569 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530570 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700571 }
572
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700573 if (t != NULL) {
574 *governor = t;
575 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700577
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800578 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700580 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530584 * cpufreq_per_cpu_attr_read() / show_##file_name() -
585 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 *
587 * Write out information from cpufreq_driver->policy[cpu]; object must be
588 * "unsigned int".
589 */
590
Dave Jones32ee8c32006-02-28 00:43:23 -0500591#define show_one(file_name, object) \
592static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500593(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500594{ \
Dave Jones29464f22009-01-18 01:37:11 -0500595 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598show_one(cpuinfo_min_freq, cpuinfo.min_freq);
599show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100600show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601show_one(scaling_min_freq, min);
602show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700603
Viresh Kumar09347b22015-01-02 12:34:24 +0530604static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700605{
606 ssize_t ret;
607
608 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
609 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
610 else
611 ret = sprintf(buf, "%u\n", policy->cur);
612 return ret;
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Viresh Kumar037ce832013-10-02 14:13:16 +0530615static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530616 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/**
619 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
620 */
621#define store_one(file_name, object) \
622static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500623(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800625 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 struct cpufreq_policy new_policy; \
627 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530628 memcpy(&new_policy, policy, sizeof(*policy)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 \
Dave Jones29464f22009-01-18 01:37:11 -0500630 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (ret != 1) \
632 return -EINVAL; \
633 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800634 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530635 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800636 if (!ret) \
637 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 \
639 return ret ? ret : count; \
640}
641
Dave Jones29464f22009-01-18 01:37:11 -0500642store_one(scaling_min_freq, min);
643store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/**
646 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
647 */
Dave Jones905d77c2008-03-05 14:28:32 -0500648static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
649 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530651 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 if (!cur_freq)
653 return sprintf(buf, "<unknown>");
654 return sprintf(buf, "%u\n", cur_freq);
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/**
658 * show_scaling_governor - show the current policy for the specified CPU
659 */
Dave Jones905d77c2008-03-05 14:28:32 -0500660static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dave Jones29464f22009-01-18 01:37:11 -0500662 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return sprintf(buf, "powersave\n");
664 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
665 return sprintf(buf, "performance\n");
666 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200667 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500668 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EINVAL;
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/**
673 * store_scaling_governor - store policy for the specified CPU
674 */
Dave Jones905d77c2008-03-05 14:28:32 -0500675static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
676 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530678 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 char str_governor[16];
680 struct cpufreq_policy new_policy;
681
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530682 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Dave Jones29464f22009-01-18 01:37:11 -0500684 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (ret != 1)
686 return -EINVAL;
687
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530688 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
689 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return -EINVAL;
691
Viresh Kumar037ce832013-10-02 14:13:16 +0530692 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530693 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696/**
697 * show_scaling_driver - show the cpufreq driver currently loaded
698 */
Dave Jones905d77c2008-03-05 14:28:32 -0500699static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200701 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
704/**
705 * show_scaling_available_governors - show the available CPUfreq governors
706 */
Dave Jones905d77c2008-03-05 14:28:32 -0500707static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
708 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
710 ssize_t i = 0;
711 struct cpufreq_governor *t;
712
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530713 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 i += sprintf(buf, "performance powersave");
715 goto out;
716 }
717
Viresh Kumarf7b27062015-01-27 14:06:09 +0530718 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500719 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
720 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200722 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500724out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 i += sprintf(&buf[i], "\n");
726 return i;
727}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700728
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800729ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 ssize_t i = 0;
732 unsigned int cpu;
733
Rusty Russell835481d2009-01-04 05:18:06 -0800734 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (i)
736 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
737 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
738 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741 i += sprintf(&buf[i], "\n");
742 return i;
743}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800744EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700746/**
747 * show_related_cpus - show the CPUs affected by each transition even if
748 * hw coordination is in use
749 */
750static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
751{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800752 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700753}
754
755/**
756 * show_affected_cpus - show the CPUs affected by each transition
757 */
758static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
759{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800760 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700761}
762
Venki Pallipadi9e769882007-10-26 10:18:21 -0700763static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500764 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700765{
766 unsigned int freq = 0;
767 unsigned int ret;
768
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700769 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700770 return -EINVAL;
771
772 ret = sscanf(buf, "%u", &freq);
773 if (ret != 1)
774 return -EINVAL;
775
776 policy->governor->store_setspeed(policy, freq);
777
778 return count;
779}
780
781static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
782{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700783 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700784 return sprintf(buf, "<unsupported>\n");
785
786 return policy->governor->show_setspeed(policy, buf);
787}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Thomas Renningere2f74f32009-11-19 12:31:01 +0100789/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200790 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100791 */
792static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
793{
794 unsigned int limit;
795 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200796 if (cpufreq_driver->bios_limit) {
797 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100798 if (!ret)
799 return sprintf(buf, "%u\n", limit);
800 }
801 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
802}
803
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200804cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
805cpufreq_freq_attr_ro(cpuinfo_min_freq);
806cpufreq_freq_attr_ro(cpuinfo_max_freq);
807cpufreq_freq_attr_ro(cpuinfo_transition_latency);
808cpufreq_freq_attr_ro(scaling_available_governors);
809cpufreq_freq_attr_ro(scaling_driver);
810cpufreq_freq_attr_ro(scaling_cur_freq);
811cpufreq_freq_attr_ro(bios_limit);
812cpufreq_freq_attr_ro(related_cpus);
813cpufreq_freq_attr_ro(affected_cpus);
814cpufreq_freq_attr_rw(scaling_min_freq);
815cpufreq_freq_attr_rw(scaling_max_freq);
816cpufreq_freq_attr_rw(scaling_governor);
817cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Dave Jones905d77c2008-03-05 14:28:32 -0500819static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 &cpuinfo_min_freq.attr,
821 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100822 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 &scaling_min_freq.attr,
824 &scaling_max_freq.attr,
825 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700826 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 &scaling_governor.attr,
828 &scaling_driver.attr,
829 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700830 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 NULL
832};
833
Dave Jones29464f22009-01-18 01:37:11 -0500834#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
835#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Dave Jones29464f22009-01-18 01:37:11 -0500837static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Dave Jones905d77c2008-03-05 14:28:32 -0500839 struct cpufreq_policy *policy = to_policy(kobj);
840 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530841 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530842
viresh kumarad7722d2013-10-18 19:10:15 +0530843 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100844 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530845 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return ret;
848}
849
Dave Jones905d77c2008-03-05 14:28:32 -0500850static ssize_t store(struct kobject *kobj, struct attribute *attr,
851 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Dave Jones905d77c2008-03-05 14:28:32 -0500853 struct cpufreq_policy *policy = to_policy(kobj);
854 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500855 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530856
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530857 get_online_cpus();
858
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100859 if (cpu_online(policy->cpu)) {
860 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530861 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100862 up_write(&policy->rwsem);
863 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530864
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530865 put_online_cpus();
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return ret;
868}
869
Dave Jones905d77c2008-03-05 14:28:32 -0500870static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Dave Jones905d77c2008-03-05 14:28:32 -0500872 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200873 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 complete(&policy->kobj_unregister);
875}
876
Emese Revfy52cf25d2010-01-19 02:58:23 +0100877static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 .show = show,
879 .store = store,
880};
881
882static struct kobj_type ktype_cpufreq = {
883 .sysfs_ops = &sysfs_ops,
884 .default_attrs = default_attrs,
885 .release = cpufreq_sysfs_release,
886};
887
Viresh Kumar87549142015-06-10 02:13:21 +0200888static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
889{
890 struct device *cpu_dev;
891
892 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
893
894 if (!policy)
895 return 0;
896
897 cpu_dev = get_cpu_device(cpu);
898 if (WARN_ON(!cpu_dev))
899 return 0;
900
901 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
902}
903
904static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
905{
906 struct device *cpu_dev;
907
908 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
909
910 cpu_dev = get_cpu_device(cpu);
911 if (WARN_ON(!cpu_dev))
912 return;
913
914 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
915}
916
917/* Add/remove symlinks for all related CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200918static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400919{
920 unsigned int j;
921 int ret = 0;
922
Viresh Kumar87549142015-06-10 02:13:21 +0200923 /* Some related CPUs might not be present (physically hotplugged) */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +0200924 for_each_cpu(j, policy->real_cpus) {
Viresh Kumar87549142015-06-10 02:13:21 +0200925 ret = add_cpu_dev_symlink(policy, j);
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200926 if (ret)
927 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400928 }
Viresh Kumar87549142015-06-10 02:13:21 +0200929
Dave Jones19d6f7e2009-07-08 17:35:39 -0400930 return ret;
931}
932
Viresh Kumar87549142015-06-10 02:13:21 +0200933static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
934{
935 unsigned int j;
936
937 /* Some related CPUs might not be present (physically hotplugged) */
Viresh Kumar96bdda62015-10-15 21:35:24 +0530938 for_each_cpu(j, policy->real_cpus)
Viresh Kumar87549142015-06-10 02:13:21 +0200939 remove_cpu_dev_symlink(policy, j);
Viresh Kumar87549142015-06-10 02:13:21 +0200940}
941
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200942static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400943{
944 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400945 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400946
Dave Jones909a6942009-07-08 18:05:42 -0400947 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200948 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530949 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400950 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
951 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100952 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400953 drv_attr++;
954 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200955 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400956 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
957 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100958 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400959 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700960
961 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
962 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100963 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700964
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200965 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100966 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
967 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100968 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100969 }
Dave Jones909a6942009-07-08 18:05:42 -0400970
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100971 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530972}
973
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100974__weak struct cpufreq_governor *cpufreq_default_governor(void)
975{
976 return NULL;
977}
978
Viresh Kumar7f0fa402015-07-08 15:12:16 +0530979static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530980{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800981 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530982 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530983
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530984 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000985
viresh kumar6e2c89d2014-03-04 11:43:59 +0800986 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +0530987 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100988 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +0800989 pr_debug("Restoring governor %s for cpu %d\n",
990 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100991 } else {
992 gov = cpufreq_default_governor();
993 if (!gov)
994 return -ENODATA;
995 }
viresh kumar6e2c89d2014-03-04 11:43:59 +0800996
997 new_policy.governor = gov;
998
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -0800999 /* Use the default policy if there is no last_policy. */
1000 if (cpufreq_driver->setpolicy) {
1001 if (policy->last_policy)
1002 new_policy.policy = policy->last_policy;
1003 else
1004 cpufreq_parse_governor(gov->name, &new_policy.policy,
1005 NULL);
1006 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001007 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301008 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001009}
1010
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001011static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001012{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301013 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001014
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301015 /* Has this CPU been taken care of already? */
1016 if (cpumask_test_cpu(cpu, policy->cpus))
1017 return 0;
1018
Viresh Kumar49f18562016-02-11 17:31:12 +05301019 down_write(&policy->rwsem);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301020 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301021 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301022 if (ret) {
1023 pr_err("%s: Failed to stop governor\n", __func__);
Viresh Kumar49f18562016-02-11 17:31:12 +05301024 goto unlock;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301025 }
1026 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001027
Viresh Kumarfcf80582013-01-29 14:39:08 +00001028 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301029
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301030 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001031 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301032 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301033 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001034 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001035
Viresh Kumar49f18562016-02-11 17:31:12 +05301036unlock:
1037 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{
1314 unsigned cpu = dev->id;
1315 int ret;
1316
1317 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1318
1319 if (cpu_online(cpu)) {
1320 ret = cpufreq_online(cpu);
1321 } else {
1322 /*
1323 * A hotplug notifier will follow and we will handle it as CPU
1324 * online then. For now, just create the sysfs link, unless
1325 * there is no policy or the link is already present.
1326 */
1327 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1328
1329 ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus)
1330 ? add_cpu_dev_symlink(policy, cpu) : 0;
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 return ret;
1334}
1335
Viresh Kumar69cee712016-02-11 17:31:11 +05301336static void cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301338 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301339 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001341 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
Viresh Kumar988bed02015-05-08 11:53:45 +05301343 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301344 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001345 pr_debug("%s: No cpu_data found\n", __func__);
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001346 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Viresh Kumar49f18562016-02-11 17:31:12 +05301349 down_write(&policy->rwsem);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301350 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301351 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001352 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301353 pr_err("%s: Failed to stop governor\n", __func__);
Viresh Kumardb5f2992015-01-02 12:34:25 +05301354 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001355
Viresh Kumar9591bec2015-06-10 02:20:23 +02001356 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301357
Viresh Kumar9591bec2015-06-10 02:20:23 +02001358 if (policy_is_inactive(policy)) {
1359 if (has_target())
1360 strncpy(policy->last_governor, policy->governor->name,
1361 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001362 else
1363 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001364 } else if (cpu == policy->cpu) {
1365 /* Nominate new CPU */
1366 policy->cpu = cpumask_any(policy->cpus);
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Viresh Kumar9591bec2015-06-10 02:20:23 +02001369 /* Start governor again for active policy */
1370 if (!policy_is_inactive(policy)) {
1371 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001372 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001373 if (ret)
1374 pr_err("%s: Failed to start governor\n", __func__);
1375 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301376
Viresh Kumar49f18562016-02-11 17:31:12 +05301377 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301378 }
1379
1380 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001381 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Viresh Kumar87549142015-06-10 02:13:21 +02001383 /* If cpu is last user of policy, free policy */
1384 if (has_target()) {
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001385 ret = cpufreq_exit_governor(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001386 if (ret)
Viresh Kumar87549142015-06-10 02:13:21 +02001387 pr_err("%s: Failed to exit governor\n", __func__);
Viresh Kumar87549142015-06-10 02:13:21 +02001388 }
1389
Viresh Kumar87549142015-06-10 02:13:21 +02001390 /*
1391 * Perform the ->exit() even during light-weight tear-down,
1392 * since this is a core component, and is essential for the
1393 * subsequent light-weight ->init() to succeed.
1394 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001395 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001396 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001397 policy->freq_table = NULL;
1398 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301399
1400unlock:
1401 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402}
1403
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301404/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301405 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301406 *
1407 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301408 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301409static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001410{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001411 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001412 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001413
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001414 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001415 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001416
Viresh Kumar69cee712016-02-11 17:31:11 +05301417 if (cpu_online(cpu))
1418 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001419
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001420 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar96bdda62015-10-15 21:35:24 +05301421 remove_cpu_dev_symlink(policy, cpu);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301422
1423 if (cpumask_empty(policy->real_cpus))
1424 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301428 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1429 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301430 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 * @new_freq: CPU frequency the CPU actually runs at
1432 *
Dave Jones29464f22009-01-18 01:37:11 -05001433 * We adjust to current frequency first, and need to clean up later.
1434 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301436static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301437 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
1439 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301440
Joe Perchese837f9b2014-03-11 10:03:00 -07001441 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 +05301442 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301444 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301446
Viresh Kumar8fec0512014-03-24 13:35:45 +05301447 cpufreq_freq_transition_begin(policy, &freqs);
1448 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449}
1450
Dave Jones32ee8c32006-02-28 00:43:23 -05001451/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301452 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001453 * @cpu: CPU number
1454 *
1455 * This is the last known freq, without actually getting it from the driver.
1456 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1457 */
1458unsigned int cpufreq_quick_get(unsigned int cpu)
1459{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001460 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301461 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001462 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001463
Richard Cochranc75361c2016-03-11 09:43:07 +01001464 read_lock_irqsave(&cpufreq_driver_lock, flags);
1465
1466 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1467 ret_freq = cpufreq_driver->get(cpu);
1468 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1469 return ret_freq;
1470 }
1471
1472 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001473
1474 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001475 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301476 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001477 cpufreq_cpu_put(policy);
1478 }
1479
Dave Jones4d34a672008-02-07 16:33:49 -05001480 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001481}
1482EXPORT_SYMBOL(cpufreq_quick_get);
1483
Jesse Barnes3d737102011-06-28 10:59:12 -07001484/**
1485 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1486 * @cpu: CPU number
1487 *
1488 * Just return the max possible frequency for a given CPU.
1489 */
1490unsigned int cpufreq_quick_get_max(unsigned int cpu)
1491{
1492 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1493 unsigned int ret_freq = 0;
1494
1495 if (policy) {
1496 ret_freq = policy->max;
1497 cpufreq_cpu_put(policy);
1498 }
1499
1500 return ret_freq;
1501}
1502EXPORT_SYMBOL(cpufreq_quick_get_max);
1503
Viresh Kumard92d50a2015-01-02 12:34:29 +05301504static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301506 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001508 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001509 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Viresh Kumard92d50a2015-01-02 12:34:29 +05301511 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001513 /*
1514 * Updating inactive policies is invalid, so avoid doing that. Also
1515 * if fast frequency switching is used with the given policy, the check
1516 * against policy->cur is pointless, so skip it in that case too.
1517 */
1518 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001519 return ret_freq;
1520
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301521 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001522 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301523 /* verify no discrepancy between actual and
1524 saved value exists */
1525 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301526 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 schedule_work(&policy->update);
1528 }
1529 }
1530
Dave Jones4d34a672008-02-07 16:33:49 -05001531 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001532}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001534/**
1535 * cpufreq_get - get the current CPU frequency (in kHz)
1536 * @cpu: CPU number
1537 *
1538 * Get the CPU current (static) CPU frequency
1539 */
1540unsigned int cpufreq_get(unsigned int cpu)
1541{
Aaron Plattner999976e2014-03-04 12:42:15 -08001542 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001543 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001544
Aaron Plattner999976e2014-03-04 12:42:15 -08001545 if (policy) {
1546 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301547 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001548 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301549
Aaron Plattner999976e2014-03-04 12:42:15 -08001550 cpufreq_cpu_put(policy);
1551 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301552
Dave Jones4d34a672008-02-07 16:33:49 -05001553 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555EXPORT_SYMBOL(cpufreq_get);
1556
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001557static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1558{
1559 unsigned int new_freq;
1560
1561 new_freq = cpufreq_driver->get(policy->cpu);
1562 if (!new_freq)
1563 return 0;
1564
1565 if (!policy->cur) {
1566 pr_debug("cpufreq: Driver did not initialize current freq\n");
1567 policy->cur = new_freq;
1568 } else if (policy->cur != new_freq && has_target()) {
1569 cpufreq_out_of_sync(policy, new_freq);
1570 }
1571
1572 return new_freq;
1573}
1574
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001575static struct subsys_interface cpufreq_interface = {
1576 .name = "cpufreq",
1577 .subsys = &cpu_subsys,
1578 .add_dev = cpufreq_add_dev,
1579 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001580};
1581
Viresh Kumare28867e2014-03-04 11:00:27 +08001582/*
1583 * In case platform wants some specific frequency to be configured
1584 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001585 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001586int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001587{
Viresh Kumare28867e2014-03-04 11:00:27 +08001588 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001589
Viresh Kumare28867e2014-03-04 11:00:27 +08001590 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001591 pr_debug("%s: suspend_freq not defined\n", __func__);
1592 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001593 }
1594
Viresh Kumare28867e2014-03-04 11:00:27 +08001595 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1596 policy->suspend_freq);
1597
1598 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1599 CPUFREQ_RELATION_H);
1600 if (ret)
1601 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1602 __func__, policy->suspend_freq, ret);
1603
Dave Jonesc9060492008-02-07 16:32:18 -05001604 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001605}
Viresh Kumare28867e2014-03-04 11:00:27 +08001606EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001607
1608/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001609 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001611 * Called during system wide Suspend/Hibernate cycles for suspending governors
1612 * as some platforms can't change frequency after this point in suspend cycle.
1613 * Because some of the devices (like: i2c, regulators, etc) they use for
1614 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001616void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301618 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301619 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001621 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001622 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001624 if (!has_target())
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301625 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001627 pr_debug("%s: Suspending Governors\n", __func__);
1628
Viresh Kumarf9637352015-05-12 12:20:11 +05301629 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301630 down_write(&policy->rwsem);
Viresh Kumara1317e02016-02-22 16:36:43 +05301631 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumar49f18562016-02-11 17:31:12 +05301632 up_write(&policy->rwsem);
1633
1634 if (ret)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001635 pr_err("%s: Failed to stop governor for policy: %p\n",
1636 __func__, policy);
1637 else if (cpufreq_driver->suspend
1638 && cpufreq_driver->suspend(policy))
1639 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1640 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301642
1643suspend:
1644 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001648 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001650 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1651 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001653void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301656 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001658 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return;
1660
Lan Tianyu8e304442014-09-18 15:03:07 +08001661 cpufreq_suspended = false;
1662
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001663 if (!has_target())
1664 return;
1665
1666 pr_debug("%s: Resuming Governors\n", __func__);
1667
Viresh Kumarf9637352015-05-12 12:20:11 +05301668 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301669 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301670 pr_err("%s: Failed to resume driver: %p\n", __func__,
1671 policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301672 } else {
1673 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001674 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301675 up_write(&policy->rwsem);
1676
1677 if (ret)
1678 pr_err("%s: Failed to start governor for policy: %p\n",
1679 __func__, policy);
1680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Borislav Petkov9d950462013-01-20 10:24:28 +00001684/**
1685 * cpufreq_get_current_driver - return current driver's name
1686 *
1687 * Return the name string of the currently loaded cpufreq driver
1688 * or NULL, if none.
1689 */
1690const char *cpufreq_get_current_driver(void)
1691{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001692 if (cpufreq_driver)
1693 return cpufreq_driver->name;
1694
1695 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001696}
1697EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001699/**
1700 * cpufreq_get_driver_data - return current driver data
1701 *
1702 * Return the private data of the currently loaded cpufreq
1703 * driver, or NULL if no cpufreq driver is loaded.
1704 */
1705void *cpufreq_get_driver_data(void)
1706{
1707 if (cpufreq_driver)
1708 return cpufreq_driver->driver_data;
1709
1710 return NULL;
1711}
1712EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*********************************************************************
1715 * NOTIFIER LISTS INTERFACE *
1716 *********************************************************************/
1717
1718/**
1719 * cpufreq_register_notifier - register a driver with cpufreq
1720 * @nb: notifier function to register
1721 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1722 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001723 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * are notified about clock rate changes (once before and once after
1725 * the transition), or a list of drivers that are notified about
1726 * changes in cpufreq policy.
1727 *
1728 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001729 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 */
1731int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1732{
1733 int ret;
1734
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001735 if (cpufreq_disabled())
1736 return -EINVAL;
1737
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001738 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 switch (list) {
1741 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001742 mutex_lock(&cpufreq_fast_switch_lock);
1743
1744 if (cpufreq_fast_switch_count > 0) {
1745 mutex_unlock(&cpufreq_fast_switch_lock);
1746 return -EBUSY;
1747 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001748 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001749 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001750 if (!ret)
1751 cpufreq_fast_switch_count--;
1752
1753 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 break;
1755 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001756 ret = blocking_notifier_chain_register(
1757 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 break;
1759 default:
1760 ret = -EINVAL;
1761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 return ret;
1764}
1765EXPORT_SYMBOL(cpufreq_register_notifier);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1769 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301770 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
1772 * Remove a driver from the CPU frequency notifier list.
1773 *
1774 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001775 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
1777int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1778{
1779 int ret;
1780
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001781 if (cpufreq_disabled())
1782 return -EINVAL;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 switch (list) {
1785 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001786 mutex_lock(&cpufreq_fast_switch_lock);
1787
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001788 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001789 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001790 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1791 cpufreq_fast_switch_count++;
1792
1793 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001796 ret = blocking_notifier_chain_unregister(
1797 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799 default:
1800 ret = -EINVAL;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 return ret;
1804}
1805EXPORT_SYMBOL(cpufreq_unregister_notifier);
1806
1807
1808/*********************************************************************
1809 * GOVERNORS *
1810 *********************************************************************/
1811
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001812/**
1813 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1814 * @policy: cpufreq policy to switch the frequency for.
1815 * @target_freq: New frequency to set (may be approximate).
1816 *
1817 * Carry out a fast frequency switch without sleeping.
1818 *
1819 * The driver's ->fast_switch() callback invoked by this function must be
1820 * suitable for being called from within RCU-sched read-side critical sections
1821 * and it is expected to select the minimum available frequency greater than or
1822 * equal to @target_freq (CPUFREQ_RELATION_L).
1823 *
1824 * This function must not be called if policy->fast_switch_enabled is unset.
1825 *
1826 * Governors calling this function must guarantee that it will never be invoked
1827 * twice in parallel for the same policy and that it will never be called in
1828 * parallel with either ->target() or ->target_index() for the same policy.
1829 *
1830 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1831 * callback to indicate an error condition, the hardware configuration must be
1832 * preserved.
1833 */
1834unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1835 unsigned int target_freq)
1836{
1837 clamp_val(target_freq, policy->min, policy->max);
1838
1839 return cpufreq_driver->fast_switch(policy, target_freq);
1840}
1841EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1842
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301843/* Must set freqs->new to intermediate frequency */
1844static int __target_intermediate(struct cpufreq_policy *policy,
1845 struct cpufreq_freqs *freqs, int index)
1846{
1847 int ret;
1848
1849 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1850
1851 /* We don't need to switch to intermediate freq */
1852 if (!freqs->new)
1853 return 0;
1854
1855 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1856 __func__, policy->cpu, freqs->old, freqs->new);
1857
1858 cpufreq_freq_transition_begin(policy, freqs);
1859 ret = cpufreq_driver->target_intermediate(policy, index);
1860 cpufreq_freq_transition_end(policy, freqs, ret);
1861
1862 if (ret)
1863 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1864 __func__, ret);
1865
1866 return ret;
1867}
1868
Viresh Kumar8d657752014-05-21 14:29:29 +05301869static int __target_index(struct cpufreq_policy *policy,
1870 struct cpufreq_frequency_table *freq_table, int index)
1871{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301872 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1873 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301874 int retval = -EINVAL;
1875 bool notify;
1876
1877 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301878 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301879 /* Handle switching to intermediate frequency */
1880 if (cpufreq_driver->get_intermediate) {
1881 retval = __target_intermediate(policy, &freqs, index);
1882 if (retval)
1883 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301884
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301885 intermediate_freq = freqs.new;
1886 /* Set old freq to intermediate */
1887 if (intermediate_freq)
1888 freqs.old = freqs.new;
1889 }
1890
1891 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301892 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1893 __func__, policy->cpu, freqs.old, freqs.new);
1894
1895 cpufreq_freq_transition_begin(policy, &freqs);
1896 }
1897
1898 retval = cpufreq_driver->target_index(policy, index);
1899 if (retval)
1900 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1901 retval);
1902
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301903 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301904 cpufreq_freq_transition_end(policy, &freqs, retval);
1905
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301906 /*
1907 * Failed after setting to intermediate freq? Driver should have
1908 * reverted back to initial frequency and so should we. Check
1909 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301910 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301911 */
1912 if (unlikely(retval && intermediate_freq)) {
1913 freqs.old = intermediate_freq;
1914 freqs.new = policy->restore_freq;
1915 cpufreq_freq_transition_begin(policy, &freqs);
1916 cpufreq_freq_transition_end(policy, &freqs, 0);
1917 }
1918 }
1919
Viresh Kumar8d657752014-05-21 14:29:29 +05301920 return retval;
1921}
1922
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923int __cpufreq_driver_target(struct cpufreq_policy *policy,
1924 unsigned int target_freq,
1925 unsigned int relation)
1926{
Viresh Kumar72499242012-10-31 01:28:21 +01001927 unsigned int old_target_freq = target_freq;
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001928 struct cpufreq_frequency_table *freq_table;
1929 int index, retval;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001930
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001931 if (cpufreq_disabled())
1932 return -ENODEV;
1933
Viresh Kumar72499242012-10-31 01:28:21 +01001934 /* Make sure that target_freq is within supported range */
1935 if (target_freq > policy->max)
1936 target_freq = policy->max;
1937 if (target_freq < policy->min)
1938 target_freq = policy->min;
1939
1940 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001941 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001942
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301943 /*
1944 * This might look like a redundant call as we are checking it again
1945 * after finding index. But it is left intentionally for cases where
1946 * exactly same freq is called again and so we can save on few function
1947 * calls.
1948 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001949 if (target_freq == policy->cur)
1950 return 0;
1951
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301952 /* Save last value to restore later on errors */
1953 policy->restore_freq = policy->cur;
1954
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001955 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001956 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001957
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001958 if (!cpufreq_driver->target_index)
1959 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301960
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001961 freq_table = cpufreq_frequency_get_table(policy->cpu);
1962 if (unlikely(!freq_table)) {
1963 pr_err("%s: Unable to find freq_table\n", __func__);
1964 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301965 }
1966
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001967 retval = cpufreq_frequency_table_target(policy, freq_table, target_freq,
1968 relation, &index);
1969 if (unlikely(retval)) {
1970 pr_err("%s: Unable to find matching freq\n", __func__);
1971 return retval;
1972 }
1973
1974 if (freq_table[index].frequency == policy->cur)
1975 return 0;
1976
1977 return __target_index(policy, freq_table, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981int cpufreq_driver_target(struct cpufreq_policy *policy,
1982 unsigned int target_freq,
1983 unsigned int relation)
1984{
Julia Lawallf1829e42008-07-25 22:44:53 +02001985 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
viresh kumarad7722d2013-10-18 19:10:15 +05301987 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 ret = __cpufreq_driver_target(policy, target_freq, relation);
1990
viresh kumarad7722d2013-10-18 19:10:15 +05301991 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return ret;
1994}
1995EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1996
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001997__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1998{
1999 return NULL;
2000}
2001
Viresh Kumara1317e02016-02-22 16:36:43 +05302002static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Dave Jonescc993ca2005-07-28 09:43:56 -07002004 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002005
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002006 /* Don't start any governor operations if we are entering suspend */
2007 if (cpufreq_suspended)
2008 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002009 /*
2010 * Governor might not be initiated here if ACPI _PPC changed
2011 * notification happened, so check it.
2012 */
2013 if (!policy->governor)
2014 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002015
Thomas Renninger1c256242007-10-02 13:28:12 -07002016 if (policy->governor->max_transition_latency &&
2017 policy->cpuinfo.transition_latency >
2018 policy->governor->max_transition_latency) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002019 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2020
2021 if (gov) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002022 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2023 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002024 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002025 } else {
2026 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002027 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Viresh Kumarfe492f32013-08-06 22:53:10 +05302030 if (event == CPUFREQ_GOV_POLICY_INIT)
2031 if (!try_module_get(policy->governor->owner))
2032 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Viresh Kumar63431f72015-07-27 17:58:06 +05302034 pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 ret = policy->governor->governor(policy, event);
2037
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002038 if (!ret) {
2039 if (event == CPUFREQ_GOV_POLICY_INIT)
2040 policy->governor->initialized++;
2041 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2042 policy->governor->initialized--;
2043 }
Viresh Kumarb3940582013-02-01 05:42:58 +00002044
Viresh Kumarfe492f32013-08-06 22:53:10 +05302045 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2046 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 module_put(policy->governor->owner);
2048
2049 return ret;
2050}
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. Wysocki3bbf8fe2016-03-21 15:47:48 +01002056 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2057 cpufreq_update_current_freq(policy);
2058
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002059 ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
2060 return ret ? ret : cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2061}
2062
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002063static int cpufreq_exit_governor(struct cpufreq_policy *policy)
2064{
2065 cpufreq_disable_fast_switch(policy);
2066 return cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2067}
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069int cpufreq_register_governor(struct cpufreq_governor *governor)
2070{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002071 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 if (!governor)
2074 return -EINVAL;
2075
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002076 if (cpufreq_disabled())
2077 return -ENODEV;
2078
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002079 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002080
Viresh Kumarb3940582013-02-01 05:42:58 +00002081 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002082 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302083 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002084 err = 0;
2085 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Dave Jones32ee8c32006-02-28 00:43:23 -05002088 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002089 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2094{
Viresh Kumar45732372015-05-12 12:22:34 +05302095 struct cpufreq_policy *policy;
2096 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 if (!governor)
2099 return;
2100
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002101 if (cpufreq_disabled())
2102 return;
2103
Viresh Kumar45732372015-05-12 12:22:34 +05302104 /* clear last_governor for all inactive policies */
2105 read_lock_irqsave(&cpufreq_driver_lock, flags);
2106 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302107 if (!strcmp(policy->last_governor, governor->name)) {
2108 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302109 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302110 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002111 }
Viresh Kumar45732372015-05-12 12:22:34 +05302112 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002113
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002114 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002116 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return;
2118}
2119EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2120
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122/*********************************************************************
2123 * POLICY INTERFACE *
2124 *********************************************************************/
2125
2126/**
2127 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002128 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2129 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 *
2131 * Reads the current cpufreq policy.
2132 */
2133int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2134{
2135 struct cpufreq_policy *cpu_policy;
2136 if (!policy)
2137 return -EINVAL;
2138
2139 cpu_policy = cpufreq_cpu_get(cpu);
2140 if (!cpu_policy)
2141 return -EINVAL;
2142
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302143 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return 0;
2147}
2148EXPORT_SYMBOL(cpufreq_get_policy);
2149
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002150/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302151 * policy : current policy.
2152 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002153 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302154static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302155 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002157 struct cpufreq_governor *old_gov;
2158 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Joe Perchese837f9b2014-03-11 10:03:00 -07002160 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2161 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302163 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Pan Xinhuifba95732015-07-30 18:10:40 +08002165 /*
2166 * This check works well when we store new min/max freq attributes,
2167 * because new_policy is a copy of policy with one field updated.
2168 */
2169 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002170 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302173 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002175 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002178 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302179 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Viresh Kumarbb176f72013-06-19 14:19:33 +05302181 /*
2182 * verify the cpu speed can be set within this limit, which might be
2183 * different to the first one
2184 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302185 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002186 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002187 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002190 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302191 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302193 policy->min = new_policy->min;
2194 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002196 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002197 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002199 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302200 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002201 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002202 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002205 if (new_policy->governor == policy->governor) {
2206 pr_debug("cpufreq: governor limits update\n");
2207 return cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2208 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002209
2210 pr_debug("governor switch\n");
2211
2212 /* save old, working values */
2213 old_gov = policy->governor;
2214 /* end old governor */
2215 if (old_gov) {
Viresh Kumara1317e02016-02-22 16:36:43 +05302216 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302217 if (ret) {
2218 /* This can happen due to race with other operations */
2219 pr_debug("%s: Failed to Stop Governor: %s (%d)\n",
2220 __func__, old_gov->name, ret);
2221 return ret;
2222 }
2223
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002224 ret = cpufreq_exit_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302225 if (ret) {
2226 pr_err("%s: Failed to Exit Governor: %s (%d)\n",
2227 __func__, old_gov->name, ret);
2228 return ret;
2229 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002230 }
2231
2232 /* start new governor */
2233 policy->governor = new_policy->governor;
Viresh Kumara1317e02016-02-22 16:36:43 +05302234 ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302235 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002236 ret = cpufreq_start_governor(policy);
2237 if (!ret) {
2238 pr_debug("cpufreq: governor change\n");
2239 return 0;
2240 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002241 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002242 }
2243
2244 /* new governor failed, so re-start old one */
2245 pr_debug("starting governor %s failed\n", policy->governor->name);
2246 if (old_gov) {
2247 policy->governor = old_gov;
Viresh Kumara1317e02016-02-22 16:36:43 +05302248 if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302249 policy->governor = NULL;
2250 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002251 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002252 }
2253
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302254 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
2257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2259 * @cpu: CPU which shall be re-evaluated
2260 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002261 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 * at different times.
2263 */
2264int cpufreq_update_policy(unsigned int cpu)
2265{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302266 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2267 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002268 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002270 if (!policy)
2271 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
viresh kumarad7722d2013-10-18 19:10:15 +05302273 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002275 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302276 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302277 new_policy.min = policy->user_policy.min;
2278 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Viresh Kumarbb176f72013-06-19 14:19:33 +05302280 /*
2281 * BIOS might change freq behind our back
2282 * -> ask driver for current freq and notify governors about a change
2283 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002284 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002285 new_policy.cur = cpufreq_update_current_freq(policy);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302286 if (WARN_ON(!new_policy.cur)) {
2287 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002288 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302289 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002290 }
2291
Viresh Kumar037ce832013-10-02 14:13:16 +05302292 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002294unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302295 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002296
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302297 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return ret;
2299}
2300EXPORT_SYMBOL(cpufreq_update_policy);
2301
Paul Gortmaker27609842013-06-19 13:54:04 -04002302static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002303 unsigned long action, void *hcpu)
2304{
2305 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002306
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002307 switch (action & ~CPU_TASKS_FROZEN) {
2308 case CPU_ONLINE:
2309 cpufreq_online(cpu);
2310 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302311
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002312 case CPU_DOWN_PREPARE:
Viresh Kumar69cee712016-02-11 17:31:11 +05302313 cpufreq_offline(cpu);
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002314 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302315
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002316 case CPU_DOWN_FAILED:
2317 cpufreq_online(cpu);
2318 break;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002319 }
2320 return NOTIFY_OK;
2321}
2322
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002323static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302324 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002325};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002328 * BOOST *
2329 *********************************************************************/
2330static int cpufreq_boost_set_sw(int state)
2331{
2332 struct cpufreq_frequency_table *freq_table;
2333 struct cpufreq_policy *policy;
2334 int ret = -EINVAL;
2335
Viresh Kumarf9637352015-05-12 12:20:11 +05302336 for_each_active_policy(policy) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002337 freq_table = cpufreq_frequency_get_table(policy->cpu);
2338 if (freq_table) {
2339 ret = cpufreq_frequency_table_cpuinfo(policy,
2340 freq_table);
2341 if (ret) {
2342 pr_err("%s: Policy frequency update failed\n",
2343 __func__);
2344 break;
2345 }
Viresh Kumar49f18562016-02-11 17:31:12 +05302346
2347 down_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002348 policy->user_policy.max = policy->max;
Viresh Kumara1317e02016-02-22 16:36:43 +05302349 cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Viresh Kumar49f18562016-02-11 17:31:12 +05302350 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002351 }
2352 }
2353
2354 return ret;
2355}
2356
2357int cpufreq_boost_trigger_state(int state)
2358{
2359 unsigned long flags;
2360 int ret = 0;
2361
2362 if (cpufreq_driver->boost_enabled == state)
2363 return 0;
2364
2365 write_lock_irqsave(&cpufreq_driver_lock, flags);
2366 cpufreq_driver->boost_enabled = state;
2367 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2368
2369 ret = cpufreq_driver->set_boost(state);
2370 if (ret) {
2371 write_lock_irqsave(&cpufreq_driver_lock, flags);
2372 cpufreq_driver->boost_enabled = !state;
2373 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2374
Joe Perchese837f9b2014-03-11 10:03:00 -07002375 pr_err("%s: Cannot %s BOOST\n",
2376 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002377 }
2378
2379 return ret;
2380}
2381
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002382static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002383{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002384 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002385}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002386
Viresh Kumar44139ed2015-07-29 16:23:09 +05302387static int create_boost_sysfs_file(void)
2388{
2389 int ret;
2390
Viresh Kumarc82bd442015-10-15 21:35:23 +05302391 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302392 if (ret)
2393 pr_err("%s: cannot register global BOOST sysfs file\n",
2394 __func__);
2395
2396 return ret;
2397}
2398
2399static void remove_boost_sysfs_file(void)
2400{
2401 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302402 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302403}
2404
2405int cpufreq_enable_boost_support(void)
2406{
2407 if (!cpufreq_driver)
2408 return -EINVAL;
2409
2410 if (cpufreq_boost_supported())
2411 return 0;
2412
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002413 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302414
2415 /* This will get removed on driver unregister */
2416 return create_boost_sysfs_file();
2417}
2418EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2419
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002420int cpufreq_boost_enabled(void)
2421{
2422 return cpufreq_driver->boost_enabled;
2423}
2424EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2425
2426/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2428 *********************************************************************/
2429
2430/**
2431 * cpufreq_register_driver - register a CPU Frequency driver
2432 * @driver_data: A struct cpufreq_driver containing the values#
2433 * submitted by the CPU Frequency driver.
2434 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302435 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002436 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002437 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 *
2439 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002440int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
2442 unsigned long flags;
2443 int ret;
2444
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002445 if (cpufreq_disabled())
2446 return -ENODEV;
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302449 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002450 driver_data->target) ||
2451 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302452 driver_data->target)) ||
2453 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return -EINVAL;
2455
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002456 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002458 /* Protect against concurrent CPU online/offline. */
2459 get_online_cpus();
2460
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002461 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002462 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002463 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002464 ret = -EEXIST;
2465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002467 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002468 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302470 if (driver_data->setpolicy)
2471 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2472
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002473 if (cpufreq_boost_supported()) {
2474 ret = create_boost_sysfs_file();
2475 if (ret)
2476 goto err_null_driver;
2477 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002478
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002479 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002480 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002481 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302483 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2484 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302486 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2487 driver_data->name);
2488 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002491 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002492 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002494out:
2495 put_online_cpus();
2496 return ret;
2497
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002498err_if_unreg:
2499 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002500err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302501 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002502err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002503 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002504 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002505 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2509
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510/**
2511 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2512 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302513 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 * the right to do so, i.e. if you have succeeded in initialising before!
2515 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2516 * currently not initialised.
2517 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002518int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 unsigned long flags;
2521
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002522 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002525 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002527 /* Protect against concurrent cpu hotplug */
2528 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002529 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302530 remove_boost_sysfs_file();
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002531 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002533 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302534
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002535 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302536
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002537 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002538 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 return 0;
2541}
2542EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002543
Doug Anderson90de2a42014-12-23 22:09:48 -08002544/*
2545 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2546 * or mutexes when secondary CPUs are halted.
2547 */
2548static struct syscore_ops cpufreq_syscore_ops = {
2549 .shutdown = cpufreq_suspend,
2550};
2551
Viresh Kumarc82bd442015-10-15 21:35:23 +05302552struct kobject *cpufreq_global_kobject;
2553EXPORT_SYMBOL(cpufreq_global_kobject);
2554
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002555static int __init cpufreq_core_init(void)
2556{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002557 if (cpufreq_disabled())
2558 return -ENODEV;
2559
Viresh Kumar8eec1022015-10-15 21:35:22 +05302560 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002561 BUG_ON(!cpufreq_global_kobject);
2562
Doug Anderson90de2a42014-12-23 22:09:48 -08002563 register_syscore_ops(&cpufreq_syscore_ops);
2564
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002565 return 0;
2566}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002567core_initcall(cpufreq_core_init);