blob: e10922709d1399c4b7ec3b804548a9ecb3166d4b [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>
Amit Kucheria5c238a82019-01-30 10:52:01 +053022#include <linux/cpu_cooling.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053025#include <linux/init.h>
26#include <linux/kernel_stat.h>
27#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080028#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053029#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080030#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080031#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053032#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020033#include <trace/events/power.h>
34
Viresh Kumarb4f06762015-01-27 14:06:08 +053035static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053036
37static inline bool policy_is_inactive(struct cpufreq_policy *policy)
38{
39 return cpumask_empty(policy->cpus);
40}
41
Viresh Kumarf9637352015-05-12 12:20:11 +053042/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060043#define for_each_suitable_policy(__policy, __active) \
44 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
45 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053046
47#define for_each_active_policy(__policy) \
48 for_each_suitable_policy(__policy, true)
49#define for_each_inactive_policy(__policy) \
50 for_each_suitable_policy(__policy, false)
51
52#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053053 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
54
Viresh Kumarf7b27062015-01-27 14:06:09 +053055/* Iterate over governors */
56static LIST_HEAD(cpufreq_governor_list);
57#define for_each_governor(__governor) \
58 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**
Dave Jonescd878472006-08-11 17:59:28 -040061 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * level driver of CPUFreq support, and its spinlock. This lock
63 * also protects the cpufreq_cpu_data array.
64 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020065static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070066static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053067static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053068
Viresh Kumar2f0aea92014-03-04 11:00:26 +080069/* Flag to suspend/resume CPUFreq governors */
70static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053072static inline bool has_target(void)
73{
74 return cpufreq_driver->target_index || cpufreq_driver->target;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053078static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020079static int cpufreq_init_governor(struct cpufreq_policy *policy);
80static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010081static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020082static void cpufreq_stop_governor(struct cpufreq_policy *policy);
83static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
Dave Jones32ee8c32006-02-28 00:43:23 -050086 * Two notifier lists: the "policy" list is involved in the
87 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * "transition" list for kernel code that needs to handle
89 * changes to devices when the CPU clock speed changes.
90 * The mutex locks both lists.
91 */
Alan Sterne041c682006-03-27 01:16:30 -080092static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Sebastian Andrzej Siewiorcc85de32018-05-25 12:19:58 +020093SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040095static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020096static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040097{
98 return off;
99}
100void disable_cpufreq(void)
101{
102 off = 1;
103}
Dave Jones29464f22009-01-18 01:37:11 -0500104static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000106bool have_governor_per_policy(void)
107{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530108 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000109}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000110EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000111
Viresh Kumar944e9a02013-05-16 05:09:57 +0000112struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
113{
114 if (have_governor_per_policy())
115 return &policy->kobj;
116 else
117 return cpufreq_global_kobject;
118}
119EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
120
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000121static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
122{
123 u64 idle_time;
124 u64 cur_wall_time;
125 u64 busy_time;
126
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100127 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000128
129 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
130 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
131 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
132 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
133 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
134 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
135
136 idle_time = cur_wall_time - busy_time;
137 if (wall)
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100138 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000139
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100140 return div_u64(idle_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000141}
142
143u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
144{
145 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
146
147 if (idle_time == -1ULL)
148 return get_cpu_idle_time_jiffy(cpu, wall);
149 else if (!io_busy)
150 idle_time += get_cpu_iowait_time_us(cpu, wall);
151
152 return idle_time;
153}
154EXPORT_SYMBOL_GPL(get_cpu_idle_time);
155
Dietmar Eggemanne7d54592017-09-26 17:41:07 +0100156__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
157 unsigned long max_freq)
158{
159}
160EXPORT_SYMBOL_GPL(arch_set_freq_scale);
161
Viresh Kumar70e9e772013-10-03 20:29:07 +0530162/*
163 * This is a generic cpufreq init() routine which can be used by cpufreq
164 * drivers of SMP systems. It will do following:
165 * - validate & show freq table passed
166 * - set policies transition latency
167 * - policy->cpus with all possible CPUs
168 */
169int cpufreq_generic_init(struct cpufreq_policy *policy,
170 struct cpufreq_frequency_table *table,
171 unsigned int transition_latency)
172{
Viresh Kumar92c99d12018-02-26 10:38:45 +0530173 policy->freq_table = table;
Viresh Kumar70e9e772013-10-03 20:29:07 +0530174 policy->cpuinfo.transition_latency = transition_latency;
175
176 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530177 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530178 * share the clock and voltage and clock.
179 */
180 cpumask_setall(policy->cpus);
181
182 return 0;
183}
184EXPORT_SYMBOL_GPL(cpufreq_generic_init);
185
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200186struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530187{
188 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
189
Viresh Kumar988bed02015-05-08 11:53:45 +0530190 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
191}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200192EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530193
194unsigned int cpufreq_generic_get(unsigned int cpu)
195{
196 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
197
Viresh Kumar652ed952014-01-09 20:38:43 +0530198 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700199 pr_err("%s: No %s associated to cpu: %d\n",
200 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530201 return 0;
202 }
203
204 return clk_get_rate(policy->clk) / 1000;
205}
206EXPORT_SYMBOL_GPL(cpufreq_generic_get);
207
Viresh Kumar50e9c852015-02-19 17:02:03 +0530208/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100209 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
210 * @cpu: CPU to find the policy for.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530211 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100212 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
213 * the kobject reference counter of that policy. Return a valid policy on
214 * success or NULL on failure.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530215 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100216 * The policy returned by this function has to be released with the help of
217 * cpufreq_cpu_put() to balance its kobject reference counter properly.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530218 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530219struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530221 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 unsigned long flags;
223
Viresh Kumar1b947c902015-02-19 17:02:05 +0530224 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530225 return NULL;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000228 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Viresh Kumar6eed9402013-08-06 22:53:11 +0530230 if (cpufreq_driver) {
231 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530232 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530233 if (policy)
234 kobject_get(&policy->kobj);
235 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200236
Viresh Kumar6eed9402013-08-06 22:53:11 +0530237 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530239 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
242
Viresh Kumar50e9c852015-02-19 17:02:03 +0530243/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100244 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
245 * @policy: cpufreq policy returned by cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530246 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530247void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530249 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
255 *********************************************************************/
256
257/**
258 * adjust_jiffies - adjust the system "loops_per_jiffy"
259 *
260 * This function alters the system "loops_per_jiffy" for the clock
261 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500262 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 * per-CPU loops_per_jiffy value wherever possible.
264 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800265static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530267#ifndef CONFIG_SMP
268 static unsigned long l_p_j_ref;
269 static unsigned int l_p_j_ref_freq;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (ci->flags & CPUFREQ_CONST_LOOPS)
272 return;
273
274 if (!l_p_j_ref_freq) {
275 l_p_j_ref = loops_per_jiffy;
276 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700277 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
278 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530280 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530281 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
282 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700283 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
284 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Viresh Kumar20b53242018-05-10 15:00:29 +0530289/**
290 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
291 * @policy: cpufreq policy to enable fast frequency switching for.
292 * @freqs: contain details of the frequency update.
293 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
294 *
295 * This function calls the transition notifiers and the "adjust_jiffies"
296 * function. It is called twice on all CPU frequency changes that have
297 * external effects.
298 */
299static void cpufreq_notify_transition(struct cpufreq_policy *policy,
300 struct cpufreq_freqs *freqs,
301 unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 BUG_ON(irqs_disabled());
304
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000305 if (cpufreq_disabled())
306 return;
307
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200308 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200309 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700310 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 switch (state) {
313 case CPUFREQ_PRECHANGE:
Viresh Kumar20b53242018-05-10 15:00:29 +0530314 /*
315 * Detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800316 * which is not equal to what the cpufreq core thinks is
317 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200319 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Viresh Kumar20b53242018-05-10 15:00:29 +0530320 if (policy->cur && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700321 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
322 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800323 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 }
Viresh Kumar20b53242018-05-10 15:00:29 +0530326
327 for_each_cpu(freqs->cpu, policy->cpus) {
328 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
329 CPUFREQ_PRECHANGE, freqs);
330 }
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
333 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case CPUFREQ_POSTCHANGE:
336 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530337 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
338 cpumask_pr_args(policy->cpus));
Viresh Kumarbb176f72013-06-19 14:19:33 +0530339
Viresh Kumar20b53242018-05-10 15:00:29 +0530340 for_each_cpu(freqs->cpu, policy->cpus) {
341 trace_cpu_frequency(freqs->new, freqs->cpu);
342 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
343 CPUFREQ_POSTCHANGE, freqs);
344 }
345
346 cpufreq_stats_record_transition(policy, freqs->new);
347 policy->cur = freqs->new;
348 }
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530349}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530351/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530352static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530353 struct cpufreq_freqs *freqs, int transition_failed)
354{
355 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
356 if (!transition_failed)
357 return;
358
359 swap(freqs->old, freqs->new);
360 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
361 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
362}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530363
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530364void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
365 struct cpufreq_freqs *freqs)
366{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530367
368 /*
369 * Catch double invocations of _begin() which lead to self-deadlock.
370 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
371 * doesn't invoke _begin() on their behalf, and hence the chances of
372 * double invocations are very low. Moreover, there are scenarios
373 * where these checks can emit false-positive warnings in these
374 * drivers; so we avoid that by skipping them altogether.
375 */
376 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
377 && current == policy->transition_task);
378
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530379wait:
380 wait_event(policy->transition_wait, !policy->transition_ongoing);
381
382 spin_lock(&policy->transition_lock);
383
384 if (unlikely(policy->transition_ongoing)) {
385 spin_unlock(&policy->transition_lock);
386 goto wait;
387 }
388
389 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530390 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530391
392 spin_unlock(&policy->transition_lock);
393
394 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
395}
396EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
397
398void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
399 struct cpufreq_freqs *freqs, int transition_failed)
400{
Igor Stoppa0e7ea2f2018-09-07 19:09:55 +0300401 if (WARN_ON(!policy->transition_ongoing))
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530402 return;
403
404 cpufreq_notify_post_transition(policy, freqs, transition_failed);
405
406 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530407 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530408
409 wake_up(&policy->transition_wait);
410}
411EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
412
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200413/*
414 * Fast frequency switching status count. Positive means "enabled", negative
415 * means "disabled" and 0 means "not decided yet".
416 */
417static int cpufreq_fast_switch_count;
418static DEFINE_MUTEX(cpufreq_fast_switch_lock);
419
420static void cpufreq_list_transition_notifiers(void)
421{
422 struct notifier_block *nb;
423
424 pr_info("Registered transition notifiers:\n");
425
426 mutex_lock(&cpufreq_transition_notifier_list.mutex);
427
428 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
429 pr_info("%pF\n", nb->notifier_call);
430
431 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
432}
433
434/**
435 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
436 * @policy: cpufreq policy to enable fast frequency switching for.
437 *
438 * Try to enable fast frequency switching for @policy.
439 *
440 * The attempt will fail if there is at least one transition notifier registered
441 * at this point, as fast frequency switching is quite fundamentally at odds
442 * with transition notifiers. Thus if successful, it will make registration of
443 * transition notifiers fail going forward.
444 */
445void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
446{
447 lockdep_assert_held(&policy->rwsem);
448
449 if (!policy->fast_switch_possible)
450 return;
451
452 mutex_lock(&cpufreq_fast_switch_lock);
453 if (cpufreq_fast_switch_count >= 0) {
454 cpufreq_fast_switch_count++;
455 policy->fast_switch_enabled = true;
456 } else {
457 pr_warn("CPU%u: Fast frequency switching not enabled\n",
458 policy->cpu);
459 cpufreq_list_transition_notifiers();
460 }
461 mutex_unlock(&cpufreq_fast_switch_lock);
462}
463EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
464
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200465/**
466 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
467 * @policy: cpufreq policy to disable fast frequency switching for.
468 */
469void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200470{
471 mutex_lock(&cpufreq_fast_switch_lock);
472 if (policy->fast_switch_enabled) {
473 policy->fast_switch_enabled = false;
474 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
475 cpufreq_fast_switch_count--;
476 }
477 mutex_unlock(&cpufreq_fast_switch_lock);
478}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200479EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Steve Mucklee3c06232016-07-13 13:25:25 -0700481/**
482 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
483 * one.
484 * @target_freq: target frequency to resolve.
485 *
486 * The target to driver frequency mapping is cached in the policy.
487 *
488 * Return: Lowest driver-supported frequency greater than or equal to the
489 * given target_freq, subject to policy (min/max) and driver limitations.
490 */
491unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
492 unsigned int target_freq)
493{
494 target_freq = clamp_val(target_freq, policy->min, policy->max);
495 policy->cached_target_freq = target_freq;
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700496
497 if (cpufreq_driver->target_index) {
498 int idx;
499
500 idx = cpufreq_frequency_table_target(policy, target_freq,
501 CPUFREQ_RELATION_L);
502 policy->cached_resolved_idx = idx;
503 return policy->freq_table[idx].frequency;
504 }
505
Steve Mucklee3c06232016-07-13 13:25:25 -0700506 if (cpufreq_driver->resolve_freq)
507 return cpufreq_driver->resolve_freq(policy, target_freq);
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700508
509 return target_freq;
Steve Mucklee3c06232016-07-13 13:25:25 -0700510}
Steve Muckleae2c1ca2016-07-21 19:24:38 -0700511EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
Steve Mucklee3c06232016-07-13 13:25:25 -0700512
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530513unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
514{
515 unsigned int latency;
516
517 if (policy->transition_delay_us)
518 return policy->transition_delay_us;
519
520 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Viresh Kumare948bc82017-08-17 09:12:27 +0530521 if (latency) {
522 /*
523 * For platforms that can change the frequency very fast (< 10
524 * us), the above formula gives a decent transition delay. But
525 * for platforms where transition_latency is in milliseconds, it
526 * ends up giving unrealistic values.
527 *
528 * Cap the default transition delay to 10 ms, which seems to be
529 * a reasonable amount of time after which we should reevaluate
530 * the frequency.
531 */
532 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
533 }
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530534
535 return LATENCY_MULTIPLIER;
536}
537EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*********************************************************************
540 * SYSFS INTERFACE *
541 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530542static ssize_t show_boost(struct kobject *kobj,
Viresh Kumar625c85a2019-01-25 12:53:07 +0530543 struct kobj_attribute *attr, char *buf)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100544{
545 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
546}
547
Viresh Kumar625c85a2019-01-25 12:53:07 +0530548static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
549 const char *buf, size_t count)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100550{
551 int ret, enable;
552
553 ret = sscanf(buf, "%d", &enable);
554 if (ret != 1 || enable < 0 || enable > 1)
555 return -EINVAL;
556
557 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700558 pr_err("%s: Cannot %s BOOST!\n",
559 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100560 return -EINVAL;
561 }
562
Joe Perchese837f9b2014-03-11 10:03:00 -0700563 pr_debug("%s: cpufreq BOOST %s\n",
564 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100565
566 return count;
567}
568define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530570static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700571{
572 struct cpufreq_governor *t;
573
Viresh Kumarf7b27062015-01-27 14:06:09 +0530574 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200575 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700576 return t;
577
578 return NULL;
579}
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581/**
582 * cpufreq_parse_governor - parse a governor string
583 */
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100584static int cpufreq_parse_governor(char *str_governor,
585 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200587 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200588 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100589 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100590 return 0;
591 }
592
593 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100594 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530597 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700599
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800600 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700601
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530602 t = find_governor(str_governor);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100603 if (!t) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700604 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700605
Kees Cook1a8e1462011-05-04 08:38:56 -0700606 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100607
Kees Cook1a8e1462011-05-04 08:38:56 -0700608 ret = request_module("cpufreq_%s", str_governor);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100609 if (ret)
610 return -EINVAL;
611
Kees Cook1a8e1462011-05-04 08:38:56 -0700612 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700613
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100614 t = find_governor(str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100616 if (t && !try_module_get(t->owner))
617 t = NULL;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700618
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800619 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100620
621 if (t) {
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100622 policy->governor = t;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100623 return 0;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100626
627 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530631 * cpufreq_per_cpu_attr_read() / show_##file_name() -
632 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 *
634 * Write out information from cpufreq_driver->policy[cpu]; object must be
635 * "unsigned int".
636 */
637
Dave Jones32ee8c32006-02-28 00:43:23 -0500638#define show_one(file_name, object) \
639static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500640(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500641{ \
Dave Jones29464f22009-01-18 01:37:11 -0500642 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645show_one(cpuinfo_min_freq, cpuinfo.min_freq);
646show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100647show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648show_one(scaling_min_freq, min);
649show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700650
Len Brownf8475ce2017-06-23 22:11:52 -0700651__weak unsigned int arch_freq_get_on_cpu(int cpu)
652{
653 return 0;
654}
655
Viresh Kumar09347b22015-01-02 12:34:24 +0530656static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700657{
658 ssize_t ret;
Len Brownf8475ce2017-06-23 22:11:52 -0700659 unsigned int freq;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700660
Len Brownf8475ce2017-06-23 22:11:52 -0700661 freq = arch_freq_get_on_cpu(policy->cpu);
662 if (freq)
663 ret = sprintf(buf, "%u\n", freq);
664 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
665 cpufreq_driver->get)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700666 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
667 else
668 ret = sprintf(buf, "%u\n", policy->cur);
669 return ret;
670}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Viresh Kumar037ce832013-10-02 14:13:16 +0530672static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530673 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
676 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
677 */
678#define store_one(file_name, object) \
679static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500680(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800682 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct cpufreq_policy new_policy; \
684 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530685 memcpy(&new_policy, policy, sizeof(*policy)); \
Tao Wangc7d1f112018-05-26 15:16:48 +0800686 new_policy.min = policy->user_policy.min; \
687 new_policy.max = policy->user_policy.max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 \
Dave Jones29464f22009-01-18 01:37:11 -0500689 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (ret != 1) \
691 return -EINVAL; \
692 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800693 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530694 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800695 if (!ret) \
696 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 \
698 return ret ? ret : count; \
699}
700
Dave Jones29464f22009-01-18 01:37:11 -0500701store_one(scaling_min_freq, min);
702store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704/**
705 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
706 */
Dave Jones905d77c2008-03-05 14:28:32 -0500707static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
708 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530710 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysocki9b4f6032017-03-15 00:12:16 +0100711
712 if (cur_freq)
713 return sprintf(buf, "%u\n", cur_freq);
714
715 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718/**
719 * show_scaling_governor - show the current policy for the specified CPU
720 */
Dave Jones905d77c2008-03-05 14:28:32 -0500721static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Dave Jones29464f22009-01-18 01:37:11 -0500723 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return sprintf(buf, "powersave\n");
725 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
726 return sprintf(buf, "performance\n");
727 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200728 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500729 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return -EINVAL;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/**
734 * store_scaling_governor - store policy for the specified CPU
735 */
Dave Jones905d77c2008-03-05 14:28:32 -0500736static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
737 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530739 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 char str_governor[16];
741 struct cpufreq_policy new_policy;
742
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530743 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Dave Jones29464f22009-01-18 01:37:11 -0500745 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (ret != 1)
747 return -EINVAL;
748
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100749 if (cpufreq_parse_governor(str_governor, &new_policy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return -EINVAL;
751
Viresh Kumar037ce832013-10-02 14:13:16 +0530752 ret = cpufreq_set_policy(policy, &new_policy);
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100753
754 if (new_policy.governor)
755 module_put(new_policy.governor->owner);
756
Viresh Kumar88dc4382015-08-03 08:36:18 +0530757 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
760/**
761 * show_scaling_driver - show the cpufreq driver currently loaded
762 */
Dave Jones905d77c2008-03-05 14:28:32 -0500763static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200765 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/**
769 * show_scaling_available_governors - show the available CPUfreq governors
770 */
Dave Jones905d77c2008-03-05 14:28:32 -0500771static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
772 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 ssize_t i = 0;
775 struct cpufreq_governor *t;
776
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530777 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 i += sprintf(buf, "performance powersave");
779 goto out;
780 }
781
Viresh Kumarf7b27062015-01-27 14:06:09 +0530782 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500783 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
784 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200786 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500788out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 i += sprintf(&buf[i], "\n");
790 return i;
791}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700792
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800793ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
795 ssize_t i = 0;
796 unsigned int cpu;
797
Rusty Russell835481d2009-01-04 05:18:06 -0800798 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (i)
800 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
801 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
802 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
805 i += sprintf(&buf[i], "\n");
806 return i;
807}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800808EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700810/**
811 * show_related_cpus - show the CPUs affected by each transition even if
812 * hw coordination is in use
813 */
814static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
815{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800816 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700817}
818
819/**
820 * show_affected_cpus - show the CPUs affected by each transition
821 */
822static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
823{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800824 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700825}
826
Venki Pallipadi9e769882007-10-26 10:18:21 -0700827static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500828 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700829{
830 unsigned int freq = 0;
831 unsigned int ret;
832
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700833 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700834 return -EINVAL;
835
836 ret = sscanf(buf, "%u", &freq);
837 if (ret != 1)
838 return -EINVAL;
839
840 policy->governor->store_setspeed(policy, freq);
841
842 return count;
843}
844
845static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
846{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700847 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700848 return sprintf(buf, "<unsupported>\n");
849
850 return policy->governor->show_setspeed(policy, buf);
851}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Thomas Renningere2f74f32009-11-19 12:31:01 +0100853/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200854 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100855 */
856static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
857{
858 unsigned int limit;
859 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200860 if (cpufreq_driver->bios_limit) {
861 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100862 if (!ret)
863 return sprintf(buf, "%u\n", limit);
864 }
865 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
866}
867
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200868cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
869cpufreq_freq_attr_ro(cpuinfo_min_freq);
870cpufreq_freq_attr_ro(cpuinfo_max_freq);
871cpufreq_freq_attr_ro(cpuinfo_transition_latency);
872cpufreq_freq_attr_ro(scaling_available_governors);
873cpufreq_freq_attr_ro(scaling_driver);
874cpufreq_freq_attr_ro(scaling_cur_freq);
875cpufreq_freq_attr_ro(bios_limit);
876cpufreq_freq_attr_ro(related_cpus);
877cpufreq_freq_attr_ro(affected_cpus);
878cpufreq_freq_attr_rw(scaling_min_freq);
879cpufreq_freq_attr_rw(scaling_max_freq);
880cpufreq_freq_attr_rw(scaling_governor);
881cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Dave Jones905d77c2008-03-05 14:28:32 -0500883static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 &cpuinfo_min_freq.attr,
885 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100886 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 &scaling_min_freq.attr,
888 &scaling_max_freq.attr,
889 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700890 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 &scaling_governor.attr,
892 &scaling_driver.attr,
893 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700894 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 NULL
896};
897
Dave Jones29464f22009-01-18 01:37:11 -0500898#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
899#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Dave Jones29464f22009-01-18 01:37:11 -0500901static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902{
Dave Jones905d77c2008-03-05 14:28:32 -0500903 struct cpufreq_policy *policy = to_policy(kobj);
904 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530905 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530906
viresh kumarad7722d2013-10-18 19:10:15 +0530907 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100908 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530909 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return ret;
912}
913
Dave Jones905d77c2008-03-05 14:28:32 -0500914static ssize_t store(struct kobject *kobj, struct attribute *attr,
915 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
Dave Jones905d77c2008-03-05 14:28:32 -0500917 struct cpufreq_policy *policy = to_policy(kobj);
918 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500919 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530920
Waiman Long9b3d9bb2018-07-24 14:26:05 -0400921 /*
922 * cpus_read_trylock() is used here to work around a circular lock
923 * dependency problem with respect to the cpufreq_register_driver().
924 */
925 if (!cpus_read_trylock())
926 return -EBUSY;
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530927
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100928 if (cpu_online(policy->cpu)) {
929 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530930 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100931 up_write(&policy->rwsem);
932 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530933
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200934 cpus_read_unlock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 return ret;
937}
938
Dave Jones905d77c2008-03-05 14:28:32 -0500939static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Dave Jones905d77c2008-03-05 14:28:32 -0500941 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200942 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 complete(&policy->kobj_unregister);
944}
945
Emese Revfy52cf25d2010-01-19 02:58:23 +0100946static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 .show = show,
948 .store = store,
949};
950
951static struct kobj_type ktype_cpufreq = {
952 .sysfs_ops = &sysfs_ops,
953 .default_attrs = default_attrs,
954 .release = cpufreq_sysfs_release,
955};
956
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200957static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +0200958{
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200959 struct device *dev = get_cpu_device(cpu);
960
961 if (!dev)
962 return;
963
964 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
965 return;
966
Viresh Kumar266198042016-09-12 12:07:05 +0530967 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200968 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
969 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +0200970}
971
Viresh Kumar266198042016-09-12 12:07:05 +0530972static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
973 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +0200974{
Viresh Kumar266198042016-09-12 12:07:05 +0530975 dev_dbg(dev, "%s: Removing symlink\n", __func__);
976 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +0200977}
978
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200979static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400980{
981 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400982 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400983
Dave Jones909a6942009-07-08 18:05:42 -0400984 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200985 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530986 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400987 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
988 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100989 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400990 drv_attr++;
991 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200992 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400993 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
994 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100995 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400996 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700997
998 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
999 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001000 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001001
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001002 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001003 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1004 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001005 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001006 }
Dave Jones909a6942009-07-08 18:05:42 -04001007
Viresh Kumar266198042016-09-12 12:07:05 +05301008 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301009}
1010
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001011__weak struct cpufreq_governor *cpufreq_default_governor(void)
1012{
1013 return NULL;
1014}
1015
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301016static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301017{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001018 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301019 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301020
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301021 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001022
viresh kumar6e2c89d2014-03-04 11:43:59 +08001023 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +05301024 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001025 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +08001026 pr_debug("Restoring governor %s for cpu %d\n",
1027 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001028 } else {
1029 gov = cpufreq_default_governor();
1030 if (!gov)
1031 return -ENODATA;
1032 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001033
1034 new_policy.governor = gov;
1035
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001036 /* Use the default policy if there is no last_policy. */
1037 if (cpufreq_driver->setpolicy) {
1038 if (policy->last_policy)
1039 new_policy.policy = policy->last_policy;
1040 else
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +01001041 cpufreq_parse_governor(gov->name, &new_policy);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001042 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001043 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301044 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001045}
1046
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001047static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001048{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301049 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001050
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301051 /* Has this CPU been taken care of already? */
1052 if (cpumask_test_cpu(cpu, policy->cpus))
1053 return 0;
1054
Viresh Kumar49f18562016-02-11 17:31:12 +05301055 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001056 if (has_target())
1057 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001058
Viresh Kumarfcf80582013-01-29 14:39:08 +00001059 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301060
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301061 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001062 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301063 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301064 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001065 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301066 up_write(&policy->rwsem);
1067 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001068}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301070static void handle_update(struct work_struct *work)
1071{
1072 struct cpufreq_policy *policy =
1073 container_of(work, struct cpufreq_policy, update);
1074 unsigned int cpu = policy->cpu;
1075 pr_debug("handle_update for cpu %u called\n", cpu);
1076 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001079static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat8414809c2013-07-30 04:25:10 +05301080{
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301081 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301082 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301083
1084 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1085 if (!policy)
1086 return NULL;
1087
1088 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1089 goto err_free_policy;
1090
1091 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1092 goto err_free_cpumask;
1093
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001094 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1095 goto err_free_rcpumask;
1096
Viresh Kumaredd4a892016-03-03 14:51:33 +05301097 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1098 cpufreq_global_kobject, "policy%u", cpu);
1099 if (ret) {
1100 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1101 goto err_free_real_cpus;
1102 }
1103
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301104 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301105 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301106 spin_lock_init(&policy->transition_lock);
1107 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c571262015-01-02 12:34:38 +05301108 init_completion(&policy->kobj_unregister);
1109 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301110
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001111 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301112 return policy;
1113
Viresh Kumaredd4a892016-03-03 14:51:33 +05301114err_free_real_cpus:
1115 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301116err_free_rcpumask:
1117 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301118err_free_cpumask:
1119 free_cpumask_var(policy->cpus);
1120err_free_policy:
1121 kfree(policy);
1122
1123 return NULL;
1124}
1125
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301126static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301127{
1128 struct kobject *kobj;
1129 struct completion *cmp;
1130
Viresh Kumar87549142015-06-10 02:13:21 +02001131 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001132 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301133 kobj = &policy->kobj;
1134 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001135 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301136 kobject_put(kobj);
1137
1138 /*
1139 * We need to make sure that the underlying kobj is
1140 * actually not referenced anymore by anybody before we
1141 * proceed with unloading.
1142 */
1143 pr_debug("waiting for dropping of refcount\n");
1144 wait_for_completion(cmp);
1145 pr_debug("wait complete\n");
1146}
1147
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301148static void cpufreq_policy_free(struct cpufreq_policy *policy)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301149{
Viresh Kumar988bed02015-05-08 11:53:45 +05301150 unsigned long flags;
1151 int cpu;
1152
1153 /* Remove policy from list */
1154 write_lock_irqsave(&cpufreq_driver_lock, flags);
1155 list_del(&policy->policy_list);
1156
1157 for_each_cpu(cpu, policy->related_cpus)
1158 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1159 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1160
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301161 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001162 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301163 free_cpumask_var(policy->related_cpus);
1164 free_cpumask_var(policy->cpus);
1165 kfree(policy);
1166}
1167
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001168static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301170 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001171 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001173 unsigned int j;
1174 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001175
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001176 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301177
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301178 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301179 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce707e62015-07-27 23:11:21 +02001180 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301181 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce707e62015-07-27 23:11:21 +02001182 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001183 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Rafael J. Wysocki11ce707e62015-07-27 23:11:21 +02001185 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001186 new_policy = false;
Rafael J. Wysocki11ce707e62015-07-27 23:11:21 +02001187 down_write(&policy->rwsem);
1188 policy->cpu = cpu;
1189 policy->governor = NULL;
1190 up_write(&policy->rwsem);
1191 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001192 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001193 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001194 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001195 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001196 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301197
Viresh Kumar91a12e92019-02-12 16:36:04 +05301198 if (!new_policy && cpufreq_driver->online) {
1199 ret = cpufreq_driver->online(policy);
1200 if (ret) {
1201 pr_debug("%s: %d: initialization failed\n", __func__,
1202 __LINE__);
1203 goto out_exit_policy;
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Viresh Kumar91a12e92019-02-12 16:36:04 +05301206 /* Recover policy->cpus using related_cpus */
1207 cpumask_copy(policy->cpus, policy->related_cpus);
1208 } else {
1209 cpumask_copy(policy->cpus, cpumask_of(cpu));
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001210
Viresh Kumar91a12e92019-02-12 16:36:04 +05301211 /*
1212 * Call driver. From then on the cpufreq must be able
1213 * to accept all calls to ->verify and ->setpolicy for this CPU.
1214 */
1215 ret = cpufreq_driver->init(policy);
1216 if (ret) {
1217 pr_debug("%s: %d: initialization failed\n", __func__,
1218 __LINE__);
1219 goto out_free_policy;
1220 }
Viresh Kumard417e062018-02-22 11:29:44 +05301221
Viresh Kumar91a12e92019-02-12 16:36:04 +05301222 ret = cpufreq_table_validate_and_sort(policy);
1223 if (ret)
1224 goto out_exit_policy;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001225
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001226 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301227 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001228 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001229
Viresh Kumar91a12e92019-02-12 16:36:04 +05301230 down_write(&policy->rwsem);
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001231 /*
1232 * affected cpus must always be the one, which are online. We aren't
1233 * managing offline cpus here.
1234 */
1235 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1236
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001237 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001238 policy->user_policy.min = policy->min;
1239 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001240
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001241 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301242 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001243 add_cpu_dev_symlink(policy, j);
1244 }
Viresh Kumarff010472017-03-21 11:36:06 +05301245 } else {
1246 policy->min = policy->user_policy.min;
1247 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301248 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301249
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001250 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301251 policy->cur = cpufreq_driver->get(policy->cpu);
1252 if (!policy->cur) {
1253 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumard417e062018-02-22 11:29:44 +05301254 goto out_destroy_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301255 }
1256 }
1257
Viresh Kumard3916692013-12-03 11:20:46 +05301258 /*
1259 * Sometimes boot loaders set CPU frequency to a value outside of
1260 * frequency table present with cpufreq core. In such cases CPU might be
1261 * unstable if it has to run on that frequency for long duration of time
1262 * and so its better to set it to a frequency which is specified in
1263 * freq-table. This also makes cpufreq stats inconsistent as
1264 * cpufreq-stats would fail to register because current frequency of CPU
1265 * isn't found in freq-table.
1266 *
1267 * Because we don't want this change to effect boot process badly, we go
1268 * for the next freq which is >= policy->cur ('cur' must be set by now,
1269 * otherwise we will end up setting freq to lowest of the table as 'cur'
1270 * is initialized to zero).
1271 *
1272 * We are passing target-freq as "policy->cur - 1" otherwise
1273 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1274 * equal to target-freq.
1275 */
1276 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1277 && has_target()) {
1278 /* Are we running at unknown frequency ? */
1279 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1280 if (ret == -EINVAL) {
1281 /* Warn user and fix it */
1282 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1283 __func__, policy->cpu, policy->cur);
1284 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1285 CPUFREQ_RELATION_L);
1286
1287 /*
1288 * Reaching here after boot in a few seconds may not
1289 * mean that system will remain stable at "unknown"
1290 * frequency for longer duration. Hence, a BUG_ON().
1291 */
1292 BUG_ON(ret);
1293 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1294 __func__, policy->cpu, policy->cur);
1295 }
1296 }
1297
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001298 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001299 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301300 if (ret)
Viresh Kumard417e062018-02-22 11:29:44 +05301301 goto out_destroy_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001302
1303 cpufreq_stats_create_table(policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001304
Viresh Kumar988bed02015-05-08 11:53:45 +05301305 write_lock_irqsave(&cpufreq_driver_lock, flags);
1306 list_add(&policy->policy_list, &cpufreq_policy_list);
1307 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1308 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301309
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301310 ret = cpufreq_init_policy(policy);
1311 if (ret) {
1312 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1313 __func__, cpu, ret);
Viresh Kumard417e062018-02-22 11:29:44 +05301314 goto out_destroy_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301315 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301316
Viresh Kumar4e97b632014-03-04 11:44:01 +08001317 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301318
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001319 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301320
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301321 /* Callback for handling stuff after policy is ready */
1322 if (cpufreq_driver->ready)
1323 cpufreq_driver->ready(policy);
1324
Amit Kucheria5c238a82019-01-30 10:52:01 +05301325 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1326 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV)
1327 policy->cdev = of_cpufreq_cooling_register(policy);
1328
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001329 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 0;
1332
Viresh Kumard417e062018-02-22 11:29:44 +05301333out_destroy_policy:
Viresh Kumarb24b6472018-02-22 11:29:43 +05301334 for_each_cpu(j, policy->real_cpus)
1335 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1336
Prarit Bhargava7106e022014-09-10 10:12:08 -04001337 up_write(&policy->rwsem);
1338
Viresh Kumard417e062018-02-22 11:29:44 +05301339out_exit_policy:
Viresh Kumarda60ce92013-10-03 20:28:30 +05301340 if (cpufreq_driver->exit)
1341 cpufreq_driver->exit(policy);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001342
Viresh Kumar8101f992015-07-08 15:12:15 +05301343out_free_policy:
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301344 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return ret;
1346}
1347
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001348/**
1349 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1350 * @dev: CPU device.
1351 * @sif: Subsystem interface structure pointer (not used)
1352 */
1353static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1354{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001355 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001356 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301357 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001358
1359 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1360
Viresh Kumar266198042016-09-12 12:07:05 +05301361 if (cpu_online(cpu)) {
1362 ret = cpufreq_online(cpu);
1363 if (ret)
1364 return ret;
1365 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001366
Viresh Kumar266198042016-09-12 12:07:05 +05301367 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001368 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001369 if (policy)
1370 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001375static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301377 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301378 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001380 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Viresh Kumar988bed02015-05-08 11:53:45 +05301382 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301383 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001384 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Viresh Kumar49f18562016-02-11 17:31:12 +05301388 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001389 if (has_target())
1390 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001391
Viresh Kumar9591bec2015-06-10 02:20:23 +02001392 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301393
Viresh Kumar9591bec2015-06-10 02:20:23 +02001394 if (policy_is_inactive(policy)) {
1395 if (has_target())
1396 strncpy(policy->last_governor, policy->governor->name,
1397 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001398 else
1399 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001400 } else if (cpu == policy->cpu) {
1401 /* Nominate new CPU */
1402 policy->cpu = cpumask_any(policy->cpus);
1403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Viresh Kumar9591bec2015-06-10 02:20:23 +02001405 /* Start governor again for active policy */
1406 if (!policy_is_inactive(policy)) {
1407 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001408 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001409 if (ret)
1410 pr_err("%s: Failed to start governor\n", __func__);
1411 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301412
Viresh Kumar49f18562016-02-11 17:31:12 +05301413 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301414 }
1415
Amit Kucheria5c238a82019-01-30 10:52:01 +05301416 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1417 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) {
1418 cpufreq_cooling_unregister(policy->cdev);
1419 policy->cdev = NULL;
1420 }
1421
Viresh Kumar69cee712016-02-11 17:31:11 +05301422 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001423 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001425 if (has_target())
1426 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001427
Viresh Kumar87549142015-06-10 02:13:21 +02001428 /*
Viresh Kumar91a12e92019-02-12 16:36:04 +05301429 * Perform the ->offline() during light-weight tear-down, as
1430 * that allows fast recovery when the CPU comes back.
Viresh Kumar87549142015-06-10 02:13:21 +02001431 */
Viresh Kumar91a12e92019-02-12 16:36:04 +05301432 if (cpufreq_driver->offline) {
1433 cpufreq_driver->offline(policy);
1434 } else if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001435 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001436 policy->freq_table = NULL;
1437 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301438
1439unlock:
1440 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301444/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301445 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301446 *
1447 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301448 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301449static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001450{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001451 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001452 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001453
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001454 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001455 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001456
Viresh Kumar69cee712016-02-11 17:31:11 +05301457 if (cpu_online(cpu))
1458 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001459
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001460 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301461 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301462
Viresh Kumar91a12e92019-02-12 16:36:04 +05301463 if (cpumask_empty(policy->real_cpus)) {
1464 /* We did light-weight exit earlier, do full tear down now */
1465 if (cpufreq_driver->offline)
1466 cpufreq_driver->exit(policy);
1467
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301468 cpufreq_policy_free(policy);
Viresh Kumar91a12e92019-02-12 16:36:04 +05301469 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001470}
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301473 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1474 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301475 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * @new_freq: CPU frequency the CPU actually runs at
1477 *
Dave Jones29464f22009-01-18 01:37:11 -05001478 * We adjust to current frequency first, and need to clean up later.
1479 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301481static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301482 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301485
Joe Perchese837f9b2014-03-11 10:03:00 -07001486 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301487 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301489 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301491
Viresh Kumar8fec0512014-03-24 13:35:45 +05301492 cpufreq_freq_transition_begin(policy, &freqs);
1493 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Dave Jones32ee8c32006-02-28 00:43:23 -05001496/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301497 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001498 * @cpu: CPU number
1499 *
1500 * This is the last known freq, without actually getting it from the driver.
1501 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1502 */
1503unsigned int cpufreq_quick_get(unsigned int cpu)
1504{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001505 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301506 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001507 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001508
Richard Cochranc75361c2016-03-11 09:43:07 +01001509 read_lock_irqsave(&cpufreq_driver_lock, flags);
1510
1511 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1512 ret_freq = cpufreq_driver->get(cpu);
1513 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1514 return ret_freq;
1515 }
1516
1517 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001518
1519 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001520 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301521 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001522 cpufreq_cpu_put(policy);
1523 }
1524
Dave Jones4d34a672008-02-07 16:33:49 -05001525 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001526}
1527EXPORT_SYMBOL(cpufreq_quick_get);
1528
Jesse Barnes3d737102011-06-28 10:59:12 -07001529/**
1530 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1531 * @cpu: CPU number
1532 *
1533 * Just return the max possible frequency for a given CPU.
1534 */
1535unsigned int cpufreq_quick_get_max(unsigned int cpu)
1536{
1537 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1538 unsigned int ret_freq = 0;
1539
1540 if (policy) {
1541 ret_freq = policy->max;
1542 cpufreq_cpu_put(policy);
1543 }
1544
1545 return ret_freq;
1546}
1547EXPORT_SYMBOL(cpufreq_quick_get_max);
1548
Viresh Kumard92d50a2015-01-02 12:34:29 +05301549static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301551 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Sudeep Holla2f661962019-01-07 18:51:53 +00001553 if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001554 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Viresh Kumard92d50a2015-01-02 12:34:29 +05301556 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001558 /*
Sudeep Holla2f661962019-01-07 18:51:53 +00001559 * If fast frequency switching is used with the given policy, the check
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001560 * against policy->cur is pointless, so skip it in that case too.
1561 */
Sudeep Holla2f661962019-01-07 18:51:53 +00001562 if (policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001563 return ret_freq;
1564
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301565 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001566 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301567 /* verify no discrepancy between actual and
1568 saved value exists */
1569 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301570 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 schedule_work(&policy->update);
1572 }
1573 }
1574
Dave Jones4d34a672008-02-07 16:33:49 -05001575 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001576}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001578/**
1579 * cpufreq_get - get the current CPU frequency (in kHz)
1580 * @cpu: CPU number
1581 *
1582 * Get the CPU current (static) CPU frequency
1583 */
1584unsigned int cpufreq_get(unsigned int cpu)
1585{
Aaron Plattner999976e2014-03-04 12:42:15 -08001586 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001587 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001588
Aaron Plattner999976e2014-03-04 12:42:15 -08001589 if (policy) {
1590 down_read(&policy->rwsem);
Sudeep Holla2f661962019-01-07 18:51:53 +00001591 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001592 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301593
Aaron Plattner999976e2014-03-04 12:42:15 -08001594 cpufreq_cpu_put(policy);
1595 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301596
Dave Jones4d34a672008-02-07 16:33:49 -05001597 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599EXPORT_SYMBOL(cpufreq_get);
1600
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001601static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1602{
1603 unsigned int new_freq;
1604
1605 new_freq = cpufreq_driver->get(policy->cpu);
1606 if (!new_freq)
1607 return 0;
1608
1609 if (!policy->cur) {
1610 pr_debug("cpufreq: Driver did not initialize current freq\n");
1611 policy->cur = new_freq;
1612 } else if (policy->cur != new_freq && has_target()) {
1613 cpufreq_out_of_sync(policy, new_freq);
1614 }
1615
1616 return new_freq;
1617}
1618
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001619static struct subsys_interface cpufreq_interface = {
1620 .name = "cpufreq",
1621 .subsys = &cpu_subsys,
1622 .add_dev = cpufreq_add_dev,
1623 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001624};
1625
Viresh Kumare28867e2014-03-04 11:00:27 +08001626/*
1627 * In case platform wants some specific frequency to be configured
1628 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001629 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001630int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001631{
Viresh Kumare28867e2014-03-04 11:00:27 +08001632 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001633
Viresh Kumare28867e2014-03-04 11:00:27 +08001634 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001635 pr_debug("%s: suspend_freq not defined\n", __func__);
1636 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001637 }
1638
Viresh Kumare28867e2014-03-04 11:00:27 +08001639 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1640 policy->suspend_freq);
1641
1642 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1643 CPUFREQ_RELATION_H);
1644 if (ret)
1645 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1646 __func__, policy->suspend_freq, ret);
1647
Dave Jonesc9060492008-02-07 16:32:18 -05001648 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001649}
Viresh Kumare28867e2014-03-04 11:00:27 +08001650EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001651
1652/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001653 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001655 * Called during system wide Suspend/Hibernate cycles for suspending governors
1656 * as some platforms can't change frequency after this point in suspend cycle.
1657 * Because some of the devices (like: i2c, regulators, etc) they use for
1658 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001660void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301662 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001664 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001665 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001667 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301668 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001670 pr_debug("%s: Suspending Governors\n", __func__);
1671
Viresh Kumarf9637352015-05-12 12:20:11 +05301672 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001673 if (has_target()) {
1674 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001675 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001676 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001677 }
1678
1679 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001680 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1681 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301683
1684suspend:
1685 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001689 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001691 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1692 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001694void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301697 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001699 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return;
1701
Bo Yan703cbaa2018-01-23 13:57:55 -08001702 if (unlikely(!cpufreq_suspended))
1703 return;
1704
Lan Tianyu8e304442014-09-18 15:03:07 +08001705 cpufreq_suspended = false;
1706
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001707 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001708 return;
1709
1710 pr_debug("%s: Resuming Governors\n", __func__);
1711
Viresh Kumarf9637352015-05-12 12:20:11 +05301712 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301713 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301714 pr_err("%s: Failed to resume driver: %p\n", __func__,
1715 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001716 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301717 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001718 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301719 up_write(&policy->rwsem);
1720
1721 if (ret)
1722 pr_err("%s: Failed to start governor for policy: %p\n",
1723 __func__, policy);
1724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Borislav Petkov9d950462013-01-20 10:24:28 +00001728/**
1729 * cpufreq_get_current_driver - return current driver's name
1730 *
1731 * Return the name string of the currently loaded cpufreq driver
1732 * or NULL, if none.
1733 */
1734const char *cpufreq_get_current_driver(void)
1735{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001736 if (cpufreq_driver)
1737 return cpufreq_driver->name;
1738
1739 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001740}
1741EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001743/**
1744 * cpufreq_get_driver_data - return current driver data
1745 *
1746 * Return the private data of the currently loaded cpufreq
1747 * driver, or NULL if no cpufreq driver is loaded.
1748 */
1749void *cpufreq_get_driver_data(void)
1750{
1751 if (cpufreq_driver)
1752 return cpufreq_driver->driver_data;
1753
1754 return NULL;
1755}
1756EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758/*********************************************************************
1759 * NOTIFIER LISTS INTERFACE *
1760 *********************************************************************/
1761
1762/**
1763 * cpufreq_register_notifier - register a driver with cpufreq
1764 * @nb: notifier function to register
1765 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1766 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001767 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * are notified about clock rate changes (once before and once after
1769 * the transition), or a list of drivers that are notified about
1770 * changes in cpufreq policy.
1771 *
1772 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001773 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 */
1775int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1776{
1777 int ret;
1778
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001779 if (cpufreq_disabled())
1780 return -EINVAL;
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 switch (list) {
1783 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001784 mutex_lock(&cpufreq_fast_switch_lock);
1785
1786 if (cpufreq_fast_switch_count > 0) {
1787 mutex_unlock(&cpufreq_fast_switch_lock);
1788 return -EBUSY;
1789 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001790 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001791 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001792 if (!ret)
1793 cpufreq_fast_switch_count--;
1794
1795 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 break;
1797 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001798 ret = blocking_notifier_chain_register(
1799 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 break;
1801 default:
1802 ret = -EINVAL;
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 return ret;
1806}
1807EXPORT_SYMBOL(cpufreq_register_notifier);
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809/**
1810 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1811 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301812 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 *
1814 * Remove a driver from the CPU frequency notifier list.
1815 *
1816 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001817 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 */
1819int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1820{
1821 int ret;
1822
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001823 if (cpufreq_disabled())
1824 return -EINVAL;
1825
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 switch (list) {
1827 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001828 mutex_lock(&cpufreq_fast_switch_lock);
1829
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001830 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001831 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001832 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1833 cpufreq_fast_switch_count++;
1834
1835 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 break;
1837 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001838 ret = blocking_notifier_chain_unregister(
1839 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break;
1841 default:
1842 ret = -EINVAL;
1843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
1845 return ret;
1846}
1847EXPORT_SYMBOL(cpufreq_unregister_notifier);
1848
1849
1850/*********************************************************************
1851 * GOVERNORS *
1852 *********************************************************************/
1853
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001854/**
1855 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1856 * @policy: cpufreq policy to switch the frequency for.
1857 * @target_freq: New frequency to set (may be approximate).
1858 *
1859 * Carry out a fast frequency switch without sleeping.
1860 *
1861 * The driver's ->fast_switch() callback invoked by this function must be
1862 * suitable for being called from within RCU-sched read-side critical sections
1863 * and it is expected to select the minimum available frequency greater than or
1864 * equal to @target_freq (CPUFREQ_RELATION_L).
1865 *
1866 * This function must not be called if policy->fast_switch_enabled is unset.
1867 *
1868 * Governors calling this function must guarantee that it will never be invoked
1869 * twice in parallel for the same policy and that it will never be called in
1870 * parallel with either ->target() or ->target_index() for the same policy.
1871 *
Viresh Kumar209887e2017-08-09 10:21:46 +05301872 * Returns the actual frequency set for the CPU.
1873 *
1874 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1875 * error condition, the hardware configuration must be preserved.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001876 */
1877unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1878 unsigned int target_freq)
1879{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001880 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001881
1882 return cpufreq_driver->fast_switch(policy, target_freq);
1883}
1884EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1885
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301886/* Must set freqs->new to intermediate frequency */
1887static int __target_intermediate(struct cpufreq_policy *policy,
1888 struct cpufreq_freqs *freqs, int index)
1889{
1890 int ret;
1891
1892 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1893
1894 /* We don't need to switch to intermediate freq */
1895 if (!freqs->new)
1896 return 0;
1897
1898 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1899 __func__, policy->cpu, freqs->old, freqs->new);
1900
1901 cpufreq_freq_transition_begin(policy, freqs);
1902 ret = cpufreq_driver->target_intermediate(policy, index);
1903 cpufreq_freq_transition_end(policy, freqs, ret);
1904
1905 if (ret)
1906 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1907 __func__, ret);
1908
1909 return ret;
1910}
1911
Viresh Kumar23727842016-06-03 10:58:50 +05301912static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301913{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301914 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1915 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301916 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301917 int retval = -EINVAL;
1918 bool notify;
1919
Viresh Kumar23727842016-06-03 10:58:50 +05301920 if (newfreq == policy->cur)
1921 return 0;
1922
Viresh Kumar8d657752014-05-21 14:29:29 +05301923 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301924 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301925 /* Handle switching to intermediate frequency */
1926 if (cpufreq_driver->get_intermediate) {
1927 retval = __target_intermediate(policy, &freqs, index);
1928 if (retval)
1929 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301930
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301931 intermediate_freq = freqs.new;
1932 /* Set old freq to intermediate */
1933 if (intermediate_freq)
1934 freqs.old = freqs.new;
1935 }
1936
Viresh Kumar23727842016-06-03 10:58:50 +05301937 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301938 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1939 __func__, policy->cpu, freqs.old, freqs.new);
1940
1941 cpufreq_freq_transition_begin(policy, &freqs);
1942 }
1943
1944 retval = cpufreq_driver->target_index(policy, index);
1945 if (retval)
1946 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1947 retval);
1948
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301949 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301950 cpufreq_freq_transition_end(policy, &freqs, retval);
1951
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301952 /*
1953 * Failed after setting to intermediate freq? Driver should have
1954 * reverted back to initial frequency and so should we. Check
1955 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301956 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301957 */
1958 if (unlikely(retval && intermediate_freq)) {
1959 freqs.old = intermediate_freq;
1960 freqs.new = policy->restore_freq;
1961 cpufreq_freq_transition_begin(policy, &freqs);
1962 cpufreq_freq_transition_end(policy, &freqs, 0);
1963 }
1964 }
1965
Viresh Kumar8d657752014-05-21 14:29:29 +05301966 return retval;
1967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969int __cpufreq_driver_target(struct cpufreq_policy *policy,
1970 unsigned int target_freq,
1971 unsigned int relation)
1972{
Viresh Kumar72499242012-10-31 01:28:21 +01001973 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05301974 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001975
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001976 if (cpufreq_disabled())
1977 return -ENODEV;
1978
Viresh Kumar72499242012-10-31 01:28:21 +01001979 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05301980 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01001981
1982 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001983 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001984
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301985 /*
1986 * This might look like a redundant call as we are checking it again
1987 * after finding index. But it is left intentionally for cases where
1988 * exactly same freq is called again and so we can save on few function
1989 * calls.
1990 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001991 if (target_freq == policy->cur)
1992 return 0;
1993
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301994 /* Save last value to restore later on errors */
1995 policy->restore_freq = policy->cur;
1996
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001997 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01001998 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001999
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002000 if (!cpufreq_driver->target_index)
2001 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302002
Viresh Kumard218ed72016-06-03 10:58:51 +05302003 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302004
Viresh Kumar23727842016-06-03 10:58:50 +05302005 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009int cpufreq_driver_target(struct cpufreq_policy *policy,
2010 unsigned int target_freq,
2011 unsigned int relation)
2012{
Julia Lawallf1829e42008-07-25 22:44:53 +02002013 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
viresh kumarad7722d2013-10-18 19:10:15 +05302015 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 ret = __cpufreq_driver_target(policy, target_freq, relation);
2018
viresh kumarad7722d2013-10-18 19:10:15 +05302019 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return ret;
2022}
2023EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2024
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002025__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2026{
2027 return NULL;
2028}
2029
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002030static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
Dave Jonescc993ca2005-07-28 09:43:56 -07002032 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002033
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002034 /* Don't start any governor operations if we are entering suspend */
2035 if (cpufreq_suspended)
2036 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002037 /*
2038 * Governor might not be initiated here if ACPI _PPC changed
2039 * notification happened, so check it.
2040 */
2041 if (!policy->governor)
2042 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002043
Viresh Kumared4676e2017-07-19 15:42:46 +05302044 /* Platform doesn't want dynamic frequency switching ? */
2045 if (policy->governor->dynamic_switching &&
Viresh Kumarfc4c7092017-07-19 15:42:49 +05302046 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002047 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2048
2049 if (gov) {
Viresh Kumarfe829ed2017-07-19 15:42:48 +05302050 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002051 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002052 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002053 } else {
2054 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002055 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002058 if (!try_module_get(policy->governor->owner))
2059 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002061 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002062
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002063 if (policy->governor->init) {
2064 ret = policy->governor->init(policy);
2065 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002066 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002067 return ret;
2068 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002071 return 0;
2072}
2073
2074static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2075{
2076 if (cpufreq_suspended || !policy->governor)
2077 return;
2078
2079 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2080
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002081 if (policy->governor->exit)
2082 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002083
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002084 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002087static int cpufreq_start_governor(struct cpufreq_policy *policy)
2088{
2089 int ret;
2090
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002091 if (cpufreq_suspended)
2092 return 0;
2093
2094 if (!policy->governor)
2095 return -EINVAL;
2096
2097 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2098
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002099 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2100 cpufreq_update_current_freq(policy);
2101
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002102 if (policy->governor->start) {
2103 ret = policy->governor->start(policy);
2104 if (ret)
2105 return ret;
2106 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002107
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002108 if (policy->governor->limits)
2109 policy->governor->limits(policy);
2110
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002111 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002112}
2113
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002114static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2115{
2116 if (cpufreq_suspended || !policy->governor)
2117 return;
2118
2119 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2120
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002121 if (policy->governor->stop)
2122 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002123}
2124
2125static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2126{
2127 if (cpufreq_suspended || !policy->governor)
2128 return;
2129
2130 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2131
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002132 if (policy->governor->limits)
2133 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136int cpufreq_register_governor(struct cpufreq_governor *governor)
2137{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002138 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 if (!governor)
2141 return -EINVAL;
2142
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002143 if (cpufreq_disabled())
2144 return -ENODEV;
2145
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002146 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002147
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002148 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302149 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002150 err = 0;
2151 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Dave Jones32ee8c32006-02-28 00:43:23 -05002154 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002155 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2160{
Viresh Kumar45732372015-05-12 12:22:34 +05302161 struct cpufreq_policy *policy;
2162 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002163
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 if (!governor)
2165 return;
2166
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002167 if (cpufreq_disabled())
2168 return;
2169
Viresh Kumar45732372015-05-12 12:22:34 +05302170 /* clear last_governor for all inactive policies */
2171 read_lock_irqsave(&cpufreq_driver_lock, flags);
2172 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302173 if (!strcmp(policy->last_governor, governor->name)) {
2174 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302175 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302176 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002177 }
Viresh Kumar45732372015-05-12 12:22:34 +05302178 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002179
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002180 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002182 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2185
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187/*********************************************************************
2188 * POLICY INTERFACE *
2189 *********************************************************************/
2190
2191/**
2192 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002193 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2194 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 *
2196 * Reads the current cpufreq policy.
2197 */
2198int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2199{
2200 struct cpufreq_policy *cpu_policy;
2201 if (!policy)
2202 return -EINVAL;
2203
2204 cpu_policy = cpufreq_cpu_get(cpu);
2205 if (!cpu_policy)
2206 return -EINVAL;
2207
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302208 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 return 0;
2212}
2213EXPORT_SYMBOL(cpufreq_get_policy);
2214
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002215/**
2216 * cpufreq_set_policy - Modify cpufreq policy parameters.
2217 * @policy: Policy object to modify.
2218 * @new_policy: New policy data.
2219 *
2220 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2221 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2222 * the driver's ->verify() callback again and run the notifiers for it again
2223 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2224 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2225 * callback (if present) or carry out a governor update for @policy. That is,
2226 * run the current governor's ->limits() callback (if the governor field in
2227 * @new_policy points to the same object as the one in @policy) or replace the
2228 * governor for @policy with the new one stored in @new_policy.
2229 *
2230 * The cpuinfo part of @policy is not updated by this function.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002231 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302232static int cpufreq_set_policy(struct cpufreq_policy *policy,
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002233 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002235 struct cpufreq_governor *old_gov;
2236 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Joe Perchese837f9b2014-03-11 10:03:00 -07002238 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2239 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302241 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Pan Xinhuifba95732015-07-30 18:10:40 +08002243 /*
2244 * This check works well when we store new min/max freq attributes,
2245 * because new_policy is a copy of policy with one field updated.
2246 */
2247 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002248 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302251 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002253 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002256 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302257 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Viresh Kumarbb176f72013-06-19 14:19:33 +05302259 /*
2260 * verify the cpu speed can be set within this limit, which might be
2261 * different to the first one
2262 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302263 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002264 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002265 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002268 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302269 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302271 policy->min = new_policy->min;
2272 policy->max = new_policy->max;
Ruchi Kandoi601b2182018-07-24 10:35:44 -07002273 trace_cpu_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Steve Mucklee3c06232016-07-13 13:25:25 -07002275 policy->cached_target_freq = UINT_MAX;
2276
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002277 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002278 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002280 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302281 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002282 pr_debug("setting range\n");
Rafael J. Wysocki167a38d2019-02-20 00:26:30 +01002283 return cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 }
2285
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002286 if (new_policy->governor == policy->governor) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002287 pr_debug("governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002288 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002289 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002290 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002291
2292 pr_debug("governor switch\n");
2293
2294 /* save old, working values */
2295 old_gov = policy->governor;
2296 /* end old governor */
2297 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002298 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002299 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002300 }
2301
2302 /* start new governor */
2303 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002304 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302305 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002306 ret = cpufreq_start_governor(policy);
2307 if (!ret) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002308 pr_debug("governor change\n");
Quentin Perret531b5c92018-12-03 09:56:21 +00002309 sched_cpufreq_governor_change(policy, old_gov);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002310 return 0;
2311 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002312 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002313 }
2314
2315 /* new governor failed, so re-start old one */
2316 pr_debug("starting governor %s failed\n", policy->governor->name);
2317 if (old_gov) {
2318 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002319 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302320 policy->governor = NULL;
2321 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002322 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002323 }
2324
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302325 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
2328/**
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002329 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2330 * @cpu: CPU to re-evaluate the policy for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 *
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002332 * Update the current frequency for the cpufreq policy of @cpu and use
2333 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2334 * user_policy sub-structure of that policy, which triggers the evaluation
2335 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2336 * policy in question, among other things.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
Rafael J. Wysocki30248fef2016-11-18 13:59:21 +01002338void cpufreq_update_policy(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302340 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2341 struct cpufreq_policy new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002343 if (!policy)
Rafael J. Wysocki30248fef2016-11-18 13:59:21 +01002344 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
viresh kumarad7722d2013-10-18 19:10:15 +05302346 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Rafael J. Wysocki30248fef2016-11-18 13:59:21 +01002348 if (policy_is_inactive(policy))
Rafael J. Wysocki182e36a2016-11-18 13:40:45 +01002349 goto unlock;
Rafael J. Wysocki182e36a2016-11-18 13:40:45 +01002350
Viresh Kumarbb176f72013-06-19 14:19:33 +05302351 /*
2352 * BIOS might change freq behind our back
2353 * -> ask driver for current freq and notify governors about a change
2354 */
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002355 if (cpufreq_driver->get && !cpufreq_driver->setpolicy &&
2356 (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy))))
2357 goto unlock;
Rafael J. Wysocki30248fef2016-11-18 13:59:21 +01002358
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002359 pr_debug("updating policy for CPU %u\n", cpu);
2360 memcpy(&new_policy, policy, sizeof(*policy));
2361 new_policy.min = policy->user_policy.min;
2362 new_policy.max = policy->user_policy.max;
Thomas Renninger0961dd02006-01-26 18:46:33 +01002363
Rafael J. Wysocki30248fef2016-11-18 13:59:21 +01002364 cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002366unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302367 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002368
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302369 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370}
2371EXPORT_SYMBOL(cpufreq_update_policy);
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002374 * BOOST *
2375 *********************************************************************/
2376static int cpufreq_boost_set_sw(int state)
2377{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002378 struct cpufreq_policy *policy;
2379 int ret = -EINVAL;
2380
Viresh Kumarf9637352015-05-12 12:20:11 +05302381 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302382 if (!policy->freq_table)
2383 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302384
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302385 ret = cpufreq_frequency_table_cpuinfo(policy,
2386 policy->freq_table);
2387 if (ret) {
2388 pr_err("%s: Policy frequency update failed\n",
2389 __func__);
2390 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002391 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302392
2393 down_write(&policy->rwsem);
2394 policy->user_policy.max = policy->max;
2395 cpufreq_governor_limits(policy);
2396 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002397 }
2398
2399 return ret;
2400}
2401
2402int cpufreq_boost_trigger_state(int state)
2403{
2404 unsigned long flags;
2405 int ret = 0;
2406
2407 if (cpufreq_driver->boost_enabled == state)
2408 return 0;
2409
2410 write_lock_irqsave(&cpufreq_driver_lock, flags);
2411 cpufreq_driver->boost_enabled = state;
2412 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2413
2414 ret = cpufreq_driver->set_boost(state);
2415 if (ret) {
2416 write_lock_irqsave(&cpufreq_driver_lock, flags);
2417 cpufreq_driver->boost_enabled = !state;
2418 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2419
Joe Perchese837f9b2014-03-11 10:03:00 -07002420 pr_err("%s: Cannot %s BOOST\n",
2421 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002422 }
2423
2424 return ret;
2425}
2426
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002427static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002428{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002429 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002430}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002431
Viresh Kumar44139ed2015-07-29 16:23:09 +05302432static int create_boost_sysfs_file(void)
2433{
2434 int ret;
2435
Viresh Kumarc82bd442015-10-15 21:35:23 +05302436 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302437 if (ret)
2438 pr_err("%s: cannot register global BOOST sysfs file\n",
2439 __func__);
2440
2441 return ret;
2442}
2443
2444static void remove_boost_sysfs_file(void)
2445{
2446 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302447 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302448}
2449
2450int cpufreq_enable_boost_support(void)
2451{
2452 if (!cpufreq_driver)
2453 return -EINVAL;
2454
2455 if (cpufreq_boost_supported())
2456 return 0;
2457
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002458 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302459
2460 /* This will get removed on driver unregister */
2461 return create_boost_sysfs_file();
2462}
2463EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2464
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002465int cpufreq_boost_enabled(void)
2466{
2467 return cpufreq_driver->boost_enabled;
2468}
2469EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2470
2471/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2473 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002474static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Chen Yuc4a3fa22017-04-09 13:45:16 +08002476static int cpuhp_cpufreq_online(unsigned int cpu)
2477{
2478 cpufreq_online(cpu);
2479
2480 return 0;
2481}
2482
2483static int cpuhp_cpufreq_offline(unsigned int cpu)
2484{
2485 cpufreq_offline(cpu);
2486
2487 return 0;
2488}
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/**
2491 * cpufreq_register_driver - register a CPU Frequency driver
2492 * @driver_data: A struct cpufreq_driver containing the values#
2493 * submitted by the CPU Frequency driver.
2494 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302495 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002496 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002497 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 *
2499 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002500int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
2502 unsigned long flags;
2503 int ret;
2504
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002505 if (cpufreq_disabled())
2506 return -ENODEV;
2507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302509 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002510 driver_data->target) ||
2511 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302512 driver_data->target)) ||
Viresh Kumara9a22b52019-02-14 16:16:21 +05302513 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
Viresh Kumar91a12e92019-02-12 16:36:04 +05302514 (!driver_data->online != !driver_data->offline))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return -EINVAL;
2516
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002517 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002519 /* Protect against concurrent CPU online/offline. */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002520 cpus_read_lock();
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002521
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002522 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002523 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002524 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002525 ret = -EEXIST;
2526 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002528 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002529 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302531 if (driver_data->setpolicy)
2532 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2533
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002534 if (cpufreq_boost_supported()) {
2535 ret = create_boost_sysfs_file();
2536 if (ret)
2537 goto err_null_driver;
2538 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002539
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002540 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002541 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002542 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302544 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2545 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 /* if all ->init() calls failed, unregister */
David Arcari6c770032017-05-26 11:37:31 -04002547 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302548 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2549 driver_data->name);
2550 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 }
2552
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002553 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2554 "cpufreq:online",
2555 cpuhp_cpufreq_online,
2556 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002557 if (ret < 0)
2558 goto err_if_unreg;
2559 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002560 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002561
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002562 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002563 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002564
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002565err_if_unreg:
2566 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002567err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302568 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002569err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002570 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002571 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002572 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002573out:
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002574 cpus_read_unlock();
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579/**
2580 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2581 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302582 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 * the right to do so, i.e. if you have succeeded in initialising before!
2584 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2585 * currently not initialised.
2586 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002587int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
2589 unsigned long flags;
2590
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002591 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002594 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002596 /* Protect against concurrent cpu hotplug */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002597 cpus_read_lock();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002598 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302599 remove_boost_sysfs_file();
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002600 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002602 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302603
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002604 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302605
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002606 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002607 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 return 0;
2610}
2611EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002612
Doug Anderson90de2a42014-12-23 22:09:48 -08002613/*
2614 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2615 * or mutexes when secondary CPUs are halted.
2616 */
2617static struct syscore_ops cpufreq_syscore_ops = {
2618 .shutdown = cpufreq_suspend,
2619};
2620
Viresh Kumarc82bd442015-10-15 21:35:23 +05302621struct kobject *cpufreq_global_kobject;
2622EXPORT_SYMBOL(cpufreq_global_kobject);
2623
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002624static int __init cpufreq_core_init(void)
2625{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002626 if (cpufreq_disabled())
2627 return -ENODEV;
2628
Viresh Kumar8eec1022015-10-15 21:35:22 +05302629 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002630 BUG_ON(!cpufreq_global_kobject);
2631
Doug Anderson90de2a42014-12-23 22:09:48 -08002632 register_syscore_ops(&cpufreq_syscore_ops);
2633
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002634 return 0;
2635}
Len Brownd82f2692017-02-28 16:44:16 -05002636module_param(off, int, 0444);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002637core_initcall(cpufreq_core_init);