blob: 4d76b7c57b7a4ed5259c8f2d388c5d102f126562 [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
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100135 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000136
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)
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100146 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000147
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100148 return div_u64(idle_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000149}
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
Dietmar Eggemanne7d54592017-09-26 17:41:07 +0100164__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
165 unsigned long max_freq)
166{
167}
168EXPORT_SYMBOL_GPL(arch_set_freq_scale);
169
Viresh Kumar70e9e772013-10-03 20:29:07 +0530170/*
171 * This is a generic cpufreq init() routine which can be used by cpufreq
172 * drivers of SMP systems. It will do following:
173 * - validate & show freq table passed
174 * - set policies transition latency
175 * - policy->cpus with all possible CPUs
176 */
177int cpufreq_generic_init(struct cpufreq_policy *policy,
178 struct cpufreq_frequency_table *table,
179 unsigned int transition_latency)
180{
181 int ret;
182
183 ret = cpufreq_table_validate_and_show(policy, table);
184 if (ret) {
185 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
186 return ret;
187 }
188
189 policy->cpuinfo.transition_latency = transition_latency;
190
191 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530192 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530193 * share the clock and voltage and clock.
194 */
195 cpumask_setall(policy->cpus);
196
197 return 0;
198}
199EXPORT_SYMBOL_GPL(cpufreq_generic_init);
200
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200201struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530202{
203 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
204
Viresh Kumar988bed02015-05-08 11:53:45 +0530205 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
206}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200207EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530208
209unsigned int cpufreq_generic_get(unsigned int cpu)
210{
211 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
212
Viresh Kumar652ed952014-01-09 20:38:43 +0530213 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700214 pr_err("%s: No %s associated to cpu: %d\n",
215 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530216 return 0;
217 }
218
219 return clk_get_rate(policy->clk) / 1000;
220}
221EXPORT_SYMBOL_GPL(cpufreq_generic_get);
222
Viresh Kumar50e9c852015-02-19 17:02:03 +0530223/**
224 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
225 *
226 * @cpu: cpu to find policy for.
227 *
228 * This returns policy for 'cpu', returns NULL if it doesn't exist.
229 * It also increments the kobject reference count to mark it busy and so would
230 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
231 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
232 * freed as that depends on the kobj count.
233 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530234 * Return: A valid policy on success, otherwise NULL on failure.
235 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530236struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530238 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 unsigned long flags;
240
Viresh Kumar1b947c92015-02-19 17:02:05 +0530241 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530242 return NULL;
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000245 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Viresh Kumar6eed9402013-08-06 22:53:11 +0530247 if (cpufreq_driver) {
248 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530249 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530250 if (policy)
251 kobject_get(&policy->kobj);
252 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200253
Viresh Kumar6eed9402013-08-06 22:53:11 +0530254 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530256 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
259
Viresh Kumar50e9c852015-02-19 17:02:03 +0530260/**
261 * cpufreq_cpu_put: Decrements the usage count of a policy
262 *
263 * @policy: policy earlier returned by cpufreq_cpu_get().
264 *
265 * This decrements the kobject reference count incremented earlier by calling
266 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530267 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530268void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530270 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
276 *********************************************************************/
277
278/**
279 * adjust_jiffies - adjust the system "loops_per_jiffy"
280 *
281 * This function alters the system "loops_per_jiffy" for the clock
282 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500283 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * per-CPU loops_per_jiffy value wherever possible.
285 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800286static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530288#ifndef CONFIG_SMP
289 static unsigned long l_p_j_ref;
290 static unsigned int l_p_j_ref_freq;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (ci->flags & CPUFREQ_CONST_LOOPS)
293 return;
294
295 if (!l_p_j_ref_freq) {
296 l_p_j_ref = loops_per_jiffy;
297 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700298 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
299 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530301 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530302 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
303 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700304 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
305 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530310static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530311 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 BUG_ON(irqs_disabled());
314
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000315 if (cpufreq_disabled())
316 return;
317
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200318 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200319 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700320 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500325 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800326 * which is not equal to what the cpufreq core thinks is
327 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200329 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800330 if ((policy) && (policy->cpu == freqs->cpu) &&
331 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700332 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
333 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800334 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700337 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800338 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
340 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 case CPUFREQ_POSTCHANGE:
343 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700344 pr_debug("FREQ: %lu - CPU: %lu\n",
345 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100346 trace_cpu_frequency(freqs->new, freqs->cpu);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200347 cpufreq_stats_record_transition(policy, freqs->new);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800349 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800350 if (likely(policy) && likely(policy->cpu == freqs->cpu))
351 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530355
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530356/**
357 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
358 * on frequency transition.
359 *
360 * This function calls the transition notifiers and the "adjust_jiffies"
361 * function. It is called twice on all CPU frequency changes that have
362 * external effects.
363 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530364static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530365 struct cpufreq_freqs *freqs, unsigned int state)
366{
367 for_each_cpu(freqs->cpu, policy->cpus)
368 __cpufreq_notify_transition(policy, freqs, state);
369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530371/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530372static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530373 struct cpufreq_freqs *freqs, int transition_failed)
374{
375 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
376 if (!transition_failed)
377 return;
378
379 swap(freqs->old, freqs->new);
380 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
381 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
382}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530383
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530384void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
385 struct cpufreq_freqs *freqs)
386{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530387
388 /*
389 * Catch double invocations of _begin() which lead to self-deadlock.
390 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
391 * doesn't invoke _begin() on their behalf, and hence the chances of
392 * double invocations are very low. Moreover, there are scenarios
393 * where these checks can emit false-positive warnings in these
394 * drivers; so we avoid that by skipping them altogether.
395 */
396 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
397 && current == policy->transition_task);
398
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530399wait:
400 wait_event(policy->transition_wait, !policy->transition_ongoing);
401
402 spin_lock(&policy->transition_lock);
403
404 if (unlikely(policy->transition_ongoing)) {
405 spin_unlock(&policy->transition_lock);
406 goto wait;
407 }
408
409 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530410 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530411
412 spin_unlock(&policy->transition_lock);
413
414 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
415}
416EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
417
418void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
419 struct cpufreq_freqs *freqs, int transition_failed)
420{
421 if (unlikely(WARN_ON(!policy->transition_ongoing)))
422 return;
423
424 cpufreq_notify_post_transition(policy, freqs, transition_failed);
425
426 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530427 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530428
429 wake_up(&policy->transition_wait);
430}
431EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
432
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200433/*
434 * Fast frequency switching status count. Positive means "enabled", negative
435 * means "disabled" and 0 means "not decided yet".
436 */
437static int cpufreq_fast_switch_count;
438static DEFINE_MUTEX(cpufreq_fast_switch_lock);
439
440static void cpufreq_list_transition_notifiers(void)
441{
442 struct notifier_block *nb;
443
444 pr_info("Registered transition notifiers:\n");
445
446 mutex_lock(&cpufreq_transition_notifier_list.mutex);
447
448 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
449 pr_info("%pF\n", nb->notifier_call);
450
451 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
452}
453
454/**
455 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
456 * @policy: cpufreq policy to enable fast frequency switching for.
457 *
458 * Try to enable fast frequency switching for @policy.
459 *
460 * The attempt will fail if there is at least one transition notifier registered
461 * at this point, as fast frequency switching is quite fundamentally at odds
462 * with transition notifiers. Thus if successful, it will make registration of
463 * transition notifiers fail going forward.
464 */
465void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
466{
467 lockdep_assert_held(&policy->rwsem);
468
469 if (!policy->fast_switch_possible)
470 return;
471
472 mutex_lock(&cpufreq_fast_switch_lock);
473 if (cpufreq_fast_switch_count >= 0) {
474 cpufreq_fast_switch_count++;
475 policy->fast_switch_enabled = true;
476 } else {
477 pr_warn("CPU%u: Fast frequency switching not enabled\n",
478 policy->cpu);
479 cpufreq_list_transition_notifiers();
480 }
481 mutex_unlock(&cpufreq_fast_switch_lock);
482}
483EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
484
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200485/**
486 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
487 * @policy: cpufreq policy to disable fast frequency switching for.
488 */
489void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200490{
491 mutex_lock(&cpufreq_fast_switch_lock);
492 if (policy->fast_switch_enabled) {
493 policy->fast_switch_enabled = false;
494 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
495 cpufreq_fast_switch_count--;
496 }
497 mutex_unlock(&cpufreq_fast_switch_lock);
498}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200499EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Steve Mucklee3c06232016-07-13 13:25:25 -0700501/**
502 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
503 * one.
504 * @target_freq: target frequency to resolve.
505 *
506 * The target to driver frequency mapping is cached in the policy.
507 *
508 * Return: Lowest driver-supported frequency greater than or equal to the
509 * given target_freq, subject to policy (min/max) and driver limitations.
510 */
511unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
512 unsigned int target_freq)
513{
514 target_freq = clamp_val(target_freq, policy->min, policy->max);
515 policy->cached_target_freq = target_freq;
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700516
517 if (cpufreq_driver->target_index) {
518 int idx;
519
520 idx = cpufreq_frequency_table_target(policy, target_freq,
521 CPUFREQ_RELATION_L);
522 policy->cached_resolved_idx = idx;
523 return policy->freq_table[idx].frequency;
524 }
525
Steve Mucklee3c06232016-07-13 13:25:25 -0700526 if (cpufreq_driver->resolve_freq)
527 return cpufreq_driver->resolve_freq(policy, target_freq);
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700528
529 return target_freq;
Steve Mucklee3c06232016-07-13 13:25:25 -0700530}
Steve Muckleae2c1ca2016-07-21 19:24:38 -0700531EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
Steve Mucklee3c06232016-07-13 13:25:25 -0700532
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530533unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
534{
535 unsigned int latency;
536
537 if (policy->transition_delay_us)
538 return policy->transition_delay_us;
539
540 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Viresh Kumare948bc82017-08-17 09:12:27 +0530541 if (latency) {
542 /*
543 * For platforms that can change the frequency very fast (< 10
544 * us), the above formula gives a decent transition delay. But
545 * for platforms where transition_latency is in milliseconds, it
546 * ends up giving unrealistic values.
547 *
548 * Cap the default transition delay to 10 ms, which seems to be
549 * a reasonable amount of time after which we should reevaluate
550 * the frequency.
551 */
552 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
553 }
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530554
555 return LATENCY_MULTIPLIER;
556}
557EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/*********************************************************************
560 * SYSFS INTERFACE *
561 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530562static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100563 struct attribute *attr, char *buf)
564{
565 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
566}
567
568static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
569 const char *buf, size_t count)
570{
571 int ret, enable;
572
573 ret = sscanf(buf, "%d", &enable);
574 if (ret != 1 || enable < 0 || enable > 1)
575 return -EINVAL;
576
577 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700578 pr_err("%s: Cannot %s BOOST!\n",
579 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100580 return -EINVAL;
581 }
582
Joe Perchese837f9b2014-03-11 10:03:00 -0700583 pr_debug("%s: cpufreq BOOST %s\n",
584 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100585
586 return count;
587}
588define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530590static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700591{
592 struct cpufreq_governor *t;
593
Viresh Kumarf7b27062015-01-27 14:06:09 +0530594 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200595 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700596 return t;
597
598 return NULL;
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601/**
602 * cpufreq_parse_governor - parse a governor string
603 */
Dave Jones905d77c2008-03-05 14:28:32 -0500604static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 struct cpufreq_governor **governor)
606{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200607 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200608 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *policy = CPUFREQ_POLICY_PERFORMANCE;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100610 return 0;
611 }
612
613 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *policy = CPUFREQ_POLICY_POWERSAVE;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530617 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700619
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800620 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700621
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530622 t = find_governor(str_governor);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100623 if (!t) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700624 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700625
Kees Cook1a8e1462011-05-04 08:38:56 -0700626 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100627
Kees Cook1a8e1462011-05-04 08:38:56 -0700628 ret = request_module("cpufreq_%s", str_governor);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100629 if (ret)
630 return -EINVAL;
631
Kees Cook1a8e1462011-05-04 08:38:56 -0700632 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700633
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100634 t = find_governor(str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700636
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800637 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100638
639 if (t) {
640 *governor = t;
641 return 0;
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100644
645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530649 * cpufreq_per_cpu_attr_read() / show_##file_name() -
650 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 *
652 * Write out information from cpufreq_driver->policy[cpu]; object must be
653 * "unsigned int".
654 */
655
Dave Jones32ee8c32006-02-28 00:43:23 -0500656#define show_one(file_name, object) \
657static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500658(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500659{ \
Dave Jones29464f22009-01-18 01:37:11 -0500660 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663show_one(cpuinfo_min_freq, cpuinfo.min_freq);
664show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100665show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666show_one(scaling_min_freq, min);
667show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700668
Len Brownf8475ce2017-06-23 22:11:52 -0700669__weak unsigned int arch_freq_get_on_cpu(int cpu)
670{
671 return 0;
672}
673
Viresh Kumar09347b22015-01-02 12:34:24 +0530674static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700675{
676 ssize_t ret;
Len Brownf8475ce2017-06-23 22:11:52 -0700677 unsigned int freq;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700678
Len Brownf8475ce2017-06-23 22:11:52 -0700679 freq = arch_freq_get_on_cpu(policy->cpu);
680 if (freq)
681 ret = sprintf(buf, "%u\n", freq);
682 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
683 cpufreq_driver->get)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700684 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
685 else
686 ret = sprintf(buf, "%u\n", policy->cur);
687 return ret;
688}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Viresh Kumar037ce832013-10-02 14:13:16 +0530690static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530691 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/**
694 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
695 */
696#define store_one(file_name, object) \
697static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500698(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800700 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct cpufreq_policy new_policy; \
702 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530703 memcpy(&new_policy, policy, sizeof(*policy)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 \
Dave Jones29464f22009-01-18 01:37:11 -0500705 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 if (ret != 1) \
707 return -EINVAL; \
708 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800709 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530710 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800711 if (!ret) \
712 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 \
714 return ret ? ret : count; \
715}
716
Dave Jones29464f22009-01-18 01:37:11 -0500717store_one(scaling_min_freq, min);
718store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720/**
721 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
722 */
Dave Jones905d77c2008-03-05 14:28:32 -0500723static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
724 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530726 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysocki9b4f6032017-03-15 00:12:16 +0100727
728 if (cur_freq)
729 return sprintf(buf, "%u\n", cur_freq);
730
731 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734/**
735 * show_scaling_governor - show the current policy for the specified CPU
736 */
Dave Jones905d77c2008-03-05 14:28:32 -0500737static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Dave Jones29464f22009-01-18 01:37:11 -0500739 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return sprintf(buf, "powersave\n");
741 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
742 return sprintf(buf, "performance\n");
743 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200744 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500745 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return -EINVAL;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/**
750 * store_scaling_governor - store policy for the specified CPU
751 */
Dave Jones905d77c2008-03-05 14:28:32 -0500752static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
753 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530755 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 char str_governor[16];
757 struct cpufreq_policy new_policy;
758
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530759 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Dave Jones29464f22009-01-18 01:37:11 -0500761 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (ret != 1)
763 return -EINVAL;
764
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530765 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
766 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EINVAL;
768
Viresh Kumar037ce832013-10-02 14:13:16 +0530769 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530770 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773/**
774 * show_scaling_driver - show the cpufreq driver currently loaded
775 */
Dave Jones905d77c2008-03-05 14:28:32 -0500776static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200778 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/**
782 * show_scaling_available_governors - show the available CPUfreq governors
783 */
Dave Jones905d77c2008-03-05 14:28:32 -0500784static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
785 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 ssize_t i = 0;
788 struct cpufreq_governor *t;
789
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530790 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 i += sprintf(buf, "performance powersave");
792 goto out;
793 }
794
Viresh Kumarf7b27062015-01-27 14:06:09 +0530795 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500796 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
797 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200799 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500801out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 i += sprintf(&buf[i], "\n");
803 return i;
804}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700805
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800806ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 ssize_t i = 0;
809 unsigned int cpu;
810
Rusty Russell835481d2009-01-04 05:18:06 -0800811 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (i)
813 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
814 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
815 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500816 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 i += sprintf(&buf[i], "\n");
819 return i;
820}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800821EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700823/**
824 * show_related_cpus - show the CPUs affected by each transition even if
825 * hw coordination is in use
826 */
827static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
828{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800829 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700830}
831
832/**
833 * show_affected_cpus - show the CPUs affected by each transition
834 */
835static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
836{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800837 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700838}
839
Venki Pallipadi9e769882007-10-26 10:18:21 -0700840static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500841 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700842{
843 unsigned int freq = 0;
844 unsigned int ret;
845
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700846 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700847 return -EINVAL;
848
849 ret = sscanf(buf, "%u", &freq);
850 if (ret != 1)
851 return -EINVAL;
852
853 policy->governor->store_setspeed(policy, freq);
854
855 return count;
856}
857
858static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
859{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700860 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700861 return sprintf(buf, "<unsupported>\n");
862
863 return policy->governor->show_setspeed(policy, buf);
864}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Thomas Renningere2f74f32009-11-19 12:31:01 +0100866/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200867 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100868 */
869static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
870{
871 unsigned int limit;
872 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200873 if (cpufreq_driver->bios_limit) {
874 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100875 if (!ret)
876 return sprintf(buf, "%u\n", limit);
877 }
878 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
879}
880
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200881cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
882cpufreq_freq_attr_ro(cpuinfo_min_freq);
883cpufreq_freq_attr_ro(cpuinfo_max_freq);
884cpufreq_freq_attr_ro(cpuinfo_transition_latency);
885cpufreq_freq_attr_ro(scaling_available_governors);
886cpufreq_freq_attr_ro(scaling_driver);
887cpufreq_freq_attr_ro(scaling_cur_freq);
888cpufreq_freq_attr_ro(bios_limit);
889cpufreq_freq_attr_ro(related_cpus);
890cpufreq_freq_attr_ro(affected_cpus);
891cpufreq_freq_attr_rw(scaling_min_freq);
892cpufreq_freq_attr_rw(scaling_max_freq);
893cpufreq_freq_attr_rw(scaling_governor);
894cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Jones905d77c2008-03-05 14:28:32 -0500896static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 &cpuinfo_min_freq.attr,
898 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100899 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 &scaling_min_freq.attr,
901 &scaling_max_freq.attr,
902 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700903 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 &scaling_governor.attr,
905 &scaling_driver.attr,
906 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700907 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 NULL
909};
910
Dave Jones29464f22009-01-18 01:37:11 -0500911#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
912#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Dave Jones29464f22009-01-18 01:37:11 -0500914static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Dave Jones905d77c2008-03-05 14:28:32 -0500916 struct cpufreq_policy *policy = to_policy(kobj);
917 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530918 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530919
viresh kumarad7722d2013-10-18 19:10:15 +0530920 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100921 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530922 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return ret;
925}
926
Dave Jones905d77c2008-03-05 14:28:32 -0500927static ssize_t store(struct kobject *kobj, struct attribute *attr,
928 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929{
Dave Jones905d77c2008-03-05 14:28:32 -0500930 struct cpufreq_policy *policy = to_policy(kobj);
931 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500932 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530933
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200934 cpus_read_lock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530935
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100936 if (cpu_online(policy->cpu)) {
937 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530938 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100939 up_write(&policy->rwsem);
940 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530941
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200942 cpus_read_unlock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return ret;
945}
946
Dave Jones905d77c2008-03-05 14:28:32 -0500947static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Dave Jones905d77c2008-03-05 14:28:32 -0500949 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200950 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 complete(&policy->kobj_unregister);
952}
953
Emese Revfy52cf25d2010-01-19 02:58:23 +0100954static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 .show = show,
956 .store = store,
957};
958
959static struct kobj_type ktype_cpufreq = {
960 .sysfs_ops = &sysfs_ops,
961 .default_attrs = default_attrs,
962 .release = cpufreq_sysfs_release,
963};
964
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200965static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +0200966{
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200967 struct device *dev = get_cpu_device(cpu);
968
969 if (!dev)
970 return;
971
972 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
973 return;
974
Viresh Kumar266198042016-09-12 12:07:05 +0530975 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200976 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
977 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +0200978}
979
Viresh Kumar266198042016-09-12 12:07:05 +0530980static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
981 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +0200982{
Viresh Kumar266198042016-09-12 12:07:05 +0530983 dev_dbg(dev, "%s: Removing symlink\n", __func__);
984 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +0200985}
986
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200987static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400988{
989 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400990 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400991
Dave Jones909a6942009-07-08 18:05:42 -0400992 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200993 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530994 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400995 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
996 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100997 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400998 drv_attr++;
999 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001000 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001001 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1002 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001003 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001004 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001005
1006 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1007 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001008 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001009
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001010 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001011 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1012 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001013 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001014 }
Dave Jones909a6942009-07-08 18:05:42 -04001015
Viresh Kumar266198042016-09-12 12:07:05 +05301016 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301017}
1018
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001019__weak struct cpufreq_governor *cpufreq_default_governor(void)
1020{
1021 return NULL;
1022}
1023
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301024static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301025{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001026 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301027 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301028
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301029 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001030
viresh kumar6e2c89d2014-03-04 11:43:59 +08001031 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +05301032 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001033 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +08001034 pr_debug("Restoring governor %s for cpu %d\n",
1035 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001036 } else {
1037 gov = cpufreq_default_governor();
1038 if (!gov)
1039 return -ENODATA;
1040 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001041
1042 new_policy.governor = gov;
1043
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001044 /* Use the default policy if there is no last_policy. */
1045 if (cpufreq_driver->setpolicy) {
1046 if (policy->last_policy)
1047 new_policy.policy = policy->last_policy;
1048 else
1049 cpufreq_parse_governor(gov->name, &new_policy.policy,
1050 NULL);
1051 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001052 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301053 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001054}
1055
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001056static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001057{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301058 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001059
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301060 /* Has this CPU been taken care of already? */
1061 if (cpumask_test_cpu(cpu, policy->cpus))
1062 return 0;
1063
Viresh Kumar49f18562016-02-11 17:31:12 +05301064 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001065 if (has_target())
1066 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001067
Viresh Kumarfcf80582013-01-29 14:39:08 +00001068 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301069
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301070 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001071 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301072 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301073 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001074 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301075 up_write(&policy->rwsem);
1076 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001077}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301079static void handle_update(struct work_struct *work)
1080{
1081 struct cpufreq_policy *policy =
1082 container_of(work, struct cpufreq_policy, update);
1083 unsigned int cpu = policy->cpu;
1084 pr_debug("handle_update for cpu %u called\n", cpu);
1085 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001088static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301089{
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301090 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301091 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301092
1093 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1094 if (!policy)
1095 return NULL;
1096
1097 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1098 goto err_free_policy;
1099
1100 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1101 goto err_free_cpumask;
1102
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001103 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1104 goto err_free_rcpumask;
1105
Viresh Kumaredd4a892016-03-03 14:51:33 +05301106 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1107 cpufreq_global_kobject, "policy%u", cpu);
1108 if (ret) {
1109 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1110 goto err_free_real_cpus;
1111 }
1112
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301113 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301114 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301115 spin_lock_init(&policy->transition_lock);
1116 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301117 init_completion(&policy->kobj_unregister);
1118 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301119
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001120 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301121 return policy;
1122
Viresh Kumaredd4a892016-03-03 14:51:33 +05301123err_free_real_cpus:
1124 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301125err_free_rcpumask:
1126 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301127err_free_cpumask:
1128 free_cpumask_var(policy->cpus);
1129err_free_policy:
1130 kfree(policy);
1131
1132 return NULL;
1133}
1134
Viresh Kumarf9f41e32017-01-05 10:17:27 +05301135static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301136{
1137 struct kobject *kobj;
1138 struct completion *cmp;
1139
Viresh Kumar87549142015-06-10 02:13:21 +02001140 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001141 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301142 kobj = &policy->kobj;
1143 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001144 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301145 kobject_put(kobj);
1146
1147 /*
1148 * We need to make sure that the underlying kobj is
1149 * actually not referenced anymore by anybody before we
1150 * proceed with unloading.
1151 */
1152 pr_debug("waiting for dropping of refcount\n");
1153 wait_for_completion(cmp);
1154 pr_debug("wait complete\n");
1155}
1156
Viresh Kumarf9f41e32017-01-05 10:17:27 +05301157static void cpufreq_policy_free(struct cpufreq_policy *policy)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301158{
Viresh Kumar988bed02015-05-08 11:53:45 +05301159 unsigned long flags;
1160 int cpu;
1161
1162 /* Remove policy from list */
1163 write_lock_irqsave(&cpufreq_driver_lock, flags);
1164 list_del(&policy->policy_list);
1165
1166 for_each_cpu(cpu, policy->related_cpus)
1167 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1168 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1169
Viresh Kumarf9f41e32017-01-05 10:17:27 +05301170 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001171 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301172 free_cpumask_var(policy->related_cpus);
1173 free_cpumask_var(policy->cpus);
1174 kfree(policy);
1175}
1176
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001177static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301179 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001180 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001182 unsigned int j;
1183 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001184
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001185 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301186
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301187 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301188 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001189 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301190 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001191 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001192 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001194 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001195 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001196 down_write(&policy->rwsem);
1197 policy->cpu = cpu;
1198 policy->governor = NULL;
1199 up_write(&policy->rwsem);
1200 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001201 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001202 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001203 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001204 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001205 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301206
Rusty Russell835481d2009-01-04 05:18:06 -08001207 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /* call driver. From then on the cpufreq must be able
1210 * to accept all calls to ->verify and ->setpolicy for this CPU
1211 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001212 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001214 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301215 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001217
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001218 down_write(&policy->rwsem);
1219
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001220 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001221 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301222 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001223 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001224
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001225 /*
1226 * affected cpus must always be the one, which are online. We aren't
1227 * managing offline cpus here.
1228 */
1229 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1230
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001231 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001232 policy->user_policy.min = policy->min;
1233 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001234
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001235 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301236 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001237 add_cpu_dev_symlink(policy, j);
1238 }
Viresh Kumarff010472017-03-21 11:36:06 +05301239 } else {
1240 policy->min = policy->user_policy.min;
1241 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301242 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301243
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001244 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301245 policy->cur = cpufreq_driver->get(policy->cpu);
1246 if (!policy->cur) {
1247 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301248 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301249 }
1250 }
1251
Viresh Kumard3916692013-12-03 11:20:46 +05301252 /*
1253 * Sometimes boot loaders set CPU frequency to a value outside of
1254 * frequency table present with cpufreq core. In such cases CPU might be
1255 * unstable if it has to run on that frequency for long duration of time
1256 * and so its better to set it to a frequency which is specified in
1257 * freq-table. This also makes cpufreq stats inconsistent as
1258 * cpufreq-stats would fail to register because current frequency of CPU
1259 * isn't found in freq-table.
1260 *
1261 * Because we don't want this change to effect boot process badly, we go
1262 * for the next freq which is >= policy->cur ('cur' must be set by now,
1263 * otherwise we will end up setting freq to lowest of the table as 'cur'
1264 * is initialized to zero).
1265 *
1266 * We are passing target-freq as "policy->cur - 1" otherwise
1267 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1268 * equal to target-freq.
1269 */
1270 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1271 && has_target()) {
1272 /* Are we running at unknown frequency ? */
1273 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1274 if (ret == -EINVAL) {
1275 /* Warn user and fix it */
1276 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1277 __func__, policy->cpu, policy->cur);
1278 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1279 CPUFREQ_RELATION_L);
1280
1281 /*
1282 * Reaching here after boot in a few seconds may not
1283 * mean that system will remain stable at "unknown"
1284 * frequency for longer duration. Hence, a BUG_ON().
1285 */
1286 BUG_ON(ret);
1287 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1288 __func__, policy->cpu, policy->cur);
1289 }
1290 }
1291
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001292 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001293 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301294 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301295 goto out_exit_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001296
1297 cpufreq_stats_create_table(policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001298
Viresh Kumar988bed02015-05-08 11:53:45 +05301299 write_lock_irqsave(&cpufreq_driver_lock, flags);
1300 list_add(&policy->policy_list, &cpufreq_policy_list);
1301 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1302 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301303
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301304 ret = cpufreq_init_policy(policy);
1305 if (ret) {
1306 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1307 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001308 /* cpufreq_policy_free() will notify based on this */
1309 new_policy = false;
1310 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301311 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301312
Viresh Kumar4e97b632014-03-04 11:44:01 +08001313 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301314
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001315 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301316
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301317 /* Callback for handling stuff after policy is ready */
1318 if (cpufreq_driver->ready)
1319 cpufreq_driver->ready(policy);
1320
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001321 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 0;
1324
Viresh Kumar8101f992015-07-08 15:12:15 +05301325out_exit_policy:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001326 up_write(&policy->rwsem);
1327
Viresh Kumarda60ce92013-10-03 20:28:30 +05301328 if (cpufreq_driver->exit)
1329 cpufreq_driver->exit(policy);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001330
1331 for_each_cpu(j, policy->real_cpus)
1332 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1333
Viresh Kumar8101f992015-07-08 15:12:15 +05301334out_free_policy:
Viresh Kumarf9f41e32017-01-05 10:17:27 +05301335 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return ret;
1337}
1338
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001339/**
1340 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1341 * @dev: CPU device.
1342 * @sif: Subsystem interface structure pointer (not used)
1343 */
1344static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1345{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001346 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001347 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301348 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001349
1350 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1351
Viresh Kumar266198042016-09-12 12:07:05 +05301352 if (cpu_online(cpu)) {
1353 ret = cpufreq_online(cpu);
1354 if (ret)
1355 return ret;
1356 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001357
Viresh Kumar266198042016-09-12 12:07:05 +05301358 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001359 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001360 if (policy)
1361 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001363 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001366static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301368 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301369 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001371 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Viresh Kumar988bed02015-05-08 11:53:45 +05301373 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301374 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001375 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Viresh Kumar49f18562016-02-11 17:31:12 +05301379 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001380 if (has_target())
1381 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001382
Viresh Kumar9591bec2015-06-10 02:20:23 +02001383 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301384
Viresh Kumar9591bec2015-06-10 02:20:23 +02001385 if (policy_is_inactive(policy)) {
1386 if (has_target())
1387 strncpy(policy->last_governor, policy->governor->name,
1388 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001389 else
1390 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001391 } else if (cpu == policy->cpu) {
1392 /* Nominate new CPU */
1393 policy->cpu = cpumask_any(policy->cpus);
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Viresh Kumar9591bec2015-06-10 02:20:23 +02001396 /* Start governor again for active policy */
1397 if (!policy_is_inactive(policy)) {
1398 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001399 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001400 if (ret)
1401 pr_err("%s: Failed to start governor\n", __func__);
1402 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301403
Viresh Kumar49f18562016-02-11 17:31:12 +05301404 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301405 }
1406
1407 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001408 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001410 if (has_target())
1411 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001412
Viresh Kumar87549142015-06-10 02:13:21 +02001413 /*
1414 * Perform the ->exit() even during light-weight tear-down,
1415 * since this is a core component, and is essential for the
1416 * subsequent light-weight ->init() to succeed.
1417 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001418 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001419 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001420 policy->freq_table = NULL;
1421 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301422
1423unlock:
1424 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301428/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301429 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301430 *
1431 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301432 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301433static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001434{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001435 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001436 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001437
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001438 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001439 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001440
Viresh Kumar69cee712016-02-11 17:31:11 +05301441 if (cpu_online(cpu))
1442 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001443
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001444 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301445 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301446
1447 if (cpumask_empty(policy->real_cpus))
Viresh Kumarf9f41e32017-01-05 10:17:27 +05301448 cpufreq_policy_free(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001449}
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301452 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1453 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301454 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * @new_freq: CPU frequency the CPU actually runs at
1456 *
Dave Jones29464f22009-01-18 01:37:11 -05001457 * We adjust to current frequency first, and need to clean up later.
1458 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301460static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301461 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301464
Joe Perchese837f9b2014-03-11 10:03:00 -07001465 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 +05301466 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301468 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301470
Viresh Kumar8fec0512014-03-24 13:35:45 +05301471 cpufreq_freq_transition_begin(policy, &freqs);
1472 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Dave Jones32ee8c32006-02-28 00:43:23 -05001475/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301476 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001477 * @cpu: CPU number
1478 *
1479 * This is the last known freq, without actually getting it from the driver.
1480 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1481 */
1482unsigned int cpufreq_quick_get(unsigned int cpu)
1483{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001484 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301485 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001486 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001487
Richard Cochranc75361c2016-03-11 09:43:07 +01001488 read_lock_irqsave(&cpufreq_driver_lock, flags);
1489
1490 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1491 ret_freq = cpufreq_driver->get(cpu);
1492 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1493 return ret_freq;
1494 }
1495
1496 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001497
1498 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001499 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301500 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001501 cpufreq_cpu_put(policy);
1502 }
1503
Dave Jones4d34a672008-02-07 16:33:49 -05001504 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001505}
1506EXPORT_SYMBOL(cpufreq_quick_get);
1507
Jesse Barnes3d737102011-06-28 10:59:12 -07001508/**
1509 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1510 * @cpu: CPU number
1511 *
1512 * Just return the max possible frequency for a given CPU.
1513 */
1514unsigned int cpufreq_quick_get_max(unsigned int cpu)
1515{
1516 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1517 unsigned int ret_freq = 0;
1518
1519 if (policy) {
1520 ret_freq = policy->max;
1521 cpufreq_cpu_put(policy);
1522 }
1523
1524 return ret_freq;
1525}
1526EXPORT_SYMBOL(cpufreq_quick_get_max);
1527
Viresh Kumard92d50a2015-01-02 12:34:29 +05301528static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301530 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001532 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001533 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Viresh Kumard92d50a2015-01-02 12:34:29 +05301535 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001537 /*
1538 * Updating inactive policies is invalid, so avoid doing that. Also
1539 * if fast frequency switching is used with the given policy, the check
1540 * against policy->cur is pointless, so skip it in that case too.
1541 */
1542 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001543 return ret_freq;
1544
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301545 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001546 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301547 /* verify no discrepancy between actual and
1548 saved value exists */
1549 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301550 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 schedule_work(&policy->update);
1552 }
1553 }
1554
Dave Jones4d34a672008-02-07 16:33:49 -05001555 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001558/**
1559 * cpufreq_get - get the current CPU frequency (in kHz)
1560 * @cpu: CPU number
1561 *
1562 * Get the CPU current (static) CPU frequency
1563 */
1564unsigned int cpufreq_get(unsigned int cpu)
1565{
Aaron Plattner999976e2014-03-04 12:42:15 -08001566 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001567 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001568
Aaron Plattner999976e2014-03-04 12:42:15 -08001569 if (policy) {
1570 down_read(&policy->rwsem);
Rafael J. Wysocki182e36a2016-11-18 13:40:45 +01001571
1572 if (!policy_is_inactive(policy))
1573 ret_freq = __cpufreq_get(policy);
1574
Aaron Plattner999976e2014-03-04 12:42:15 -08001575 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301576
Aaron Plattner999976e2014-03-04 12:42:15 -08001577 cpufreq_cpu_put(policy);
1578 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301579
Dave Jones4d34a672008-02-07 16:33:49 -05001580 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582EXPORT_SYMBOL(cpufreq_get);
1583
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001584static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1585{
1586 unsigned int new_freq;
1587
1588 new_freq = cpufreq_driver->get(policy->cpu);
1589 if (!new_freq)
1590 return 0;
1591
1592 if (!policy->cur) {
1593 pr_debug("cpufreq: Driver did not initialize current freq\n");
1594 policy->cur = new_freq;
1595 } else if (policy->cur != new_freq && has_target()) {
1596 cpufreq_out_of_sync(policy, new_freq);
1597 }
1598
1599 return new_freq;
1600}
1601
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001602static struct subsys_interface cpufreq_interface = {
1603 .name = "cpufreq",
1604 .subsys = &cpu_subsys,
1605 .add_dev = cpufreq_add_dev,
1606 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001607};
1608
Viresh Kumare28867e2014-03-04 11:00:27 +08001609/*
1610 * In case platform wants some specific frequency to be configured
1611 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001612 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001613int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001614{
Viresh Kumare28867e2014-03-04 11:00:27 +08001615 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001616
Viresh Kumare28867e2014-03-04 11:00:27 +08001617 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001618 pr_debug("%s: suspend_freq not defined\n", __func__);
1619 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001620 }
1621
Viresh Kumare28867e2014-03-04 11:00:27 +08001622 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1623 policy->suspend_freq);
1624
1625 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1626 CPUFREQ_RELATION_H);
1627 if (ret)
1628 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1629 __func__, policy->suspend_freq, ret);
1630
Dave Jonesc9060492008-02-07 16:32:18 -05001631 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001632}
Viresh Kumare28867e2014-03-04 11:00:27 +08001633EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001634
1635/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001636 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001638 * Called during system wide Suspend/Hibernate cycles for suspending governors
1639 * as some platforms can't change frequency after this point in suspend cycle.
1640 * Because some of the devices (like: i2c, regulators, etc) they use for
1641 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001643void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301645 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001647 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001648 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001650 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301651 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001653 pr_debug("%s: Suspending Governors\n", __func__);
1654
Viresh Kumarf9637352015-05-12 12:20:11 +05301655 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001656 if (has_target()) {
1657 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001658 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001659 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001660 }
1661
1662 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001663 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1664 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301666
1667suspend:
1668 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001672 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001674 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1675 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001677void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301680 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001682 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return;
1684
Lan Tianyu8e304442014-09-18 15:03:07 +08001685 cpufreq_suspended = false;
1686
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001687 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001688 return;
1689
1690 pr_debug("%s: Resuming Governors\n", __func__);
1691
Viresh Kumarf9637352015-05-12 12:20:11 +05301692 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301693 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301694 pr_err("%s: Failed to resume driver: %p\n", __func__,
1695 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001696 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301697 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001698 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301699 up_write(&policy->rwsem);
1700
1701 if (ret)
1702 pr_err("%s: Failed to start governor for policy: %p\n",
1703 __func__, policy);
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
Borislav Petkov9d950462013-01-20 10:24:28 +00001708/**
1709 * cpufreq_get_current_driver - return current driver's name
1710 *
1711 * Return the name string of the currently loaded cpufreq driver
1712 * or NULL, if none.
1713 */
1714const char *cpufreq_get_current_driver(void)
1715{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001716 if (cpufreq_driver)
1717 return cpufreq_driver->name;
1718
1719 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001720}
1721EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001723/**
1724 * cpufreq_get_driver_data - return current driver data
1725 *
1726 * Return the private data of the currently loaded cpufreq
1727 * driver, or NULL if no cpufreq driver is loaded.
1728 */
1729void *cpufreq_get_driver_data(void)
1730{
1731 if (cpufreq_driver)
1732 return cpufreq_driver->driver_data;
1733
1734 return NULL;
1735}
1736EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738/*********************************************************************
1739 * NOTIFIER LISTS INTERFACE *
1740 *********************************************************************/
1741
1742/**
1743 * cpufreq_register_notifier - register a driver with cpufreq
1744 * @nb: notifier function to register
1745 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1746 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001747 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * are notified about clock rate changes (once before and once after
1749 * the transition), or a list of drivers that are notified about
1750 * changes in cpufreq policy.
1751 *
1752 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001753 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
1755int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1756{
1757 int ret;
1758
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001759 if (cpufreq_disabled())
1760 return -EINVAL;
1761
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001762 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 switch (list) {
1765 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001766 mutex_lock(&cpufreq_fast_switch_lock);
1767
1768 if (cpufreq_fast_switch_count > 0) {
1769 mutex_unlock(&cpufreq_fast_switch_lock);
1770 return -EBUSY;
1771 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001772 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001773 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001774 if (!ret)
1775 cpufreq_fast_switch_count--;
1776
1777 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 break;
1779 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001780 ret = blocking_notifier_chain_register(
1781 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783 default:
1784 ret = -EINVAL;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787 return ret;
1788}
1789EXPORT_SYMBOL(cpufreq_register_notifier);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/**
1792 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1793 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301794 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 *
1796 * Remove a driver from the CPU frequency notifier list.
1797 *
1798 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001799 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 */
1801int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1802{
1803 int ret;
1804
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001805 if (cpufreq_disabled())
1806 return -EINVAL;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 switch (list) {
1809 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001810 mutex_lock(&cpufreq_fast_switch_lock);
1811
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001812 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001813 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001814 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1815 cpufreq_fast_switch_count++;
1816
1817 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
1819 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001820 ret = blocking_notifier_chain_unregister(
1821 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 break;
1823 default:
1824 ret = -EINVAL;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 return ret;
1828}
1829EXPORT_SYMBOL(cpufreq_unregister_notifier);
1830
1831
1832/*********************************************************************
1833 * GOVERNORS *
1834 *********************************************************************/
1835
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001836/**
1837 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1838 * @policy: cpufreq policy to switch the frequency for.
1839 * @target_freq: New frequency to set (may be approximate).
1840 *
1841 * Carry out a fast frequency switch without sleeping.
1842 *
1843 * The driver's ->fast_switch() callback invoked by this function must be
1844 * suitable for being called from within RCU-sched read-side critical sections
1845 * and it is expected to select the minimum available frequency greater than or
1846 * equal to @target_freq (CPUFREQ_RELATION_L).
1847 *
1848 * This function must not be called if policy->fast_switch_enabled is unset.
1849 *
1850 * Governors calling this function must guarantee that it will never be invoked
1851 * twice in parallel for the same policy and that it will never be called in
1852 * parallel with either ->target() or ->target_index() for the same policy.
1853 *
Viresh Kumar209887e2017-08-09 10:21:46 +05301854 * Returns the actual frequency set for the CPU.
1855 *
1856 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1857 * error condition, the hardware configuration must be preserved.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001858 */
1859unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1860 unsigned int target_freq)
1861{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001862 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001863
1864 return cpufreq_driver->fast_switch(policy, target_freq);
1865}
1866EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1867
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301868/* Must set freqs->new to intermediate frequency */
1869static int __target_intermediate(struct cpufreq_policy *policy,
1870 struct cpufreq_freqs *freqs, int index)
1871{
1872 int ret;
1873
1874 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1875
1876 /* We don't need to switch to intermediate freq */
1877 if (!freqs->new)
1878 return 0;
1879
1880 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1881 __func__, policy->cpu, freqs->old, freqs->new);
1882
1883 cpufreq_freq_transition_begin(policy, freqs);
1884 ret = cpufreq_driver->target_intermediate(policy, index);
1885 cpufreq_freq_transition_end(policy, freqs, ret);
1886
1887 if (ret)
1888 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1889 __func__, ret);
1890
1891 return ret;
1892}
1893
Viresh Kumar23727842016-06-03 10:58:50 +05301894static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301895{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301896 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1897 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301898 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301899 int retval = -EINVAL;
1900 bool notify;
1901
Viresh Kumar23727842016-06-03 10:58:50 +05301902 if (newfreq == policy->cur)
1903 return 0;
1904
Viresh Kumar8d657752014-05-21 14:29:29 +05301905 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301906 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301907 /* Handle switching to intermediate frequency */
1908 if (cpufreq_driver->get_intermediate) {
1909 retval = __target_intermediate(policy, &freqs, index);
1910 if (retval)
1911 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301912
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301913 intermediate_freq = freqs.new;
1914 /* Set old freq to intermediate */
1915 if (intermediate_freq)
1916 freqs.old = freqs.new;
1917 }
1918
Viresh Kumar23727842016-06-03 10:58:50 +05301919 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301920 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1921 __func__, policy->cpu, freqs.old, freqs.new);
1922
1923 cpufreq_freq_transition_begin(policy, &freqs);
1924 }
1925
1926 retval = cpufreq_driver->target_index(policy, index);
1927 if (retval)
1928 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1929 retval);
1930
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301931 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301932 cpufreq_freq_transition_end(policy, &freqs, retval);
1933
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301934 /*
1935 * Failed after setting to intermediate freq? Driver should have
1936 * reverted back to initial frequency and so should we. Check
1937 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301938 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301939 */
1940 if (unlikely(retval && intermediate_freq)) {
1941 freqs.old = intermediate_freq;
1942 freqs.new = policy->restore_freq;
1943 cpufreq_freq_transition_begin(policy, &freqs);
1944 cpufreq_freq_transition_end(policy, &freqs, 0);
1945 }
1946 }
1947
Viresh Kumar8d657752014-05-21 14:29:29 +05301948 return retval;
1949}
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951int __cpufreq_driver_target(struct cpufreq_policy *policy,
1952 unsigned int target_freq,
1953 unsigned int relation)
1954{
Viresh Kumar72499242012-10-31 01:28:21 +01001955 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05301956 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001957
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001958 if (cpufreq_disabled())
1959 return -ENODEV;
1960
Viresh Kumar72499242012-10-31 01:28:21 +01001961 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05301962 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01001963
1964 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001965 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001966
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301967 /*
1968 * This might look like a redundant call as we are checking it again
1969 * after finding index. But it is left intentionally for cases where
1970 * exactly same freq is called again and so we can save on few function
1971 * calls.
1972 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001973 if (target_freq == policy->cur)
1974 return 0;
1975
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301976 /* Save last value to restore later on errors */
1977 policy->restore_freq = policy->cur;
1978
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001979 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001980 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001981
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001982 if (!cpufreq_driver->target_index)
1983 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301984
Viresh Kumard218ed72016-06-03 10:58:51 +05301985 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301986
Viresh Kumar23727842016-06-03 10:58:50 +05301987 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991int cpufreq_driver_target(struct cpufreq_policy *policy,
1992 unsigned int target_freq,
1993 unsigned int relation)
1994{
Julia Lawallf1829e42008-07-25 22:44:53 +02001995 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
viresh kumarad7722d2013-10-18 19:10:15 +05301997 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 ret = __cpufreq_driver_target(policy, target_freq, relation);
2000
viresh kumarad7722d2013-10-18 19:10:15 +05302001 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return ret;
2004}
2005EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2006
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002007__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2008{
2009 return NULL;
2010}
2011
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002012static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
Dave Jonescc993ca2005-07-28 09:43:56 -07002014 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002015
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002016 /* Don't start any governor operations if we are entering suspend */
2017 if (cpufreq_suspended)
2018 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002019 /*
2020 * Governor might not be initiated here if ACPI _PPC changed
2021 * notification happened, so check it.
2022 */
2023 if (!policy->governor)
2024 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002025
Viresh Kumared4676e2017-07-19 15:42:46 +05302026 /* Platform doesn't want dynamic frequency switching ? */
2027 if (policy->governor->dynamic_switching &&
Viresh Kumarfc4c7092017-07-19 15:42:49 +05302028 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002029 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2030
2031 if (gov) {
Viresh Kumarfe829ed2017-07-19 15:42:48 +05302032 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002033 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002034 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002035 } else {
2036 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002037 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002040 if (!try_module_get(policy->governor->owner))
2041 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002043 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002044
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002045 if (policy->governor->init) {
2046 ret = policy->governor->init(policy);
2047 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002048 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002049 return ret;
2050 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002053 return 0;
2054}
2055
2056static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2057{
2058 if (cpufreq_suspended || !policy->governor)
2059 return;
2060
2061 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2062
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002063 if (policy->governor->exit)
2064 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002065
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002066 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002069static int cpufreq_start_governor(struct cpufreq_policy *policy)
2070{
2071 int ret;
2072
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002073 if (cpufreq_suspended)
2074 return 0;
2075
2076 if (!policy->governor)
2077 return -EINVAL;
2078
2079 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2080
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002081 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2082 cpufreq_update_current_freq(policy);
2083
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002084 if (policy->governor->start) {
2085 ret = policy->governor->start(policy);
2086 if (ret)
2087 return ret;
2088 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002089
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002090 if (policy->governor->limits)
2091 policy->governor->limits(policy);
2092
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002093 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002094}
2095
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002096static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2097{
2098 if (cpufreq_suspended || !policy->governor)
2099 return;
2100
2101 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2102
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002103 if (policy->governor->stop)
2104 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002105}
2106
2107static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2108{
2109 if (cpufreq_suspended || !policy->governor)
2110 return;
2111
2112 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2113
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002114 if (policy->governor->limits)
2115 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118int cpufreq_register_governor(struct cpufreq_governor *governor)
2119{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002120 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 if (!governor)
2123 return -EINVAL;
2124
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002125 if (cpufreq_disabled())
2126 return -ENODEV;
2127
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002128 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002129
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002130 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302131 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002132 err = 0;
2133 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Dave Jones32ee8c32006-02-28 00:43:23 -05002136 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002137 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138}
2139EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2140
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2142{
Viresh Kumar45732372015-05-12 12:22:34 +05302143 struct cpufreq_policy *policy;
2144 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (!governor)
2147 return;
2148
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002149 if (cpufreq_disabled())
2150 return;
2151
Viresh Kumar45732372015-05-12 12:22:34 +05302152 /* clear last_governor for all inactive policies */
2153 read_lock_irqsave(&cpufreq_driver_lock, flags);
2154 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302155 if (!strcmp(policy->last_governor, governor->name)) {
2156 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302157 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302158 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002159 }
Viresh Kumar45732372015-05-12 12:22:34 +05302160 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002161
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002162 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002164 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 return;
2166}
2167EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2168
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170/*********************************************************************
2171 * POLICY INTERFACE *
2172 *********************************************************************/
2173
2174/**
2175 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002176 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2177 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 *
2179 * Reads the current cpufreq policy.
2180 */
2181int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2182{
2183 struct cpufreq_policy *cpu_policy;
2184 if (!policy)
2185 return -EINVAL;
2186
2187 cpu_policy = cpufreq_cpu_get(cpu);
2188 if (!cpu_policy)
2189 return -EINVAL;
2190
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302191 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
2193 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 return 0;
2195}
2196EXPORT_SYMBOL(cpufreq_get_policy);
2197
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002198/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302199 * policy : current policy.
2200 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002201 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302202static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302203 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002205 struct cpufreq_governor *old_gov;
2206 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Joe Perchese837f9b2014-03-11 10:03:00 -07002208 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2209 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302211 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Pan Xinhuifba95732015-07-30 18:10:40 +08002213 /*
2214 * This check works well when we store new min/max freq attributes,
2215 * because new_policy is a copy of policy with one field updated.
2216 */
2217 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002218 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302221 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002226 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302227 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Viresh Kumarbb176f72013-06-19 14:19:33 +05302229 /*
2230 * verify the cpu speed can be set within this limit, which might be
2231 * different to the first one
2232 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302233 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002234 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002235 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002238 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302239 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302241 policy->min = new_policy->min;
2242 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Steve Mucklee3c06232016-07-13 13:25:25 -07002244 policy->cached_target_freq = UINT_MAX;
2245
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002246 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002247 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002249 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302250 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002251 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002252 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
2254
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002255 if (new_policy->governor == policy->governor) {
2256 pr_debug("cpufreq: governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002257 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002258 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002259 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002260
2261 pr_debug("governor switch\n");
2262
2263 /* save old, working values */
2264 old_gov = policy->governor;
2265 /* end old governor */
2266 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002267 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002268 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002269 }
2270
2271 /* start new governor */
2272 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002273 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302274 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002275 ret = cpufreq_start_governor(policy);
2276 if (!ret) {
2277 pr_debug("cpufreq: governor change\n");
2278 return 0;
2279 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002280 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002281 }
2282
2283 /* new governor failed, so re-start old one */
2284 pr_debug("starting governor %s failed\n", policy->governor->name);
2285 if (old_gov) {
2286 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002287 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302288 policy->governor = NULL;
2289 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002290 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002291 }
2292
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302293 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}
2295
2296/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2298 * @cpu: CPU which shall be re-evaluated
2299 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002300 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 * at different times.
2302 */
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002303void cpufreq_update_policy(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302305 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2306 struct cpufreq_policy new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002308 if (!policy)
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002309 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
viresh kumarad7722d2013-10-18 19:10:15 +05302311 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002313 if (policy_is_inactive(policy))
Rafael J. Wysocki182e36a2016-11-18 13:40:45 +01002314 goto unlock;
Rafael J. Wysocki182e36a2016-11-18 13:40:45 +01002315
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002316 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302317 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302318 new_policy.min = policy->user_policy.min;
2319 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Viresh Kumarbb176f72013-06-19 14:19:33 +05302321 /*
2322 * BIOS might change freq behind our back
2323 * -> ask driver for current freq and notify governors about a change
2324 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002325 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002326 if (cpufreq_suspended)
Rafael J. Wysocki742c87b2016-06-28 03:29:29 +02002327 goto unlock;
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002328
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002329 new_policy.cur = cpufreq_update_current_freq(policy);
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002330 if (WARN_ON(!new_policy.cur))
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002331 goto unlock;
Thomas Renninger0961dd02006-01-26 18:46:33 +01002332 }
2333
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002334 cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002336unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302337 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002338
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302339 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341EXPORT_SYMBOL(cpufreq_update_policy);
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002344 * BOOST *
2345 *********************************************************************/
2346static int cpufreq_boost_set_sw(int state)
2347{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002348 struct cpufreq_policy *policy;
2349 int ret = -EINVAL;
2350
Viresh Kumarf9637352015-05-12 12:20:11 +05302351 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302352 if (!policy->freq_table)
2353 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302354
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302355 ret = cpufreq_frequency_table_cpuinfo(policy,
2356 policy->freq_table);
2357 if (ret) {
2358 pr_err("%s: Policy frequency update failed\n",
2359 __func__);
2360 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002361 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302362
2363 down_write(&policy->rwsem);
2364 policy->user_policy.max = policy->max;
2365 cpufreq_governor_limits(policy);
2366 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002367 }
2368
2369 return ret;
2370}
2371
2372int cpufreq_boost_trigger_state(int state)
2373{
2374 unsigned long flags;
2375 int ret = 0;
2376
2377 if (cpufreq_driver->boost_enabled == state)
2378 return 0;
2379
2380 write_lock_irqsave(&cpufreq_driver_lock, flags);
2381 cpufreq_driver->boost_enabled = state;
2382 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2383
2384 ret = cpufreq_driver->set_boost(state);
2385 if (ret) {
2386 write_lock_irqsave(&cpufreq_driver_lock, flags);
2387 cpufreq_driver->boost_enabled = !state;
2388 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2389
Joe Perchese837f9b2014-03-11 10:03:00 -07002390 pr_err("%s: Cannot %s BOOST\n",
2391 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002392 }
2393
2394 return ret;
2395}
2396
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002397static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002398{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002399 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002400}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002401
Viresh Kumar44139ed2015-07-29 16:23:09 +05302402static int create_boost_sysfs_file(void)
2403{
2404 int ret;
2405
Viresh Kumarc82bd442015-10-15 21:35:23 +05302406 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302407 if (ret)
2408 pr_err("%s: cannot register global BOOST sysfs file\n",
2409 __func__);
2410
2411 return ret;
2412}
2413
2414static void remove_boost_sysfs_file(void)
2415{
2416 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302417 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302418}
2419
2420int cpufreq_enable_boost_support(void)
2421{
2422 if (!cpufreq_driver)
2423 return -EINVAL;
2424
2425 if (cpufreq_boost_supported())
2426 return 0;
2427
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002428 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302429
2430 /* This will get removed on driver unregister */
2431 return create_boost_sysfs_file();
2432}
2433EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2434
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002435int cpufreq_boost_enabled(void)
2436{
2437 return cpufreq_driver->boost_enabled;
2438}
2439EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2440
2441/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2443 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002444static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Chen Yuc4a3fa22017-04-09 13:45:16 +08002446static int cpuhp_cpufreq_online(unsigned int cpu)
2447{
2448 cpufreq_online(cpu);
2449
2450 return 0;
2451}
2452
2453static int cpuhp_cpufreq_offline(unsigned int cpu)
2454{
2455 cpufreq_offline(cpu);
2456
2457 return 0;
2458}
2459
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460/**
2461 * cpufreq_register_driver - register a CPU Frequency driver
2462 * @driver_data: A struct cpufreq_driver containing the values#
2463 * submitted by the CPU Frequency driver.
2464 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302465 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002466 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002467 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 *
2469 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002470int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
2472 unsigned long flags;
2473 int ret;
2474
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002475 if (cpufreq_disabled())
2476 return -ENODEV;
2477
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302479 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002480 driver_data->target) ||
2481 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302482 driver_data->target)) ||
2483 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return -EINVAL;
2485
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002486 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002488 /* Protect against concurrent CPU online/offline. */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002489 cpus_read_lock();
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002490
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002491 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002492 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002493 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002494 ret = -EEXIST;
2495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002497 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002498 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302500 if (driver_data->setpolicy)
2501 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2502
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002503 if (cpufreq_boost_supported()) {
2504 ret = create_boost_sysfs_file();
2505 if (ret)
2506 goto err_null_driver;
2507 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002508
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002509 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002510 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002511 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302513 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2514 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 /* if all ->init() calls failed, unregister */
David Arcari6c770032017-05-26 11:37:31 -04002516 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302517 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2518 driver_data->name);
2519 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
2521
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002522 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2523 "cpufreq:online",
2524 cpuhp_cpufreq_online,
2525 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002526 if (ret < 0)
2527 goto err_if_unreg;
2528 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002529 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002530
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002531 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002532 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002533
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002534err_if_unreg:
2535 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002536err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302537 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002538err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002539 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002540 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002541 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002542out:
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002543 cpus_read_unlock();
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002544 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548/**
2549 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2550 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302551 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 * the right to do so, i.e. if you have succeeded in initialising before!
2553 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2554 * currently not initialised.
2555 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002556int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557{
2558 unsigned long flags;
2559
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002560 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002563 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002565 /* Protect against concurrent cpu hotplug */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002566 cpus_read_lock();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002567 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302568 remove_boost_sysfs_file();
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002569 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002571 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302572
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002573 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302574
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002575 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002576 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 return 0;
2579}
2580EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002581
Doug Anderson90de2a42014-12-23 22:09:48 -08002582/*
2583 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2584 * or mutexes when secondary CPUs are halted.
2585 */
2586static struct syscore_ops cpufreq_syscore_ops = {
2587 .shutdown = cpufreq_suspend,
2588};
2589
Viresh Kumarc82bd442015-10-15 21:35:23 +05302590struct kobject *cpufreq_global_kobject;
2591EXPORT_SYMBOL(cpufreq_global_kobject);
2592
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002593static int __init cpufreq_core_init(void)
2594{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002595 if (cpufreq_disabled())
2596 return -ENODEV;
2597
Viresh Kumar8eec1022015-10-15 21:35:22 +05302598 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002599 BUG_ON(!cpufreq_global_kobject);
2600
Doug Anderson90de2a42014-12-23 22:09:48 -08002601 register_syscore_ops(&cpufreq_syscore_ops);
2602
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002603 return 0;
2604}
Len Brownd82f2692017-02-28 16:44:16 -05002605module_param(off, int, 0444);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002606core_initcall(cpufreq_core_init);