blob: 1685e930770f4fced719b4f154526895274d0c15 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080029#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080030#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053031#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020032#include <trace/events/power.h>
33
Viresh Kumarb4f06762015-01-27 14:06:08 +053034static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053035
36static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37{
38 return cpumask_empty(policy->cpus);
39}
40
Viresh Kumarf9637352015-05-12 12:20:11 +053041/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060042#define for_each_suitable_policy(__policy, __active) \
43 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
44 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053045
46#define for_each_active_policy(__policy) \
47 for_each_suitable_policy(__policy, true)
48#define for_each_inactive_policy(__policy) \
49 for_each_suitable_policy(__policy, false)
50
51#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053052 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
53
Viresh Kumarf7b27062015-01-27 14:06:09 +053054/* Iterate over governors */
55static LIST_HEAD(cpufreq_governor_list);
56#define for_each_governor(__governor) \
57 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**
Dave Jonescd878472006-08-11 17:59:28 -040060 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * level driver of CPUFreq support, and its spinlock. This lock
62 * also protects the cpufreq_cpu_data array.
63 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020064static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070065static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053066static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053067
Viresh Kumar2f0aea92014-03-04 11:00:26 +080068/* Flag to suspend/resume CPUFreq governors */
69static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053071static inline bool has_target(void)
72{
73 return cpufreq_driver->target_index || cpufreq_driver->target;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053077static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020078static int cpufreq_init_governor(struct cpufreq_policy *policy);
79static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010080static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020081static void cpufreq_stop_governor(struct cpufreq_policy *policy);
82static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
Dave Jones32ee8c32006-02-28 00:43:23 -050085 * Two notifier lists: the "policy" list is involved in the
86 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * "transition" list for kernel code that needs to handle
88 * changes to devices when the CPU clock speed changes.
89 * The mutex locks both lists.
90 */
Alan Sterne041c682006-03-27 01:16:30 -080091static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070092static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020094static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070095static int __init init_cpufreq_transition_notifier_list(void)
96{
97 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020098 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070099 return 0;
100}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800101pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400103static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200104static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400105{
106 return off;
107}
108void disable_cpufreq(void)
109{
110 off = 1;
111}
Dave Jones29464f22009-01-18 01:37:11 -0500112static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000114bool have_governor_per_policy(void)
115{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530116 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000117}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000118EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000119
Viresh Kumar944e9a02013-05-16 05:09:57 +0000120struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
121{
122 if (have_governor_per_policy())
123 return &policy->kobj;
124 else
125 return cpufreq_global_kobject;
126}
127EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
128
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000129static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
130{
131 u64 idle_time;
132 u64 cur_wall_time;
133 u64 busy_time;
134
135 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
136
137 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
138 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
139 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
140 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
141 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
142 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
143
144 idle_time = cur_wall_time - busy_time;
145 if (wall)
146 *wall = cputime_to_usecs(cur_wall_time);
147
148 return cputime_to_usecs(idle_time);
149}
150
151u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
152{
153 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
154
155 if (idle_time == -1ULL)
156 return get_cpu_idle_time_jiffy(cpu, wall);
157 else if (!io_busy)
158 idle_time += get_cpu_iowait_time_us(cpu, wall);
159
160 return idle_time;
161}
162EXPORT_SYMBOL_GPL(get_cpu_idle_time);
163
Viresh Kumar70e9e772013-10-03 20:29:07 +0530164/*
165 * This is a generic cpufreq init() routine which can be used by cpufreq
166 * drivers of SMP systems. It will do following:
167 * - validate & show freq table passed
168 * - set policies transition latency
169 * - policy->cpus with all possible CPUs
170 */
171int cpufreq_generic_init(struct cpufreq_policy *policy,
172 struct cpufreq_frequency_table *table,
173 unsigned int transition_latency)
174{
175 int ret;
176
177 ret = cpufreq_table_validate_and_show(policy, table);
178 if (ret) {
179 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
180 return ret;
181 }
182
183 policy->cpuinfo.transition_latency = transition_latency;
184
185 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530186 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530187 * share the clock and voltage and clock.
188 */
189 cpumask_setall(policy->cpus);
190
191 return 0;
192}
193EXPORT_SYMBOL_GPL(cpufreq_generic_init);
194
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200195struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530196{
197 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
198
Viresh Kumar988bed02015-05-08 11:53:45 +0530199 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
200}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200201EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530202
203unsigned int cpufreq_generic_get(unsigned int cpu)
204{
205 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
206
Viresh Kumar652ed952014-01-09 20:38:43 +0530207 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700208 pr_err("%s: No %s associated to cpu: %d\n",
209 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530210 return 0;
211 }
212
213 return clk_get_rate(policy->clk) / 1000;
214}
215EXPORT_SYMBOL_GPL(cpufreq_generic_get);
216
Viresh Kumar50e9c852015-02-19 17:02:03 +0530217/**
218 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
219 *
220 * @cpu: cpu to find policy for.
221 *
222 * This returns policy for 'cpu', returns NULL if it doesn't exist.
223 * It also increments the kobject reference count to mark it busy and so would
224 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
225 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
226 * freed as that depends on the kobj count.
227 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530228 * Return: A valid policy on success, otherwise NULL on failure.
229 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530230struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530232 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 unsigned long flags;
234
Viresh Kumar1b947c92015-02-19 17:02:05 +0530235 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530236 return NULL;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000239 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Viresh Kumar6eed9402013-08-06 22:53:11 +0530241 if (cpufreq_driver) {
242 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530243 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530244 if (policy)
245 kobject_get(&policy->kobj);
246 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200247
Viresh Kumar6eed9402013-08-06 22:53:11 +0530248 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530250 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
253
Viresh Kumar50e9c852015-02-19 17:02:03 +0530254/**
255 * cpufreq_cpu_put: Decrements the usage count of a policy
256 *
257 * @policy: policy earlier returned by cpufreq_cpu_get().
258 *
259 * This decrements the kobject reference count incremented earlier by calling
260 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530261 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530262void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530264 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
270 *********************************************************************/
271
272/**
273 * adjust_jiffies - adjust the system "loops_per_jiffy"
274 *
275 * This function alters the system "loops_per_jiffy" for the clock
276 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500277 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 * per-CPU loops_per_jiffy value wherever possible.
279 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800280static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530282#ifndef CONFIG_SMP
283 static unsigned long l_p_j_ref;
284 static unsigned int l_p_j_ref_freq;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (ci->flags & CPUFREQ_CONST_LOOPS)
287 return;
288
289 if (!l_p_j_ref_freq) {
290 l_p_j_ref = loops_per_jiffy;
291 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700292 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
293 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530295 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530296 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
297 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700298 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
299 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530302}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530304static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530305 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 BUG_ON(irqs_disabled());
308
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000309 if (cpufreq_disabled())
310 return;
311
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200312 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200313 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700314 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500319 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800320 * which is not equal to what the cpufreq core thinks is
321 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200323 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800324 if ((policy) && (policy->cpu == freqs->cpu) &&
325 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700326 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
327 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800328 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700331 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800332 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
334 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 case CPUFREQ_POSTCHANGE:
337 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700338 pr_debug("FREQ: %lu - CPU: %lu\n",
339 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100340 trace_cpu_frequency(freqs->new, freqs->cpu);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200341 cpufreq_stats_record_transition(policy, freqs->new);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700342 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800343 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800344 if (likely(policy) && likely(policy->cpu == freqs->cpu))
345 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 break;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530349
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530350/**
351 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
352 * on frequency transition.
353 *
354 * This function calls the transition notifiers and the "adjust_jiffies"
355 * function. It is called twice on all CPU frequency changes that have
356 * external effects.
357 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530358static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530359 struct cpufreq_freqs *freqs, unsigned int state)
360{
361 for_each_cpu(freqs->cpu, policy->cpus)
362 __cpufreq_notify_transition(policy, freqs, state);
363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530365/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530366static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530367 struct cpufreq_freqs *freqs, int transition_failed)
368{
369 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
370 if (!transition_failed)
371 return;
372
373 swap(freqs->old, freqs->new);
374 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
375 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
376}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530377
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530378void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
379 struct cpufreq_freqs *freqs)
380{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530381
382 /*
383 * Catch double invocations of _begin() which lead to self-deadlock.
384 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
385 * doesn't invoke _begin() on their behalf, and hence the chances of
386 * double invocations are very low. Moreover, there are scenarios
387 * where these checks can emit false-positive warnings in these
388 * drivers; so we avoid that by skipping them altogether.
389 */
390 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
391 && current == policy->transition_task);
392
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530393wait:
394 wait_event(policy->transition_wait, !policy->transition_ongoing);
395
396 spin_lock(&policy->transition_lock);
397
398 if (unlikely(policy->transition_ongoing)) {
399 spin_unlock(&policy->transition_lock);
400 goto wait;
401 }
402
403 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530404 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530405
406 spin_unlock(&policy->transition_lock);
407
408 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
409}
410EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
411
412void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
413 struct cpufreq_freqs *freqs, int transition_failed)
414{
415 if (unlikely(WARN_ON(!policy->transition_ongoing)))
416 return;
417
418 cpufreq_notify_post_transition(policy, freqs, transition_failed);
419
420 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530421 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530422
423 wake_up(&policy->transition_wait);
424}
425EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
426
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200427/*
428 * Fast frequency switching status count. Positive means "enabled", negative
429 * means "disabled" and 0 means "not decided yet".
430 */
431static int cpufreq_fast_switch_count;
432static DEFINE_MUTEX(cpufreq_fast_switch_lock);
433
434static void cpufreq_list_transition_notifiers(void)
435{
436 struct notifier_block *nb;
437
438 pr_info("Registered transition notifiers:\n");
439
440 mutex_lock(&cpufreq_transition_notifier_list.mutex);
441
442 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
443 pr_info("%pF\n", nb->notifier_call);
444
445 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
446}
447
448/**
449 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
450 * @policy: cpufreq policy to enable fast frequency switching for.
451 *
452 * Try to enable fast frequency switching for @policy.
453 *
454 * The attempt will fail if there is at least one transition notifier registered
455 * at this point, as fast frequency switching is quite fundamentally at odds
456 * with transition notifiers. Thus if successful, it will make registration of
457 * transition notifiers fail going forward.
458 */
459void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
460{
461 lockdep_assert_held(&policy->rwsem);
462
463 if (!policy->fast_switch_possible)
464 return;
465
466 mutex_lock(&cpufreq_fast_switch_lock);
467 if (cpufreq_fast_switch_count >= 0) {
468 cpufreq_fast_switch_count++;
469 policy->fast_switch_enabled = true;
470 } else {
471 pr_warn("CPU%u: Fast frequency switching not enabled\n",
472 policy->cpu);
473 cpufreq_list_transition_notifiers();
474 }
475 mutex_unlock(&cpufreq_fast_switch_lock);
476}
477EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
478
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200479/**
480 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
481 * @policy: cpufreq policy to disable fast frequency switching for.
482 */
483void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200484{
485 mutex_lock(&cpufreq_fast_switch_lock);
486 if (policy->fast_switch_enabled) {
487 policy->fast_switch_enabled = false;
488 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
489 cpufreq_fast_switch_count--;
490 }
491 mutex_unlock(&cpufreq_fast_switch_lock);
492}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200493EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*********************************************************************
496 * SYSFS INTERFACE *
497 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530498static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100499 struct attribute *attr, char *buf)
500{
501 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
502}
503
504static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
505 const char *buf, size_t count)
506{
507 int ret, enable;
508
509 ret = sscanf(buf, "%d", &enable);
510 if (ret != 1 || enable < 0 || enable > 1)
511 return -EINVAL;
512
513 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700514 pr_err("%s: Cannot %s BOOST!\n",
515 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100516 return -EINVAL;
517 }
518
Joe Perchese837f9b2014-03-11 10:03:00 -0700519 pr_debug("%s: cpufreq BOOST %s\n",
520 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100521
522 return count;
523}
524define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530526static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700527{
528 struct cpufreq_governor *t;
529
Viresh Kumarf7b27062015-01-27 14:06:09 +0530530 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200531 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700532 return t;
533
534 return NULL;
535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/**
538 * cpufreq_parse_governor - parse a governor string
539 */
Dave Jones905d77c2008-03-05 14:28:32 -0500540static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct cpufreq_governor **governor)
542{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700543 int err = -EINVAL;
544
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200545 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200546 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700548 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200549 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530550 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700552 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530554 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700556
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800557 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700558
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530559 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700560
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700561 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700562 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700563
Kees Cook1a8e1462011-05-04 08:38:56 -0700564 mutex_unlock(&cpufreq_governor_mutex);
565 ret = request_module("cpufreq_%s", str_governor);
566 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700567
Kees Cook1a8e1462011-05-04 08:38:56 -0700568 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530569 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700570 }
571
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700572 if (t != NULL) {
573 *governor = t;
574 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700576
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800577 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700579 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530583 * cpufreq_per_cpu_attr_read() / show_##file_name() -
584 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 *
586 * Write out information from cpufreq_driver->policy[cpu]; object must be
587 * "unsigned int".
588 */
589
Dave Jones32ee8c32006-02-28 00:43:23 -0500590#define show_one(file_name, object) \
591static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500592(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500593{ \
Dave Jones29464f22009-01-18 01:37:11 -0500594 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597show_one(cpuinfo_min_freq, cpuinfo.min_freq);
598show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100599show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600show_one(scaling_min_freq, min);
601show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700602
Viresh Kumar09347b22015-01-02 12:34:24 +0530603static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700604{
605 ssize_t ret;
606
607 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
608 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
609 else
610 ret = sprintf(buf, "%u\n", policy->cur);
611 return ret;
612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Viresh Kumar037ce832013-10-02 14:13:16 +0530614static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530615 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/**
618 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
619 */
620#define store_one(file_name, object) \
621static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500622(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800624 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct cpufreq_policy new_policy; \
626 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530627 memcpy(&new_policy, policy, sizeof(*policy)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 \
Dave Jones29464f22009-01-18 01:37:11 -0500629 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (ret != 1) \
631 return -EINVAL; \
632 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800633 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530634 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800635 if (!ret) \
636 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 \
638 return ret ? ret : count; \
639}
640
Dave Jones29464f22009-01-18 01:37:11 -0500641store_one(scaling_min_freq, min);
642store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644/**
645 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
646 */
Dave Jones905d77c2008-03-05 14:28:32 -0500647static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
648 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530650 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!cur_freq)
652 return sprintf(buf, "<unknown>");
653 return sprintf(buf, "%u\n", cur_freq);
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/**
657 * show_scaling_governor - show the current policy for the specified CPU
658 */
Dave Jones905d77c2008-03-05 14:28:32 -0500659static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Dave Jones29464f22009-01-18 01:37:11 -0500661 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return sprintf(buf, "powersave\n");
663 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
664 return sprintf(buf, "performance\n");
665 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200666 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500667 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EINVAL;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/**
672 * store_scaling_governor - store policy for the specified CPU
673 */
Dave Jones905d77c2008-03-05 14:28:32 -0500674static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
675 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530677 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 char str_governor[16];
679 struct cpufreq_policy new_policy;
680
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530681 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Jones29464f22009-01-18 01:37:11 -0500683 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (ret != 1)
685 return -EINVAL;
686
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530687 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
688 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return -EINVAL;
690
Viresh Kumar037ce832013-10-02 14:13:16 +0530691 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530692 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695/**
696 * show_scaling_driver - show the cpufreq driver currently loaded
697 */
Dave Jones905d77c2008-03-05 14:28:32 -0500698static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200700 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
703/**
704 * show_scaling_available_governors - show the available CPUfreq governors
705 */
Dave Jones905d77c2008-03-05 14:28:32 -0500706static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
707 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 ssize_t i = 0;
710 struct cpufreq_governor *t;
711
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530712 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 i += sprintf(buf, "performance powersave");
714 goto out;
715 }
716
Viresh Kumarf7b27062015-01-27 14:06:09 +0530717 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500718 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
719 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200721 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500723out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 i += sprintf(&buf[i], "\n");
725 return i;
726}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700727
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800728ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 ssize_t i = 0;
731 unsigned int cpu;
732
Rusty Russell835481d2009-01-04 05:18:06 -0800733 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (i)
735 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
736 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
737 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500738 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 i += sprintf(&buf[i], "\n");
741 return i;
742}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800743EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700745/**
746 * show_related_cpus - show the CPUs affected by each transition even if
747 * hw coordination is in use
748 */
749static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
750{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800751 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700752}
753
754/**
755 * show_affected_cpus - show the CPUs affected by each transition
756 */
757static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
758{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800759 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700760}
761
Venki Pallipadi9e769882007-10-26 10:18:21 -0700762static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500763 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700764{
765 unsigned int freq = 0;
766 unsigned int ret;
767
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700768 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700769 return -EINVAL;
770
771 ret = sscanf(buf, "%u", &freq);
772 if (ret != 1)
773 return -EINVAL;
774
775 policy->governor->store_setspeed(policy, freq);
776
777 return count;
778}
779
780static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
781{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700782 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700783 return sprintf(buf, "<unsupported>\n");
784
785 return policy->governor->show_setspeed(policy, buf);
786}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Thomas Renningere2f74f32009-11-19 12:31:01 +0100788/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200789 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100790 */
791static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
792{
793 unsigned int limit;
794 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200795 if (cpufreq_driver->bios_limit) {
796 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100797 if (!ret)
798 return sprintf(buf, "%u\n", limit);
799 }
800 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
801}
802
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200803cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
804cpufreq_freq_attr_ro(cpuinfo_min_freq);
805cpufreq_freq_attr_ro(cpuinfo_max_freq);
806cpufreq_freq_attr_ro(cpuinfo_transition_latency);
807cpufreq_freq_attr_ro(scaling_available_governors);
808cpufreq_freq_attr_ro(scaling_driver);
809cpufreq_freq_attr_ro(scaling_cur_freq);
810cpufreq_freq_attr_ro(bios_limit);
811cpufreq_freq_attr_ro(related_cpus);
812cpufreq_freq_attr_ro(affected_cpus);
813cpufreq_freq_attr_rw(scaling_min_freq);
814cpufreq_freq_attr_rw(scaling_max_freq);
815cpufreq_freq_attr_rw(scaling_governor);
816cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Dave Jones905d77c2008-03-05 14:28:32 -0500818static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 &cpuinfo_min_freq.attr,
820 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100821 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 &scaling_min_freq.attr,
823 &scaling_max_freq.attr,
824 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700825 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 &scaling_governor.attr,
827 &scaling_driver.attr,
828 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700829 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 NULL
831};
832
Dave Jones29464f22009-01-18 01:37:11 -0500833#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
834#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Dave Jones29464f22009-01-18 01:37:11 -0500836static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Dave Jones905d77c2008-03-05 14:28:32 -0500838 struct cpufreq_policy *policy = to_policy(kobj);
839 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530840 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530841
viresh kumarad7722d2013-10-18 19:10:15 +0530842 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100843 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530844 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return ret;
847}
848
Dave Jones905d77c2008-03-05 14:28:32 -0500849static ssize_t store(struct kobject *kobj, struct attribute *attr,
850 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Dave Jones905d77c2008-03-05 14:28:32 -0500852 struct cpufreq_policy *policy = to_policy(kobj);
853 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500854 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530855
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530856 get_online_cpus();
857
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100858 if (cpu_online(policy->cpu)) {
859 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530860 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100861 up_write(&policy->rwsem);
862 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530863
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530864 put_online_cpus();
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return ret;
867}
868
Dave Jones905d77c2008-03-05 14:28:32 -0500869static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Dave Jones905d77c2008-03-05 14:28:32 -0500871 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200872 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 complete(&policy->kobj_unregister);
874}
875
Emese Revfy52cf25d2010-01-19 02:58:23 +0100876static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .show = show,
878 .store = store,
879};
880
881static struct kobj_type ktype_cpufreq = {
882 .sysfs_ops = &sysfs_ops,
883 .default_attrs = default_attrs,
884 .release = cpufreq_sysfs_release,
885};
886
Viresh Kumar87549142015-06-10 02:13:21 +0200887static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
888{
889 struct device *cpu_dev;
890
891 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
892
893 if (!policy)
894 return 0;
895
896 cpu_dev = get_cpu_device(cpu);
897 if (WARN_ON(!cpu_dev))
898 return 0;
899
900 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
901}
902
903static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
904{
905 struct device *cpu_dev;
906
907 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
908
909 cpu_dev = get_cpu_device(cpu);
910 if (WARN_ON(!cpu_dev))
911 return;
912
913 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
914}
915
916/* Add/remove symlinks for all related CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200917static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400918{
919 unsigned int j;
920 int ret = 0;
921
Viresh Kumar87549142015-06-10 02:13:21 +0200922 /* Some related CPUs might not be present (physically hotplugged) */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +0200923 for_each_cpu(j, policy->real_cpus) {
Viresh Kumar87549142015-06-10 02:13:21 +0200924 ret = add_cpu_dev_symlink(policy, j);
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200925 if (ret)
926 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400927 }
Viresh Kumar87549142015-06-10 02:13:21 +0200928
Dave Jones19d6f7e2009-07-08 17:35:39 -0400929 return ret;
930}
931
Viresh Kumar87549142015-06-10 02:13:21 +0200932static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
933{
934 unsigned int j;
935
936 /* Some related CPUs might not be present (physically hotplugged) */
Viresh Kumar96bdda62015-10-15 21:35:24 +0530937 for_each_cpu(j, policy->real_cpus)
Viresh Kumar87549142015-06-10 02:13:21 +0200938 remove_cpu_dev_symlink(policy, j);
Viresh Kumar87549142015-06-10 02:13:21 +0200939}
940
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200941static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400942{
943 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400944 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400945
Dave Jones909a6942009-07-08 18:05:42 -0400946 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200947 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530948 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400949 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
950 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100951 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400952 drv_attr++;
953 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200954 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400955 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
956 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100957 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400958 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700959
960 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
961 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100962 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700963
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200964 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100965 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
966 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100967 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100968 }
Dave Jones909a6942009-07-08 18:05:42 -0400969
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100970 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530971}
972
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100973__weak struct cpufreq_governor *cpufreq_default_governor(void)
974{
975 return NULL;
976}
977
Viresh Kumar7f0fa402015-07-08 15:12:16 +0530978static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530979{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800980 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530981 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530982
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530983 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000984
viresh kumar6e2c89d2014-03-04 11:43:59 +0800985 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +0530986 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100987 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +0800988 pr_debug("Restoring governor %s for cpu %d\n",
989 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100990 } else {
991 gov = cpufreq_default_governor();
992 if (!gov)
993 return -ENODATA;
994 }
viresh kumar6e2c89d2014-03-04 11:43:59 +0800995
996 new_policy.governor = gov;
997
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -0800998 /* Use the default policy if there is no last_policy. */
999 if (cpufreq_driver->setpolicy) {
1000 if (policy->last_policy)
1001 new_policy.policy = policy->last_policy;
1002 else
1003 cpufreq_parse_governor(gov->name, &new_policy.policy,
1004 NULL);
1005 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001006 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301007 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001008}
1009
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001010static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001011{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301012 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001013
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301014 /* Has this CPU been taken care of already? */
1015 if (cpumask_test_cpu(cpu, policy->cpus))
1016 return 0;
1017
Viresh Kumar49f18562016-02-11 17:31:12 +05301018 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001019 if (has_target())
1020 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001021
Viresh Kumarfcf80582013-01-29 14:39:08 +00001022 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301023
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301024 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001025 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301026 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301027 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001028 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301029 up_write(&policy->rwsem);
1030 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001031}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301033static void handle_update(struct work_struct *work)
1034{
1035 struct cpufreq_policy *policy =
1036 container_of(work, struct cpufreq_policy, update);
1037 unsigned int cpu = policy->cpu;
1038 pr_debug("handle_update for cpu %u called\n", cpu);
1039 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001042static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301043{
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001044 struct device *dev = get_cpu_device(cpu);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301045 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301046 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301047
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001048 if (WARN_ON(!dev))
1049 return NULL;
1050
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301051 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1052 if (!policy)
1053 return NULL;
1054
1055 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1056 goto err_free_policy;
1057
1058 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1059 goto err_free_cpumask;
1060
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001061 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1062 goto err_free_rcpumask;
1063
Viresh Kumaredd4a892016-03-03 14:51:33 +05301064 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1065 cpufreq_global_kobject, "policy%u", cpu);
1066 if (ret) {
1067 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1068 goto err_free_real_cpus;
1069 }
1070
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301071 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301072 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301073 spin_lock_init(&policy->transition_lock);
1074 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301075 init_completion(&policy->kobj_unregister);
1076 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301077
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001078 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301079 return policy;
1080
Viresh Kumaredd4a892016-03-03 14:51:33 +05301081err_free_real_cpus:
1082 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301083err_free_rcpumask:
1084 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301085err_free_cpumask:
1086 free_cpumask_var(policy->cpus);
1087err_free_policy:
1088 kfree(policy);
1089
1090 return NULL;
1091}
1092
Viresh Kumar2fc33842015-06-08 18:25:29 +05301093static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301094{
1095 struct kobject *kobj;
1096 struct completion *cmp;
1097
Viresh Kumar2fc33842015-06-08 18:25:29 +05301098 if (notify)
1099 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1100 CPUFREQ_REMOVE_POLICY, policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301101
Viresh Kumar87549142015-06-10 02:13:21 +02001102 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001103 cpufreq_stats_free_table(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001104 cpufreq_remove_dev_symlink(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301105 kobj = &policy->kobj;
1106 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001107 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301108 kobject_put(kobj);
1109
1110 /*
1111 * We need to make sure that the underlying kobj is
1112 * actually not referenced anymore by anybody before we
1113 * proceed with unloading.
1114 */
1115 pr_debug("waiting for dropping of refcount\n");
1116 wait_for_completion(cmp);
1117 pr_debug("wait complete\n");
1118}
1119
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301120static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301121{
Viresh Kumar988bed02015-05-08 11:53:45 +05301122 unsigned long flags;
1123 int cpu;
1124
1125 /* Remove policy from list */
1126 write_lock_irqsave(&cpufreq_driver_lock, flags);
1127 list_del(&policy->policy_list);
1128
1129 for_each_cpu(cpu, policy->related_cpus)
1130 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1131 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1132
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301133 cpufreq_policy_put_kobj(policy, notify);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001134 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301135 free_cpumask_var(policy->related_cpus);
1136 free_cpumask_var(policy->cpus);
1137 kfree(policy);
1138}
1139
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001140static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301142 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001143 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001145 unsigned int j;
1146 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001147
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001148 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301149
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301150 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301151 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001152 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301153 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001154 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001155 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001157 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001158 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001159 down_write(&policy->rwsem);
1160 policy->cpu = cpu;
1161 policy->governor = NULL;
1162 up_write(&policy->rwsem);
1163 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001164 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001165 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001166 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001167 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001168 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301169
Rusty Russell835481d2009-01-04 05:18:06 -08001170 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* call driver. From then on the cpufreq must be able
1173 * to accept all calls to ->verify and ->setpolicy for this CPU
1174 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001175 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001177 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301178 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001180
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001181 down_write(&policy->rwsem);
1182
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001183 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001184 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301185 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001186 /* Remember CPUs present at the policy creation time. */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001187 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001188 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001189
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001190 /*
1191 * affected cpus must always be the one, which are online. We aren't
1192 * managing offline cpus here.
1193 */
1194 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1195
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001196 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001197 policy->user_policy.min = policy->min;
1198 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001199
Viresh Kumar988bed02015-05-08 11:53:45 +05301200 write_lock_irqsave(&cpufreq_driver_lock, flags);
1201 for_each_cpu(j, policy->related_cpus)
1202 per_cpu(cpufreq_cpu_data, j) = policy;
1203 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1204 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301205
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001206 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301207 policy->cur = cpufreq_driver->get(policy->cpu);
1208 if (!policy->cur) {
1209 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301210 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301211 }
1212 }
1213
Viresh Kumard3916692013-12-03 11:20:46 +05301214 /*
1215 * Sometimes boot loaders set CPU frequency to a value outside of
1216 * frequency table present with cpufreq core. In such cases CPU might be
1217 * unstable if it has to run on that frequency for long duration of time
1218 * and so its better to set it to a frequency which is specified in
1219 * freq-table. This also makes cpufreq stats inconsistent as
1220 * cpufreq-stats would fail to register because current frequency of CPU
1221 * isn't found in freq-table.
1222 *
1223 * Because we don't want this change to effect boot process badly, we go
1224 * for the next freq which is >= policy->cur ('cur' must be set by now,
1225 * otherwise we will end up setting freq to lowest of the table as 'cur'
1226 * is initialized to zero).
1227 *
1228 * We are passing target-freq as "policy->cur - 1" otherwise
1229 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1230 * equal to target-freq.
1231 */
1232 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1233 && has_target()) {
1234 /* Are we running at unknown frequency ? */
1235 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1236 if (ret == -EINVAL) {
1237 /* Warn user and fix it */
1238 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1239 __func__, policy->cpu, policy->cur);
1240 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1241 CPUFREQ_RELATION_L);
1242
1243 /*
1244 * Reaching here after boot in a few seconds may not
1245 * mean that system will remain stable at "unknown"
1246 * frequency for longer duration. Hence, a BUG_ON().
1247 */
1248 BUG_ON(ret);
1249 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1250 __func__, policy->cpu, policy->cur);
1251 }
1252 }
1253
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001254 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001255 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301256 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301257 goto out_exit_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001258
1259 cpufreq_stats_create_table(policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301260 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1261 CPUFREQ_CREATE_POLICY, policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001262
Viresh Kumar988bed02015-05-08 11:53:45 +05301263 write_lock_irqsave(&cpufreq_driver_lock, flags);
1264 list_add(&policy->policy_list, &cpufreq_policy_list);
1265 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1266 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301267
Viresh Kumar388612b2016-05-24 10:26:50 +05301268 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1269 CPUFREQ_START, policy);
1270
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301271 ret = cpufreq_init_policy(policy);
1272 if (ret) {
1273 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1274 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001275 /* cpufreq_policy_free() will notify based on this */
1276 new_policy = false;
1277 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301278 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301279
Viresh Kumar4e97b632014-03-04 11:44:01 +08001280 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301281
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001282 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301283
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301284 /* Callback for handling stuff after policy is ready */
1285 if (cpufreq_driver->ready)
1286 cpufreq_driver->ready(policy);
1287
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001288 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291
Viresh Kumar8101f992015-07-08 15:12:15 +05301292out_exit_policy:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001293 up_write(&policy->rwsem);
1294
Viresh Kumarda60ce92013-10-03 20:28:30 +05301295 if (cpufreq_driver->exit)
1296 cpufreq_driver->exit(policy);
Viresh Kumar8101f992015-07-08 15:12:15 +05301297out_free_policy:
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001298 cpufreq_policy_free(policy, !new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return ret;
1300}
1301
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001302/**
1303 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1304 * @dev: CPU device.
1305 * @sif: Subsystem interface structure pointer (not used)
1306 */
1307static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1308{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001309 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001310 unsigned cpu = dev->id;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001311
1312 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1313
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001314 if (cpu_online(cpu))
1315 return cpufreq_online(cpu);
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001316
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001317 /*
1318 * A hotplug notifier will follow and we will handle it as CPU online
1319 * then. For now, just create the sysfs link, unless there is no policy
1320 * or the link is already present.
1321 */
1322 policy = per_cpu(cpufreq_cpu_data, cpu);
1323 if (!policy || cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001326 return add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
Viresh Kumar69cee712016-02-11 17:31:11 +05301329static void cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301331 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301332 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001334 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Viresh Kumar988bed02015-05-08 11:53:45 +05301336 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301337 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001338 pr_debug("%s: No cpu_data found\n", __func__);
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001339 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Viresh Kumar49f18562016-02-11 17:31:12 +05301342 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001343 if (has_target())
1344 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001345
Viresh Kumar9591bec2015-06-10 02:20:23 +02001346 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301347
Viresh Kumar9591bec2015-06-10 02:20:23 +02001348 if (policy_is_inactive(policy)) {
1349 if (has_target())
1350 strncpy(policy->last_governor, policy->governor->name,
1351 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001352 else
1353 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001354 } else if (cpu == policy->cpu) {
1355 /* Nominate new CPU */
1356 policy->cpu = cpumask_any(policy->cpus);
1357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
Viresh Kumar9591bec2015-06-10 02:20:23 +02001359 /* Start governor again for active policy */
1360 if (!policy_is_inactive(policy)) {
1361 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001362 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001363 if (ret)
1364 pr_err("%s: Failed to start governor\n", __func__);
1365 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301366
Viresh Kumar49f18562016-02-11 17:31:12 +05301367 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301368 }
1369
1370 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001371 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001373 if (has_target())
1374 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001375
Viresh Kumar87549142015-06-10 02:13:21 +02001376 /*
1377 * Perform the ->exit() even during light-weight tear-down,
1378 * since this is a core component, and is essential for the
1379 * subsequent light-weight ->init() to succeed.
1380 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001381 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001382 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001383 policy->freq_table = NULL;
1384 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301385
1386unlock:
1387 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388}
1389
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301390/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301391 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301392 *
1393 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301394 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301395static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001396{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001397 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001398 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001399
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001400 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001401 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001402
Viresh Kumar69cee712016-02-11 17:31:11 +05301403 if (cpu_online(cpu))
1404 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001405
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001406 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar96bdda62015-10-15 21:35:24 +05301407 remove_cpu_dev_symlink(policy, cpu);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301408
1409 if (cpumask_empty(policy->real_cpus))
1410 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301414 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1415 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301416 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 * @new_freq: CPU frequency the CPU actually runs at
1418 *
Dave Jones29464f22009-01-18 01:37:11 -05001419 * We adjust to current frequency first, and need to clean up later.
1420 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301422static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301423 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424{
1425 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301426
Joe Perchese837f9b2014-03-11 10:03:00 -07001427 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 +05301428 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301430 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301432
Viresh Kumar8fec0512014-03-24 13:35:45 +05301433 cpufreq_freq_transition_begin(policy, &freqs);
1434 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Dave Jones32ee8c32006-02-28 00:43:23 -05001437/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301438 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001439 * @cpu: CPU number
1440 *
1441 * This is the last known freq, without actually getting it from the driver.
1442 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1443 */
1444unsigned int cpufreq_quick_get(unsigned int cpu)
1445{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001446 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301447 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001448 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001449
Richard Cochranc75361c2016-03-11 09:43:07 +01001450 read_lock_irqsave(&cpufreq_driver_lock, flags);
1451
1452 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1453 ret_freq = cpufreq_driver->get(cpu);
1454 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1455 return ret_freq;
1456 }
1457
1458 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001459
1460 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001461 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301462 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001463 cpufreq_cpu_put(policy);
1464 }
1465
Dave Jones4d34a672008-02-07 16:33:49 -05001466 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001467}
1468EXPORT_SYMBOL(cpufreq_quick_get);
1469
Jesse Barnes3d737102011-06-28 10:59:12 -07001470/**
1471 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1472 * @cpu: CPU number
1473 *
1474 * Just return the max possible frequency for a given CPU.
1475 */
1476unsigned int cpufreq_quick_get_max(unsigned int cpu)
1477{
1478 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1479 unsigned int ret_freq = 0;
1480
1481 if (policy) {
1482 ret_freq = policy->max;
1483 cpufreq_cpu_put(policy);
1484 }
1485
1486 return ret_freq;
1487}
1488EXPORT_SYMBOL(cpufreq_quick_get_max);
1489
Viresh Kumard92d50a2015-01-02 12:34:29 +05301490static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301492 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001494 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001495 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Viresh Kumard92d50a2015-01-02 12:34:29 +05301497 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001499 /*
1500 * Updating inactive policies is invalid, so avoid doing that. Also
1501 * if fast frequency switching is used with the given policy, the check
1502 * against policy->cur is pointless, so skip it in that case too.
1503 */
1504 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001505 return ret_freq;
1506
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301507 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001508 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301509 /* verify no discrepancy between actual and
1510 saved value exists */
1511 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301512 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 schedule_work(&policy->update);
1514 }
1515 }
1516
Dave Jones4d34a672008-02-07 16:33:49 -05001517 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001518}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001520/**
1521 * cpufreq_get - get the current CPU frequency (in kHz)
1522 * @cpu: CPU number
1523 *
1524 * Get the CPU current (static) CPU frequency
1525 */
1526unsigned int cpufreq_get(unsigned int cpu)
1527{
Aaron Plattner999976e2014-03-04 12:42:15 -08001528 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001529 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001530
Aaron Plattner999976e2014-03-04 12:42:15 -08001531 if (policy) {
1532 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301533 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001534 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301535
Aaron Plattner999976e2014-03-04 12:42:15 -08001536 cpufreq_cpu_put(policy);
1537 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301538
Dave Jones4d34a672008-02-07 16:33:49 -05001539 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541EXPORT_SYMBOL(cpufreq_get);
1542
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001543static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1544{
1545 unsigned int new_freq;
1546
Rafael J. Wysockic9d9c922016-04-10 05:59:33 +02001547 if (cpufreq_suspended)
1548 return 0;
1549
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001550 new_freq = cpufreq_driver->get(policy->cpu);
1551 if (!new_freq)
1552 return 0;
1553
1554 if (!policy->cur) {
1555 pr_debug("cpufreq: Driver did not initialize current freq\n");
1556 policy->cur = new_freq;
1557 } else if (policy->cur != new_freq && has_target()) {
1558 cpufreq_out_of_sync(policy, new_freq);
1559 }
1560
1561 return new_freq;
1562}
1563
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001564static struct subsys_interface cpufreq_interface = {
1565 .name = "cpufreq",
1566 .subsys = &cpu_subsys,
1567 .add_dev = cpufreq_add_dev,
1568 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001569};
1570
Viresh Kumare28867e2014-03-04 11:00:27 +08001571/*
1572 * In case platform wants some specific frequency to be configured
1573 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001574 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001575int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001576{
Viresh Kumare28867e2014-03-04 11:00:27 +08001577 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001578
Viresh Kumare28867e2014-03-04 11:00:27 +08001579 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001580 pr_debug("%s: suspend_freq not defined\n", __func__);
1581 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001582 }
1583
Viresh Kumare28867e2014-03-04 11:00:27 +08001584 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1585 policy->suspend_freq);
1586
1587 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1588 CPUFREQ_RELATION_H);
1589 if (ret)
1590 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1591 __func__, policy->suspend_freq, ret);
1592
Dave Jonesc9060492008-02-07 16:32:18 -05001593 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001594}
Viresh Kumare28867e2014-03-04 11:00:27 +08001595EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001596
1597/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001598 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001600 * Called during system wide Suspend/Hibernate cycles for suspending governors
1601 * as some platforms can't change frequency after this point in suspend cycle.
1602 * Because some of the devices (like: i2c, regulators, etc) they use for
1603 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001605void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301607 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001609 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001612 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301613 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001615 pr_debug("%s: Suspending Governors\n", __func__);
1616
Viresh Kumarf9637352015-05-12 12:20:11 +05301617 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001618 if (has_target()) {
1619 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001620 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001621 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001622 }
1623
1624 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001625 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1626 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301628
1629suspend:
1630 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001634 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001636 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1637 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001639void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301642 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001644 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return;
1646
Lan Tianyu8e304442014-09-18 15:03:07 +08001647 cpufreq_suspended = false;
1648
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001649 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001650 return;
1651
1652 pr_debug("%s: Resuming Governors\n", __func__);
1653
Viresh Kumarf9637352015-05-12 12:20:11 +05301654 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301655 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301656 pr_err("%s: Failed to resume driver: %p\n", __func__,
1657 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001658 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301659 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001660 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301661 up_write(&policy->rwsem);
1662
1663 if (ret)
1664 pr_err("%s: Failed to start governor for policy: %p\n",
1665 __func__, policy);
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Borislav Petkov9d950462013-01-20 10:24:28 +00001670/**
1671 * cpufreq_get_current_driver - return current driver's name
1672 *
1673 * Return the name string of the currently loaded cpufreq driver
1674 * or NULL, if none.
1675 */
1676const char *cpufreq_get_current_driver(void)
1677{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001678 if (cpufreq_driver)
1679 return cpufreq_driver->name;
1680
1681 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001682}
1683EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001685/**
1686 * cpufreq_get_driver_data - return current driver data
1687 *
1688 * Return the private data of the currently loaded cpufreq
1689 * driver, or NULL if no cpufreq driver is loaded.
1690 */
1691void *cpufreq_get_driver_data(void)
1692{
1693 if (cpufreq_driver)
1694 return cpufreq_driver->driver_data;
1695
1696 return NULL;
1697}
1698EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700/*********************************************************************
1701 * NOTIFIER LISTS INTERFACE *
1702 *********************************************************************/
1703
1704/**
1705 * cpufreq_register_notifier - register a driver with cpufreq
1706 * @nb: notifier function to register
1707 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1708 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001709 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 * are notified about clock rate changes (once before and once after
1711 * the transition), or a list of drivers that are notified about
1712 * changes in cpufreq policy.
1713 *
1714 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001715 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
1717int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1718{
1719 int ret;
1720
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001721 if (cpufreq_disabled())
1722 return -EINVAL;
1723
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001724 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 switch (list) {
1727 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001728 mutex_lock(&cpufreq_fast_switch_lock);
1729
1730 if (cpufreq_fast_switch_count > 0) {
1731 mutex_unlock(&cpufreq_fast_switch_lock);
1732 return -EBUSY;
1733 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001734 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001735 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001736 if (!ret)
1737 cpufreq_fast_switch_count--;
1738
1739 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001742 ret = blocking_notifier_chain_register(
1743 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745 default:
1746 ret = -EINVAL;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 return ret;
1750}
1751EXPORT_SYMBOL(cpufreq_register_notifier);
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753/**
1754 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1755 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301756 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 *
1758 * Remove a driver from the CPU frequency notifier list.
1759 *
1760 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001761 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
1763int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1764{
1765 int ret;
1766
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001767 if (cpufreq_disabled())
1768 return -EINVAL;
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 switch (list) {
1771 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001772 mutex_lock(&cpufreq_fast_switch_lock);
1773
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001774 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001775 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001776 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1777 cpufreq_fast_switch_count++;
1778
1779 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001782 ret = blocking_notifier_chain_unregister(
1783 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 break;
1785 default:
1786 ret = -EINVAL;
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
1789 return ret;
1790}
1791EXPORT_SYMBOL(cpufreq_unregister_notifier);
1792
1793
1794/*********************************************************************
1795 * GOVERNORS *
1796 *********************************************************************/
1797
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001798/**
1799 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1800 * @policy: cpufreq policy to switch the frequency for.
1801 * @target_freq: New frequency to set (may be approximate).
1802 *
1803 * Carry out a fast frequency switch without sleeping.
1804 *
1805 * The driver's ->fast_switch() callback invoked by this function must be
1806 * suitable for being called from within RCU-sched read-side critical sections
1807 * and it is expected to select the minimum available frequency greater than or
1808 * equal to @target_freq (CPUFREQ_RELATION_L).
1809 *
1810 * This function must not be called if policy->fast_switch_enabled is unset.
1811 *
1812 * Governors calling this function must guarantee that it will never be invoked
1813 * twice in parallel for the same policy and that it will never be called in
1814 * parallel with either ->target() or ->target_index() for the same policy.
1815 *
1816 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1817 * callback to indicate an error condition, the hardware configuration must be
1818 * preserved.
1819 */
1820unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1821 unsigned int target_freq)
1822{
1823 clamp_val(target_freq, policy->min, policy->max);
1824
1825 return cpufreq_driver->fast_switch(policy, target_freq);
1826}
1827EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1828
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301829/* Must set freqs->new to intermediate frequency */
1830static int __target_intermediate(struct cpufreq_policy *policy,
1831 struct cpufreq_freqs *freqs, int index)
1832{
1833 int ret;
1834
1835 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1836
1837 /* We don't need to switch to intermediate freq */
1838 if (!freqs->new)
1839 return 0;
1840
1841 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1842 __func__, policy->cpu, freqs->old, freqs->new);
1843
1844 cpufreq_freq_transition_begin(policy, freqs);
1845 ret = cpufreq_driver->target_intermediate(policy, index);
1846 cpufreq_freq_transition_end(policy, freqs, ret);
1847
1848 if (ret)
1849 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1850 __func__, ret);
1851
1852 return ret;
1853}
1854
Viresh Kumar23727842016-06-03 10:58:50 +05301855static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301856{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301857 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1858 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301859 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301860 int retval = -EINVAL;
1861 bool notify;
1862
Viresh Kumar23727842016-06-03 10:58:50 +05301863 if (newfreq == policy->cur)
1864 return 0;
1865
Viresh Kumar8d657752014-05-21 14:29:29 +05301866 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301867 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301868 /* Handle switching to intermediate frequency */
1869 if (cpufreq_driver->get_intermediate) {
1870 retval = __target_intermediate(policy, &freqs, index);
1871 if (retval)
1872 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301873
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301874 intermediate_freq = freqs.new;
1875 /* Set old freq to intermediate */
1876 if (intermediate_freq)
1877 freqs.old = freqs.new;
1878 }
1879
Viresh Kumar23727842016-06-03 10:58:50 +05301880 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301881 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1882 __func__, policy->cpu, freqs.old, freqs.new);
1883
1884 cpufreq_freq_transition_begin(policy, &freqs);
1885 }
1886
1887 retval = cpufreq_driver->target_index(policy, index);
1888 if (retval)
1889 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1890 retval);
1891
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301892 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301893 cpufreq_freq_transition_end(policy, &freqs, retval);
1894
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301895 /*
1896 * Failed after setting to intermediate freq? Driver should have
1897 * reverted back to initial frequency and so should we. Check
1898 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301899 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301900 */
1901 if (unlikely(retval && intermediate_freq)) {
1902 freqs.old = intermediate_freq;
1903 freqs.new = policy->restore_freq;
1904 cpufreq_freq_transition_begin(policy, &freqs);
1905 cpufreq_freq_transition_end(policy, &freqs, 0);
1906 }
1907 }
1908
Viresh Kumar8d657752014-05-21 14:29:29 +05301909 return retval;
1910}
1911
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912int __cpufreq_driver_target(struct cpufreq_policy *policy,
1913 unsigned int target_freq,
1914 unsigned int relation)
1915{
Viresh Kumar72499242012-10-31 01:28:21 +01001916 unsigned int old_target_freq = target_freq;
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001917 int index, retval;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001918
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001919 if (cpufreq_disabled())
1920 return -ENODEV;
1921
Viresh Kumar72499242012-10-31 01:28:21 +01001922 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05301923 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01001924
1925 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001926 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001927
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301928 /*
1929 * This might look like a redundant call as we are checking it again
1930 * after finding index. But it is left intentionally for cases where
1931 * exactly same freq is called again and so we can save on few function
1932 * calls.
1933 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001934 if (target_freq == policy->cur)
1935 return 0;
1936
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301937 /* Save last value to restore later on errors */
1938 policy->restore_freq = policy->cur;
1939
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001940 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001941 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001942
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001943 if (!cpufreq_driver->target_index)
1944 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301945
Viresh Kumar7ab4aab2016-06-03 10:58:49 +05301946 retval = cpufreq_frequency_table_target(policy, target_freq, relation,
1947 &index);
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001948 if (unlikely(retval)) {
1949 pr_err("%s: Unable to find matching freq\n", __func__);
1950 return retval;
1951 }
1952
Viresh Kumar23727842016-06-03 10:58:50 +05301953 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957int cpufreq_driver_target(struct cpufreq_policy *policy,
1958 unsigned int target_freq,
1959 unsigned int relation)
1960{
Julia Lawallf1829e42008-07-25 22:44:53 +02001961 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
viresh kumarad7722d2013-10-18 19:10:15 +05301963 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 ret = __cpufreq_driver_target(policy, target_freq, relation);
1966
viresh kumarad7722d2013-10-18 19:10:15 +05301967 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 return ret;
1970}
1971EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1972
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001973__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1974{
1975 return NULL;
1976}
1977
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02001978static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Dave Jonescc993ca2005-07-28 09:43:56 -07001980 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001981
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001982 /* Don't start any governor operations if we are entering suspend */
1983 if (cpufreq_suspended)
1984 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09001985 /*
1986 * Governor might not be initiated here if ACPI _PPC changed
1987 * notification happened, so check it.
1988 */
1989 if (!policy->governor)
1990 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001991
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02001992 if (policy->governor->max_transition_latency &&
1993 policy->cpuinfo.transition_latency >
1994 policy->governor->max_transition_latency) {
1995 struct cpufreq_governor *gov = cpufreq_fallback_governor();
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001996
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02001997 if (gov) {
1998 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
1999 policy->governor->name, gov->name);
2000 policy->governor = gov;
2001 } else {
Viresh Kumarfe492f32013-08-06 22:53:10 +05302002 return -EINVAL;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002003 }
Rafael J. Wysocki9e8c0a82016-05-14 01:00:28 +02002004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002006 if (!try_module_get(policy->governor->owner))
2007 return -EINVAL;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002008
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002009 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002011 if (policy->governor->init) {
2012 ret = policy->governor->init(policy);
2013 if (ret) {
2014 module_put(policy->governor->owner);
2015 return ret;
2016 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002019 return 0;
2020}
2021
2022static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2023{
2024 if (cpufreq_suspended || !policy->governor)
2025 return;
2026
2027 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2028
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002029 if (policy->governor->exit)
2030 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002031
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002032 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033}
2034
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002035static int cpufreq_start_governor(struct cpufreq_policy *policy)
2036{
2037 int ret;
2038
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002039 if (cpufreq_suspended)
2040 return 0;
2041
2042 if (!policy->governor)
2043 return -EINVAL;
2044
2045 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2046
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002047 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2048 cpufreq_update_current_freq(policy);
2049
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002050 if (policy->governor->start) {
2051 ret = policy->governor->start(policy);
2052 if (ret)
2053 return ret;
2054 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002055
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002056 if (policy->governor->limits)
2057 policy->governor->limits(policy);
2058
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002059 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002060}
2061
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002062static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2063{
2064 if (cpufreq_suspended || !policy->governor)
2065 return;
2066
2067 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2068
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002069 if (policy->governor->stop)
2070 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002071}
2072
2073static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2074{
2075 if (cpufreq_suspended || !policy->governor)
2076 return;
2077
2078 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2079
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002080 if (policy->governor->limits)
2081 policy->governor->limits(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002082}
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084int cpufreq_register_governor(struct cpufreq_governor *governor)
2085{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002086 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 if (!governor)
2089 return -EINVAL;
2090
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002091 if (cpufreq_disabled())
2092 return -ENODEV;
2093
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002094 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002095
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002096 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302097 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002098 err = 0;
2099 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Dave Jones32ee8c32006-02-28 00:43:23 -05002102 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2108{
Viresh Kumar45732372015-05-12 12:22:34 +05302109 struct cpufreq_policy *policy;
2110 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (!governor)
2113 return;
2114
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002115 if (cpufreq_disabled())
2116 return;
2117
Viresh Kumar45732372015-05-12 12:22:34 +05302118 /* clear last_governor for all inactive policies */
2119 read_lock_irqsave(&cpufreq_driver_lock, flags);
2120 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302121 if (!strcmp(policy->last_governor, governor->name)) {
2122 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302123 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302124 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002125 }
Viresh Kumar45732372015-05-12 12:22:34 +05302126 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002127
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002128 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002130 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return;
2132}
2133EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2134
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136/*********************************************************************
2137 * POLICY INTERFACE *
2138 *********************************************************************/
2139
2140/**
2141 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002142 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2143 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 *
2145 * Reads the current cpufreq policy.
2146 */
2147int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2148{
2149 struct cpufreq_policy *cpu_policy;
2150 if (!policy)
2151 return -EINVAL;
2152
2153 cpu_policy = cpufreq_cpu_get(cpu);
2154 if (!cpu_policy)
2155 return -EINVAL;
2156
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302157 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161}
2162EXPORT_SYMBOL(cpufreq_get_policy);
2163
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002164/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302165 * policy : current policy.
2166 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002167 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302168static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302169 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002171 struct cpufreq_governor *old_gov;
2172 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Joe Perchese837f9b2014-03-11 10:03:00 -07002174 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2175 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302177 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Pan Xinhuifba95732015-07-30 18:10:40 +08002179 /*
2180 * This check works well when we store new min/max freq attributes,
2181 * because new_policy is a copy of policy with one field updated.
2182 */
2183 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002184 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302187 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002189 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002192 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302193 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Viresh Kumarbb176f72013-06-19 14:19:33 +05302195 /*
2196 * verify the cpu speed can be set within this limit, which might be
2197 * different to the first one
2198 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302199 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002200 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002201 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002204 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302205 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302207 policy->min = new_policy->min;
2208 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002210 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002211 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002213 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302214 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002215 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002216 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002219 if (new_policy->governor == policy->governor) {
2220 pr_debug("cpufreq: governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002221 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002222 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002223 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002224
2225 pr_debug("governor switch\n");
2226
2227 /* save old, working values */
2228 old_gov = policy->governor;
2229 /* end old governor */
2230 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002231 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002232 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002233 }
2234
2235 /* start new governor */
2236 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002237 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302238 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002239 ret = cpufreq_start_governor(policy);
2240 if (!ret) {
2241 pr_debug("cpufreq: governor change\n");
2242 return 0;
2243 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002244 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002245 }
2246
2247 /* new governor failed, so re-start old one */
2248 pr_debug("starting governor %s failed\n", policy->governor->name);
2249 if (old_gov) {
2250 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002251 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302252 policy->governor = NULL;
2253 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002254 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002255 }
2256
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302257 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2262 * @cpu: CPU which shall be re-evaluated
2263 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002264 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * at different times.
2266 */
2267int cpufreq_update_policy(unsigned int cpu)
2268{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302269 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2270 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002271 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002273 if (!policy)
2274 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
viresh kumarad7722d2013-10-18 19:10:15 +05302276 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002278 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302279 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302280 new_policy.min = policy->user_policy.min;
2281 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Viresh Kumarbb176f72013-06-19 14:19:33 +05302283 /*
2284 * BIOS might change freq behind our back
2285 * -> ask driver for current freq and notify governors about a change
2286 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002287 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002288 new_policy.cur = cpufreq_update_current_freq(policy);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302289 if (WARN_ON(!new_policy.cur)) {
2290 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002291 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302292 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002293 }
2294
Viresh Kumar037ce832013-10-02 14:13:16 +05302295 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002297unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302298 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002299
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302300 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 return ret;
2302}
2303EXPORT_SYMBOL(cpufreq_update_policy);
2304
Paul Gortmaker27609842013-06-19 13:54:04 -04002305static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002306 unsigned long action, void *hcpu)
2307{
2308 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002309
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002310 switch (action & ~CPU_TASKS_FROZEN) {
2311 case CPU_ONLINE:
Rafael J. Wysockicd73e9b2016-04-07 03:30:40 +02002312 case CPU_DOWN_FAILED:
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002313 cpufreq_online(cpu);
2314 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302315
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002316 case CPU_DOWN_PREPARE:
Viresh Kumar69cee712016-02-11 17:31:11 +05302317 cpufreq_offline(cpu);
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002318 break;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002319 }
2320 return NOTIFY_OK;
2321}
2322
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002323static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302324 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002325};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002328 * BOOST *
2329 *********************************************************************/
2330static int cpufreq_boost_set_sw(int state)
2331{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002332 struct cpufreq_policy *policy;
2333 int ret = -EINVAL;
2334
Viresh Kumarf9637352015-05-12 12:20:11 +05302335 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302336 if (!policy->freq_table)
2337 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302338
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302339 ret = cpufreq_frequency_table_cpuinfo(policy,
2340 policy->freq_table);
2341 if (ret) {
2342 pr_err("%s: Policy frequency update failed\n",
2343 __func__);
2344 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002345 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302346
2347 down_write(&policy->rwsem);
2348 policy->user_policy.max = policy->max;
2349 cpufreq_governor_limits(policy);
2350 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002351 }
2352
2353 return ret;
2354}
2355
2356int cpufreq_boost_trigger_state(int state)
2357{
2358 unsigned long flags;
2359 int ret = 0;
2360
2361 if (cpufreq_driver->boost_enabled == state)
2362 return 0;
2363
2364 write_lock_irqsave(&cpufreq_driver_lock, flags);
2365 cpufreq_driver->boost_enabled = state;
2366 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2367
2368 ret = cpufreq_driver->set_boost(state);
2369 if (ret) {
2370 write_lock_irqsave(&cpufreq_driver_lock, flags);
2371 cpufreq_driver->boost_enabled = !state;
2372 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2373
Joe Perchese837f9b2014-03-11 10:03:00 -07002374 pr_err("%s: Cannot %s BOOST\n",
2375 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002376 }
2377
2378 return ret;
2379}
2380
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002381static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002382{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002383 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002384}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002385
Viresh Kumar44139ed2015-07-29 16:23:09 +05302386static int create_boost_sysfs_file(void)
2387{
2388 int ret;
2389
Viresh Kumarc82bd442015-10-15 21:35:23 +05302390 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302391 if (ret)
2392 pr_err("%s: cannot register global BOOST sysfs file\n",
2393 __func__);
2394
2395 return ret;
2396}
2397
2398static void remove_boost_sysfs_file(void)
2399{
2400 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302401 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302402}
2403
2404int cpufreq_enable_boost_support(void)
2405{
2406 if (!cpufreq_driver)
2407 return -EINVAL;
2408
2409 if (cpufreq_boost_supported())
2410 return 0;
2411
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002412 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302413
2414 /* This will get removed on driver unregister */
2415 return create_boost_sysfs_file();
2416}
2417EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2418
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002419int cpufreq_boost_enabled(void)
2420{
2421 return cpufreq_driver->boost_enabled;
2422}
2423EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2424
2425/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2427 *********************************************************************/
2428
2429/**
2430 * cpufreq_register_driver - register a CPU Frequency driver
2431 * @driver_data: A struct cpufreq_driver containing the values#
2432 * submitted by the CPU Frequency driver.
2433 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302434 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002435 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002436 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 *
2438 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002439int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
2441 unsigned long flags;
2442 int ret;
2443
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002444 if (cpufreq_disabled())
2445 return -ENODEV;
2446
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302448 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002449 driver_data->target) ||
2450 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302451 driver_data->target)) ||
2452 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 return -EINVAL;
2454
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002455 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002457 /* Protect against concurrent CPU online/offline. */
2458 get_online_cpus();
2459
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002460 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002461 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002462 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002463 ret = -EEXIST;
2464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002466 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002467 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302469 if (driver_data->setpolicy)
2470 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2471
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002472 if (cpufreq_boost_supported()) {
2473 ret = create_boost_sysfs_file();
2474 if (ret)
2475 goto err_null_driver;
2476 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002477
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002478 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002479 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002480 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302482 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2483 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302485 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2486 driver_data->name);
2487 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002490 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002491 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002492 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002493
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002494err_if_unreg:
2495 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002496err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302497 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002498err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002499 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002500 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002501 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002502out:
2503 put_online_cpus();
2504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508/**
2509 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2510 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302511 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * the right to do so, i.e. if you have succeeded in initialising before!
2513 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2514 * currently not initialised.
2515 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002516int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
2518 unsigned long flags;
2519
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002520 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002523 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002525 /* Protect against concurrent cpu hotplug */
2526 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002527 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302528 remove_boost_sysfs_file();
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002529 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002531 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302532
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002533 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302534
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002535 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002536 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
2538 return 0;
2539}
2540EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002541
Doug Anderson90de2a42014-12-23 22:09:48 -08002542/*
2543 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2544 * or mutexes when secondary CPUs are halted.
2545 */
2546static struct syscore_ops cpufreq_syscore_ops = {
2547 .shutdown = cpufreq_suspend,
2548};
2549
Viresh Kumarc82bd442015-10-15 21:35:23 +05302550struct kobject *cpufreq_global_kobject;
2551EXPORT_SYMBOL(cpufreq_global_kobject);
2552
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002553static int __init cpufreq_core_init(void)
2554{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002555 if (cpufreq_disabled())
2556 return -ENODEV;
2557
Viresh Kumar8eec1022015-10-15 21:35:22 +05302558 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002559 BUG_ON(!cpufreq_global_kobject);
2560
Doug Anderson90de2a42014-12-23 22:09:48 -08002561 register_syscore_ops(&cpufreq_syscore_ops);
2562
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002563 return 0;
2564}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002565core_initcall(cpufreq_core_init);