blob: 99aa7d20b4583ff8598b164363695bd845e1ea8f [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{ \
Viresh Kumar18c49922019-07-05 16:21:24 +0530721 unsigned long val; \
722 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 \
Viresh Kumar18c49922019-07-05 16:21:24 +0530724 ret = sscanf(buf, "%lu", &val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 if (ret != 1) \
726 return -EINVAL; \
727 \
Viresh Kumar18c49922019-07-05 16:21:24 +0530728 ret = dev_pm_qos_update_request(policy->object##_freq_req, val);\
729 return ret >= 0 ? count : ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Dave Jones29464f22009-01-18 01:37:11 -0500732store_one(scaling_min_freq, min);
733store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735/**
736 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
737 */
Dave Jones905d77c2008-03-05 14:28:32 -0500738static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
739 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530741 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysocki9b4f6032017-03-15 00:12:16 +0100742
743 if (cur_freq)
744 return sprintf(buf, "%u\n", cur_freq);
745
746 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749/**
750 * show_scaling_governor - show the current policy for the specified CPU
751 */
Dave Jones905d77c2008-03-05 14:28:32 -0500752static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Dave Jones29464f22009-01-18 01:37:11 -0500754 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return sprintf(buf, "powersave\n");
756 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
757 return sprintf(buf, "performance\n");
758 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200759 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500760 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return -EINVAL;
762}
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/**
765 * store_scaling_governor - store policy for the specified CPU
766 */
Dave Jones905d77c2008-03-05 14:28:32 -0500767static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
768 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530770 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 char str_governor[16];
772 struct cpufreq_policy new_policy;
773
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530774 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dave Jones29464f22009-01-18 01:37:11 -0500776 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (ret != 1)
778 return -EINVAL;
779
Yue Huab05d972019-04-29 15:24:18 +0800780 if (cpufreq_driver->setpolicy) {
781 if (cpufreq_parse_policy(str_governor, &new_policy))
782 return -EINVAL;
783 } else {
784 if (cpufreq_parse_governor(str_governor, &new_policy))
785 return -EINVAL;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Viresh Kumar037ce832013-10-02 14:13:16 +0530788 ret = cpufreq_set_policy(policy, &new_policy);
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100789
790 if (new_policy.governor)
791 module_put(new_policy.governor->owner);
792
Viresh Kumar88dc4382015-08-03 08:36:18 +0530793 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
795
796/**
797 * show_scaling_driver - show the cpufreq driver currently loaded
798 */
Dave Jones905d77c2008-03-05 14:28:32 -0500799static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200801 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
803
804/**
805 * show_scaling_available_governors - show the available CPUfreq governors
806 */
Dave Jones905d77c2008-03-05 14:28:32 -0500807static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
808 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 ssize_t i = 0;
811 struct cpufreq_governor *t;
812
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530813 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 i += sprintf(buf, "performance powersave");
815 goto out;
816 }
817
Viresh Kumarf7b27062015-01-27 14:06:09 +0530818 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500819 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
820 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200822 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500824out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 i += sprintf(&buf[i], "\n");
826 return i;
827}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700828
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800829ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 ssize_t i = 0;
832 unsigned int cpu;
833
Rusty Russell835481d2009-01-04 05:18:06 -0800834 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (i)
836 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
837 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
838 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500839 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841 i += sprintf(&buf[i], "\n");
842 return i;
843}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800844EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700846/**
847 * show_related_cpus - show the CPUs affected by each transition even if
848 * hw coordination is in use
849 */
850static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
851{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800852 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700853}
854
855/**
856 * show_affected_cpus - show the CPUs affected by each transition
857 */
858static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
859{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800860 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700861}
862
Venki Pallipadi9e769882007-10-26 10:18:21 -0700863static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500864 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700865{
866 unsigned int freq = 0;
867 unsigned int ret;
868
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700869 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700870 return -EINVAL;
871
872 ret = sscanf(buf, "%u", &freq);
873 if (ret != 1)
874 return -EINVAL;
875
876 policy->governor->store_setspeed(policy, freq);
877
878 return count;
879}
880
881static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
882{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700883 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700884 return sprintf(buf, "<unsupported>\n");
885
886 return policy->governor->show_setspeed(policy, buf);
887}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Thomas Renningere2f74f32009-11-19 12:31:01 +0100889/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200890 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100891 */
892static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
893{
894 unsigned int limit;
895 int ret;
Yue Hub23aa312019-04-16 10:40:27 +0800896 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
897 if (!ret)
898 return sprintf(buf, "%u\n", limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100899 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
900}
901
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200902cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
903cpufreq_freq_attr_ro(cpuinfo_min_freq);
904cpufreq_freq_attr_ro(cpuinfo_max_freq);
905cpufreq_freq_attr_ro(cpuinfo_transition_latency);
906cpufreq_freq_attr_ro(scaling_available_governors);
907cpufreq_freq_attr_ro(scaling_driver);
908cpufreq_freq_attr_ro(scaling_cur_freq);
909cpufreq_freq_attr_ro(bios_limit);
910cpufreq_freq_attr_ro(related_cpus);
911cpufreq_freq_attr_ro(affected_cpus);
912cpufreq_freq_attr_rw(scaling_min_freq);
913cpufreq_freq_attr_rw(scaling_max_freq);
914cpufreq_freq_attr_rw(scaling_governor);
915cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Dave Jones905d77c2008-03-05 14:28:32 -0500917static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 &cpuinfo_min_freq.attr,
919 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100920 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 &scaling_min_freq.attr,
922 &scaling_max_freq.attr,
923 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700924 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 &scaling_governor.attr,
926 &scaling_driver.attr,
927 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700928 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 NULL
930};
931
Dave Jones29464f22009-01-18 01:37:11 -0500932#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
933#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Dave Jones29464f22009-01-18 01:37:11 -0500935static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
Dave Jones905d77c2008-03-05 14:28:32 -0500937 struct cpufreq_policy *policy = to_policy(kobj);
938 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530939 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530940
viresh kumarad7722d2013-10-18 19:10:15 +0530941 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100942 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530943 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return ret;
946}
947
Dave Jones905d77c2008-03-05 14:28:32 -0500948static ssize_t store(struct kobject *kobj, struct attribute *attr,
949 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Dave Jones905d77c2008-03-05 14:28:32 -0500951 struct cpufreq_policy *policy = to_policy(kobj);
952 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500953 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530954
Waiman Long9b3d9bb2018-07-24 14:26:05 -0400955 /*
956 * cpus_read_trylock() is used here to work around a circular lock
957 * dependency problem with respect to the cpufreq_register_driver().
958 */
959 if (!cpus_read_trylock())
960 return -EBUSY;
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530961
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100962 if (cpu_online(policy->cpu)) {
963 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530964 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100965 up_write(&policy->rwsem);
966 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530967
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200968 cpus_read_unlock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return ret;
971}
972
Dave Jones905d77c2008-03-05 14:28:32 -0500973static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Dave Jones905d77c2008-03-05 14:28:32 -0500975 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200976 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 complete(&policy->kobj_unregister);
978}
979
Emese Revfy52cf25d2010-01-19 02:58:23 +0100980static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 .show = show,
982 .store = store,
983};
984
985static struct kobj_type ktype_cpufreq = {
986 .sysfs_ops = &sysfs_ops,
987 .default_attrs = default_attrs,
988 .release = cpufreq_sysfs_release,
989};
990
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200991static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +0200992{
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200993 struct device *dev = get_cpu_device(cpu);
994
Viresh Kumar67d874c2019-07-08 16:27:52 +0530995 if (unlikely(!dev))
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200996 return;
997
998 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
999 return;
1000
Viresh Kumar266198042016-09-12 12:07:05 +05301001 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001002 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1003 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +02001004}
1005
Viresh Kumar266198042016-09-12 12:07:05 +05301006static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1007 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +02001008{
Viresh Kumar266198042016-09-12 12:07:05 +05301009 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1010 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +02001011}
1012
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001013static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -04001014{
1015 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -04001016 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -04001017
Dave Jones909a6942009-07-08 18:05:42 -04001018 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001019 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +05301020 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -04001021 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1022 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001023 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001024 drv_attr++;
1025 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001026 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001027 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1028 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001029 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001030 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001031
1032 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1033 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001034 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001035
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001036 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001037 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1038 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001039 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001040 }
Dave Jones909a6942009-07-08 18:05:42 -04001041
Viresh Kumar266198042016-09-12 12:07:05 +05301042 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301043}
1044
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001045__weak struct cpufreq_governor *cpufreq_default_governor(void)
1046{
1047 return NULL;
1048}
1049
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301050static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301051{
Yue Huab05d972019-04-29 15:24:18 +08001052 struct cpufreq_governor *gov = NULL, *def_gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301053 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301054
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301055 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001056
Yue Huab05d972019-04-29 15:24:18 +08001057 def_gov = cpufreq_default_governor();
1058
1059 if (has_target()) {
1060 /*
1061 * Update governor of new_policy to the governor used before
1062 * hotplug
1063 */
1064 gov = find_governor(policy->last_governor);
1065 if (gov) {
1066 pr_debug("Restoring governor %s for cpu %d\n",
viresh kumar6e2c89d2014-03-04 11:43:59 +08001067 policy->governor->name, policy->cpu);
Yue Huab05d972019-04-29 15:24:18 +08001068 } else {
1069 if (!def_gov)
1070 return -ENODATA;
1071 gov = def_gov;
1072 }
1073 new_policy.governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001074 } else {
Yue Huab05d972019-04-29 15:24:18 +08001075 /* Use the default policy if there is no last_policy. */
1076 if (policy->last_policy) {
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001077 new_policy.policy = policy->last_policy;
Yue Huab05d972019-04-29 15:24:18 +08001078 } else {
1079 if (!def_gov)
1080 return -ENODATA;
1081 cpufreq_parse_policy(def_gov->name, &new_policy);
1082 }
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001083 }
Yue Huab05d972019-04-29 15:24:18 +08001084
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301085 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001086}
1087
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001088static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001089{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301090 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001091
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301092 /* Has this CPU been taken care of already? */
1093 if (cpumask_test_cpu(cpu, policy->cpus))
1094 return 0;
1095
Viresh Kumar49f18562016-02-11 17:31:12 +05301096 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001097 if (has_target())
1098 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001099
Viresh Kumarfcf80582013-01-29 14:39:08 +00001100 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301101
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301102 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001103 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301104 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301105 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001106 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301107 up_write(&policy->rwsem);
1108 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001109}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Viresh Kumarc57b25b2019-07-04 13:06:22 +05301111void refresh_frequency_limits(struct cpufreq_policy *policy)
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301112{
Viresh Kumar67d874c2019-07-08 16:27:52 +05301113 struct cpufreq_policy new_policy;
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301114
Viresh Kumar67d874c2019-07-08 16:27:52 +05301115 if (!policy_is_inactive(policy)) {
1116 new_policy = *policy;
1117 pr_debug("updating policy for CPU %u\n", policy->cpu);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301118
Viresh Kumar67d874c2019-07-08 16:27:52 +05301119 cpufreq_set_policy(policy, &new_policy);
1120 }
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301121}
Viresh Kumarc57b25b2019-07-04 13:06:22 +05301122EXPORT_SYMBOL(refresh_frequency_limits);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301123
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301124static void handle_update(struct work_struct *work)
1125{
1126 struct cpufreq_policy *policy =
1127 container_of(work, struct cpufreq_policy, update);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301128
1129 pr_debug("handle_update for cpu %u called\n", policy->cpu);
Viresh Kumar67d874c2019-07-08 16:27:52 +05301130 down_write(&policy->rwsem);
Viresh Kumar70a59fd2019-06-20 08:35:50 +05301131 refresh_frequency_limits(policy);
Viresh Kumar67d874c2019-07-08 16:27:52 +05301132 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Viresh Kumar67d874c2019-07-08 16:27:52 +05301135static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
1136 void *data)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301137{
Viresh Kumar67d874c2019-07-08 16:27:52 +05301138 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301139
Viresh Kumar67d874c2019-07-08 16:27:52 +05301140 schedule_work(&policy->update);
1141 return 0;
1142}
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301143
Viresh Kumar67d874c2019-07-08 16:27:52 +05301144static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
1145 void *data)
1146{
1147 struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301148
Viresh Kumar67d874c2019-07-08 16:27:52 +05301149 schedule_work(&policy->update);
1150 return 0;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301151}
1152
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301153static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301154{
1155 struct kobject *kobj;
1156 struct completion *cmp;
1157
Viresh Kumar87549142015-06-10 02:13:21 +02001158 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001159 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301160 kobj = &policy->kobj;
1161 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001162 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301163 kobject_put(kobj);
1164
1165 /*
1166 * We need to make sure that the underlying kobj is
1167 * actually not referenced anymore by anybody before we
1168 * proceed with unloading.
1169 */
1170 pr_debug("waiting for dropping of refcount\n");
1171 wait_for_completion(cmp);
1172 pr_debug("wait complete\n");
1173}
1174
Viresh Kumar67d874c2019-07-08 16:27:52 +05301175static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1176{
1177 struct cpufreq_policy *policy;
1178 struct device *dev = get_cpu_device(cpu);
1179 int ret;
1180
1181 if (!dev)
1182 return NULL;
1183
1184 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1185 if (!policy)
1186 return NULL;
1187
1188 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1189 goto err_free_policy;
1190
1191 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1192 goto err_free_cpumask;
1193
1194 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1195 goto err_free_rcpumask;
1196
1197 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1198 cpufreq_global_kobject, "policy%u", cpu);
1199 if (ret) {
1200 dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
1201 /*
1202 * The entire policy object will be freed below, but the extra
1203 * memory allocated for the kobject name needs to be freed by
1204 * releasing the kobject.
1205 */
1206 kobject_put(&policy->kobj);
1207 goto err_free_real_cpus;
1208 }
1209
1210 policy->nb_min.notifier_call = cpufreq_notifier_min;
1211 policy->nb_max.notifier_call = cpufreq_notifier_max;
1212
1213 ret = dev_pm_qos_add_notifier(dev, &policy->nb_min,
1214 DEV_PM_QOS_MIN_FREQUENCY);
1215 if (ret) {
1216 dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n",
1217 ret, cpumask_pr_args(policy->cpus));
1218 goto err_kobj_remove;
1219 }
1220
1221 ret = dev_pm_qos_add_notifier(dev, &policy->nb_max,
1222 DEV_PM_QOS_MAX_FREQUENCY);
1223 if (ret) {
1224 dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n",
1225 ret, cpumask_pr_args(policy->cpus));
1226 goto err_min_qos_notifier;
1227 }
1228
1229 INIT_LIST_HEAD(&policy->policy_list);
1230 init_rwsem(&policy->rwsem);
1231 spin_lock_init(&policy->transition_lock);
1232 init_waitqueue_head(&policy->transition_wait);
1233 init_completion(&policy->kobj_unregister);
1234 INIT_WORK(&policy->update, handle_update);
1235
1236 policy->cpu = cpu;
1237 return policy;
1238
1239err_min_qos_notifier:
1240 dev_pm_qos_remove_notifier(dev, &policy->nb_min,
1241 DEV_PM_QOS_MIN_FREQUENCY);
1242err_kobj_remove:
1243 cpufreq_policy_put_kobj(policy);
1244err_free_real_cpus:
1245 free_cpumask_var(policy->real_cpus);
1246err_free_rcpumask:
1247 free_cpumask_var(policy->related_cpus);
1248err_free_cpumask:
1249 free_cpumask_var(policy->cpus);
1250err_free_policy:
1251 kfree(policy);
1252
1253 return NULL;
1254}
1255
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301256static void cpufreq_policy_free(struct cpufreq_policy *policy)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301257{
Viresh Kumar67d874c2019-07-08 16:27:52 +05301258 struct device *dev = get_cpu_device(policy->cpu);
Viresh Kumar988bed02015-05-08 11:53:45 +05301259 unsigned long flags;
1260 int cpu;
1261
1262 /* Remove policy from list */
1263 write_lock_irqsave(&cpufreq_driver_lock, flags);
1264 list_del(&policy->policy_list);
1265
1266 for_each_cpu(cpu, policy->related_cpus)
1267 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1268 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1269
Viresh Kumar67d874c2019-07-08 16:27:52 +05301270 dev_pm_qos_remove_notifier(dev, &policy->nb_max,
1271 DEV_PM_QOS_MAX_FREQUENCY);
1272 dev_pm_qos_remove_notifier(dev, &policy->nb_min,
1273 DEV_PM_QOS_MIN_FREQUENCY);
Viresh Kumar18c49922019-07-05 16:21:24 +05301274 dev_pm_qos_remove_request(policy->max_freq_req);
1275 dev_pm_qos_remove_request(policy->min_freq_req);
1276 kfree(policy->min_freq_req);
Viresh Kumar67d874c2019-07-08 16:27:52 +05301277
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301278 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001279 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301280 free_cpumask_var(policy->related_cpus);
1281 free_cpumask_var(policy->cpus);
1282 kfree(policy);
1283}
1284
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001285static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301287 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001288 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001290 unsigned int j;
1291 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001292
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001293 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301294
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301295 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301296 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001297 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301298 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001299 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001300 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001302 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001303 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001304 down_write(&policy->rwsem);
1305 policy->cpu = cpu;
1306 policy->governor = NULL;
1307 up_write(&policy->rwsem);
1308 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001309 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001310 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001311 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001312 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001313 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301314
Viresh Kumar91a12e92019-02-12 16:36:04 +05301315 if (!new_policy && cpufreq_driver->online) {
1316 ret = cpufreq_driver->online(policy);
1317 if (ret) {
1318 pr_debug("%s: %d: initialization failed\n", __func__,
1319 __LINE__);
1320 goto out_exit_policy;
1321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Viresh Kumar91a12e92019-02-12 16:36:04 +05301323 /* Recover policy->cpus using related_cpus */
1324 cpumask_copy(policy->cpus, policy->related_cpus);
1325 } else {
1326 cpumask_copy(policy->cpus, cpumask_of(cpu));
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001327
Viresh Kumar91a12e92019-02-12 16:36:04 +05301328 /*
1329 * Call driver. From then on the cpufreq must be able
1330 * to accept all calls to ->verify and ->setpolicy for this CPU.
1331 */
1332 ret = cpufreq_driver->init(policy);
1333 if (ret) {
1334 pr_debug("%s: %d: initialization failed\n", __func__,
1335 __LINE__);
1336 goto out_free_policy;
1337 }
Viresh Kumard417e062018-02-22 11:29:44 +05301338
Viresh Kumar91a12e92019-02-12 16:36:04 +05301339 ret = cpufreq_table_validate_and_sort(policy);
1340 if (ret)
1341 goto out_exit_policy;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001342
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001343 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301344 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001345 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001346
Viresh Kumar91a12e92019-02-12 16:36:04 +05301347 down_write(&policy->rwsem);
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001348 /*
1349 * affected cpus must always be the one, which are online. We aren't
1350 * managing offline cpus here.
1351 */
1352 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1353
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001354 if (new_policy) {
Viresh Kumar18c49922019-07-05 16:21:24 +05301355 struct device *dev = get_cpu_device(cpu);
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001356
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001357 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301358 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001359 add_cpu_dev_symlink(policy, j);
1360 }
Viresh Kumar18c49922019-07-05 16:21:24 +05301361
1362 policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
1363 GFP_KERNEL);
1364 if (!policy->min_freq_req)
1365 goto out_destroy_policy;
1366
1367 ret = dev_pm_qos_add_request(dev, policy->min_freq_req,
1368 DEV_PM_QOS_MIN_FREQUENCY,
1369 policy->min);
1370 if (ret < 0) {
1371 /*
1372 * So we don't call dev_pm_qos_remove_request() for an
1373 * uninitialized request.
1374 */
1375 kfree(policy->min_freq_req);
1376 policy->min_freq_req = NULL;
1377
1378 dev_err(dev, "Failed to add min-freq constraint (%d)\n",
1379 ret);
1380 goto out_destroy_policy;
1381 }
1382
1383 /*
1384 * This must be initialized right here to avoid calling
1385 * dev_pm_qos_remove_request() on uninitialized request in case
1386 * of errors.
1387 */
1388 policy->max_freq_req = policy->min_freq_req + 1;
1389
1390 ret = dev_pm_qos_add_request(dev, policy->max_freq_req,
1391 DEV_PM_QOS_MAX_FREQUENCY,
1392 policy->max);
1393 if (ret < 0) {
1394 policy->max_freq_req = NULL;
1395 dev_err(dev, "Failed to add max-freq constraint (%d)\n",
1396 ret);
1397 goto out_destroy_policy;
1398 }
Viresh Kumar988bed02015-05-08 11:53:45 +05301399 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301400
Viresh Kumar5ddc6d42019-06-20 08:35:48 +05301401 if (cpufreq_driver->get && has_target()) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301402 policy->cur = cpufreq_driver->get(policy->cpu);
1403 if (!policy->cur) {
1404 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumard417e062018-02-22 11:29:44 +05301405 goto out_destroy_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301406 }
1407 }
1408
Viresh Kumard3916692013-12-03 11:20:46 +05301409 /*
1410 * Sometimes boot loaders set CPU frequency to a value outside of
1411 * frequency table present with cpufreq core. In such cases CPU might be
1412 * unstable if it has to run on that frequency for long duration of time
1413 * and so its better to set it to a frequency which is specified in
1414 * freq-table. This also makes cpufreq stats inconsistent as
1415 * cpufreq-stats would fail to register because current frequency of CPU
1416 * isn't found in freq-table.
1417 *
1418 * Because we don't want this change to effect boot process badly, we go
1419 * for the next freq which is >= policy->cur ('cur' must be set by now,
1420 * otherwise we will end up setting freq to lowest of the table as 'cur'
1421 * is initialized to zero).
1422 *
1423 * We are passing target-freq as "policy->cur - 1" otherwise
1424 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1425 * equal to target-freq.
1426 */
1427 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1428 && has_target()) {
1429 /* Are we running at unknown frequency ? */
1430 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1431 if (ret == -EINVAL) {
1432 /* Warn user and fix it */
1433 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1434 __func__, policy->cpu, policy->cur);
1435 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1436 CPUFREQ_RELATION_L);
1437
1438 /*
1439 * Reaching here after boot in a few seconds may not
1440 * mean that system will remain stable at "unknown"
1441 * frequency for longer duration. Hence, a BUG_ON().
1442 */
1443 BUG_ON(ret);
1444 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1445 __func__, policy->cpu, policy->cur);
1446 }
1447 }
1448
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001449 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001450 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301451 if (ret)
Viresh Kumard417e062018-02-22 11:29:44 +05301452 goto out_destroy_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001453
1454 cpufreq_stats_create_table(policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001455
Viresh Kumar988bed02015-05-08 11:53:45 +05301456 write_lock_irqsave(&cpufreq_driver_lock, flags);
1457 list_add(&policy->policy_list, &cpufreq_policy_list);
1458 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1459 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301460
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301461 ret = cpufreq_init_policy(policy);
1462 if (ret) {
1463 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1464 __func__, cpu, ret);
Viresh Kumard417e062018-02-22 11:29:44 +05301465 goto out_destroy_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301466 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301467
Viresh Kumar4e97b632014-03-04 11:44:01 +08001468 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301469
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001470 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301471
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301472 /* Callback for handling stuff after policy is ready */
1473 if (cpufreq_driver->ready)
1474 cpufreq_driver->ready(policy);
1475
Daniel Lezcanobcc61562019-06-25 13:32:41 +02001476 if (cpufreq_thermal_control_enabled(cpufreq_driver))
Amit Kucheria5c238a82019-01-30 10:52:01 +05301477 policy->cdev = of_cpufreq_cooling_register(policy);
1478
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001479 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 return 0;
1482
Viresh Kumard417e062018-02-22 11:29:44 +05301483out_destroy_policy:
Viresh Kumarb24b6472018-02-22 11:29:43 +05301484 for_each_cpu(j, policy->real_cpus)
1485 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1486
Prarit Bhargava7106e022014-09-10 10:12:08 -04001487 up_write(&policy->rwsem);
1488
Viresh Kumard417e062018-02-22 11:29:44 +05301489out_exit_policy:
Viresh Kumarda60ce92013-10-03 20:28:30 +05301490 if (cpufreq_driver->exit)
1491 cpufreq_driver->exit(policy);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001492
Viresh Kumar8101f992015-07-08 15:12:15 +05301493out_free_policy:
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301494 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return ret;
1496}
1497
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001498/**
1499 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1500 * @dev: CPU device.
1501 * @sif: Subsystem interface structure pointer (not used)
1502 */
1503static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1504{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001505 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001506 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301507 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001508
1509 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1510
Viresh Kumar266198042016-09-12 12:07:05 +05301511 if (cpu_online(cpu)) {
1512 ret = cpufreq_online(cpu);
1513 if (ret)
1514 return ret;
1515 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001516
Viresh Kumar266198042016-09-12 12:07:05 +05301517 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001518 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001519 if (policy)
1520 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001522 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001525static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301527 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301528 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001530 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Viresh Kumar988bed02015-05-08 11:53:45 +05301532 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301533 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001534 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Viresh Kumar49f18562016-02-11 17:31:12 +05301538 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001539 if (has_target())
1540 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001541
Viresh Kumar9591bec2015-06-10 02:20:23 +02001542 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301543
Viresh Kumar9591bec2015-06-10 02:20:23 +02001544 if (policy_is_inactive(policy)) {
1545 if (has_target())
1546 strncpy(policy->last_governor, policy->governor->name,
1547 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001548 else
1549 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001550 } else if (cpu == policy->cpu) {
1551 /* Nominate new CPU */
1552 policy->cpu = cpumask_any(policy->cpus);
1553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Viresh Kumar9591bec2015-06-10 02:20:23 +02001555 /* Start governor again for active policy */
1556 if (!policy_is_inactive(policy)) {
1557 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001558 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001559 if (ret)
1560 pr_err("%s: Failed to start governor\n", __func__);
1561 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301562
Viresh Kumar49f18562016-02-11 17:31:12 +05301563 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301564 }
1565
Daniel Lezcanobcc61562019-06-25 13:32:41 +02001566 if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
Amit Kucheria5c238a82019-01-30 10:52:01 +05301567 cpufreq_cooling_unregister(policy->cdev);
1568 policy->cdev = NULL;
1569 }
1570
Viresh Kumar69cee712016-02-11 17:31:11 +05301571 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001572 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001574 if (has_target())
1575 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001576
Viresh Kumar87549142015-06-10 02:13:21 +02001577 /*
Viresh Kumar91a12e92019-02-12 16:36:04 +05301578 * Perform the ->offline() during light-weight tear-down, as
1579 * that allows fast recovery when the CPU comes back.
Viresh Kumar87549142015-06-10 02:13:21 +02001580 */
Viresh Kumar91a12e92019-02-12 16:36:04 +05301581 if (cpufreq_driver->offline) {
1582 cpufreq_driver->offline(policy);
1583 } else if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001584 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001585 policy->freq_table = NULL;
1586 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301587
1588unlock:
1589 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301593/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301594 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301595 *
1596 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301597 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301598static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001599{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001600 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001601 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001602
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001603 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001604 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001605
Viresh Kumar69cee712016-02-11 17:31:11 +05301606 if (cpu_online(cpu))
1607 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001608
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001609 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301610 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301611
Viresh Kumar91a12e92019-02-12 16:36:04 +05301612 if (cpumask_empty(policy->real_cpus)) {
1613 /* We did light-weight exit earlier, do full tear down now */
1614 if (cpufreq_driver->offline)
1615 cpufreq_driver->exit(policy);
1616
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301617 cpufreq_policy_free(policy);
Viresh Kumar91a12e92019-02-12 16:36:04 +05301618 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301622 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1623 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301624 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 * @new_freq: CPU frequency the CPU actually runs at
1626 *
Dave Jones29464f22009-01-18 01:37:11 -05001627 * We adjust to current frequency first, and need to clean up later.
1628 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301630static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301631 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301634
Joe Perchese837f9b2014-03-11 10:03:00 -07001635 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 +05301636 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301638 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301640
Viresh Kumar8fec0512014-03-24 13:35:45 +05301641 cpufreq_freq_transition_begin(policy, &freqs);
1642 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
Viresh Kumar59807522019-06-20 08:35:49 +05301645static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
1646{
1647 unsigned int new_freq;
1648
1649 new_freq = cpufreq_driver->get(policy->cpu);
1650 if (!new_freq)
1651 return 0;
1652
1653 /*
1654 * If fast frequency switching is used with the given policy, the check
1655 * against policy->cur is pointless, so skip it in that case.
1656 */
1657 if (policy->fast_switch_enabled || !has_target())
1658 return new_freq;
1659
1660 if (policy->cur != new_freq) {
1661 cpufreq_out_of_sync(policy, new_freq);
1662 if (update)
1663 schedule_work(&policy->update);
1664 }
1665
1666 return new_freq;
1667}
1668
Dave Jones32ee8c32006-02-28 00:43:23 -05001669/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301670 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001671 * @cpu: CPU number
1672 *
1673 * This is the last known freq, without actually getting it from the driver.
1674 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1675 */
1676unsigned int cpufreq_quick_get(unsigned int cpu)
1677{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001678 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301679 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001680 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001681
Richard Cochranc75361c2016-03-11 09:43:07 +01001682 read_lock_irqsave(&cpufreq_driver_lock, flags);
1683
1684 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1685 ret_freq = cpufreq_driver->get(cpu);
1686 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1687 return ret_freq;
1688 }
1689
1690 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001691
1692 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001693 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301694 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001695 cpufreq_cpu_put(policy);
1696 }
1697
Dave Jones4d34a672008-02-07 16:33:49 -05001698 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001699}
1700EXPORT_SYMBOL(cpufreq_quick_get);
1701
Jesse Barnes3d737102011-06-28 10:59:12 -07001702/**
1703 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1704 * @cpu: CPU number
1705 *
1706 * Just return the max possible frequency for a given CPU.
1707 */
1708unsigned int cpufreq_quick_get_max(unsigned int cpu)
1709{
1710 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1711 unsigned int ret_freq = 0;
1712
1713 if (policy) {
1714 ret_freq = policy->max;
1715 cpufreq_cpu_put(policy);
1716 }
1717
1718 return ret_freq;
1719}
1720EXPORT_SYMBOL(cpufreq_quick_get_max);
1721
Viresh Kumard92d50a2015-01-02 12:34:29 +05301722static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Yue Hu4db7c342019-04-19 14:27:58 +08001724 if (unlikely(policy_is_inactive(policy)))
Viresh Kumar59807522019-06-20 08:35:49 +05301725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Viresh Kumar59807522019-06-20 08:35:49 +05301727 return cpufreq_verify_current_freq(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001728}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001730/**
1731 * cpufreq_get - get the current CPU frequency (in kHz)
1732 * @cpu: CPU number
1733 *
1734 * Get the CPU current (static) CPU frequency
1735 */
1736unsigned int cpufreq_get(unsigned int cpu)
1737{
Aaron Plattner999976e2014-03-04 12:42:15 -08001738 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001739 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001740
Aaron Plattner999976e2014-03-04 12:42:15 -08001741 if (policy) {
1742 down_read(&policy->rwsem);
Yue Hu4db7c342019-04-19 14:27:58 +08001743 if (cpufreq_driver->get)
1744 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001745 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301746
Aaron Plattner999976e2014-03-04 12:42:15 -08001747 cpufreq_cpu_put(policy);
1748 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301749
Dave Jones4d34a672008-02-07 16:33:49 -05001750 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752EXPORT_SYMBOL(cpufreq_get);
1753
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001754static struct subsys_interface cpufreq_interface = {
1755 .name = "cpufreq",
1756 .subsys = &cpu_subsys,
1757 .add_dev = cpufreq_add_dev,
1758 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001759};
1760
Viresh Kumare28867e2014-03-04 11:00:27 +08001761/*
1762 * In case platform wants some specific frequency to be configured
1763 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001764 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001765int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001766{
Viresh Kumare28867e2014-03-04 11:00:27 +08001767 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001768
Viresh Kumare28867e2014-03-04 11:00:27 +08001769 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001770 pr_debug("%s: suspend_freq not defined\n", __func__);
1771 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001772 }
1773
Viresh Kumare28867e2014-03-04 11:00:27 +08001774 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1775 policy->suspend_freq);
1776
1777 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1778 CPUFREQ_RELATION_H);
1779 if (ret)
1780 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1781 __func__, policy->suspend_freq, ret);
1782
Dave Jonesc9060492008-02-07 16:32:18 -05001783 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001784}
Viresh Kumare28867e2014-03-04 11:00:27 +08001785EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001786
1787/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001788 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001790 * Called during system wide Suspend/Hibernate cycles for suspending governors
1791 * as some platforms can't change frequency after this point in suspend cycle.
1792 * Because some of the devices (like: i2c, regulators, etc) they use for
1793 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001795void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301797 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001799 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001800 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001802 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301803 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001805 pr_debug("%s: Suspending Governors\n", __func__);
1806
Viresh Kumarf9637352015-05-12 12:20:11 +05301807 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001808 if (has_target()) {
1809 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001810 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001811 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001812 }
1813
1814 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001815 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1816 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 }
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301818
1819suspend:
1820 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001824 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001826 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1827 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001829void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301832 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001834 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 return;
1836
Bo Yan703cbaa2018-01-23 13:57:55 -08001837 if (unlikely(!cpufreq_suspended))
1838 return;
1839
Lan Tianyu8e304442014-09-18 15:03:07 +08001840 cpufreq_suspended = false;
1841
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001842 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001843 return;
1844
1845 pr_debug("%s: Resuming Governors\n", __func__);
1846
Viresh Kumarf9637352015-05-12 12:20:11 +05301847 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301848 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301849 pr_err("%s: Failed to resume driver: %p\n", __func__,
1850 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001851 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301852 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001853 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301854 up_write(&policy->rwsem);
1855
1856 if (ret)
1857 pr_err("%s: Failed to start governor for policy: %p\n",
1858 __func__, policy);
1859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Borislav Petkov9d950462013-01-20 10:24:28 +00001863/**
1864 * cpufreq_get_current_driver - return current driver's name
1865 *
1866 * Return the name string of the currently loaded cpufreq driver
1867 * or NULL, if none.
1868 */
1869const char *cpufreq_get_current_driver(void)
1870{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001871 if (cpufreq_driver)
1872 return cpufreq_driver->name;
1873
1874 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001875}
1876EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001878/**
1879 * cpufreq_get_driver_data - return current driver data
1880 *
1881 * Return the private data of the currently loaded cpufreq
1882 * driver, or NULL if no cpufreq driver is loaded.
1883 */
1884void *cpufreq_get_driver_data(void)
1885{
1886 if (cpufreq_driver)
1887 return cpufreq_driver->driver_data;
1888
1889 return NULL;
1890}
1891EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1892
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893/*********************************************************************
1894 * NOTIFIER LISTS INTERFACE *
1895 *********************************************************************/
1896
1897/**
1898 * cpufreq_register_notifier - register a driver with cpufreq
1899 * @nb: notifier function to register
1900 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1901 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001902 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 * are notified about clock rate changes (once before and once after
1904 * the transition), or a list of drivers that are notified about
1905 * changes in cpufreq policy.
1906 *
1907 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001908 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
1910int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1911{
1912 int ret;
1913
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001914 if (cpufreq_disabled())
1915 return -EINVAL;
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 switch (list) {
1918 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001919 mutex_lock(&cpufreq_fast_switch_lock);
1920
1921 if (cpufreq_fast_switch_count > 0) {
1922 mutex_unlock(&cpufreq_fast_switch_lock);
1923 return -EBUSY;
1924 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001925 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001926 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001927 if (!ret)
1928 cpufreq_fast_switch_count--;
1929
1930 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
1932 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001933 ret = blocking_notifier_chain_register(
1934 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
1936 default:
1937 ret = -EINVAL;
1938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 return ret;
1941}
1942EXPORT_SYMBOL(cpufreq_register_notifier);
1943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944/**
1945 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1946 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301947 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 *
1949 * Remove a driver from the CPU frequency notifier list.
1950 *
1951 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001952 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 */
1954int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1955{
1956 int ret;
1957
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001958 if (cpufreq_disabled())
1959 return -EINVAL;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 switch (list) {
1962 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001963 mutex_lock(&cpufreq_fast_switch_lock);
1964
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001965 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001966 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001967 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1968 cpufreq_fast_switch_count++;
1969
1970 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
1972 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001973 ret = blocking_notifier_chain_unregister(
1974 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 break;
1976 default:
1977 ret = -EINVAL;
1978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 return ret;
1981}
1982EXPORT_SYMBOL(cpufreq_unregister_notifier);
1983
1984
1985/*********************************************************************
1986 * GOVERNORS *
1987 *********************************************************************/
1988
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001989/**
1990 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1991 * @policy: cpufreq policy to switch the frequency for.
1992 * @target_freq: New frequency to set (may be approximate).
1993 *
1994 * Carry out a fast frequency switch without sleeping.
1995 *
1996 * The driver's ->fast_switch() callback invoked by this function must be
1997 * suitable for being called from within RCU-sched read-side critical sections
1998 * and it is expected to select the minimum available frequency greater than or
1999 * equal to @target_freq (CPUFREQ_RELATION_L).
2000 *
2001 * This function must not be called if policy->fast_switch_enabled is unset.
2002 *
2003 * Governors calling this function must guarantee that it will never be invoked
2004 * twice in parallel for the same policy and that it will never be called in
2005 * parallel with either ->target() or ->target_index() for the same policy.
2006 *
Viresh Kumar209887e2017-08-09 10:21:46 +05302007 * Returns the actual frequency set for the CPU.
2008 *
2009 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2010 * error condition, the hardware configuration must be preserved.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002011 */
2012unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
2013 unsigned int target_freq)
2014{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02002015 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002016
2017 return cpufreq_driver->fast_switch(policy, target_freq);
2018}
2019EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
2020
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302021/* Must set freqs->new to intermediate frequency */
2022static int __target_intermediate(struct cpufreq_policy *policy,
2023 struct cpufreq_freqs *freqs, int index)
2024{
2025 int ret;
2026
2027 freqs->new = cpufreq_driver->get_intermediate(policy, index);
2028
2029 /* We don't need to switch to intermediate freq */
2030 if (!freqs->new)
2031 return 0;
2032
2033 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
2034 __func__, policy->cpu, freqs->old, freqs->new);
2035
2036 cpufreq_freq_transition_begin(policy, freqs);
2037 ret = cpufreq_driver->target_intermediate(policy, index);
2038 cpufreq_freq_transition_end(policy, freqs, ret);
2039
2040 if (ret)
2041 pr_err("%s: Failed to change to intermediate frequency: %d\n",
2042 __func__, ret);
2043
2044 return ret;
2045}
2046
Viresh Kumar23727842016-06-03 10:58:50 +05302047static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05302048{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302049 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
2050 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05302051 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05302052 int retval = -EINVAL;
2053 bool notify;
2054
Viresh Kumar23727842016-06-03 10:58:50 +05302055 if (newfreq == policy->cur)
2056 return 0;
2057
Viresh Kumar8d657752014-05-21 14:29:29 +05302058 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05302059 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302060 /* Handle switching to intermediate frequency */
2061 if (cpufreq_driver->get_intermediate) {
2062 retval = __target_intermediate(policy, &freqs, index);
2063 if (retval)
2064 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05302065
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302066 intermediate_freq = freqs.new;
2067 /* Set old freq to intermediate */
2068 if (intermediate_freq)
2069 freqs.old = freqs.new;
2070 }
2071
Viresh Kumar23727842016-06-03 10:58:50 +05302072 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05302073 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2074 __func__, policy->cpu, freqs.old, freqs.new);
2075
2076 cpufreq_freq_transition_begin(policy, &freqs);
2077 }
2078
2079 retval = cpufreq_driver->target_index(policy, index);
2080 if (retval)
2081 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2082 retval);
2083
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302084 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05302085 cpufreq_freq_transition_end(policy, &freqs, retval);
2086
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302087 /*
2088 * Failed after setting to intermediate freq? Driver should have
2089 * reverted back to initial frequency and so should we. Check
2090 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302091 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302092 */
2093 if (unlikely(retval && intermediate_freq)) {
2094 freqs.old = intermediate_freq;
2095 freqs.new = policy->restore_freq;
2096 cpufreq_freq_transition_begin(policy, &freqs);
2097 cpufreq_freq_transition_end(policy, &freqs, 0);
2098 }
2099 }
2100
Viresh Kumar8d657752014-05-21 14:29:29 +05302101 return retval;
2102}
2103
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104int __cpufreq_driver_target(struct cpufreq_policy *policy,
2105 unsigned int target_freq,
2106 unsigned int relation)
2107{
Viresh Kumar72499242012-10-31 01:28:21 +01002108 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302109 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002110
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002111 if (cpufreq_disabled())
2112 return -ENODEV;
2113
Viresh Kumar72499242012-10-31 01:28:21 +01002114 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302115 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002116
2117 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002118 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002119
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302120 /*
2121 * This might look like a redundant call as we are checking it again
2122 * after finding index. But it is left intentionally for cases where
2123 * exactly same freq is called again and so we can save on few function
2124 * calls.
2125 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002126 if (target_freq == policy->cur)
2127 return 0;
2128
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302129 /* Save last value to restore later on errors */
2130 policy->restore_freq = policy->cur;
2131
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002132 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002133 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002134
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002135 if (!cpufreq_driver->target_index)
2136 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302137
Viresh Kumard218ed72016-06-03 10:58:51 +05302138 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302139
Viresh Kumar23727842016-06-03 10:58:50 +05302140 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144int cpufreq_driver_target(struct cpufreq_policy *policy,
2145 unsigned int target_freq,
2146 unsigned int relation)
2147{
Julia Lawallf1829e42008-07-25 22:44:53 +02002148 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
viresh kumarad7722d2013-10-18 19:10:15 +05302150 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 ret = __cpufreq_driver_target(policy, target_freq, relation);
2153
viresh kumarad7722d2013-10-18 19:10:15 +05302154 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return ret;
2157}
2158EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2159
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002160__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2161{
2162 return NULL;
2163}
2164
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002165static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Dave Jonescc993ca2005-07-28 09:43:56 -07002167 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002168
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002169 /* Don't start any governor operations if we are entering suspend */
2170 if (cpufreq_suspended)
2171 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002172 /*
2173 * Governor might not be initiated here if ACPI _PPC changed
2174 * notification happened, so check it.
2175 */
2176 if (!policy->governor)
2177 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002178
Viresh Kumared4676e2017-07-19 15:42:46 +05302179 /* Platform doesn't want dynamic frequency switching ? */
2180 if (policy->governor->dynamic_switching &&
Viresh Kumarfc4c7092017-07-19 15:42:49 +05302181 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002182 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2183
2184 if (gov) {
Viresh Kumarfe829ed2017-07-19 15:42:48 +05302185 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002186 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002187 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002188 } else {
2189 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002190 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002193 if (!try_module_get(policy->governor->owner))
2194 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002196 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002197
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002198 if (policy->governor->init) {
2199 ret = policy->governor->init(policy);
2200 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002201 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002202 return ret;
2203 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002206 return 0;
2207}
2208
2209static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2210{
2211 if (cpufreq_suspended || !policy->governor)
2212 return;
2213
2214 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2215
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002216 if (policy->governor->exit)
2217 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002218
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002219 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002222static int cpufreq_start_governor(struct cpufreq_policy *policy)
2223{
2224 int ret;
2225
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002226 if (cpufreq_suspended)
2227 return 0;
2228
2229 if (!policy->governor)
2230 return -EINVAL;
2231
2232 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2233
Viresh Kumar407d0ff2019-06-20 08:35:46 +05302234 if (cpufreq_driver->get)
Viresh Kumar59807522019-06-20 08:35:49 +05302235 cpufreq_verify_current_freq(policy, false);
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002236
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002237 if (policy->governor->start) {
2238 ret = policy->governor->start(policy);
2239 if (ret)
2240 return ret;
2241 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002242
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002243 if (policy->governor->limits)
2244 policy->governor->limits(policy);
2245
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002246 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002247}
2248
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002249static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2250{
2251 if (cpufreq_suspended || !policy->governor)
2252 return;
2253
2254 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2255
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002256 if (policy->governor->stop)
2257 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002258}
2259
2260static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2261{
2262 if (cpufreq_suspended || !policy->governor)
2263 return;
2264
2265 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2266
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002267 if (policy->governor->limits)
2268 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269}
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271int cpufreq_register_governor(struct cpufreq_governor *governor)
2272{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002273 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if (!governor)
2276 return -EINVAL;
2277
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002278 if (cpufreq_disabled())
2279 return -ENODEV;
2280
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002281 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002282
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002283 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302284 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002285 err = 0;
2286 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Dave Jones32ee8c32006-02-28 00:43:23 -05002289 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002290 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2295{
Viresh Kumar45732372015-05-12 12:22:34 +05302296 struct cpufreq_policy *policy;
2297 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 if (!governor)
2300 return;
2301
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002302 if (cpufreq_disabled())
2303 return;
2304
Viresh Kumar45732372015-05-12 12:22:34 +05302305 /* clear last_governor for all inactive policies */
2306 read_lock_irqsave(&cpufreq_driver_lock, flags);
2307 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302308 if (!strcmp(policy->last_governor, governor->name)) {
2309 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302310 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302311 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002312 }
Viresh Kumar45732372015-05-12 12:22:34 +05302313 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002314
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002315 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002317 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318}
2319EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2320
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/*********************************************************************
2323 * POLICY INTERFACE *
2324 *********************************************************************/
2325
2326/**
2327 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002328 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2329 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 *
2331 * Reads the current cpufreq policy.
2332 */
2333int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2334{
2335 struct cpufreq_policy *cpu_policy;
2336 if (!policy)
2337 return -EINVAL;
2338
2339 cpu_policy = cpufreq_cpu_get(cpu);
2340 if (!cpu_policy)
2341 return -EINVAL;
2342
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302343 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
2345 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return 0;
2347}
2348EXPORT_SYMBOL(cpufreq_get_policy);
2349
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002350/**
2351 * cpufreq_set_policy - Modify cpufreq policy parameters.
2352 * @policy: Policy object to modify.
2353 * @new_policy: New policy data.
2354 *
2355 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2356 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2357 * the driver's ->verify() callback again and run the notifiers for it again
2358 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2359 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2360 * callback (if present) or carry out a governor update for @policy. That is,
2361 * run the current governor's ->limits() callback (if the governor field in
2362 * @new_policy points to the same object as the one in @policy) or replace the
2363 * governor for @policy with the new one stored in @new_policy.
2364 *
2365 * The cpuinfo part of @policy is not updated by this function.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002366 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +01002367int cpufreq_set_policy(struct cpufreq_policy *policy,
2368 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002370 struct cpufreq_governor *old_gov;
Viresh Kumar67d874c2019-07-08 16:27:52 +05302371 struct device *cpu_dev = get_cpu_device(policy->cpu);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002372 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Joe Perchese837f9b2014-03-11 10:03:00 -07002374 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2375 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302377 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Pan Xinhuifba95732015-07-30 18:10:40 +08002379 /*
Viresh Kumar67d874c2019-07-08 16:27:52 +05302380 * PM QoS framework collects all the requests from users and provide us
2381 * the final aggregated value here.
2382 */
Viresh Kumar18c49922019-07-05 16:21:24 +05302383 new_policy->min = dev_pm_qos_read_value(cpu_dev, DEV_PM_QOS_MIN_FREQUENCY);
2384 new_policy->max = dev_pm_qos_read_value(cpu_dev, DEV_PM_QOS_MAX_FREQUENCY);
Viresh Kumar67d874c2019-07-08 16:27:52 +05302385
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302387 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Viresh Kumar67d874c2019-07-08 16:27:52 +05302391 /*
2392 * The notifier-chain shall be removed once all the users of
2393 * CPUFREQ_ADJUST are moved to use the QoS framework.
2394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002396 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302397 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Viresh Kumarbb176f72013-06-19 14:19:33 +05302399 /*
2400 * verify the cpu speed can be set within this limit, which might be
2401 * different to the first one
2402 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302403 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002404 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002408 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302409 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302411 policy->min = new_policy->min;
2412 policy->max = new_policy->max;
Ruchi Kandoi601b2182018-07-24 10:35:44 -07002413 trace_cpu_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Steve Mucklee3c06232016-07-13 13:25:25 -07002415 policy->cached_target_freq = UINT_MAX;
2416
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002417 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002418 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002420 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302421 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002422 pr_debug("setting range\n");
Rafael J. Wysocki167a38d2019-02-20 00:26:30 +01002423 return cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 }
2425
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002426 if (new_policy->governor == policy->governor) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002427 pr_debug("governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002428 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002429 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002430 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002431
2432 pr_debug("governor switch\n");
2433
2434 /* save old, working values */
2435 old_gov = policy->governor;
2436 /* end old governor */
2437 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002438 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002439 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002440 }
2441
2442 /* start new governor */
2443 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002444 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302445 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002446 ret = cpufreq_start_governor(policy);
2447 if (!ret) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002448 pr_debug("governor change\n");
Quentin Perret531b5c92018-12-03 09:56:21 +00002449 sched_cpufreq_governor_change(policy, old_gov);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002450 return 0;
2451 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002452 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002453 }
2454
2455 /* new governor failed, so re-start old one */
2456 pr_debug("starting governor %s failed\n", policy->governor->name);
2457 if (old_gov) {
2458 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002459 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302460 policy->governor = NULL;
2461 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002462 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002463 }
2464
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302465 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
2468/**
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002469 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2470 * @cpu: CPU to re-evaluate the policy for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 *
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002472 * Update the current frequency for the cpufreq policy of @cpu and use
Viresh Kumar18c49922019-07-05 16:21:24 +05302473 * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
2474 * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
2475 * for the policy in question, among other things.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 */
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002477void cpufreq_update_policy(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002479 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002481 if (!policy)
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002482 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Viresh Kumarbb176f72013-06-19 14:19:33 +05302484 /*
2485 * BIOS might change freq behind our back
2486 * -> ask driver for current freq and notify governors about a change
2487 */
Viresh Kumar5ddc6d42019-06-20 08:35:48 +05302488 if (cpufreq_driver->get && has_target() &&
Viresh Kumar59807522019-06-20 08:35:49 +05302489 (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002490 goto unlock;
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002491
Viresh Kumar70a59fd2019-06-20 08:35:50 +05302492 refresh_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002494unlock:
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002495 cpufreq_cpu_release(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497EXPORT_SYMBOL(cpufreq_update_policy);
2498
Rafael J. Wysocki5a25e3f2019-03-26 12:15:13 +01002499/**
2500 * cpufreq_update_limits - Update policy limits for a given CPU.
2501 * @cpu: CPU to update the policy limits for.
2502 *
2503 * Invoke the driver's ->update_limits callback if present or call
2504 * cpufreq_update_policy() for @cpu.
2505 */
2506void cpufreq_update_limits(unsigned int cpu)
2507{
2508 if (cpufreq_driver->update_limits)
2509 cpufreq_driver->update_limits(cpu);
2510 else
2511 cpufreq_update_policy(cpu);
2512}
2513EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002516 * BOOST *
2517 *********************************************************************/
2518static int cpufreq_boost_set_sw(int state)
2519{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002520 struct cpufreq_policy *policy;
2521 int ret = -EINVAL;
2522
Viresh Kumarf9637352015-05-12 12:20:11 +05302523 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302524 if (!policy->freq_table)
2525 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302526
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302527 ret = cpufreq_frequency_table_cpuinfo(policy,
2528 policy->freq_table);
2529 if (ret) {
2530 pr_err("%s: Policy frequency update failed\n",
2531 __func__);
2532 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002533 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302534
Viresh Kumar18c49922019-07-05 16:21:24 +05302535 ret = dev_pm_qos_update_request(policy->max_freq_req, policy->max);
2536 if (ret)
2537 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002538 }
2539
2540 return ret;
2541}
2542
2543int cpufreq_boost_trigger_state(int state)
2544{
2545 unsigned long flags;
2546 int ret = 0;
2547
2548 if (cpufreq_driver->boost_enabled == state)
2549 return 0;
2550
2551 write_lock_irqsave(&cpufreq_driver_lock, flags);
2552 cpufreq_driver->boost_enabled = state;
2553 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2554
2555 ret = cpufreq_driver->set_boost(state);
2556 if (ret) {
2557 write_lock_irqsave(&cpufreq_driver_lock, flags);
2558 cpufreq_driver->boost_enabled = !state;
2559 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2560
Joe Perchese837f9b2014-03-11 10:03:00 -07002561 pr_err("%s: Cannot %s BOOST\n",
2562 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002563 }
2564
2565 return ret;
2566}
2567
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002568static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002569{
Yue Hu89f98d72019-04-09 10:25:36 +08002570 return cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002571}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002572
Viresh Kumar44139ed2015-07-29 16:23:09 +05302573static int create_boost_sysfs_file(void)
2574{
2575 int ret;
2576
Viresh Kumarc82bd442015-10-15 21:35:23 +05302577 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302578 if (ret)
2579 pr_err("%s: cannot register global BOOST sysfs file\n",
2580 __func__);
2581
2582 return ret;
2583}
2584
2585static void remove_boost_sysfs_file(void)
2586{
2587 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302588 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302589}
2590
2591int cpufreq_enable_boost_support(void)
2592{
2593 if (!cpufreq_driver)
2594 return -EINVAL;
2595
2596 if (cpufreq_boost_supported())
2597 return 0;
2598
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002599 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302600
2601 /* This will get removed on driver unregister */
2602 return create_boost_sysfs_file();
2603}
2604EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2605
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002606int cpufreq_boost_enabled(void)
2607{
2608 return cpufreq_driver->boost_enabled;
2609}
2610EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2611
2612/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2614 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002615static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Chen Yuc4a3fa22017-04-09 13:45:16 +08002617static int cpuhp_cpufreq_online(unsigned int cpu)
2618{
2619 cpufreq_online(cpu);
2620
2621 return 0;
2622}
2623
2624static int cpuhp_cpufreq_offline(unsigned int cpu)
2625{
2626 cpufreq_offline(cpu);
2627
2628 return 0;
2629}
2630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631/**
2632 * cpufreq_register_driver - register a CPU Frequency driver
2633 * @driver_data: A struct cpufreq_driver containing the values#
2634 * submitted by the CPU Frequency driver.
2635 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302636 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002637 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002638 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 *
2640 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002641int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 unsigned long flags;
2644 int ret;
2645
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002646 if (cpufreq_disabled())
2647 return -ENODEV;
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302650 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002651 driver_data->target) ||
2652 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302653 driver_data->target)) ||
Viresh Kumara9a22b52019-02-14 16:16:21 +05302654 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
Viresh Kumar91a12e92019-02-12 16:36:04 +05302655 (!driver_data->online != !driver_data->offline))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return -EINVAL;
2657
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002658 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002660 /* Protect against concurrent CPU online/offline. */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002661 cpus_read_lock();
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002662
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002663 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002664 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002665 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002666 ret = -EEXIST;
2667 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002669 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002670 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302672 if (driver_data->setpolicy)
2673 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2674
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002675 if (cpufreq_boost_supported()) {
2676 ret = create_boost_sysfs_file();
2677 if (ret)
2678 goto err_null_driver;
2679 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002680
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002681 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002682 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002683 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302685 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2686 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 /* if all ->init() calls failed, unregister */
David Arcari6c770032017-05-26 11:37:31 -04002688 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302689 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2690 driver_data->name);
2691 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
2693
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002694 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2695 "cpufreq:online",
2696 cpuhp_cpufreq_online,
2697 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002698 if (ret < 0)
2699 goto err_if_unreg;
2700 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002701 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002702
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002703 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002704 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002705
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002706err_if_unreg:
2707 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002708err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302709 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002710err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002711 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002712 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002713 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002714out:
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002715 cpus_read_unlock();
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002716 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720/**
2721 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2722 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302723 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 * the right to do so, i.e. if you have succeeded in initialising before!
2725 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2726 * currently not initialised.
2727 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002728int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
2730 unsigned long flags;
2731
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002732 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002735 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002737 /* Protect against concurrent cpu hotplug */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002738 cpus_read_lock();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002739 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302740 remove_boost_sysfs_file();
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002741 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002743 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302744
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002745 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302746
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002747 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002748 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 return 0;
2751}
2752EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002753
Doug Anderson90de2a42014-12-23 22:09:48 -08002754/*
2755 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2756 * or mutexes when secondary CPUs are halted.
2757 */
2758static struct syscore_ops cpufreq_syscore_ops = {
2759 .shutdown = cpufreq_suspend,
2760};
2761
Viresh Kumarc82bd442015-10-15 21:35:23 +05302762struct kobject *cpufreq_global_kobject;
2763EXPORT_SYMBOL(cpufreq_global_kobject);
2764
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002765static int __init cpufreq_core_init(void)
2766{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002767 if (cpufreq_disabled())
2768 return -ENODEV;
2769
Viresh Kumar8eec1022015-10-15 21:35:22 +05302770 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002771 BUG_ON(!cpufreq_global_kobject);
2772
Doug Anderson90de2a42014-12-23 22:09:48 -08002773 register_syscore_ops(&cpufreq_syscore_ops);
2774
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002775 return 0;
2776}
Len Brownd82f2692017-02-28 16:44:16 -05002777module_param(off, int, 0444);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002778core_initcall(cpufreq_core_init);