blob: 79bac52919a59749368d3a401cba47a434cd4807 [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 Kumar67d874c2019-07-08 16:27:52 +053029#include <linux/pm_qos.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053030#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080031#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080032#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053033#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020034#include <trace/events/power.h>
35
Viresh Kumarb4f06762015-01-27 14:06:08 +053036static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053037
Viresh Kumarf9637352015-05-12 12:20:11 +053038/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060039#define for_each_suitable_policy(__policy, __active) \
40 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
41 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053042
43#define for_each_active_policy(__policy) \
44 for_each_suitable_policy(__policy, true)
45#define for_each_inactive_policy(__policy) \
46 for_each_suitable_policy(__policy, false)
47
48#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053049 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
50
Viresh Kumarf7b27062015-01-27 14:06:09 +053051/* Iterate over governors */
52static LIST_HEAD(cpufreq_governor_list);
53#define for_each_governor(__governor) \
54 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/**
Dave Jonescd878472006-08-11 17:59:28 -040057 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * level driver of CPUFreq support, and its spinlock. This lock
59 * also protects the cpufreq_cpu_data array.
60 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020061static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070062static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053063static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053064
Viresh Kumar2f0aea92014-03-04 11:00:26 +080065/* Flag to suspend/resume CPUFreq governors */
66static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053068static inline bool has_target(void)
69{
70 return cpufreq_driver->target_index || cpufreq_driver->target;
71}
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053074static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020075static int cpufreq_init_governor(struct cpufreq_policy *policy);
76static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010077static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020078static void cpufreq_stop_governor(struct cpufreq_policy *policy);
79static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/**
Dave Jones32ee8c32006-02-28 00:43:23 -050082 * Two notifier lists: the "policy" list is involved in the
83 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 * "transition" list for kernel code that needs to handle
85 * changes to devices when the CPU clock speed changes.
86 * The mutex locks both lists.
87 */
Alan Sterne041c682006-03-27 01:16:30 -080088static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Sebastian Andrzej Siewiorcc85de32018-05-25 12:19:58 +020089SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040091static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020092static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040093{
94 return off;
95}
96void disable_cpufreq(void)
97{
98 off = 1;
99}
Dave Jones29464f22009-01-18 01:37:11 -0500100static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000102bool have_governor_per_policy(void)
103{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530104 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000105}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000106EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000107
Viresh Kumar944e9a02013-05-16 05:09:57 +0000108struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
109{
110 if (have_governor_per_policy())
111 return &policy->kobj;
112 else
113 return cpufreq_global_kobject;
114}
115EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
116
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000117static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
118{
119 u64 idle_time;
120 u64 cur_wall_time;
121 u64 busy_time;
122
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100123 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000124
125 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
127 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
128 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
129 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
130 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
131
132 idle_time = cur_wall_time - busy_time;
133 if (wall)
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100134 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000135
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100136 return div_u64(idle_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000137}
138
139u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
140{
141 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
142
143 if (idle_time == -1ULL)
144 return get_cpu_idle_time_jiffy(cpu, wall);
145 else if (!io_busy)
146 idle_time += get_cpu_iowait_time_us(cpu, wall);
147
148 return idle_time;
149}
150EXPORT_SYMBOL_GPL(get_cpu_idle_time);
151
Dietmar Eggemanne7d54592017-09-26 17:41:07 +0100152__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
153 unsigned long max_freq)
154{
155}
156EXPORT_SYMBOL_GPL(arch_set_freq_scale);
157
Viresh Kumar70e9e772013-10-03 20:29:07 +0530158/*
159 * This is a generic cpufreq init() routine which can be used by cpufreq
160 * drivers of SMP systems. It will do following:
161 * - validate & show freq table passed
162 * - set policies transition latency
163 * - policy->cpus with all possible CPUs
164 */
165int cpufreq_generic_init(struct cpufreq_policy *policy,
166 struct cpufreq_frequency_table *table,
167 unsigned int transition_latency)
168{
Viresh Kumar92c99d12018-02-26 10:38:45 +0530169 policy->freq_table = table;
Viresh Kumar70e9e772013-10-03 20:29:07 +0530170 policy->cpuinfo.transition_latency = transition_latency;
171
172 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530173 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530174 * share the clock and voltage and clock.
175 */
176 cpumask_setall(policy->cpus);
177
178 return 0;
179}
180EXPORT_SYMBOL_GPL(cpufreq_generic_init);
181
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200182struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530183{
184 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
185
Viresh Kumar988bed02015-05-08 11:53:45 +0530186 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
187}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200188EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530189
190unsigned int cpufreq_generic_get(unsigned int cpu)
191{
192 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
193
Viresh Kumar652ed952014-01-09 20:38:43 +0530194 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700195 pr_err("%s: No %s associated to cpu: %d\n",
196 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530197 return 0;
198 }
199
200 return clk_get_rate(policy->clk) / 1000;
201}
202EXPORT_SYMBOL_GPL(cpufreq_generic_get);
203
Viresh Kumar50e9c852015-02-19 17:02:03 +0530204/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100205 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
206 * @cpu: CPU to find the policy for.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530207 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100208 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
209 * the kobject reference counter of that policy. Return a valid policy on
210 * success or NULL on failure.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530211 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100212 * The policy returned by this function has to be released with the help of
213 * cpufreq_cpu_put() to balance its kobject reference counter properly.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530214 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530215struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530217 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 unsigned long flags;
219
Viresh Kumar1b947c902015-02-19 17:02:05 +0530220 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530221 return NULL;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000224 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Viresh Kumar6eed9402013-08-06 22:53:11 +0530226 if (cpufreq_driver) {
227 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530228 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530229 if (policy)
230 kobject_get(&policy->kobj);
231 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200232
Viresh Kumar6eed9402013-08-06 22:53:11 +0530233 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530235 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
238
Viresh Kumar50e9c852015-02-19 17:02:03 +0530239/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100240 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
241 * @policy: cpufreq policy returned by cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530242 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530243void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530245 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
248
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100249/**
250 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
251 * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
252 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +0100253void cpufreq_cpu_release(struct cpufreq_policy *policy)
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100254{
255 if (WARN_ON(!policy))
256 return;
257
258 lockdep_assert_held(&policy->rwsem);
259
260 up_write(&policy->rwsem);
261
262 cpufreq_cpu_put(policy);
263}
264
265/**
266 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
267 * @cpu: CPU to find the policy for.
268 *
269 * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
270 * if the policy returned by it is not NULL, acquire its rwsem for writing.
271 * Return the policy if it is active or release it and return NULL otherwise.
272 *
273 * The policy returned by this function has to be released with the help of
274 * cpufreq_cpu_release() in order to release its rwsem and balance its usage
275 * counter properly.
276 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +0100277struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100278{
279 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
280
281 if (!policy)
282 return NULL;
283
284 down_write(&policy->rwsem);
285
286 if (policy_is_inactive(policy)) {
287 cpufreq_cpu_release(policy);
288 return NULL;
289 }
290
291 return policy;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
296 *********************************************************************/
297
298/**
299 * adjust_jiffies - adjust the system "loops_per_jiffy"
300 *
301 * This function alters the system "loops_per_jiffy" for the clock
302 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500303 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * per-CPU loops_per_jiffy value wherever possible.
305 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800306static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530308#ifndef CONFIG_SMP
309 static unsigned long l_p_j_ref;
310 static unsigned int l_p_j_ref_freq;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (ci->flags & CPUFREQ_CONST_LOOPS)
313 return;
314
315 if (!l_p_j_ref_freq) {
316 l_p_j_ref = loops_per_jiffy;
317 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700318 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
319 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530321 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530322 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
323 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700324 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
325 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Viresh Kumar20b53242018-05-10 15:00:29 +0530330/**
331 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
332 * @policy: cpufreq policy to enable fast frequency switching for.
333 * @freqs: contain details of the frequency update.
334 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
335 *
336 * This function calls the transition notifiers and the "adjust_jiffies"
337 * function. It is called twice on all CPU frequency changes that have
338 * external effects.
339 */
340static void cpufreq_notify_transition(struct cpufreq_policy *policy,
341 struct cpufreq_freqs *freqs,
342 unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Viresh Kumardf240142019-04-29 15:03:58 +0530344 int cpu;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 BUG_ON(irqs_disabled());
347
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000348 if (cpufreq_disabled())
349 return;
350
Viresh Kumardf240142019-04-29 15:03:58 +0530351 freqs->policy = policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200352 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200353 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700354 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 switch (state) {
357 case CPUFREQ_PRECHANGE:
Viresh Kumar20b53242018-05-10 15:00:29 +0530358 /*
359 * Detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800360 * which is not equal to what the cpufreq core thinks is
361 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
Viresh Kumar98015222019-06-28 10:46:55 +0530363 if (policy->cur && policy->cur != freqs->old) {
364 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
365 freqs->old, policy->cur);
366 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Viresh Kumar20b53242018-05-10 15:00:29 +0530368
Viresh Kumardf240142019-04-29 15:03:58 +0530369 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
370 CPUFREQ_PRECHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
373 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 case CPUFREQ_POSTCHANGE:
376 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530377 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
378 cpumask_pr_args(policy->cpus));
Viresh Kumarbb176f72013-06-19 14:19:33 +0530379
Viresh Kumardf240142019-04-29 15:03:58 +0530380 for_each_cpu(cpu, policy->cpus)
381 trace_cpu_frequency(freqs->new, cpu);
382
383 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
384 CPUFREQ_POSTCHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530385
386 cpufreq_stats_record_transition(policy, freqs->new);
387 policy->cur = freqs->new;
388 }
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530391/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530392static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530393 struct cpufreq_freqs *freqs, int transition_failed)
394{
395 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
396 if (!transition_failed)
397 return;
398
399 swap(freqs->old, freqs->new);
400 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
401 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
402}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530403
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530404void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
405 struct cpufreq_freqs *freqs)
406{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530407
408 /*
409 * Catch double invocations of _begin() which lead to self-deadlock.
410 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
411 * doesn't invoke _begin() on their behalf, and hence the chances of
412 * double invocations are very low. Moreover, there are scenarios
413 * where these checks can emit false-positive warnings in these
414 * drivers; so we avoid that by skipping them altogether.
415 */
416 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
417 && current == policy->transition_task);
418
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530419wait:
420 wait_event(policy->transition_wait, !policy->transition_ongoing);
421
422 spin_lock(&policy->transition_lock);
423
424 if (unlikely(policy->transition_ongoing)) {
425 spin_unlock(&policy->transition_lock);
426 goto wait;
427 }
428
429 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530430 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530431
432 spin_unlock(&policy->transition_lock);
433
434 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
435}
436EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
437
438void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
439 struct cpufreq_freqs *freqs, int transition_failed)
440{
Igor Stoppa0e7ea2f2018-09-07 19:09:55 +0300441 if (WARN_ON(!policy->transition_ongoing))
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530442 return;
443
444 cpufreq_notify_post_transition(policy, freqs, transition_failed);
445
446 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530447 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530448
449 wake_up(&policy->transition_wait);
450}
451EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
452
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200453/*
454 * Fast frequency switching status count. Positive means "enabled", negative
455 * means "disabled" and 0 means "not decided yet".
456 */
457static int cpufreq_fast_switch_count;
458static DEFINE_MUTEX(cpufreq_fast_switch_lock);
459
460static void cpufreq_list_transition_notifiers(void)
461{
462 struct notifier_block *nb;
463
464 pr_info("Registered transition notifiers:\n");
465
466 mutex_lock(&cpufreq_transition_notifier_list.mutex);
467
468 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
Sakari Ailusd75f7732019-03-25 21:32:28 +0200469 pr_info("%pS\n", nb->notifier_call);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200470
471 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
472}
473
474/**
475 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
476 * @policy: cpufreq policy to enable fast frequency switching for.
477 *
478 * Try to enable fast frequency switching for @policy.
479 *
480 * The attempt will fail if there is at least one transition notifier registered
481 * at this point, as fast frequency switching is quite fundamentally at odds
482 * with transition notifiers. Thus if successful, it will make registration of
483 * transition notifiers fail going forward.
484 */
485void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
486{
487 lockdep_assert_held(&policy->rwsem);
488
489 if (!policy->fast_switch_possible)
490 return;
491
492 mutex_lock(&cpufreq_fast_switch_lock);
493 if (cpufreq_fast_switch_count >= 0) {
494 cpufreq_fast_switch_count++;
495 policy->fast_switch_enabled = true;
496 } else {
497 pr_warn("CPU%u: Fast frequency switching not enabled\n",
498 policy->cpu);
499 cpufreq_list_transition_notifiers();
500 }
501 mutex_unlock(&cpufreq_fast_switch_lock);
502}
503EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
504
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200505/**
506 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
507 * @policy: cpufreq policy to disable fast frequency switching for.
508 */
509void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200510{
511 mutex_lock(&cpufreq_fast_switch_lock);
512 if (policy->fast_switch_enabled) {
513 policy->fast_switch_enabled = false;
514 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
515 cpufreq_fast_switch_count--;
516 }
517 mutex_unlock(&cpufreq_fast_switch_lock);
518}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200519EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Steve Mucklee3c06232016-07-13 13:25:25 -0700521/**
522 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
523 * one.
524 * @target_freq: target frequency to resolve.
525 *
526 * The target to driver frequency mapping is cached in the policy.
527 *
528 * Return: Lowest driver-supported frequency greater than or equal to the
529 * given target_freq, subject to policy (min/max) and driver limitations.
530 */
531unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
532 unsigned int target_freq)
533{
534 target_freq = clamp_val(target_freq, policy->min, policy->max);
535 policy->cached_target_freq = target_freq;
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700536
537 if (cpufreq_driver->target_index) {
538 int idx;
539
540 idx = cpufreq_frequency_table_target(policy, target_freq,
541 CPUFREQ_RELATION_L);
542 policy->cached_resolved_idx = idx;
543 return policy->freq_table[idx].frequency;
544 }
545
Steve Mucklee3c06232016-07-13 13:25:25 -0700546 if (cpufreq_driver->resolve_freq)
547 return cpufreq_driver->resolve_freq(policy, target_freq);
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700548
549 return target_freq;
Steve Mucklee3c06232016-07-13 13:25:25 -0700550}
Steve Muckleae2c1ca2016-07-21 19:24:38 -0700551EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
Steve Mucklee3c06232016-07-13 13:25:25 -0700552
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530553unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
554{
555 unsigned int latency;
556
557 if (policy->transition_delay_us)
558 return policy->transition_delay_us;
559
560 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Viresh Kumare948bc82017-08-17 09:12:27 +0530561 if (latency) {
562 /*
563 * For platforms that can change the frequency very fast (< 10
564 * us), the above formula gives a decent transition delay. But
565 * for platforms where transition_latency is in milliseconds, it
566 * ends up giving unrealistic values.
567 *
568 * Cap the default transition delay to 10 ms, which seems to be
569 * a reasonable amount of time after which we should reevaluate
570 * the frequency.
571 */
572 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
573 }
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530574
575 return LATENCY_MULTIPLIER;
576}
577EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*********************************************************************
580 * SYSFS INTERFACE *
581 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530582static ssize_t show_boost(struct kobject *kobj,
Viresh Kumar625c85a2019-01-25 12:53:07 +0530583 struct kobj_attribute *attr, char *buf)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100584{
585 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
586}
587
Viresh Kumar625c85a2019-01-25 12:53:07 +0530588static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
589 const char *buf, size_t count)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100590{
591 int ret, enable;
592
593 ret = sscanf(buf, "%d", &enable);
594 if (ret != 1 || enable < 0 || enable > 1)
595 return -EINVAL;
596
597 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700598 pr_err("%s: Cannot %s BOOST!\n",
599 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100600 return -EINVAL;
601 }
602
Joe Perchese837f9b2014-03-11 10:03:00 -0700603 pr_debug("%s: cpufreq BOOST %s\n",
604 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100605
606 return count;
607}
608define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530610static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700611{
612 struct cpufreq_governor *t;
613
Viresh Kumarf7b27062015-01-27 14:06:09 +0530614 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200615 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700616 return t;
617
618 return NULL;
619}
620
Yue Huab05d972019-04-29 15:24:18 +0800621static int cpufreq_parse_policy(char *str_governor,
622 struct cpufreq_policy *policy)
623{
624 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
625 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
626 return 0;
627 }
628 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
629 policy->policy = CPUFREQ_POLICY_POWERSAVE;
630 return 0;
631 }
632 return -EINVAL;
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/**
Viresh Kumar5ddc6d42019-06-20 08:35:48 +0530636 * cpufreq_parse_governor - parse a governor string only for has_target()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100638static int cpufreq_parse_governor(char *str_governor,
639 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Yue Huab05d972019-04-29 15:24:18 +0800641 struct cpufreq_governor *t;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100642
Yue Huab05d972019-04-29 15:24:18 +0800643 mutex_lock(&cpufreq_governor_mutex);
644
645 t = find_governor(str_governor);
646 if (!t) {
647 int ret;
648
649 mutex_unlock(&cpufreq_governor_mutex);
650
651 ret = request_module("cpufreq_%s", str_governor);
652 if (ret)
653 return -EINVAL;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700654
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800655 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700656
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530657 t = find_governor(str_governor);
Yue Huab05d972019-04-29 15:24:18 +0800658 }
659 if (t && !try_module_get(t->owner))
660 t = NULL;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700661
Yue Huab05d972019-04-29 15:24:18 +0800662 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100663
Yue Huab05d972019-04-29 15:24:18 +0800664 if (t) {
665 policy->governor = t;
666 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100668
669 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530673 * cpufreq_per_cpu_attr_read() / show_##file_name() -
674 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
676 * Write out information from cpufreq_driver->policy[cpu]; object must be
677 * "unsigned int".
678 */
679
Dave Jones32ee8c32006-02-28 00:43:23 -0500680#define show_one(file_name, object) \
681static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500682(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500683{ \
Dave Jones29464f22009-01-18 01:37:11 -0500684 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687show_one(cpuinfo_min_freq, cpuinfo.min_freq);
688show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100689show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690show_one(scaling_min_freq, min);
691show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700692
Len Brownf8475ce2017-06-23 22:11:52 -0700693__weak unsigned int arch_freq_get_on_cpu(int cpu)
694{
695 return 0;
696}
697
Viresh Kumar09347b22015-01-02 12:34:24 +0530698static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700699{
700 ssize_t ret;
Len Brownf8475ce2017-06-23 22:11:52 -0700701 unsigned int freq;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700702
Len Brownf8475ce2017-06-23 22:11:52 -0700703 freq = arch_freq_get_on_cpu(policy->cpu);
704 if (freq)
705 ret = sprintf(buf, "%u\n", freq);
706 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
707 cpufreq_driver->get)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700708 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
709 else
710 ret = sprintf(buf, "%u\n", policy->cur);
711 return ret;
712}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714/**
715 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
716 */
717#define store_one(file_name, object) \
718static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500719(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800721 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct cpufreq_policy new_policy; \
723 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530724 memcpy(&new_policy, policy, sizeof(*policy)); \
Tao Wangc7d1f112018-05-26 15:16:48 +0800725 new_policy.min = policy->user_policy.min; \
726 new_policy.max = policy->user_policy.max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 \
Dave Jones29464f22009-01-18 01:37:11 -0500728 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 if (ret != 1) \
730 return -EINVAL; \
731 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800732 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530733 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800734 if (!ret) \
735 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 \
737 return ret ? ret : count; \
738}
739
Dave Jones29464f22009-01-18 01:37:11 -0500740store_one(scaling_min_freq, min);
741store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743/**
744 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
745 */
Dave Jones905d77c2008-03-05 14:28:32 -0500746static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
747 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530749 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysocki9b4f6032017-03-15 00:12:16 +0100750
751 if (cur_freq)
752 return sprintf(buf, "%u\n", cur_freq);
753
754 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/**
758 * show_scaling_governor - show the current policy for the specified CPU
759 */
Dave Jones905d77c2008-03-05 14:28:32 -0500760static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Dave Jones29464f22009-01-18 01:37:11 -0500762 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return sprintf(buf, "powersave\n");
764 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
765 return sprintf(buf, "performance\n");
766 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200767 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500768 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return -EINVAL;
770}
771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772/**
773 * store_scaling_governor - store policy for the specified CPU
774 */
Dave Jones905d77c2008-03-05 14:28:32 -0500775static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
776 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530778 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 char str_governor[16];
780 struct cpufreq_policy new_policy;
781
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530782 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Dave Jones29464f22009-01-18 01:37:11 -0500784 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (ret != 1)
786 return -EINVAL;
787
Yue Huab05d972019-04-29 15:24:18 +0800788 if (cpufreq_driver->setpolicy) {
789 if (cpufreq_parse_policy(str_governor, &new_policy))
790 return -EINVAL;
791 } else {
792 if (cpufreq_parse_governor(str_governor, &new_policy))
793 return -EINVAL;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Viresh Kumar037ce832013-10-02 14:13:16 +0530796 ret = cpufreq_set_policy(policy, &new_policy);
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100797
798 if (new_policy.governor)
799 module_put(new_policy.governor->owner);
800
Viresh Kumar88dc4382015-08-03 08:36:18 +0530801 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/**
805 * show_scaling_driver - show the cpufreq driver currently loaded
806 */
Dave Jones905d77c2008-03-05 14:28:32 -0500807static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200809 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812/**
813 * show_scaling_available_governors - show the available CPUfreq governors
814 */
Dave Jones905d77c2008-03-05 14:28:32 -0500815static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
816 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 ssize_t i = 0;
819 struct cpufreq_governor *t;
820
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530821 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 i += sprintf(buf, "performance powersave");
823 goto out;
824 }
825
Viresh Kumarf7b27062015-01-27 14:06:09 +0530826 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500827 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
828 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200830 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500832out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 i += sprintf(&buf[i], "\n");
834 return i;
835}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700836
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800837ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 ssize_t i = 0;
840 unsigned int cpu;
841
Rusty Russell835481d2009-01-04 05:18:06 -0800842 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (i)
844 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
845 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
846 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500847 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 i += sprintf(&buf[i], "\n");
850 return i;
851}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800852EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700854/**
855 * show_related_cpus - show the CPUs affected by each transition even if
856 * hw coordination is in use
857 */
858static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
859{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800860 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700861}
862
863/**
864 * show_affected_cpus - show the CPUs affected by each transition
865 */
866static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
867{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800868 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700869}
870
Venki Pallipadi9e769882007-10-26 10:18:21 -0700871static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500872 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700873{
874 unsigned int freq = 0;
875 unsigned int ret;
876
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700877 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700878 return -EINVAL;
879
880 ret = sscanf(buf, "%u", &freq);
881 if (ret != 1)
882 return -EINVAL;
883
884 policy->governor->store_setspeed(policy, freq);
885
886 return count;
887}
888
889static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
890{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700891 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700892 return sprintf(buf, "<unsupported>\n");
893
894 return policy->governor->show_setspeed(policy, buf);
895}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Thomas Renningere2f74f32009-11-19 12:31:01 +0100897/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200898 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100899 */
900static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
901{
902 unsigned int limit;
903 int ret;
Yue Hub23aa312019-04-16 10:40:27 +0800904 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
905 if (!ret)
906 return sprintf(buf, "%u\n", limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100907 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
908}
909
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200910cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
911cpufreq_freq_attr_ro(cpuinfo_min_freq);
912cpufreq_freq_attr_ro(cpuinfo_max_freq);
913cpufreq_freq_attr_ro(cpuinfo_transition_latency);
914cpufreq_freq_attr_ro(scaling_available_governors);
915cpufreq_freq_attr_ro(scaling_driver);
916cpufreq_freq_attr_ro(scaling_cur_freq);
917cpufreq_freq_attr_ro(bios_limit);
918cpufreq_freq_attr_ro(related_cpus);
919cpufreq_freq_attr_ro(affected_cpus);
920cpufreq_freq_attr_rw(scaling_min_freq);
921cpufreq_freq_attr_rw(scaling_max_freq);
922cpufreq_freq_attr_rw(scaling_governor);
923cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Dave Jones905d77c2008-03-05 14:28:32 -0500925static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 &cpuinfo_min_freq.attr,
927 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100928 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 &scaling_min_freq.attr,
930 &scaling_max_freq.attr,
931 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700932 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 &scaling_governor.attr,
934 &scaling_driver.attr,
935 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700936 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 NULL
938};
939
Dave Jones29464f22009-01-18 01:37:11 -0500940#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
941#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Dave Jones29464f22009-01-18 01:37:11 -0500943static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Dave Jones905d77c2008-03-05 14:28:32 -0500945 struct cpufreq_policy *policy = to_policy(kobj);
946 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530947 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530948
viresh kumarad7722d2013-10-18 19:10:15 +0530949 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100950 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530951 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return ret;
954}
955
Dave Jones905d77c2008-03-05 14:28:32 -0500956static ssize_t store(struct kobject *kobj, struct attribute *attr,
957 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958{
Dave Jones905d77c2008-03-05 14:28:32 -0500959 struct cpufreq_policy *policy = to_policy(kobj);
960 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500961 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530962
Waiman Long9b3d9bb2018-07-24 14:26:05 -0400963 /*
964 * cpus_read_trylock() is used here to work around a circular lock
965 * dependency problem with respect to the cpufreq_register_driver().
966 */
967 if (!cpus_read_trylock())
968 return -EBUSY;
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530969
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100970 if (cpu_online(policy->cpu)) {
971 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530972 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100973 up_write(&policy->rwsem);
974 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530975
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200976 cpus_read_unlock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 return ret;
979}
980
Dave Jones905d77c2008-03-05 14:28:32 -0500981static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982{
Dave Jones905d77c2008-03-05 14:28:32 -0500983 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200984 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 complete(&policy->kobj_unregister);
986}
987
Emese Revfy52cf25d2010-01-19 02:58:23 +0100988static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 .show = show,
990 .store = store,
991};
992
993static struct kobj_type ktype_cpufreq = {
994 .sysfs_ops = &sysfs_ops,
995 .default_attrs = default_attrs,
996 .release = cpufreq_sysfs_release,
997};
998
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200999static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +02001000{
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001001 struct device *dev = get_cpu_device(cpu);
1002
Viresh Kumar67d874c2019-07-08 16:27:52 +05301003 if (unlikely(!dev))
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001004 return;
1005
1006 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1007 return;
1008
Viresh Kumar266198042016-09-12 12:07:05 +05301009 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001010 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1011 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +02001012}
1013
Viresh Kumar266198042016-09-12 12:07:05 +05301014static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1015 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +02001016{
Viresh Kumar266198042016-09-12 12:07:05 +05301017 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1018 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +02001019}
1020
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001021static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -04001022{
1023 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -04001024 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -04001025
Dave Jones909a6942009-07-08 18:05:42 -04001026 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001027 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +05301028 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -04001029 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1030 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001031 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001032 drv_attr++;
1033 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001034 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001035 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1036 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001037 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001038 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001039
1040 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1041 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001042 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001043
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001044 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001045 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1046 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001047 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001048 }
Dave Jones909a6942009-07-08 18:05:42 -04001049
Viresh Kumar266198042016-09-12 12:07:05 +05301050 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301051}
1052
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001053__weak struct cpufreq_governor *cpufreq_default_governor(void)
1054{
1055 return NULL;
1056}
1057
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301058static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301059{
Yue Huab05d972019-04-29 15:24:18 +08001060 struct cpufreq_governor *gov = NULL, *def_gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301061 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301062
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301063 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001064
Yue Huab05d972019-04-29 15:24:18 +08001065 def_gov = cpufreq_default_governor();
1066
1067 if (has_target()) {
1068 /*
1069 * Update governor of new_policy to the governor used before
1070 * hotplug
1071 */
1072 gov = find_governor(policy->last_governor);
1073 if (gov) {
1074 pr_debug("Restoring governor %s for cpu %d\n",
viresh kumar6e2c89d2014-03-04 11:43:59 +08001075 policy->governor->name, policy->cpu);
Yue Huab05d972019-04-29 15:24:18 +08001076 } else {
1077 if (!def_gov)
1078 return -ENODATA;
1079 gov = def_gov;
1080 }
1081 new_policy.governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001082 } else {
Yue Huab05d972019-04-29 15:24:18 +08001083 /* Use the default policy if there is no last_policy. */
1084 if (policy->last_policy) {
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001085 new_policy.policy = policy->last_policy;
Yue Huab05d972019-04-29 15:24:18 +08001086 } else {
1087 if (!def_gov)
1088 return -ENODATA;
1089 cpufreq_parse_policy(def_gov->name, &new_policy);
1090 }
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001091 }
Yue Huab05d972019-04-29 15:24:18 +08001092
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301093 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001094}
1095
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001096static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001097{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301098 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001099
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301100 /* Has this CPU been taken care of already? */
1101 if (cpumask_test_cpu(cpu, policy->cpus))
1102 return 0;
1103
Viresh Kumar49f18562016-02-11 17:31:12 +05301104 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001105 if (has_target())
1106 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001107
Viresh Kumarfcf80582013-01-29 14:39:08 +00001108 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301109
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301110 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001111 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301112 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301113 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001114 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301115 up_write(&policy->rwsem);
1116 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Viresh Kumarc57b25b2019-07-04 13:06:22 +05301119void refresh_frequency_limits(struct cpufreq_policy *policy)
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301120{
Viresh Kumar67d874c2019-07-08 16:27:52 +05301121 struct cpufreq_policy new_policy;
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301122
Viresh Kumar67d874c2019-07-08 16:27:52 +05301123 if (!policy_is_inactive(policy)) {
1124 new_policy = *policy;
1125 pr_debug("updating policy for CPU %u\n", policy->cpu);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301126
Viresh Kumar67d874c2019-07-08 16:27:52 +05301127 new_policy.min = policy->user_policy.min;
1128 new_policy.max = policy->user_policy.max;
1129 cpufreq_set_policy(policy, &new_policy);
1130 }
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301131}
Viresh Kumarc57b25b2019-07-04 13:06:22 +05301132EXPORT_SYMBOL(refresh_frequency_limits);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301133
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301134static void handle_update(struct work_struct *work)
1135{
1136 struct cpufreq_policy *policy =
1137 container_of(work, struct cpufreq_policy, update);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301138
1139 pr_debug("handle_update for cpu %u called\n", policy->cpu);
Viresh Kumar67d874c2019-07-08 16:27:52 +05301140 down_write(&policy->rwsem);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301141 refresh_frequency_limits(policy);
Viresh Kumar67d874c2019-07-08 16:27:52 +05301142 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Viresh Kumar67d874c2019-07-08 16:27:52 +05301145static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
1146 void *data)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301147{
Viresh Kumar67d874c2019-07-08 16:27:52 +05301148 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301149
Viresh Kumar67d874c2019-07-08 16:27:52 +05301150 schedule_work(&policy->update);
1151 return 0;
1152}
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301153
Viresh Kumar67d874c2019-07-08 16:27:52 +05301154static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
1155 void *data)
1156{
1157 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301158
Viresh Kumar67d874c2019-07-08 16:27:52 +05301159 schedule_work(&policy->update);
1160 return 0;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301161}
1162
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301163static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301164{
1165 struct kobject *kobj;
1166 struct completion *cmp;
1167
Viresh Kumar87549142015-06-10 02:13:21 +02001168 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001169 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301170 kobj = &policy->kobj;
1171 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001172 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301173 kobject_put(kobj);
1174
1175 /*
1176 * We need to make sure that the underlying kobj is
1177 * actually not referenced anymore by anybody before we
1178 * proceed with unloading.
1179 */
1180 pr_debug("waiting for dropping of refcount\n");
1181 wait_for_completion(cmp);
1182 pr_debug("wait complete\n");
1183}
1184
Viresh Kumar67d874c2019-07-08 16:27:52 +05301185static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1186{
1187 struct cpufreq_policy *policy;
1188 struct device *dev = get_cpu_device(cpu);
1189 int ret;
1190
1191 if (!dev)
1192 return NULL;
1193
1194 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1195 if (!policy)
1196 return NULL;
1197
1198 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1199 goto err_free_policy;
1200
1201 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1202 goto err_free_cpumask;
1203
1204 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1205 goto err_free_rcpumask;
1206
1207 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1208 cpufreq_global_kobject, "policy%u", cpu);
1209 if (ret) {
1210 dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
1211 /*
1212 * The entire policy object will be freed below, but the extra
1213 * memory allocated for the kobject name needs to be freed by
1214 * releasing the kobject.
1215 */
1216 kobject_put(&policy->kobj);
1217 goto err_free_real_cpus;
1218 }
1219
1220 policy->nb_min.notifier_call = cpufreq_notifier_min;
1221 policy->nb_max.notifier_call = cpufreq_notifier_max;
1222
1223 ret = dev_pm_qos_add_notifier(dev, &policy->nb_min,
1224 DEV_PM_QOS_MIN_FREQUENCY);
1225 if (ret) {
1226 dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n",
1227 ret, cpumask_pr_args(policy->cpus));
1228 goto err_kobj_remove;
1229 }
1230
1231 ret = dev_pm_qos_add_notifier(dev, &policy->nb_max,
1232 DEV_PM_QOS_MAX_FREQUENCY);
1233 if (ret) {
1234 dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n",
1235 ret, cpumask_pr_args(policy->cpus));
1236 goto err_min_qos_notifier;
1237 }
1238
1239 INIT_LIST_HEAD(&policy->policy_list);
1240 init_rwsem(&policy->rwsem);
1241 spin_lock_init(&policy->transition_lock);
1242 init_waitqueue_head(&policy->transition_wait);
1243 init_completion(&policy->kobj_unregister);
1244 INIT_WORK(&policy->update, handle_update);
1245
1246 policy->cpu = cpu;
1247 return policy;
1248
1249err_min_qos_notifier:
1250 dev_pm_qos_remove_notifier(dev, &policy->nb_min,
1251 DEV_PM_QOS_MIN_FREQUENCY);
1252err_kobj_remove:
1253 cpufreq_policy_put_kobj(policy);
1254err_free_real_cpus:
1255 free_cpumask_var(policy->real_cpus);
1256err_free_rcpumask:
1257 free_cpumask_var(policy->related_cpus);
1258err_free_cpumask:
1259 free_cpumask_var(policy->cpus);
1260err_free_policy:
1261 kfree(policy);
1262
1263 return NULL;
1264}
1265
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301266static void cpufreq_policy_free(struct cpufreq_policy *policy)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301267{
Viresh Kumar67d874c2019-07-08 16:27:52 +05301268 struct device *dev = get_cpu_device(policy->cpu);
Viresh Kumar988bed02015-05-08 11:53:45 +05301269 unsigned long flags;
1270 int cpu;
1271
1272 /* Remove policy from list */
1273 write_lock_irqsave(&cpufreq_driver_lock, flags);
1274 list_del(&policy->policy_list);
1275
1276 for_each_cpu(cpu, policy->related_cpus)
1277 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1278 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1279
Viresh Kumar67d874c2019-07-08 16:27:52 +05301280 dev_pm_qos_remove_notifier(dev, &policy->nb_max,
1281 DEV_PM_QOS_MAX_FREQUENCY);
1282 dev_pm_qos_remove_notifier(dev, &policy->nb_min,
1283 DEV_PM_QOS_MIN_FREQUENCY);
1284
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301285 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001286 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301287 free_cpumask_var(policy->related_cpus);
1288 free_cpumask_var(policy->cpus);
1289 kfree(policy);
1290}
1291
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001292static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301294 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001295 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001297 unsigned int j;
1298 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001299
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001300 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301301
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301302 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301303 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001304 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301305 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001306 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001307 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001309 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001310 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001311 down_write(&policy->rwsem);
1312 policy->cpu = cpu;
1313 policy->governor = NULL;
1314 up_write(&policy->rwsem);
1315 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001316 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001317 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001318 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001319 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001320 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301321
Viresh Kumar91a12e92019-02-12 16:36:04 +05301322 if (!new_policy && cpufreq_driver->online) {
1323 ret = cpufreq_driver->online(policy);
1324 if (ret) {
1325 pr_debug("%s: %d: initialization failed\n", __func__,
1326 __LINE__);
1327 goto out_exit_policy;
1328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Viresh Kumar91a12e92019-02-12 16:36:04 +05301330 /* Recover policy->cpus using related_cpus */
1331 cpumask_copy(policy->cpus, policy->related_cpus);
1332 } else {
1333 cpumask_copy(policy->cpus, cpumask_of(cpu));
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001334
Viresh Kumar91a12e92019-02-12 16:36:04 +05301335 /*
1336 * Call driver. From then on the cpufreq must be able
1337 * to accept all calls to ->verify and ->setpolicy for this CPU.
1338 */
1339 ret = cpufreq_driver->init(policy);
1340 if (ret) {
1341 pr_debug("%s: %d: initialization failed\n", __func__,
1342 __LINE__);
1343 goto out_free_policy;
1344 }
Viresh Kumard417e062018-02-22 11:29:44 +05301345
Viresh Kumar91a12e92019-02-12 16:36:04 +05301346 ret = cpufreq_table_validate_and_sort(policy);
1347 if (ret)
1348 goto out_exit_policy;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001349
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001350 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301351 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001352 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001353
Viresh Kumar91a12e92019-02-12 16:36:04 +05301354 down_write(&policy->rwsem);
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001355 /*
1356 * affected cpus must always be the one, which are online. We aren't
1357 * managing offline cpus here.
1358 */
1359 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1360
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001361 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001362 policy->user_policy.min = policy->min;
1363 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001364
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001365 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301366 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001367 add_cpu_dev_symlink(policy, j);
1368 }
Viresh Kumarff010472017-03-21 11:36:06 +05301369 } else {
1370 policy->min = policy->user_policy.min;
1371 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301372 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301373
Viresh Kumar5ddc6d42019-06-20 08:35:48 +05301374 if (cpufreq_driver->get && has_target()) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301375 policy->cur = cpufreq_driver->get(policy->cpu);
1376 if (!policy->cur) {
1377 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumard417e062018-02-22 11:29:44 +05301378 goto out_destroy_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301379 }
1380 }
1381
Viresh Kumard3916692013-12-03 11:20:46 +05301382 /*
1383 * Sometimes boot loaders set CPU frequency to a value outside of
1384 * frequency table present with cpufreq core. In such cases CPU might be
1385 * unstable if it has to run on that frequency for long duration of time
1386 * and so its better to set it to a frequency which is specified in
1387 * freq-table. This also makes cpufreq stats inconsistent as
1388 * cpufreq-stats would fail to register because current frequency of CPU
1389 * isn't found in freq-table.
1390 *
1391 * Because we don't want this change to effect boot process badly, we go
1392 * for the next freq which is >= policy->cur ('cur' must be set by now,
1393 * otherwise we will end up setting freq to lowest of the table as 'cur'
1394 * is initialized to zero).
1395 *
1396 * We are passing target-freq as "policy->cur - 1" otherwise
1397 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1398 * equal to target-freq.
1399 */
1400 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1401 && has_target()) {
1402 /* Are we running at unknown frequency ? */
1403 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1404 if (ret == -EINVAL) {
1405 /* Warn user and fix it */
1406 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1407 __func__, policy->cpu, policy->cur);
1408 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1409 CPUFREQ_RELATION_L);
1410
1411 /*
1412 * Reaching here after boot in a few seconds may not
1413 * mean that system will remain stable at "unknown"
1414 * frequency for longer duration. Hence, a BUG_ON().
1415 */
1416 BUG_ON(ret);
1417 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1418 __func__, policy->cpu, policy->cur);
1419 }
1420 }
1421
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001422 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001423 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301424 if (ret)
Viresh Kumard417e062018-02-22 11:29:44 +05301425 goto out_destroy_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001426
1427 cpufreq_stats_create_table(policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001428
Viresh Kumar988bed02015-05-08 11:53:45 +05301429 write_lock_irqsave(&cpufreq_driver_lock, flags);
1430 list_add(&policy->policy_list, &cpufreq_policy_list);
1431 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1432 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301433
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301434 ret = cpufreq_init_policy(policy);
1435 if (ret) {
1436 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1437 __func__, cpu, ret);
Viresh Kumard417e062018-02-22 11:29:44 +05301438 goto out_destroy_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301439 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301440
Viresh Kumar4e97b632014-03-04 11:44:01 +08001441 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301442
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001443 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301444
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301445 /* Callback for handling stuff after policy is ready */
1446 if (cpufreq_driver->ready)
1447 cpufreq_driver->ready(policy);
1448
Daniel Lezcanobcc61562019-06-25 13:32:41 +02001449 if (cpufreq_thermal_control_enabled(cpufreq_driver))
Amit Kucheria5c238a82019-01-30 10:52:01 +05301450 policy->cdev = of_cpufreq_cooling_register(policy);
1451
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001452 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return 0;
1455
Viresh Kumard417e062018-02-22 11:29:44 +05301456out_destroy_policy:
Viresh Kumarb24b6472018-02-22 11:29:43 +05301457 for_each_cpu(j, policy->real_cpus)
1458 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1459
Prarit Bhargava7106e022014-09-10 10:12:08 -04001460 up_write(&policy->rwsem);
1461
Viresh Kumard417e062018-02-22 11:29:44 +05301462out_exit_policy:
Viresh Kumarda60ce92013-10-03 20:28:30 +05301463 if (cpufreq_driver->exit)
1464 cpufreq_driver->exit(policy);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001465
Viresh Kumar8101f992015-07-08 15:12:15 +05301466out_free_policy:
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301467 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return ret;
1469}
1470
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001471/**
1472 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1473 * @dev: CPU device.
1474 * @sif: Subsystem interface structure pointer (not used)
1475 */
1476static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1477{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001478 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001479 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301480 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001481
1482 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1483
Viresh Kumar266198042016-09-12 12:07:05 +05301484 if (cpu_online(cpu)) {
1485 ret = cpufreq_online(cpu);
1486 if (ret)
1487 return ret;
1488 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001489
Viresh Kumar266198042016-09-12 12:07:05 +05301490 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001491 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001492 if (policy)
1493 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001495 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001498static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301500 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301501 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001503 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Viresh Kumar988bed02015-05-08 11:53:45 +05301505 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301506 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001507 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Viresh Kumar49f18562016-02-11 17:31:12 +05301511 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001512 if (has_target())
1513 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001514
Viresh Kumar9591bec2015-06-10 02:20:23 +02001515 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301516
Viresh Kumar9591bec2015-06-10 02:20:23 +02001517 if (policy_is_inactive(policy)) {
1518 if (has_target())
1519 strncpy(policy->last_governor, policy->governor->name,
1520 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001521 else
1522 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001523 } else if (cpu == policy->cpu) {
1524 /* Nominate new CPU */
1525 policy->cpu = cpumask_any(policy->cpus);
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Viresh Kumar9591bec2015-06-10 02:20:23 +02001528 /* Start governor again for active policy */
1529 if (!policy_is_inactive(policy)) {
1530 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001531 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001532 if (ret)
1533 pr_err("%s: Failed to start governor\n", __func__);
1534 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301535
Viresh Kumar49f18562016-02-11 17:31:12 +05301536 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301537 }
1538
Daniel Lezcanobcc61562019-06-25 13:32:41 +02001539 if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
Amit Kucheria5c238a82019-01-30 10:52:01 +05301540 cpufreq_cooling_unregister(policy->cdev);
1541 policy->cdev = NULL;
1542 }
1543
Viresh Kumar69cee712016-02-11 17:31:11 +05301544 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001545 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001547 if (has_target())
1548 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001549
Viresh Kumar87549142015-06-10 02:13:21 +02001550 /*
Viresh Kumar91a12e92019-02-12 16:36:04 +05301551 * Perform the ->offline() during light-weight tear-down, as
1552 * that allows fast recovery when the CPU comes back.
Viresh Kumar87549142015-06-10 02:13:21 +02001553 */
Viresh Kumar91a12e92019-02-12 16:36:04 +05301554 if (cpufreq_driver->offline) {
1555 cpufreq_driver->offline(policy);
1556 } else if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001557 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001558 policy->freq_table = NULL;
1559 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301560
1561unlock:
1562 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301566/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301567 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301568 *
1569 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301570 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301571static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001572{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001573 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001574 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001575
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001576 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001577 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001578
Viresh Kumar69cee712016-02-11 17:31:11 +05301579 if (cpu_online(cpu))
1580 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001581
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001582 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301583 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301584
Viresh Kumar91a12e92019-02-12 16:36:04 +05301585 if (cpumask_empty(policy->real_cpus)) {
1586 /* We did light-weight exit earlier, do full tear down now */
1587 if (cpufreq_driver->offline)
1588 cpufreq_driver->exit(policy);
1589
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301590 cpufreq_policy_free(policy);
Viresh Kumar91a12e92019-02-12 16:36:04 +05301591 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001592}
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301595 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1596 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301597 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 * @new_freq: CPU frequency the CPU actually runs at
1599 *
Dave Jones29464f22009-01-18 01:37:11 -05001600 * We adjust to current frequency first, and need to clean up later.
1601 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301603static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301604 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
1606 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301607
Joe Perchese837f9b2014-03-11 10:03:00 -07001608 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 +05301609 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301611 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301613
Viresh Kumar8fec0512014-03-24 13:35:45 +05301614 cpufreq_freq_transition_begin(policy, &freqs);
1615 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Viresh Kumar59807522019-06-20 08:35:49 +05301618static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
1619{
1620 unsigned int new_freq;
1621
1622 new_freq = cpufreq_driver->get(policy->cpu);
1623 if (!new_freq)
1624 return 0;
1625
1626 /*
1627 * If fast frequency switching is used with the given policy, the check
1628 * against policy->cur is pointless, so skip it in that case.
1629 */
1630 if (policy->fast_switch_enabled || !has_target())
1631 return new_freq;
1632
1633 if (policy->cur != new_freq) {
1634 cpufreq_out_of_sync(policy, new_freq);
1635 if (update)
1636 schedule_work(&policy->update);
1637 }
1638
1639 return new_freq;
1640}
1641
Dave Jones32ee8c32006-02-28 00:43:23 -05001642/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301643 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001644 * @cpu: CPU number
1645 *
1646 * This is the last known freq, without actually getting it from the driver.
1647 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1648 */
1649unsigned int cpufreq_quick_get(unsigned int cpu)
1650{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001651 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301652 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001653 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001654
Richard Cochranc75361c2016-03-11 09:43:07 +01001655 read_lock_irqsave(&cpufreq_driver_lock, flags);
1656
1657 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1658 ret_freq = cpufreq_driver->get(cpu);
1659 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1660 return ret_freq;
1661 }
1662
1663 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001664
1665 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001666 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301667 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001668 cpufreq_cpu_put(policy);
1669 }
1670
Dave Jones4d34a672008-02-07 16:33:49 -05001671 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001672}
1673EXPORT_SYMBOL(cpufreq_quick_get);
1674
Jesse Barnes3d737102011-06-28 10:59:12 -07001675/**
1676 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1677 * @cpu: CPU number
1678 *
1679 * Just return the max possible frequency for a given CPU.
1680 */
1681unsigned int cpufreq_quick_get_max(unsigned int cpu)
1682{
1683 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1684 unsigned int ret_freq = 0;
1685
1686 if (policy) {
1687 ret_freq = policy->max;
1688 cpufreq_cpu_put(policy);
1689 }
1690
1691 return ret_freq;
1692}
1693EXPORT_SYMBOL(cpufreq_quick_get_max);
1694
Viresh Kumard92d50a2015-01-02 12:34:29 +05301695static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Yue Hu4db7c342019-04-19 14:27:58 +08001697 if (unlikely(policy_is_inactive(policy)))
Viresh Kumar59807522019-06-20 08:35:49 +05301698 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Viresh Kumar59807522019-06-20 08:35:49 +05301700 return cpufreq_verify_current_freq(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001703/**
1704 * cpufreq_get - get the current CPU frequency (in kHz)
1705 * @cpu: CPU number
1706 *
1707 * Get the CPU current (static) CPU frequency
1708 */
1709unsigned int cpufreq_get(unsigned int cpu)
1710{
Aaron Plattner999976e2014-03-04 12:42:15 -08001711 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001712 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001713
Aaron Plattner999976e2014-03-04 12:42:15 -08001714 if (policy) {
1715 down_read(&policy->rwsem);
Yue Hu4db7c342019-04-19 14:27:58 +08001716 if (cpufreq_driver->get)
1717 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001718 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301719
Aaron Plattner999976e2014-03-04 12:42:15 -08001720 cpufreq_cpu_put(policy);
1721 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301722
Dave Jones4d34a672008-02-07 16:33:49 -05001723 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725EXPORT_SYMBOL(cpufreq_get);
1726
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001727static struct subsys_interface cpufreq_interface = {
1728 .name = "cpufreq",
1729 .subsys = &cpu_subsys,
1730 .add_dev = cpufreq_add_dev,
1731 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001732};
1733
Viresh Kumare28867e2014-03-04 11:00:27 +08001734/*
1735 * In case platform wants some specific frequency to be configured
1736 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001737 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001738int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001739{
Viresh Kumare28867e2014-03-04 11:00:27 +08001740 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001741
Viresh Kumare28867e2014-03-04 11:00:27 +08001742 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001743 pr_debug("%s: suspend_freq not defined\n", __func__);
1744 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001745 }
1746
Viresh Kumare28867e2014-03-04 11:00:27 +08001747 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1748 policy->suspend_freq);
1749
1750 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1751 CPUFREQ_RELATION_H);
1752 if (ret)
1753 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1754 __func__, policy->suspend_freq, ret);
1755
Dave Jonesc9060492008-02-07 16:32:18 -05001756 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001757}
Viresh Kumare28867e2014-03-04 11:00:27 +08001758EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001759
1760/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001761 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001763 * Called during system wide Suspend/Hibernate cycles for suspending governors
1764 * as some platforms can't change frequency after this point in suspend cycle.
1765 * Because some of the devices (like: i2c, regulators, etc) they use for
1766 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001768void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301770 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001772 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001773 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001775 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301776 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001778 pr_debug("%s: Suspending Governors\n", __func__);
1779
Viresh Kumarf9637352015-05-12 12:20:11 +05301780 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001781 if (has_target()) {
1782 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001783 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001784 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001785 }
1786
1787 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001788 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1789 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 }
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301791
1792suspend:
1793 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001797 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001799 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1800 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001802void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301805 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001807 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 return;
1809
Bo Yan703cbaa2018-01-23 13:57:55 -08001810 if (unlikely(!cpufreq_suspended))
1811 return;
1812
Lan Tianyu8e304442014-09-18 15:03:07 +08001813 cpufreq_suspended = false;
1814
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001815 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001816 return;
1817
1818 pr_debug("%s: Resuming Governors\n", __func__);
1819
Viresh Kumarf9637352015-05-12 12:20:11 +05301820 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301821 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301822 pr_err("%s: Failed to resume driver: %p\n", __func__,
1823 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001824 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301825 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001826 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301827 up_write(&policy->rwsem);
1828
1829 if (ret)
1830 pr_err("%s: Failed to start governor for policy: %p\n",
1831 __func__, policy);
1832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Borislav Petkov9d950462013-01-20 10:24:28 +00001836/**
1837 * cpufreq_get_current_driver - return current driver's name
1838 *
1839 * Return the name string of the currently loaded cpufreq driver
1840 * or NULL, if none.
1841 */
1842const char *cpufreq_get_current_driver(void)
1843{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001844 if (cpufreq_driver)
1845 return cpufreq_driver->name;
1846
1847 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001848}
1849EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001851/**
1852 * cpufreq_get_driver_data - return current driver data
1853 *
1854 * Return the private data of the currently loaded cpufreq
1855 * driver, or NULL if no cpufreq driver is loaded.
1856 */
1857void *cpufreq_get_driver_data(void)
1858{
1859 if (cpufreq_driver)
1860 return cpufreq_driver->driver_data;
1861
1862 return NULL;
1863}
1864EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866/*********************************************************************
1867 * NOTIFIER LISTS INTERFACE *
1868 *********************************************************************/
1869
1870/**
1871 * cpufreq_register_notifier - register a driver with cpufreq
1872 * @nb: notifier function to register
1873 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1874 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001875 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 * are notified about clock rate changes (once before and once after
1877 * the transition), or a list of drivers that are notified about
1878 * changes in cpufreq policy.
1879 *
1880 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001881 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 */
1883int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1884{
1885 int ret;
1886
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001887 if (cpufreq_disabled())
1888 return -EINVAL;
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 switch (list) {
1891 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001892 mutex_lock(&cpufreq_fast_switch_lock);
1893
1894 if (cpufreq_fast_switch_count > 0) {
1895 mutex_unlock(&cpufreq_fast_switch_lock);
1896 return -EBUSY;
1897 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001898 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001899 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001900 if (!ret)
1901 cpufreq_fast_switch_count--;
1902
1903 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 break;
1905 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001906 ret = blocking_notifier_chain_register(
1907 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 break;
1909 default:
1910 ret = -EINVAL;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 return ret;
1914}
1915EXPORT_SYMBOL(cpufreq_register_notifier);
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917/**
1918 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1919 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301920 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 *
1922 * Remove a driver from the CPU frequency notifier list.
1923 *
1924 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001925 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
1927int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1928{
1929 int ret;
1930
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001931 if (cpufreq_disabled())
1932 return -EINVAL;
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 switch (list) {
1935 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001936 mutex_lock(&cpufreq_fast_switch_lock);
1937
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001938 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001939 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001940 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1941 cpufreq_fast_switch_count++;
1942
1943 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 break;
1945 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001946 ret = blocking_notifier_chain_unregister(
1947 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949 default:
1950 ret = -EINVAL;
1951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 return ret;
1954}
1955EXPORT_SYMBOL(cpufreq_unregister_notifier);
1956
1957
1958/*********************************************************************
1959 * GOVERNORS *
1960 *********************************************************************/
1961
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001962/**
1963 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1964 * @policy: cpufreq policy to switch the frequency for.
1965 * @target_freq: New frequency to set (may be approximate).
1966 *
1967 * Carry out a fast frequency switch without sleeping.
1968 *
1969 * The driver's ->fast_switch() callback invoked by this function must be
1970 * suitable for being called from within RCU-sched read-side critical sections
1971 * and it is expected to select the minimum available frequency greater than or
1972 * equal to @target_freq (CPUFREQ_RELATION_L).
1973 *
1974 * This function must not be called if policy->fast_switch_enabled is unset.
1975 *
1976 * Governors calling this function must guarantee that it will never be invoked
1977 * twice in parallel for the same policy and that it will never be called in
1978 * parallel with either ->target() or ->target_index() for the same policy.
1979 *
Viresh Kumar209887e2017-08-09 10:21:46 +05301980 * Returns the actual frequency set for the CPU.
1981 *
1982 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1983 * error condition, the hardware configuration must be preserved.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001984 */
1985unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1986 unsigned int target_freq)
1987{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001988 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001989
1990 return cpufreq_driver->fast_switch(policy, target_freq);
1991}
1992EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1993
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301994/* Must set freqs->new to intermediate frequency */
1995static int __target_intermediate(struct cpufreq_policy *policy,
1996 struct cpufreq_freqs *freqs, int index)
1997{
1998 int ret;
1999
2000 freqs->new = cpufreq_driver->get_intermediate(policy, index);
2001
2002 /* We don't need to switch to intermediate freq */
2003 if (!freqs->new)
2004 return 0;
2005
2006 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
2007 __func__, policy->cpu, freqs->old, freqs->new);
2008
2009 cpufreq_freq_transition_begin(policy, freqs);
2010 ret = cpufreq_driver->target_intermediate(policy, index);
2011 cpufreq_freq_transition_end(policy, freqs, ret);
2012
2013 if (ret)
2014 pr_err("%s: Failed to change to intermediate frequency: %d\n",
2015 __func__, ret);
2016
2017 return ret;
2018}
2019
Viresh Kumar23727842016-06-03 10:58:50 +05302020static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05302021{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302022 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
2023 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05302024 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05302025 int retval = -EINVAL;
2026 bool notify;
2027
Viresh Kumar23727842016-06-03 10:58:50 +05302028 if (newfreq == policy->cur)
2029 return 0;
2030
Viresh Kumar8d657752014-05-21 14:29:29 +05302031 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05302032 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302033 /* Handle switching to intermediate frequency */
2034 if (cpufreq_driver->get_intermediate) {
2035 retval = __target_intermediate(policy, &freqs, index);
2036 if (retval)
2037 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05302038
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302039 intermediate_freq = freqs.new;
2040 /* Set old freq to intermediate */
2041 if (intermediate_freq)
2042 freqs.old = freqs.new;
2043 }
2044
Viresh Kumar23727842016-06-03 10:58:50 +05302045 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05302046 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2047 __func__, policy->cpu, freqs.old, freqs.new);
2048
2049 cpufreq_freq_transition_begin(policy, &freqs);
2050 }
2051
2052 retval = cpufreq_driver->target_index(policy, index);
2053 if (retval)
2054 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2055 retval);
2056
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302057 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05302058 cpufreq_freq_transition_end(policy, &freqs, retval);
2059
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302060 /*
2061 * Failed after setting to intermediate freq? Driver should have
2062 * reverted back to initial frequency and so should we. Check
2063 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302064 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302065 */
2066 if (unlikely(retval && intermediate_freq)) {
2067 freqs.old = intermediate_freq;
2068 freqs.new = policy->restore_freq;
2069 cpufreq_freq_transition_begin(policy, &freqs);
2070 cpufreq_freq_transition_end(policy, &freqs, 0);
2071 }
2072 }
2073
Viresh Kumar8d657752014-05-21 14:29:29 +05302074 return retval;
2075}
2076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077int __cpufreq_driver_target(struct cpufreq_policy *policy,
2078 unsigned int target_freq,
2079 unsigned int relation)
2080{
Viresh Kumar72499242012-10-31 01:28:21 +01002081 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302082 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002083
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002084 if (cpufreq_disabled())
2085 return -ENODEV;
2086
Viresh Kumar72499242012-10-31 01:28:21 +01002087 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302088 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002089
2090 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002091 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002092
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302093 /*
2094 * This might look like a redundant call as we are checking it again
2095 * after finding index. But it is left intentionally for cases where
2096 * exactly same freq is called again and so we can save on few function
2097 * calls.
2098 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002099 if (target_freq == policy->cur)
2100 return 0;
2101
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302102 /* Save last value to restore later on errors */
2103 policy->restore_freq = policy->cur;
2104
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002105 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002106 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002107
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002108 if (!cpufreq_driver->target_index)
2109 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302110
Viresh Kumard218ed72016-06-03 10:58:51 +05302111 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302112
Viresh Kumar23727842016-06-03 10:58:50 +05302113 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117int cpufreq_driver_target(struct cpufreq_policy *policy,
2118 unsigned int target_freq,
2119 unsigned int relation)
2120{
Julia Lawallf1829e42008-07-25 22:44:53 +02002121 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
viresh kumarad7722d2013-10-18 19:10:15 +05302123 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 ret = __cpufreq_driver_target(policy, target_freq, relation);
2126
viresh kumarad7722d2013-10-18 19:10:15 +05302127 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return ret;
2130}
2131EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2132
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002133__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2134{
2135 return NULL;
2136}
2137
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002138static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
Dave Jonescc993ca2005-07-28 09:43:56 -07002140 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002141
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002142 /* Don't start any governor operations if we are entering suspend */
2143 if (cpufreq_suspended)
2144 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002145 /*
2146 * Governor might not be initiated here if ACPI _PPC changed
2147 * notification happened, so check it.
2148 */
2149 if (!policy->governor)
2150 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002151
Viresh Kumared4676e2017-07-19 15:42:46 +05302152 /* Platform doesn't want dynamic frequency switching ? */
2153 if (policy->governor->dynamic_switching &&
Viresh Kumarfc4c7092017-07-19 15:42:49 +05302154 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002155 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2156
2157 if (gov) {
Viresh Kumarfe829ed2017-07-19 15:42:48 +05302158 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002159 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002160 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002161 } else {
2162 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002163 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002166 if (!try_module_get(policy->governor->owner))
2167 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002169 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002170
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002171 if (policy->governor->init) {
2172 ret = policy->governor->init(policy);
2173 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002174 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002175 return ret;
2176 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002179 return 0;
2180}
2181
2182static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2183{
2184 if (cpufreq_suspended || !policy->governor)
2185 return;
2186
2187 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2188
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002189 if (policy->governor->exit)
2190 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002191
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002192 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002195static int cpufreq_start_governor(struct cpufreq_policy *policy)
2196{
2197 int ret;
2198
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002199 if (cpufreq_suspended)
2200 return 0;
2201
2202 if (!policy->governor)
2203 return -EINVAL;
2204
2205 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2206
Viresh Kumar407d0ff2019-06-20 08:35:46 +05302207 if (cpufreq_driver->get)
Viresh Kumar59807522019-06-20 08:35:49 +05302208 cpufreq_verify_current_freq(policy, false);
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002209
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002210 if (policy->governor->start) {
2211 ret = policy->governor->start(policy);
2212 if (ret)
2213 return ret;
2214 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002215
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002216 if (policy->governor->limits)
2217 policy->governor->limits(policy);
2218
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002219 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002220}
2221
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002222static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2223{
2224 if (cpufreq_suspended || !policy->governor)
2225 return;
2226
2227 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2228
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002229 if (policy->governor->stop)
2230 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002231}
2232
2233static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2234{
2235 if (cpufreq_suspended || !policy->governor)
2236 return;
2237
2238 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2239
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002240 if (policy->governor->limits)
2241 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244int cpufreq_register_governor(struct cpufreq_governor *governor)
2245{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002246 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248 if (!governor)
2249 return -EINVAL;
2250
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002251 if (cpufreq_disabled())
2252 return -ENODEV;
2253
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002254 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002255
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002256 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302257 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002258 err = 0;
2259 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Dave Jones32ee8c32006-02-28 00:43:23 -05002262 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002263 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
2265EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2268{
Viresh Kumar45732372015-05-12 12:22:34 +05302269 struct cpufreq_policy *policy;
2270 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (!governor)
2273 return;
2274
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002275 if (cpufreq_disabled())
2276 return;
2277
Viresh Kumar45732372015-05-12 12:22:34 +05302278 /* clear last_governor for all inactive policies */
2279 read_lock_irqsave(&cpufreq_driver_lock, flags);
2280 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302281 if (!strcmp(policy->last_governor, governor->name)) {
2282 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302283 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302284 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002285 }
Viresh Kumar45732372015-05-12 12:22:34 +05302286 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002287
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002288 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002290 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2293
2294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295/*********************************************************************
2296 * POLICY INTERFACE *
2297 *********************************************************************/
2298
2299/**
2300 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002301 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2302 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 *
2304 * Reads the current cpufreq policy.
2305 */
2306int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2307{
2308 struct cpufreq_policy *cpu_policy;
2309 if (!policy)
2310 return -EINVAL;
2311
2312 cpu_policy = cpufreq_cpu_get(cpu);
2313 if (!cpu_policy)
2314 return -EINVAL;
2315
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302316 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return 0;
2320}
2321EXPORT_SYMBOL(cpufreq_get_policy);
2322
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002323/**
2324 * cpufreq_set_policy - Modify cpufreq policy parameters.
2325 * @policy: Policy object to modify.
2326 * @new_policy: New policy data.
2327 *
2328 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2329 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2330 * the driver's ->verify() callback again and run the notifiers for it again
2331 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2332 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2333 * callback (if present) or carry out a governor update for @policy. That is,
2334 * run the current governor's ->limits() callback (if the governor field in
2335 * @new_policy points to the same object as the one in @policy) or replace the
2336 * governor for @policy with the new one stored in @new_policy.
2337 *
2338 * The cpuinfo part of @policy is not updated by this function.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002339 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +01002340int cpufreq_set_policy(struct cpufreq_policy *policy,
2341 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002343 struct cpufreq_governor *old_gov;
Viresh Kumar67d874c2019-07-08 16:27:52 +05302344 struct device *cpu_dev = get_cpu_device(policy->cpu);
2345 unsigned long min, max;
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002346 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Joe Perchese837f9b2014-03-11 10:03:00 -07002348 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2349 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302351 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Pan Xinhuifba95732015-07-30 18:10:40 +08002353 /*
2354 * This check works well when we store new min/max freq attributes,
2355 * because new_policy is a copy of policy with one field updated.
2356 */
2357 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002358 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002359
Viresh Kumar67d874c2019-07-08 16:27:52 +05302360 /*
2361 * PM QoS framework collects all the requests from users and provide us
2362 * the final aggregated value here.
2363 */
2364 min = dev_pm_qos_read_value(cpu_dev, DEV_PM_QOS_MIN_FREQUENCY);
2365 max = dev_pm_qos_read_value(cpu_dev, DEV_PM_QOS_MAX_FREQUENCY);
2366
2367 if (min > new_policy->min)
2368 new_policy->min = min;
2369 if (max < new_policy->max)
2370 new_policy->max = max;
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302373 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002375 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Viresh Kumar67d874c2019-07-08 16:27:52 +05302377 /*
2378 * The notifier-chain shall be removed once all the users of
2379 * CPUFREQ_ADJUST are moved to use the QoS framework.
2380 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002382 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302383 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
Viresh Kumarbb176f72013-06-19 14:19:33 +05302385 /*
2386 * verify the cpu speed can be set within this limit, which might be
2387 * different to the first one
2388 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302389 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002390 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002391 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002394 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302395 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302397 policy->min = new_policy->min;
2398 policy->max = new_policy->max;
Ruchi Kandoi601b2182018-07-24 10:35:44 -07002399 trace_cpu_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Steve Mucklee3c06232016-07-13 13:25:25 -07002401 policy->cached_target_freq = UINT_MAX;
2402
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002403 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002404 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002406 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302407 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002408 pr_debug("setting range\n");
Rafael J. Wysocki167a38d2019-02-20 00:26:30 +01002409 return cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002412 if (new_policy->governor == policy->governor) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002413 pr_debug("governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002414 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002415 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002416 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002417
2418 pr_debug("governor switch\n");
2419
2420 /* save old, working values */
2421 old_gov = policy->governor;
2422 /* end old governor */
2423 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002424 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002425 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002426 }
2427
2428 /* start new governor */
2429 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002430 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302431 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002432 ret = cpufreq_start_governor(policy);
2433 if (!ret) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002434 pr_debug("governor change\n");
Quentin Perret531b5c92018-12-03 09:56:21 +00002435 sched_cpufreq_governor_change(policy, old_gov);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002436 return 0;
2437 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002438 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002439 }
2440
2441 /* new governor failed, so re-start old one */
2442 pr_debug("starting governor %s failed\n", policy->governor->name);
2443 if (old_gov) {
2444 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002445 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302446 policy->governor = NULL;
2447 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002448 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002449 }
2450
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302451 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
2454/**
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002455 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2456 * @cpu: CPU to re-evaluate the policy for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 *
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002458 * Update the current frequency for the cpufreq policy of @cpu and use
2459 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2460 * user_policy sub-structure of that policy, which triggers the evaluation
2461 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2462 * policy in question, among other things.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 */
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002464void cpufreq_update_policy(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002466 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002468 if (!policy)
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002469 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Viresh Kumarbb176f72013-06-19 14:19:33 +05302471 /*
2472 * BIOS might change freq behind our back
2473 * -> ask driver for current freq and notify governors about a change
2474 */
Viresh Kumar5ddc6d42019-06-20 08:35:48 +05302475 if (cpufreq_driver->get && has_target() &&
Viresh Kumar59807522019-06-20 08:35:49 +05302476 (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002477 goto unlock;
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002478
Viresh Kumar70a59fd2019-06-20 08:35:50 +05302479 refresh_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002481unlock:
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002482 cpufreq_cpu_release(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483}
2484EXPORT_SYMBOL(cpufreq_update_policy);
2485
Rafael J. Wysocki5a25e3f2019-03-26 12:15:13 +01002486/**
2487 * cpufreq_update_limits - Update policy limits for a given CPU.
2488 * @cpu: CPU to update the policy limits for.
2489 *
2490 * Invoke the driver's ->update_limits callback if present or call
2491 * cpufreq_update_policy() for @cpu.
2492 */
2493void cpufreq_update_limits(unsigned int cpu)
2494{
2495 if (cpufreq_driver->update_limits)
2496 cpufreq_driver->update_limits(cpu);
2497 else
2498 cpufreq_update_policy(cpu);
2499}
2500EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002503 * BOOST *
2504 *********************************************************************/
2505static int cpufreq_boost_set_sw(int state)
2506{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002507 struct cpufreq_policy *policy;
2508 int ret = -EINVAL;
2509
Viresh Kumarf9637352015-05-12 12:20:11 +05302510 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302511 if (!policy->freq_table)
2512 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302513
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302514 ret = cpufreq_frequency_table_cpuinfo(policy,
2515 policy->freq_table);
2516 if (ret) {
2517 pr_err("%s: Policy frequency update failed\n",
2518 __func__);
2519 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002520 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302521
2522 down_write(&policy->rwsem);
2523 policy->user_policy.max = policy->max;
2524 cpufreq_governor_limits(policy);
2525 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002526 }
2527
2528 return ret;
2529}
2530
2531int cpufreq_boost_trigger_state(int state)
2532{
2533 unsigned long flags;
2534 int ret = 0;
2535
2536 if (cpufreq_driver->boost_enabled == state)
2537 return 0;
2538
2539 write_lock_irqsave(&cpufreq_driver_lock, flags);
2540 cpufreq_driver->boost_enabled = state;
2541 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2542
2543 ret = cpufreq_driver->set_boost(state);
2544 if (ret) {
2545 write_lock_irqsave(&cpufreq_driver_lock, flags);
2546 cpufreq_driver->boost_enabled = !state;
2547 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2548
Joe Perchese837f9b2014-03-11 10:03:00 -07002549 pr_err("%s: Cannot %s BOOST\n",
2550 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002551 }
2552
2553 return ret;
2554}
2555
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002556static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002557{
Yue Hu89f98d72019-04-09 10:25:36 +08002558 return cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002559}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002560
Viresh Kumar44139ed2015-07-29 16:23:09 +05302561static int create_boost_sysfs_file(void)
2562{
2563 int ret;
2564
Viresh Kumarc82bd442015-10-15 21:35:23 +05302565 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302566 if (ret)
2567 pr_err("%s: cannot register global BOOST sysfs file\n",
2568 __func__);
2569
2570 return ret;
2571}
2572
2573static void remove_boost_sysfs_file(void)
2574{
2575 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302576 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302577}
2578
2579int cpufreq_enable_boost_support(void)
2580{
2581 if (!cpufreq_driver)
2582 return -EINVAL;
2583
2584 if (cpufreq_boost_supported())
2585 return 0;
2586
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002587 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302588
2589 /* This will get removed on driver unregister */
2590 return create_boost_sysfs_file();
2591}
2592EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2593
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002594int cpufreq_boost_enabled(void)
2595{
2596 return cpufreq_driver->boost_enabled;
2597}
2598EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2599
2600/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2602 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002603static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Chen Yuc4a3fa22017-04-09 13:45:16 +08002605static int cpuhp_cpufreq_online(unsigned int cpu)
2606{
2607 cpufreq_online(cpu);
2608
2609 return 0;
2610}
2611
2612static int cpuhp_cpufreq_offline(unsigned int cpu)
2613{
2614 cpufreq_offline(cpu);
2615
2616 return 0;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/**
2620 * cpufreq_register_driver - register a CPU Frequency driver
2621 * @driver_data: A struct cpufreq_driver containing the values#
2622 * submitted by the CPU Frequency driver.
2623 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302624 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002625 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002626 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 *
2628 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002629int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
2631 unsigned long flags;
2632 int ret;
2633
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002634 if (cpufreq_disabled())
2635 return -ENODEV;
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302638 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002639 driver_data->target) ||
2640 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302641 driver_data->target)) ||
Viresh Kumara9a22b52019-02-14 16:16:21 +05302642 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
Viresh Kumar91a12e92019-02-12 16:36:04 +05302643 (!driver_data->online != !driver_data->offline))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return -EINVAL;
2645
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002646 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002648 /* Protect against concurrent CPU online/offline. */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002649 cpus_read_lock();
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002650
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002651 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002652 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002653 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002654 ret = -EEXIST;
2655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002657 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002658 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302660 if (driver_data->setpolicy)
2661 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2662
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002663 if (cpufreq_boost_supported()) {
2664 ret = create_boost_sysfs_file();
2665 if (ret)
2666 goto err_null_driver;
2667 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002668
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002669 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002670 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002671 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302673 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2674 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 /* if all ->init() calls failed, unregister */
David Arcari6c770032017-05-26 11:37:31 -04002676 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302677 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2678 driver_data->name);
2679 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
2681
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002682 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2683 "cpufreq:online",
2684 cpuhp_cpufreq_online,
2685 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002686 if (ret < 0)
2687 goto err_if_unreg;
2688 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002689 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002690
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002691 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002692 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002693
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002694err_if_unreg:
2695 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002696err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302697 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002698err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002699 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002700 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002701 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002702out:
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002703 cpus_read_unlock();
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002704 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708/**
2709 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2710 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302711 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 * the right to do so, i.e. if you have succeeded in initialising before!
2713 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2714 * currently not initialised.
2715 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002716int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 unsigned long flags;
2719
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002720 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002723 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002725 /* Protect against concurrent cpu hotplug */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002726 cpus_read_lock();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002727 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302728 remove_boost_sysfs_file();
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002729 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002731 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302732
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002733 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302734
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002735 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002736 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 return 0;
2739}
2740EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002741
Doug Anderson90de2a42014-12-23 22:09:48 -08002742/*
2743 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2744 * or mutexes when secondary CPUs are halted.
2745 */
2746static struct syscore_ops cpufreq_syscore_ops = {
2747 .shutdown = cpufreq_suspend,
2748};
2749
Viresh Kumarc82bd442015-10-15 21:35:23 +05302750struct kobject *cpufreq_global_kobject;
2751EXPORT_SYMBOL(cpufreq_global_kobject);
2752
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002753static int __init cpufreq_core_init(void)
2754{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002755 if (cpufreq_disabled())
2756 return -ENODEV;
2757
Viresh Kumar8eec1022015-10-15 21:35:22 +05302758 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002759 BUG_ON(!cpufreq_global_kobject);
2760
Doug Anderson90de2a42014-12-23 22:09:48 -08002761 register_syscore_ops(&cpufreq_syscore_ops);
2762
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002763 return 0;
2764}
Len Brownd82f2692017-02-28 16:44:16 -05002765module_param(off, int, 0444);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002766core_initcall(cpufreq_core_init);