blob: 6eca12ab71d7a5efa3b89dc2032e647ec0b3bc84 [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
41static bool suitable_policy(struct cpufreq_policy *policy, bool active)
42{
43 return active == !policy_is_inactive(policy);
44}
45
46/* Finds Next Acive/Inactive policy */
47static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy,
48 bool active)
49{
50 do {
Viresh Kumarf9637352015-05-12 12:20:11 +053051 /* No more policies in the list */
Gautham R Shenoy2dadfd72016-01-27 12:02:26 +053052 if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
Viresh Kumarf9637352015-05-12 12:20:11 +053053 return NULL;
Gautham R Shenoy2dadfd72016-01-27 12:02:26 +053054
55 policy = list_next_entry(policy, policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053056 } while (!suitable_policy(policy, active));
57
58 return policy;
59}
60
61static struct cpufreq_policy *first_policy(bool active)
62{
63 struct cpufreq_policy *policy;
64
65 /* No policies in the list */
66 if (list_empty(&cpufreq_policy_list))
67 return NULL;
68
69 policy = list_first_entry(&cpufreq_policy_list, typeof(*policy),
70 policy_list);
71
72 if (!suitable_policy(policy, active))
73 policy = next_policy(policy, active);
74
75 return policy;
76}
77
78/* Macros to iterate over CPU policies */
79#define for_each_suitable_policy(__policy, __active) \
80 for (__policy = first_policy(__active); \
81 __policy; \
82 __policy = next_policy(__policy, __active))
83
84#define for_each_active_policy(__policy) \
85 for_each_suitable_policy(__policy, true)
86#define for_each_inactive_policy(__policy) \
87 for_each_suitable_policy(__policy, false)
88
89#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053090 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
91
Viresh Kumarf7b27062015-01-27 14:06:09 +053092/* Iterate over governors */
93static LIST_HEAD(cpufreq_governor_list);
94#define for_each_governor(__governor) \
95 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/**
Dave Jonescd878472006-08-11 17:59:28 -040098 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * level driver of CPUFreq support, and its spinlock. This lock
100 * also protects the cpufreq_cpu_data array.
101 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200102static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -0700103static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +0530104static DEFINE_RWLOCK(cpufreq_driver_lock);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100105
106static DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);
107
108/**
109 * cpufreq_set_update_util_data - Populate the CPU's update_util_data pointer.
110 * @cpu: The CPU to set the pointer for.
111 * @data: New pointer value.
112 *
113 * Set and publish the update_util_data pointer for the given CPU. That pointer
114 * points to a struct update_util_data object containing a callback function
115 * to call from cpufreq_update_util(). That function will be called from an RCU
116 * read-side critical section, so it must not sleep.
117 *
Rafael J. Wysocki08f511f2016-03-04 03:58:22 +0100118 * Callers must use RCU-sched callbacks to free any memory that might be
119 * accessed via the old update_util_data pointer or invoke synchronize_sched()
120 * right after this function to avoid use-after-free.
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100121 */
122void cpufreq_set_update_util_data(int cpu, struct update_util_data *data)
123{
Rafael J. Wysocki08f511f2016-03-04 03:58:22 +0100124 if (WARN_ON(data && !data->func))
125 return;
126
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100127 rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
128}
129EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data);
130
131/**
132 * cpufreq_update_util - Take a note about CPU utilization changes.
133 * @time: Current time.
134 * @util: Current utilization.
135 * @max: Utilization ceiling.
136 *
137 * This function is called by the scheduler on every invocation of
138 * update_load_avg() on the CPU whose utilization is being updated.
Rafael J. Wysocki08f511f2016-03-04 03:58:22 +0100139 *
140 * It can only be called from RCU-sched read-side critical sections.
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100141 */
142void cpufreq_update_util(u64 time, unsigned long util, unsigned long max)
143{
144 struct update_util_data *data;
145
Rafael J. Wysocki08f511f2016-03-04 03:58:22 +0100146#ifdef CONFIG_LOCKDEP
147 WARN_ON(debug_locks && !rcu_read_lock_sched_held());
148#endif
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100149
Rafael J. Wysocki08f511f2016-03-04 03:58:22 +0100150 data = rcu_dereference_sched(*this_cpu_ptr(&cpufreq_update_util_data));
151 /*
152 * If this isn't inside of an RCU-sched read-side critical section, data
153 * may become NULL after the check below.
154 */
155 if (data)
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100156 data->func(data, time, util, max);
Rafael J. Wysocki34e2c552016-02-15 20:20:42 +0100157}
158
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800159/* Flag to suspend/resume CPUFreq governors */
160static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530162static inline bool has_target(void)
163{
164 return cpufreq_driver->target_index || cpufreq_driver->target;
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/* internal prototypes */
Viresh Kumara1317e02016-02-22 16:36:43 +0530168static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
Viresh Kumard92d50a2015-01-02 12:34:29 +0530169static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500172 * Two notifier lists: the "policy" list is involved in the
173 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * "transition" list for kernel code that needs to handle
175 * changes to devices when the CPU clock speed changes.
176 * The mutex locks both lists.
177 */
Alan Sterne041c682006-03-27 01:16:30 -0800178static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700179static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200181static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700182static int __init init_cpufreq_transition_notifier_list(void)
183{
184 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200185 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700186 return 0;
187}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800188pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400190static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200191static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400192{
193 return off;
194}
195void disable_cpufreq(void)
196{
197 off = 1;
198}
Dave Jones29464f22009-01-18 01:37:11 -0500199static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000201bool have_governor_per_policy(void)
202{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530203 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000204}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000205EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000206
Viresh Kumar944e9a02013-05-16 05:09:57 +0000207struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
208{
209 if (have_governor_per_policy())
210 return &policy->kobj;
211 else
212 return cpufreq_global_kobject;
213}
214EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
215
Viresh Kumar5a31d592015-07-10 01:43:27 +0200216struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
217{
218 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
219
220 return policy && !policy_is_inactive(policy) ?
221 policy->freq_table : NULL;
222}
223EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
224
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000225static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
226{
227 u64 idle_time;
228 u64 cur_wall_time;
229 u64 busy_time;
230
231 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
232
233 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
234 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
235 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
236 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
237 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
238 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
239
240 idle_time = cur_wall_time - busy_time;
241 if (wall)
242 *wall = cputime_to_usecs(cur_wall_time);
243
244 return cputime_to_usecs(idle_time);
245}
246
247u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
248{
249 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
250
251 if (idle_time == -1ULL)
252 return get_cpu_idle_time_jiffy(cpu, wall);
253 else if (!io_busy)
254 idle_time += get_cpu_iowait_time_us(cpu, wall);
255
256 return idle_time;
257}
258EXPORT_SYMBOL_GPL(get_cpu_idle_time);
259
Viresh Kumar70e9e772013-10-03 20:29:07 +0530260/*
261 * This is a generic cpufreq init() routine which can be used by cpufreq
262 * drivers of SMP systems. It will do following:
263 * - validate & show freq table passed
264 * - set policies transition latency
265 * - policy->cpus with all possible CPUs
266 */
267int cpufreq_generic_init(struct cpufreq_policy *policy,
268 struct cpufreq_frequency_table *table,
269 unsigned int transition_latency)
270{
271 int ret;
272
273 ret = cpufreq_table_validate_and_show(policy, table);
274 if (ret) {
275 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
276 return ret;
277 }
278
279 policy->cpuinfo.transition_latency = transition_latency;
280
281 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530282 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530283 * share the clock and voltage and clock.
284 */
285 cpumask_setall(policy->cpus);
286
287 return 0;
288}
289EXPORT_SYMBOL_GPL(cpufreq_generic_init);
290
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200291struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530292{
293 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
294
Viresh Kumar988bed02015-05-08 11:53:45 +0530295 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
296}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200297EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530298
299unsigned int cpufreq_generic_get(unsigned int cpu)
300{
301 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
302
Viresh Kumar652ed952014-01-09 20:38:43 +0530303 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700304 pr_err("%s: No %s associated to cpu: %d\n",
305 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530306 return 0;
307 }
308
309 return clk_get_rate(policy->clk) / 1000;
310}
311EXPORT_SYMBOL_GPL(cpufreq_generic_get);
312
Viresh Kumar50e9c852015-02-19 17:02:03 +0530313/**
314 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
315 *
316 * @cpu: cpu to find policy for.
317 *
318 * This returns policy for 'cpu', returns NULL if it doesn't exist.
319 * It also increments the kobject reference count to mark it busy and so would
320 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
321 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
322 * freed as that depends on the kobj count.
323 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530324 * Return: A valid policy on success, otherwise NULL on failure.
325 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530326struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530328 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned long flags;
330
Viresh Kumar1b947c92015-02-19 17:02:05 +0530331 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530332 return NULL;
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000335 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Viresh Kumar6eed9402013-08-06 22:53:11 +0530337 if (cpufreq_driver) {
338 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530339 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530340 if (policy)
341 kobject_get(&policy->kobj);
342 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200343
Viresh Kumar6eed9402013-08-06 22:53:11 +0530344 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530346 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000347}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
349
Viresh Kumar50e9c852015-02-19 17:02:03 +0530350/**
351 * cpufreq_cpu_put: Decrements the usage count of a policy
352 *
353 * @policy: policy earlier returned by cpufreq_cpu_get().
354 *
355 * This decrements the kobject reference count incremented earlier by calling
356 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530357 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530358void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530360 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
366 *********************************************************************/
367
368/**
369 * adjust_jiffies - adjust the system "loops_per_jiffy"
370 *
371 * This function alters the system "loops_per_jiffy" for the clock
372 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500373 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 * per-CPU loops_per_jiffy value wherever possible.
375 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800376static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530378#ifndef CONFIG_SMP
379 static unsigned long l_p_j_ref;
380 static unsigned int l_p_j_ref_freq;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (ci->flags & CPUFREQ_CONST_LOOPS)
383 return;
384
385 if (!l_p_j_ref_freq) {
386 l_p_j_ref = loops_per_jiffy;
387 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700388 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
389 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530391 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530392 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
393 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700394 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
395 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530400static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530401 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 BUG_ON(irqs_disabled());
404
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000405 if (cpufreq_disabled())
406 return;
407
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200408 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200409 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700410 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500415 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800416 * which is not equal to what the cpufreq core thinks is
417 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200419 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800420 if ((policy) && (policy->cpu == freqs->cpu) &&
421 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700422 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
423 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800424 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700427 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800428 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
430 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 case CPUFREQ_POSTCHANGE:
433 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700434 pr_debug("FREQ: %lu - CPU: %lu\n",
435 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100436 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700437 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800438 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800439 if (likely(policy) && likely(policy->cpu == freqs->cpu))
440 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 break;
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530444
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530445/**
446 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
447 * on frequency transition.
448 *
449 * This function calls the transition notifiers and the "adjust_jiffies"
450 * function. It is called twice on all CPU frequency changes that have
451 * external effects.
452 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530453static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530454 struct cpufreq_freqs *freqs, unsigned int state)
455{
456 for_each_cpu(freqs->cpu, policy->cpus)
457 __cpufreq_notify_transition(policy, freqs, state);
458}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530460/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530461static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530462 struct cpufreq_freqs *freqs, int transition_failed)
463{
464 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
465 if (!transition_failed)
466 return;
467
468 swap(freqs->old, freqs->new);
469 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
470 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
471}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530472
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530473void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
474 struct cpufreq_freqs *freqs)
475{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530476
477 /*
478 * Catch double invocations of _begin() which lead to self-deadlock.
479 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
480 * doesn't invoke _begin() on their behalf, and hence the chances of
481 * double invocations are very low. Moreover, there are scenarios
482 * where these checks can emit false-positive warnings in these
483 * drivers; so we avoid that by skipping them altogether.
484 */
485 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
486 && current == policy->transition_task);
487
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530488wait:
489 wait_event(policy->transition_wait, !policy->transition_ongoing);
490
491 spin_lock(&policy->transition_lock);
492
493 if (unlikely(policy->transition_ongoing)) {
494 spin_unlock(&policy->transition_lock);
495 goto wait;
496 }
497
498 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530499 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530500
501 spin_unlock(&policy->transition_lock);
502
503 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
504}
505EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
506
507void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
508 struct cpufreq_freqs *freqs, int transition_failed)
509{
510 if (unlikely(WARN_ON(!policy->transition_ongoing)))
511 return;
512
513 cpufreq_notify_post_transition(policy, freqs, transition_failed);
514
515 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530516 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530517
518 wake_up(&policy->transition_wait);
519}
520EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*********************************************************************
524 * SYSFS INTERFACE *
525 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530526static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100527 struct attribute *attr, char *buf)
528{
529 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
530}
531
532static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
533 const char *buf, size_t count)
534{
535 int ret, enable;
536
537 ret = sscanf(buf, "%d", &enable);
538 if (ret != 1 || enable < 0 || enable > 1)
539 return -EINVAL;
540
541 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700542 pr_err("%s: Cannot %s BOOST!\n",
543 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100544 return -EINVAL;
545 }
546
Joe Perchese837f9b2014-03-11 10:03:00 -0700547 pr_debug("%s: cpufreq BOOST %s\n",
548 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100549
550 return count;
551}
552define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530554static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700555{
556 struct cpufreq_governor *t;
557
Viresh Kumarf7b27062015-01-27 14:06:09 +0530558 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200559 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700560 return t;
561
562 return NULL;
563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/**
566 * cpufreq_parse_governor - parse a governor string
567 */
Dave Jones905d77c2008-03-05 14:28:32 -0500568static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct cpufreq_governor **governor)
570{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700571 int err = -EINVAL;
572
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200573 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200574 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700576 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200577 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530578 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700580 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530582 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700584
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800585 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700586
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530587 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700588
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700589 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700590 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700591
Kees Cook1a8e1462011-05-04 08:38:56 -0700592 mutex_unlock(&cpufreq_governor_mutex);
593 ret = request_module("cpufreq_%s", str_governor);
594 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700595
Kees Cook1a8e1462011-05-04 08:38:56 -0700596 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530597 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700598 }
599
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700600 if (t != NULL) {
601 *governor = t;
602 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700604
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800605 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700607 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530611 * cpufreq_per_cpu_attr_read() / show_##file_name() -
612 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 *
614 * Write out information from cpufreq_driver->policy[cpu]; object must be
615 * "unsigned int".
616 */
617
Dave Jones32ee8c32006-02-28 00:43:23 -0500618#define show_one(file_name, object) \
619static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500620(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500621{ \
Dave Jones29464f22009-01-18 01:37:11 -0500622 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
625show_one(cpuinfo_min_freq, cpuinfo.min_freq);
626show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100627show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628show_one(scaling_min_freq, min);
629show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700630
Viresh Kumar09347b22015-01-02 12:34:24 +0530631static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700632{
633 ssize_t ret;
634
635 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
636 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
637 else
638 ret = sprintf(buf, "%u\n", policy->cur);
639 return ret;
640}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Viresh Kumar037ce832013-10-02 14:13:16 +0530642static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530643 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645/**
646 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
647 */
648#define store_one(file_name, object) \
649static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500650(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800652 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct cpufreq_policy new_policy; \
654 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530655 memcpy(&new_policy, policy, sizeof(*policy)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 \
Dave Jones29464f22009-01-18 01:37:11 -0500657 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (ret != 1) \
659 return -EINVAL; \
660 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800661 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530662 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800663 if (!ret) \
664 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 \
666 return ret ? ret : count; \
667}
668
Dave Jones29464f22009-01-18 01:37:11 -0500669store_one(scaling_min_freq, min);
670store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672/**
673 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
674 */
Dave Jones905d77c2008-03-05 14:28:32 -0500675static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
676 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530678 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (!cur_freq)
680 return sprintf(buf, "<unknown>");
681 return sprintf(buf, "%u\n", cur_freq);
682}
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684/**
685 * show_scaling_governor - show the current policy for the specified CPU
686 */
Dave Jones905d77c2008-03-05 14:28:32 -0500687static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Dave Jones29464f22009-01-18 01:37:11 -0500689 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return sprintf(buf, "powersave\n");
691 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
692 return sprintf(buf, "performance\n");
693 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200694 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500695 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/**
700 * store_scaling_governor - store policy for the specified CPU
701 */
Dave Jones905d77c2008-03-05 14:28:32 -0500702static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
703 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530705 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 char str_governor[16];
707 struct cpufreq_policy new_policy;
708
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530709 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Dave Jones29464f22009-01-18 01:37:11 -0500711 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (ret != 1)
713 return -EINVAL;
714
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530715 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
716 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return -EINVAL;
718
Viresh Kumar037ce832013-10-02 14:13:16 +0530719 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530720 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/**
724 * show_scaling_driver - show the cpufreq driver currently loaded
725 */
Dave Jones905d77c2008-03-05 14:28:32 -0500726static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200728 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731/**
732 * show_scaling_available_governors - show the available CPUfreq governors
733 */
Dave Jones905d77c2008-03-05 14:28:32 -0500734static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
735 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 ssize_t i = 0;
738 struct cpufreq_governor *t;
739
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530740 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 i += sprintf(buf, "performance powersave");
742 goto out;
743 }
744
Viresh Kumarf7b27062015-01-27 14:06:09 +0530745 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500746 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
747 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200749 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500751out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 i += sprintf(&buf[i], "\n");
753 return i;
754}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700755
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800756ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 ssize_t i = 0;
759 unsigned int cpu;
760
Rusty Russell835481d2009-01-04 05:18:06 -0800761 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (i)
763 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
764 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
765 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
768 i += sprintf(&buf[i], "\n");
769 return i;
770}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800771EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700773/**
774 * show_related_cpus - show the CPUs affected by each transition even if
775 * hw coordination is in use
776 */
777static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
778{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800779 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700780}
781
782/**
783 * show_affected_cpus - show the CPUs affected by each transition
784 */
785static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
786{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800787 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700788}
789
Venki Pallipadi9e769882007-10-26 10:18:21 -0700790static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500791 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700792{
793 unsigned int freq = 0;
794 unsigned int ret;
795
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700796 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700797 return -EINVAL;
798
799 ret = sscanf(buf, "%u", &freq);
800 if (ret != 1)
801 return -EINVAL;
802
803 policy->governor->store_setspeed(policy, freq);
804
805 return count;
806}
807
808static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
809{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700810 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700811 return sprintf(buf, "<unsupported>\n");
812
813 return policy->governor->show_setspeed(policy, buf);
814}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Thomas Renningere2f74f32009-11-19 12:31:01 +0100816/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200817 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100818 */
819static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
820{
821 unsigned int limit;
822 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200823 if (cpufreq_driver->bios_limit) {
824 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100825 if (!ret)
826 return sprintf(buf, "%u\n", limit);
827 }
828 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
829}
830
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200831cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
832cpufreq_freq_attr_ro(cpuinfo_min_freq);
833cpufreq_freq_attr_ro(cpuinfo_max_freq);
834cpufreq_freq_attr_ro(cpuinfo_transition_latency);
835cpufreq_freq_attr_ro(scaling_available_governors);
836cpufreq_freq_attr_ro(scaling_driver);
837cpufreq_freq_attr_ro(scaling_cur_freq);
838cpufreq_freq_attr_ro(bios_limit);
839cpufreq_freq_attr_ro(related_cpus);
840cpufreq_freq_attr_ro(affected_cpus);
841cpufreq_freq_attr_rw(scaling_min_freq);
842cpufreq_freq_attr_rw(scaling_max_freq);
843cpufreq_freq_attr_rw(scaling_governor);
844cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dave Jones905d77c2008-03-05 14:28:32 -0500846static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 &cpuinfo_min_freq.attr,
848 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100849 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 &scaling_min_freq.attr,
851 &scaling_max_freq.attr,
852 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700853 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 &scaling_governor.attr,
855 &scaling_driver.attr,
856 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700857 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 NULL
859};
860
Dave Jones29464f22009-01-18 01:37:11 -0500861#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
862#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Dave Jones29464f22009-01-18 01:37:11 -0500864static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Dave Jones905d77c2008-03-05 14:28:32 -0500866 struct cpufreq_policy *policy = to_policy(kobj);
867 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530868 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530869
viresh kumarad7722d2013-10-18 19:10:15 +0530870 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800871
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530872 if (fattr->show)
873 ret = fattr->show(policy, buf);
874 else
875 ret = -EIO;
876
viresh kumarad7722d2013-10-18 19:10:15 +0530877 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return ret;
880}
881
Dave Jones905d77c2008-03-05 14:28:32 -0500882static ssize_t store(struct kobject *kobj, struct attribute *attr,
883 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Dave Jones905d77c2008-03-05 14:28:32 -0500885 struct cpufreq_policy *policy = to_policy(kobj);
886 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500887 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530888
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530889 get_online_cpus();
890
891 if (!cpu_online(policy->cpu))
892 goto unlock;
893
viresh kumarad7722d2013-10-18 19:10:15 +0530894 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800895
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530896 if (fattr->store)
897 ret = fattr->store(policy, buf, count);
898 else
899 ret = -EIO;
900
viresh kumarad7722d2013-10-18 19:10:15 +0530901 up_write(&policy->rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530902unlock:
903 put_online_cpus();
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return ret;
906}
907
Dave Jones905d77c2008-03-05 14:28:32 -0500908static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Dave Jones905d77c2008-03-05 14:28:32 -0500910 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200911 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 complete(&policy->kobj_unregister);
913}
914
Emese Revfy52cf25d2010-01-19 02:58:23 +0100915static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 .show = show,
917 .store = store,
918};
919
920static struct kobj_type ktype_cpufreq = {
921 .sysfs_ops = &sysfs_ops,
922 .default_attrs = default_attrs,
923 .release = cpufreq_sysfs_release,
924};
925
Viresh Kumar87549142015-06-10 02:13:21 +0200926static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
927{
928 struct device *cpu_dev;
929
930 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
931
932 if (!policy)
933 return 0;
934
935 cpu_dev = get_cpu_device(cpu);
936 if (WARN_ON(!cpu_dev))
937 return 0;
938
939 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
940}
941
942static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
943{
944 struct device *cpu_dev;
945
946 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
947
948 cpu_dev = get_cpu_device(cpu);
949 if (WARN_ON(!cpu_dev))
950 return;
951
952 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
953}
954
955/* Add/remove symlinks for all related CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200956static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400957{
958 unsigned int j;
959 int ret = 0;
960
Viresh Kumar87549142015-06-10 02:13:21 +0200961 /* Some related CPUs might not be present (physically hotplugged) */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +0200962 for_each_cpu(j, policy->real_cpus) {
Viresh Kumar87549142015-06-10 02:13:21 +0200963 ret = add_cpu_dev_symlink(policy, j);
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200964 if (ret)
965 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400966 }
Viresh Kumar87549142015-06-10 02:13:21 +0200967
Dave Jones19d6f7e2009-07-08 17:35:39 -0400968 return ret;
969}
970
Viresh Kumar87549142015-06-10 02:13:21 +0200971static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
972{
973 unsigned int j;
974
975 /* Some related CPUs might not be present (physically hotplugged) */
Viresh Kumar96bdda62015-10-15 21:35:24 +0530976 for_each_cpu(j, policy->real_cpus)
Viresh Kumar87549142015-06-10 02:13:21 +0200977 remove_cpu_dev_symlink(policy, j);
Viresh Kumar87549142015-06-10 02:13:21 +0200978}
979
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200980static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400981{
982 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400983 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400984
Dave Jones909a6942009-07-08 18:05:42 -0400985 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200986 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530987 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400988 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
989 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100990 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400991 drv_attr++;
992 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200993 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400994 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
995 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100996 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400997 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700998
999 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1000 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001001 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001002
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001003 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001004 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1005 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001006 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001007 }
Dave Jones909a6942009-07-08 18:05:42 -04001008
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001009 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301010}
1011
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001012__weak struct cpufreq_governor *cpufreq_default_governor(void)
1013{
1014 return NULL;
1015}
1016
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301017static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301018{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001019 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301020 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301021
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301022 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001023
viresh kumar6e2c89d2014-03-04 11:43:59 +08001024 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +05301025 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001026 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +08001027 pr_debug("Restoring governor %s for cpu %d\n",
1028 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001029 } else {
1030 gov = cpufreq_default_governor();
1031 if (!gov)
1032 return -ENODATA;
1033 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001034
1035 new_policy.governor = gov;
1036
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001037 /* Use the default policy if there is no last_policy. */
1038 if (cpufreq_driver->setpolicy) {
1039 if (policy->last_policy)
1040 new_policy.policy = policy->last_policy;
1041 else
1042 cpufreq_parse_governor(gov->name, &new_policy.policy,
1043 NULL);
1044 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001045 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301046 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001047}
1048
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001049static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001050{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301051 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001052
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301053 /* Has this CPU been taken care of already? */
1054 if (cpumask_test_cpu(cpu, policy->cpus))
1055 return 0;
1056
Viresh Kumar49f18562016-02-11 17:31:12 +05301057 down_write(&policy->rwsem);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301058 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301059 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301060 if (ret) {
1061 pr_err("%s: Failed to stop governor\n", __func__);
Viresh Kumar49f18562016-02-11 17:31:12 +05301062 goto unlock;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301063 }
1064 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001065
Viresh Kumarfcf80582013-01-29 14:39:08 +00001066 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301067
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301068 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301069 ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02001070 if (!ret)
Viresh Kumara1317e02016-02-22 16:36:43 +05301071 ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02001072
Viresh Kumar49f18562016-02-11 17:31:12 +05301073 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301074 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001075 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001076
Viresh Kumar49f18562016-02-11 17:31:12 +05301077unlock:
1078 up_write(&policy->rwsem);
1079 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301082static void handle_update(struct work_struct *work)
1083{
1084 struct cpufreq_policy *policy =
1085 container_of(work, struct cpufreq_policy, update);
1086 unsigned int cpu = policy->cpu;
1087 pr_debug("handle_update for cpu %u called\n", cpu);
1088 cpufreq_update_policy(cpu);
1089}
1090
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001091static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301092{
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001093 struct device *dev = get_cpu_device(cpu);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301094 struct cpufreq_policy *policy;
1095
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001096 if (WARN_ON(!dev))
1097 return NULL;
1098
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301099 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1100 if (!policy)
1101 return NULL;
1102
1103 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1104 goto err_free_policy;
1105
1106 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1107 goto err_free_cpumask;
1108
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001109 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1110 goto err_free_rcpumask;
1111
Viresh Kumar3510fac2015-10-16 12:41:12 +05301112 kobject_init(&policy->kobj, &ktype_cpufreq);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301113 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301114 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301115 spin_lock_init(&policy->transition_lock);
1116 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301117 init_completion(&policy->kobj_unregister);
1118 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301119
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001120 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301121 return policy;
1122
Viresh Kumar2fc33842015-06-08 18:25:29 +05301123err_free_rcpumask:
1124 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301125err_free_cpumask:
1126 free_cpumask_var(policy->cpus);
1127err_free_policy:
1128 kfree(policy);
1129
1130 return NULL;
1131}
1132
Viresh Kumar2fc33842015-06-08 18:25:29 +05301133static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301134{
1135 struct kobject *kobj;
1136 struct completion *cmp;
1137
Viresh Kumar2fc33842015-06-08 18:25:29 +05301138 if (notify)
1139 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1140 CPUFREQ_REMOVE_POLICY, policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301141
Viresh Kumar87549142015-06-10 02:13:21 +02001142 down_write(&policy->rwsem);
1143 cpufreq_remove_dev_symlink(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301144 kobj = &policy->kobj;
1145 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001146 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301147 kobject_put(kobj);
1148
1149 /*
1150 * We need to make sure that the underlying kobj is
1151 * actually not referenced anymore by anybody before we
1152 * proceed with unloading.
1153 */
1154 pr_debug("waiting for dropping of refcount\n");
1155 wait_for_completion(cmp);
1156 pr_debug("wait complete\n");
1157}
1158
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301159static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301160{
Viresh Kumar988bed02015-05-08 11:53:45 +05301161 unsigned long flags;
1162 int cpu;
1163
1164 /* Remove policy from list */
1165 write_lock_irqsave(&cpufreq_driver_lock, flags);
1166 list_del(&policy->policy_list);
1167
1168 for_each_cpu(cpu, policy->related_cpus)
1169 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1170 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1171
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301172 cpufreq_policy_put_kobj(policy, notify);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001173 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301174 free_cpumask_var(policy->related_cpus);
1175 free_cpumask_var(policy->cpus);
1176 kfree(policy);
1177}
1178
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001179static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301181 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001182 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001184 unsigned int j;
1185 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001186
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001187 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301188
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301189 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301190 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001191 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301192 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001193 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001194 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001196 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001197 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001198 down_write(&policy->rwsem);
1199 policy->cpu = cpu;
1200 policy->governor = NULL;
1201 up_write(&policy->rwsem);
1202 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001203 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001204 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001205 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001206 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001207 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301208
Rusty Russell835481d2009-01-04 05:18:06 -08001209 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /* call driver. From then on the cpufreq must be able
1212 * to accept all calls to ->verify and ->setpolicy for this CPU
1213 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001214 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001216 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301217 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001219
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001220 down_write(&policy->rwsem);
1221
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001222 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001223 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301224 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001225 /* Remember CPUs present at the policy creation time. */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001226 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
Viresh Kumar3510fac2015-10-16 12:41:12 +05301227
1228 /* Name and add the kobject */
1229 ret = kobject_add(&policy->kobj, cpufreq_global_kobject,
1230 "policy%u",
1231 cpumask_first(policy->related_cpus));
1232 if (ret) {
1233 pr_err("%s: failed to add policy->kobj: %d\n", __func__,
1234 ret);
1235 goto out_exit_policy;
1236 }
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001237 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001238
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001239 /*
1240 * affected cpus must always be the one, which are online. We aren't
1241 * managing offline cpus here.
1242 */
1243 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1244
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001245 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001246 policy->user_policy.min = policy->min;
1247 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001248
Viresh Kumar988bed02015-05-08 11:53:45 +05301249 write_lock_irqsave(&cpufreq_driver_lock, flags);
1250 for_each_cpu(j, policy->related_cpus)
1251 per_cpu(cpufreq_cpu_data, j) = policy;
1252 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1253 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301254
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001255 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301256 policy->cur = cpufreq_driver->get(policy->cpu);
1257 if (!policy->cur) {
1258 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301259 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301260 }
1261 }
1262
Viresh Kumard3916692013-12-03 11:20:46 +05301263 /*
1264 * Sometimes boot loaders set CPU frequency to a value outside of
1265 * frequency table present with cpufreq core. In such cases CPU might be
1266 * unstable if it has to run on that frequency for long duration of time
1267 * and so its better to set it to a frequency which is specified in
1268 * freq-table. This also makes cpufreq stats inconsistent as
1269 * cpufreq-stats would fail to register because current frequency of CPU
1270 * isn't found in freq-table.
1271 *
1272 * Because we don't want this change to effect boot process badly, we go
1273 * for the next freq which is >= policy->cur ('cur' must be set by now,
1274 * otherwise we will end up setting freq to lowest of the table as 'cur'
1275 * is initialized to zero).
1276 *
1277 * We are passing target-freq as "policy->cur - 1" otherwise
1278 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1279 * equal to target-freq.
1280 */
1281 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1282 && has_target()) {
1283 /* Are we running at unknown frequency ? */
1284 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1285 if (ret == -EINVAL) {
1286 /* Warn user and fix it */
1287 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1288 __func__, policy->cpu, policy->cur);
1289 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1290 CPUFREQ_RELATION_L);
1291
1292 /*
1293 * Reaching here after boot in a few seconds may not
1294 * mean that system will remain stable at "unknown"
1295 * frequency for longer duration. Hence, a BUG_ON().
1296 */
1297 BUG_ON(ret);
1298 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1299 __func__, policy->cpu, policy->cur);
1300 }
1301 }
1302
Thomas Renningera1531ac2008-07-29 22:32:58 -07001303 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1304 CPUFREQ_START, policy);
1305
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001306 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001307 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301308 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301309 goto out_exit_policy;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301310 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1311 CPUFREQ_CREATE_POLICY, policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001312
Viresh Kumar988bed02015-05-08 11:53:45 +05301313 write_lock_irqsave(&cpufreq_driver_lock, flags);
1314 list_add(&policy->policy_list, &cpufreq_policy_list);
1315 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1316 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301317
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301318 ret = cpufreq_init_policy(policy);
1319 if (ret) {
1320 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1321 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001322 /* cpufreq_policy_free() will notify based on this */
1323 new_policy = false;
1324 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301325 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301326
Viresh Kumar4e97b632014-03-04 11:44:01 +08001327 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301328
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001329 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301330
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301331 /* Callback for handling stuff after policy is ready */
1332 if (cpufreq_driver->ready)
1333 cpufreq_driver->ready(policy);
1334
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001335 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return 0;
1338
Viresh Kumar8101f992015-07-08 15:12:15 +05301339out_exit_policy:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001340 up_write(&policy->rwsem);
1341
Viresh Kumarda60ce92013-10-03 20:28:30 +05301342 if (cpufreq_driver->exit)
1343 cpufreq_driver->exit(policy);
Viresh Kumar8101f992015-07-08 15:12:15 +05301344out_free_policy:
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001345 cpufreq_policy_free(policy, !new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return ret;
1347}
1348
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001349/**
1350 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1351 * @dev: CPU device.
1352 * @sif: Subsystem interface structure pointer (not used)
1353 */
1354static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1355{
1356 unsigned cpu = dev->id;
1357 int ret;
1358
1359 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1360
1361 if (cpu_online(cpu)) {
1362 ret = cpufreq_online(cpu);
1363 } else {
1364 /*
1365 * A hotplug notifier will follow and we will handle it as CPU
1366 * online then. For now, just create the sysfs link, unless
1367 * there is no policy or the link is already present.
1368 */
1369 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1370
1371 ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus)
1372 ? add_cpu_dev_symlink(policy, cpu) : 0;
1373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 return ret;
1376}
1377
Viresh Kumar69cee712016-02-11 17:31:11 +05301378static void cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301380 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301381 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001383 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Viresh Kumar988bed02015-05-08 11:53:45 +05301385 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301386 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001387 pr_debug("%s: No cpu_data found\n", __func__);
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001388 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Viresh Kumar49f18562016-02-11 17:31:12 +05301391 down_write(&policy->rwsem);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301392 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301393 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001394 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301395 pr_err("%s: Failed to stop governor\n", __func__);
Viresh Kumardb5f2992015-01-02 12:34:25 +05301396 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001397
Viresh Kumar9591bec2015-06-10 02:20:23 +02001398 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301399
Viresh Kumar9591bec2015-06-10 02:20:23 +02001400 if (policy_is_inactive(policy)) {
1401 if (has_target())
1402 strncpy(policy->last_governor, policy->governor->name,
1403 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001404 else
1405 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001406 } else if (cpu == policy->cpu) {
1407 /* Nominate new CPU */
1408 policy->cpu = cpumask_any(policy->cpus);
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Viresh Kumar9591bec2015-06-10 02:20:23 +02001411 /* Start governor again for active policy */
1412 if (!policy_is_inactive(policy)) {
1413 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301414 ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001415 if (!ret)
Viresh Kumara1317e02016-02-22 16:36:43 +05301416 ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Viresh Kumar87549142015-06-10 02:13:21 +02001417
Viresh Kumar9591bec2015-06-10 02:20:23 +02001418 if (ret)
1419 pr_err("%s: Failed to start governor\n", __func__);
1420 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301421
Viresh Kumar49f18562016-02-11 17:31:12 +05301422 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301423 }
1424
1425 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001426 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Viresh Kumar87549142015-06-10 02:13:21 +02001428 /* If cpu is last user of policy, free policy */
1429 if (has_target()) {
Viresh Kumara1317e02016-02-22 16:36:43 +05301430 ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001431 if (ret)
Viresh Kumar87549142015-06-10 02:13:21 +02001432 pr_err("%s: Failed to exit governor\n", __func__);
Viresh Kumar87549142015-06-10 02:13:21 +02001433 }
1434
Viresh Kumar87549142015-06-10 02:13:21 +02001435 /*
1436 * Perform the ->exit() even during light-weight tear-down,
1437 * since this is a core component, and is essential for the
1438 * subsequent light-weight ->init() to succeed.
1439 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001440 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001441 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001442 policy->freq_table = NULL;
1443 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301444
1445unlock:
1446 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301449/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301450 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301451 *
1452 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301453 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301454static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001455{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001456 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001457 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001458
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001459 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001460 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001461
Viresh Kumar69cee712016-02-11 17:31:11 +05301462 if (cpu_online(cpu))
1463 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001464
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001465 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar96bdda62015-10-15 21:35:24 +05301466 remove_cpu_dev_symlink(policy, cpu);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301467
1468 if (cpumask_empty(policy->real_cpus))
1469 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301473 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1474 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301475 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * @new_freq: CPU frequency the CPU actually runs at
1477 *
Dave Jones29464f22009-01-18 01:37:11 -05001478 * We adjust to current frequency first, and need to clean up later.
1479 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301481static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301482 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301485
Joe Perchese837f9b2014-03-11 10:03:00 -07001486 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 +05301487 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301489 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301491
Viresh Kumar8fec0512014-03-24 13:35:45 +05301492 cpufreq_freq_transition_begin(policy, &freqs);
1493 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Dave Jones32ee8c32006-02-28 00:43:23 -05001496/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301497 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001498 * @cpu: CPU number
1499 *
1500 * This is the last known freq, without actually getting it from the driver.
1501 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1502 */
1503unsigned int cpufreq_quick_get(unsigned int cpu)
1504{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001505 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301506 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001507
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001508 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1509 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001510
1511 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001512 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301513 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001514 cpufreq_cpu_put(policy);
1515 }
1516
Dave Jones4d34a672008-02-07 16:33:49 -05001517 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001518}
1519EXPORT_SYMBOL(cpufreq_quick_get);
1520
Jesse Barnes3d737102011-06-28 10:59:12 -07001521/**
1522 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1523 * @cpu: CPU number
1524 *
1525 * Just return the max possible frequency for a given CPU.
1526 */
1527unsigned int cpufreq_quick_get_max(unsigned int cpu)
1528{
1529 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1530 unsigned int ret_freq = 0;
1531
1532 if (policy) {
1533 ret_freq = policy->max;
1534 cpufreq_cpu_put(policy);
1535 }
1536
1537 return ret_freq;
1538}
1539EXPORT_SYMBOL(cpufreq_quick_get_max);
1540
Viresh Kumard92d50a2015-01-02 12:34:29 +05301541static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301543 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001545 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001546 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Viresh Kumard92d50a2015-01-02 12:34:29 +05301548 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Viresh Kumar11e584c2015-06-10 02:11:45 +02001550 /* Updating inactive policies is invalid, so avoid doing that. */
1551 if (unlikely(policy_is_inactive(policy)))
1552 return ret_freq;
1553
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301554 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001555 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301556 /* verify no discrepancy between actual and
1557 saved value exists */
1558 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301559 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 schedule_work(&policy->update);
1561 }
1562 }
1563
Dave Jones4d34a672008-02-07 16:33:49 -05001564 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001567/**
1568 * cpufreq_get - get the current CPU frequency (in kHz)
1569 * @cpu: CPU number
1570 *
1571 * Get the CPU current (static) CPU frequency
1572 */
1573unsigned int cpufreq_get(unsigned int cpu)
1574{
Aaron Plattner999976e2014-03-04 12:42:15 -08001575 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001576 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001577
Aaron Plattner999976e2014-03-04 12:42:15 -08001578 if (policy) {
1579 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301580 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001581 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301582
Aaron Plattner999976e2014-03-04 12:42:15 -08001583 cpufreq_cpu_put(policy);
1584 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301585
Dave Jones4d34a672008-02-07 16:33:49 -05001586 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588EXPORT_SYMBOL(cpufreq_get);
1589
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001590static struct subsys_interface cpufreq_interface = {
1591 .name = "cpufreq",
1592 .subsys = &cpu_subsys,
1593 .add_dev = cpufreq_add_dev,
1594 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001595};
1596
Viresh Kumare28867e2014-03-04 11:00:27 +08001597/*
1598 * In case platform wants some specific frequency to be configured
1599 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001600 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001601int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001602{
Viresh Kumare28867e2014-03-04 11:00:27 +08001603 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001604
Viresh Kumare28867e2014-03-04 11:00:27 +08001605 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001606 pr_debug("%s: suspend_freq not defined\n", __func__);
1607 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001608 }
1609
Viresh Kumare28867e2014-03-04 11:00:27 +08001610 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1611 policy->suspend_freq);
1612
1613 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1614 CPUFREQ_RELATION_H);
1615 if (ret)
1616 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1617 __func__, policy->suspend_freq, ret);
1618
Dave Jonesc9060492008-02-07 16:32:18 -05001619 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001620}
Viresh Kumare28867e2014-03-04 11:00:27 +08001621EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001622
1623/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001624 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001626 * Called during system wide Suspend/Hibernate cycles for suspending governors
1627 * as some platforms can't change frequency after this point in suspend cycle.
1628 * Because some of the devices (like: i2c, regulators, etc) they use for
1629 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001631void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301633 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301634 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001636 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001639 if (!has_target())
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301640 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001642 pr_debug("%s: Suspending Governors\n", __func__);
1643
Viresh Kumarf9637352015-05-12 12:20:11 +05301644 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301645 down_write(&policy->rwsem);
Viresh Kumara1317e02016-02-22 16:36:43 +05301646 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumar49f18562016-02-11 17:31:12 +05301647 up_write(&policy->rwsem);
1648
1649 if (ret)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001650 pr_err("%s: Failed to stop governor for policy: %p\n",
1651 __func__, policy);
1652 else if (cpufreq_driver->suspend
1653 && cpufreq_driver->suspend(policy))
1654 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1655 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301657
1658suspend:
1659 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001663 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001665 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1666 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001668void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301671 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001673 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return;
1675
Lan Tianyu8e304442014-09-18 15:03:07 +08001676 cpufreq_suspended = false;
1677
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001678 if (!has_target())
1679 return;
1680
1681 pr_debug("%s: Resuming Governors\n", __func__);
1682
Viresh Kumarf9637352015-05-12 12:20:11 +05301683 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301684 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301685 pr_err("%s: Failed to resume driver: %p\n", __func__,
1686 policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301687 } else {
1688 down_write(&policy->rwsem);
Viresh Kumara1317e02016-02-22 16:36:43 +05301689 ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
Viresh Kumar49f18562016-02-11 17:31:12 +05301690 if (!ret)
Viresh Kumara1317e02016-02-22 16:36:43 +05301691 cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Viresh Kumar49f18562016-02-11 17:31:12 +05301692 up_write(&policy->rwsem);
1693
1694 if (ret)
1695 pr_err("%s: Failed to start governor for policy: %p\n",
1696 __func__, policy);
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Viresh Kumarc75de0a2015-04-02 10:21:33 +05301699
1700 /*
1701 * schedule call cpufreq_update_policy() for first-online CPU, as that
1702 * wouldn't be hotplugged-out on suspend. It will verify that the
1703 * current freq is in sync with what we believe it to be.
1704 */
1705 policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1706 if (WARN_ON(!policy))
1707 return;
1708
1709 schedule_work(&policy->update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Borislav Petkov9d950462013-01-20 10:24:28 +00001712/**
1713 * cpufreq_get_current_driver - return current driver's name
1714 *
1715 * Return the name string of the currently loaded cpufreq driver
1716 * or NULL, if none.
1717 */
1718const char *cpufreq_get_current_driver(void)
1719{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001720 if (cpufreq_driver)
1721 return cpufreq_driver->name;
1722
1723 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001724}
1725EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001727/**
1728 * cpufreq_get_driver_data - return current driver data
1729 *
1730 * Return the private data of the currently loaded cpufreq
1731 * driver, or NULL if no cpufreq driver is loaded.
1732 */
1733void *cpufreq_get_driver_data(void)
1734{
1735 if (cpufreq_driver)
1736 return cpufreq_driver->driver_data;
1737
1738 return NULL;
1739}
1740EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/*********************************************************************
1743 * NOTIFIER LISTS INTERFACE *
1744 *********************************************************************/
1745
1746/**
1747 * cpufreq_register_notifier - register a driver with cpufreq
1748 * @nb: notifier function to register
1749 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1750 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001751 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * are notified about clock rate changes (once before and once after
1753 * the transition), or a list of drivers that are notified about
1754 * changes in cpufreq policy.
1755 *
1756 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001757 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
1759int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1760{
1761 int ret;
1762
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001763 if (cpufreq_disabled())
1764 return -EINVAL;
1765
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001766 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 switch (list) {
1769 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001770 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001771 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 break;
1773 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001774 ret = blocking_notifier_chain_register(
1775 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777 default:
1778 ret = -EINVAL;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 return ret;
1782}
1783EXPORT_SYMBOL(cpufreq_register_notifier);
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/**
1786 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1787 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301788 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 *
1790 * Remove a driver from the CPU frequency notifier list.
1791 *
1792 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001793 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
1795int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1796{
1797 int ret;
1798
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001799 if (cpufreq_disabled())
1800 return -EINVAL;
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 switch (list) {
1803 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001804 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001805 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001808 ret = blocking_notifier_chain_unregister(
1809 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811 default:
1812 ret = -EINVAL;
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 return ret;
1816}
1817EXPORT_SYMBOL(cpufreq_unregister_notifier);
1818
1819
1820/*********************************************************************
1821 * GOVERNORS *
1822 *********************************************************************/
1823
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301824/* Must set freqs->new to intermediate frequency */
1825static int __target_intermediate(struct cpufreq_policy *policy,
1826 struct cpufreq_freqs *freqs, int index)
1827{
1828 int ret;
1829
1830 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1831
1832 /* We don't need to switch to intermediate freq */
1833 if (!freqs->new)
1834 return 0;
1835
1836 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1837 __func__, policy->cpu, freqs->old, freqs->new);
1838
1839 cpufreq_freq_transition_begin(policy, freqs);
1840 ret = cpufreq_driver->target_intermediate(policy, index);
1841 cpufreq_freq_transition_end(policy, freqs, ret);
1842
1843 if (ret)
1844 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1845 __func__, ret);
1846
1847 return ret;
1848}
1849
Viresh Kumar8d657752014-05-21 14:29:29 +05301850static int __target_index(struct cpufreq_policy *policy,
1851 struct cpufreq_frequency_table *freq_table, int index)
1852{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301853 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1854 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301855 int retval = -EINVAL;
1856 bool notify;
1857
1858 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301859 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301860 /* Handle switching to intermediate frequency */
1861 if (cpufreq_driver->get_intermediate) {
1862 retval = __target_intermediate(policy, &freqs, index);
1863 if (retval)
1864 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301865
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301866 intermediate_freq = freqs.new;
1867 /* Set old freq to intermediate */
1868 if (intermediate_freq)
1869 freqs.old = freqs.new;
1870 }
1871
1872 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301873 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1874 __func__, policy->cpu, freqs.old, freqs.new);
1875
1876 cpufreq_freq_transition_begin(policy, &freqs);
1877 }
1878
1879 retval = cpufreq_driver->target_index(policy, index);
1880 if (retval)
1881 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1882 retval);
1883
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301884 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301885 cpufreq_freq_transition_end(policy, &freqs, retval);
1886
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301887 /*
1888 * Failed after setting to intermediate freq? Driver should have
1889 * reverted back to initial frequency and so should we. Check
1890 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301891 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301892 */
1893 if (unlikely(retval && intermediate_freq)) {
1894 freqs.old = intermediate_freq;
1895 freqs.new = policy->restore_freq;
1896 cpufreq_freq_transition_begin(policy, &freqs);
1897 cpufreq_freq_transition_end(policy, &freqs, 0);
1898 }
1899 }
1900
Viresh Kumar8d657752014-05-21 14:29:29 +05301901 return retval;
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904int __cpufreq_driver_target(struct cpufreq_policy *policy,
1905 unsigned int target_freq,
1906 unsigned int relation)
1907{
Viresh Kumar72499242012-10-31 01:28:21 +01001908 unsigned int old_target_freq = target_freq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301909 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001910
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001911 if (cpufreq_disabled())
1912 return -ENODEV;
1913
Viresh Kumar72499242012-10-31 01:28:21 +01001914 /* Make sure that target_freq is within supported range */
1915 if (target_freq > policy->max)
1916 target_freq = policy->max;
1917 if (target_freq < policy->min)
1918 target_freq = policy->min;
1919
1920 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001921 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001922
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301923 /*
1924 * This might look like a redundant call as we are checking it again
1925 * after finding index. But it is left intentionally for cases where
1926 * exactly same freq is called again and so we can save on few function
1927 * calls.
1928 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001929 if (target_freq == policy->cur)
1930 return 0;
1931
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301932 /* Save last value to restore later on errors */
1933 policy->restore_freq = policy->cur;
1934
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001935 if (cpufreq_driver->target)
1936 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301937 else if (cpufreq_driver->target_index) {
1938 struct cpufreq_frequency_table *freq_table;
1939 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001940
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301941 freq_table = cpufreq_frequency_get_table(policy->cpu);
1942 if (unlikely(!freq_table)) {
1943 pr_err("%s: Unable to find freq_table\n", __func__);
1944 goto out;
1945 }
1946
1947 retval = cpufreq_frequency_table_target(policy, freq_table,
1948 target_freq, relation, &index);
1949 if (unlikely(retval)) {
1950 pr_err("%s: Unable to find matching freq\n", __func__);
1951 goto out;
1952 }
1953
Viresh Kumard4019f02013-08-14 19:38:24 +05301954 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301955 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301956 goto out;
1957 }
1958
Viresh Kumar8d657752014-05-21 14:29:29 +05301959 retval = __target_index(policy, freq_table, index);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301960 }
1961
1962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return retval;
1964}
1965EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967int cpufreq_driver_target(struct cpufreq_policy *policy,
1968 unsigned int target_freq,
1969 unsigned int relation)
1970{
Julia Lawallf1829e42008-07-25 22:44:53 +02001971 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
viresh kumarad7722d2013-10-18 19:10:15 +05301973 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 ret = __cpufreq_driver_target(policy, target_freq, relation);
1976
viresh kumarad7722d2013-10-18 19:10:15 +05301977 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return ret;
1980}
1981EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1982
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001983__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1984{
1985 return NULL;
1986}
1987
Viresh Kumara1317e02016-02-22 16:36:43 +05301988static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Dave Jonescc993ca2005-07-28 09:43:56 -07001990 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001991
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001992 /* Don't start any governor operations if we are entering suspend */
1993 if (cpufreq_suspended)
1994 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09001995 /*
1996 * Governor might not be initiated here if ACPI _PPC changed
1997 * notification happened, so check it.
1998 */
1999 if (!policy->governor)
2000 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002001
Thomas Renninger1c256242007-10-02 13:28:12 -07002002 if (policy->governor->max_transition_latency &&
2003 policy->cpuinfo.transition_latency >
2004 policy->governor->max_transition_latency) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002005 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2006
2007 if (gov) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002008 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2009 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002010 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002011 } else {
2012 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002013 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Viresh Kumarfe492f32013-08-06 22:53:10 +05302016 if (event == CPUFREQ_GOV_POLICY_INIT)
2017 if (!try_module_get(policy->governor->owner))
2018 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Viresh Kumar63431f72015-07-27 17:58:06 +05302020 pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 ret = policy->governor->governor(policy, event);
2023
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002024 if (!ret) {
2025 if (event == CPUFREQ_GOV_POLICY_INIT)
2026 policy->governor->initialized++;
2027 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2028 policy->governor->initialized--;
2029 }
Viresh Kumarb3940582013-02-01 05:42:58 +00002030
Viresh Kumarfe492f32013-08-06 22:53:10 +05302031 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2032 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 module_put(policy->governor->owner);
2034
2035 return ret;
2036}
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038int cpufreq_register_governor(struct cpufreq_governor *governor)
2039{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002040 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 if (!governor)
2043 return -EINVAL;
2044
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002045 if (cpufreq_disabled())
2046 return -ENODEV;
2047
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002048 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002049
Viresh Kumarb3940582013-02-01 05:42:58 +00002050 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002051 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302052 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002053 err = 0;
2054 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Dave Jones32ee8c32006-02-28 00:43:23 -05002057 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002058 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059}
2060EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2063{
Viresh Kumar45732372015-05-12 12:22:34 +05302064 struct cpufreq_policy *policy;
2065 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (!governor)
2068 return;
2069
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002070 if (cpufreq_disabled())
2071 return;
2072
Viresh Kumar45732372015-05-12 12:22:34 +05302073 /* clear last_governor for all inactive policies */
2074 read_lock_irqsave(&cpufreq_driver_lock, flags);
2075 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302076 if (!strcmp(policy->last_governor, governor->name)) {
2077 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302078 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302079 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002080 }
Viresh Kumar45732372015-05-12 12:22:34 +05302081 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002082
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002083 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002085 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return;
2087}
2088EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2089
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*********************************************************************
2092 * POLICY INTERFACE *
2093 *********************************************************************/
2094
2095/**
2096 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002097 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2098 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 *
2100 * Reads the current cpufreq policy.
2101 */
2102int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2103{
2104 struct cpufreq_policy *cpu_policy;
2105 if (!policy)
2106 return -EINVAL;
2107
2108 cpu_policy = cpufreq_cpu_get(cpu);
2109 if (!cpu_policy)
2110 return -EINVAL;
2111
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302112 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 0;
2116}
2117EXPORT_SYMBOL(cpufreq_get_policy);
2118
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002119/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302120 * policy : current policy.
2121 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002122 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302123static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302124 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002126 struct cpufreq_governor *old_gov;
2127 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Joe Perchese837f9b2014-03-11 10:03:00 -07002129 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2130 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302132 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Pan Xinhuifba95732015-07-30 18:10:40 +08002134 /*
2135 * This check works well when we store new min/max freq attributes,
2136 * because new_policy is a copy of policy with one field updated.
2137 */
2138 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002139 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302142 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002144 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002147 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302148 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Viresh Kumarbb176f72013-06-19 14:19:33 +05302150 /*
2151 * verify the cpu speed can be set within this limit, which might be
2152 * different to the first one
2153 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302154 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002155 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002159 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302160 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302162 policy->min = new_policy->min;
2163 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002165 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002166 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002168 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302169 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002170 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002171 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002174 if (new_policy->governor == policy->governor)
2175 goto out;
2176
2177 pr_debug("governor switch\n");
2178
2179 /* save old, working values */
2180 old_gov = policy->governor;
2181 /* end old governor */
2182 if (old_gov) {
Viresh Kumara1317e02016-02-22 16:36:43 +05302183 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302184 if (ret) {
2185 /* This can happen due to race with other operations */
2186 pr_debug("%s: Failed to Stop Governor: %s (%d)\n",
2187 __func__, old_gov->name, ret);
2188 return ret;
2189 }
2190
Viresh Kumara1317e02016-02-22 16:36:43 +05302191 ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302192 if (ret) {
2193 pr_err("%s: Failed to Exit Governor: %s (%d)\n",
2194 __func__, old_gov->name, ret);
2195 return ret;
2196 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002197 }
2198
2199 /* start new governor */
2200 policy->governor = new_policy->governor;
Viresh Kumara1317e02016-02-22 16:36:43 +05302201 ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302202 if (!ret) {
Viresh Kumara1317e02016-02-22 16:36:43 +05302203 ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302204 if (!ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002205 goto out;
2206
Viresh Kumara1317e02016-02-22 16:36:43 +05302207 cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002208 }
2209
2210 /* new governor failed, so re-start old one */
2211 pr_debug("starting governor %s failed\n", policy->governor->name);
2212 if (old_gov) {
2213 policy->governor = old_gov;
Viresh Kumara1317e02016-02-22 16:36:43 +05302214 if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302215 policy->governor = NULL;
2216 else
Viresh Kumara1317e02016-02-22 16:36:43 +05302217 cpufreq_governor(policy, CPUFREQ_GOV_START);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002218 }
2219
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302220 return ret;
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002221
2222 out:
2223 pr_debug("governor: change or update limits\n");
Viresh Kumara1317e02016-02-22 16:36:43 +05302224 return cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225}
2226
2227/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2229 * @cpu: CPU which shall be re-evaluated
2230 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002231 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 * at different times.
2233 */
2234int cpufreq_update_policy(unsigned int cpu)
2235{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302236 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2237 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002238 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002240 if (!policy)
2241 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
viresh kumarad7722d2013-10-18 19:10:15 +05302243 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002245 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302246 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302247 new_policy.min = policy->user_policy.min;
2248 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Viresh Kumarbb176f72013-06-19 14:19:33 +05302250 /*
2251 * BIOS might change freq behind our back
2252 * -> ask driver for current freq and notify governors about a change
2253 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002254 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302255 new_policy.cur = cpufreq_driver->get(cpu);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302256 if (WARN_ON(!new_policy.cur)) {
2257 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002258 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302259 }
2260
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302261 if (!policy->cur) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002262 pr_debug("Driver did not initialize current freq\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302263 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002264 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302265 if (policy->cur != new_policy.cur && has_target())
Viresh Kumara1e1dc42015-01-02 12:34:28 +05302266 cpufreq_out_of_sync(policy, new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002267 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002268 }
2269
Viresh Kumar037ce832013-10-02 14:13:16 +05302270 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002272unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302273 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002274
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302275 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return ret;
2277}
2278EXPORT_SYMBOL(cpufreq_update_policy);
2279
Paul Gortmaker27609842013-06-19 13:54:04 -04002280static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002281 unsigned long action, void *hcpu)
2282{
2283 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002284
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002285 switch (action & ~CPU_TASKS_FROZEN) {
2286 case CPU_ONLINE:
2287 cpufreq_online(cpu);
2288 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302289
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002290 case CPU_DOWN_PREPARE:
Viresh Kumar69cee712016-02-11 17:31:11 +05302291 cpufreq_offline(cpu);
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002292 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302293
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002294 case CPU_DOWN_FAILED:
2295 cpufreq_online(cpu);
2296 break;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002297 }
2298 return NOTIFY_OK;
2299}
2300
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002301static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302302 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002303};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002306 * BOOST *
2307 *********************************************************************/
2308static int cpufreq_boost_set_sw(int state)
2309{
2310 struct cpufreq_frequency_table *freq_table;
2311 struct cpufreq_policy *policy;
2312 int ret = -EINVAL;
2313
Viresh Kumarf9637352015-05-12 12:20:11 +05302314 for_each_active_policy(policy) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002315 freq_table = cpufreq_frequency_get_table(policy->cpu);
2316 if (freq_table) {
2317 ret = cpufreq_frequency_table_cpuinfo(policy,
2318 freq_table);
2319 if (ret) {
2320 pr_err("%s: Policy frequency update failed\n",
2321 __func__);
2322 break;
2323 }
Viresh Kumar49f18562016-02-11 17:31:12 +05302324
2325 down_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002326 policy->user_policy.max = policy->max;
Viresh Kumara1317e02016-02-22 16:36:43 +05302327 cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Viresh Kumar49f18562016-02-11 17:31:12 +05302328 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002329 }
2330 }
2331
2332 return ret;
2333}
2334
2335int cpufreq_boost_trigger_state(int state)
2336{
2337 unsigned long flags;
2338 int ret = 0;
2339
2340 if (cpufreq_driver->boost_enabled == state)
2341 return 0;
2342
2343 write_lock_irqsave(&cpufreq_driver_lock, flags);
2344 cpufreq_driver->boost_enabled = state;
2345 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2346
2347 ret = cpufreq_driver->set_boost(state);
2348 if (ret) {
2349 write_lock_irqsave(&cpufreq_driver_lock, flags);
2350 cpufreq_driver->boost_enabled = !state;
2351 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2352
Joe Perchese837f9b2014-03-11 10:03:00 -07002353 pr_err("%s: Cannot %s BOOST\n",
2354 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002355 }
2356
2357 return ret;
2358}
2359
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002360static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002361{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002362 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002363}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002364
Viresh Kumar44139ed2015-07-29 16:23:09 +05302365static int create_boost_sysfs_file(void)
2366{
2367 int ret;
2368
Viresh Kumarc82bd442015-10-15 21:35:23 +05302369 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302370 if (ret)
2371 pr_err("%s: cannot register global BOOST sysfs file\n",
2372 __func__);
2373
2374 return ret;
2375}
2376
2377static void remove_boost_sysfs_file(void)
2378{
2379 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302380 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302381}
2382
2383int cpufreq_enable_boost_support(void)
2384{
2385 if (!cpufreq_driver)
2386 return -EINVAL;
2387
2388 if (cpufreq_boost_supported())
2389 return 0;
2390
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002391 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302392
2393 /* This will get removed on driver unregister */
2394 return create_boost_sysfs_file();
2395}
2396EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2397
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002398int cpufreq_boost_enabled(void)
2399{
2400 return cpufreq_driver->boost_enabled;
2401}
2402EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2403
2404/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2406 *********************************************************************/
2407
2408/**
2409 * cpufreq_register_driver - register a CPU Frequency driver
2410 * @driver_data: A struct cpufreq_driver containing the values#
2411 * submitted by the CPU Frequency driver.
2412 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302413 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002415 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 *
2417 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002418int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
2420 unsigned long flags;
2421 int ret;
2422
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002423 if (cpufreq_disabled())
2424 return -ENODEV;
2425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302427 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002428 driver_data->target) ||
2429 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302430 driver_data->target)) ||
2431 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return -EINVAL;
2433
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002434 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002436 /* Protect against concurrent CPU online/offline. */
2437 get_online_cpus();
2438
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002439 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002440 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002441 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002442 ret = -EEXIST;
2443 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002445 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002446 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302448 if (driver_data->setpolicy)
2449 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2450
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002451 if (cpufreq_boost_supported()) {
2452 ret = create_boost_sysfs_file();
2453 if (ret)
2454 goto err_null_driver;
2455 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002456
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002457 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002458 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002459 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302461 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2462 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302464 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2465 driver_data->name);
2466 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 }
2468
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002469 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002470 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002472out:
2473 put_online_cpus();
2474 return ret;
2475
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002476err_if_unreg:
2477 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002478err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302479 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002480err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002481 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002482 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002483 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002484 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485}
2486EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/**
2489 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2490 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302491 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 * the right to do so, i.e. if you have succeeded in initialising before!
2493 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2494 * currently not initialised.
2495 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002496int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497{
2498 unsigned long flags;
2499
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002500 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002503 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002505 /* Protect against concurrent cpu hotplug */
2506 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002507 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302508 remove_boost_sysfs_file();
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002509 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002511 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302512
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002513 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302514
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002515 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002516 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 return 0;
2519}
2520EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002521
Doug Anderson90de2a42014-12-23 22:09:48 -08002522/*
2523 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2524 * or mutexes when secondary CPUs are halted.
2525 */
2526static struct syscore_ops cpufreq_syscore_ops = {
2527 .shutdown = cpufreq_suspend,
2528};
2529
Viresh Kumarc82bd442015-10-15 21:35:23 +05302530struct kobject *cpufreq_global_kobject;
2531EXPORT_SYMBOL(cpufreq_global_kobject);
2532
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002533static int __init cpufreq_core_init(void)
2534{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002535 if (cpufreq_disabled())
2536 return -ENODEV;
2537
Viresh Kumar8eec1022015-10-15 21:35:22 +05302538 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002539 BUG_ON(!cpufreq_global_kobject);
2540
Doug Anderson90de2a42014-12-23 22:09:48 -08002541 register_syscore_ops(&cpufreq_syscore_ops);
2542
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002543 return 0;
2544}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002545core_initcall(cpufreq_core_init);