blob: e84bf0eb7239af787df0a225b05d80af8a5119bc [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/drivers/cpufreq/cpufreq.c
4 *
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05307 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08009 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -050010 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050011 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
12 * Fix handling for CPU hotplug -- affected CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Viresh Kumardb701152012-10-23 01:29:03 +020015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Viresh Kumar5ff0a262013-08-06 22:53:03 +053017#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/cpufreq.h>
Amit Kucheria5c238a82019-01-30 10:52:01 +053019#include <linux/cpu_cooling.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053022#include <linux/init.h>
23#include <linux/kernel_stat.h>
24#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080025#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053026#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080027#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080028#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053029#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020030#include <trace/events/power.h>
31
Viresh Kumarb4f06762015-01-27 14:06:08 +053032static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053033
Viresh Kumarf9637352015-05-12 12:20:11 +053034/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060035#define for_each_suitable_policy(__policy, __active) \
36 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
37 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053038
39#define for_each_active_policy(__policy) \
40 for_each_suitable_policy(__policy, true)
41#define for_each_inactive_policy(__policy) \
42 for_each_suitable_policy(__policy, false)
43
44#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053045 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
46
Viresh Kumarf7b27062015-01-27 14:06:09 +053047/* Iterate over governors */
48static LIST_HEAD(cpufreq_governor_list);
49#define for_each_governor(__governor) \
50 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/**
Dave Jonescd878472006-08-11 17:59:28 -040053 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * level driver of CPUFreq support, and its spinlock. This lock
55 * also protects the cpufreq_cpu_data array.
56 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020057static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070058static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053059static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053060
Viresh Kumar2f0aea92014-03-04 11:00:26 +080061/* Flag to suspend/resume CPUFreq governors */
62static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053064static inline bool has_target(void)
65{
66 return cpufreq_driver->target_index || cpufreq_driver->target;
67}
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053070static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020071static int cpufreq_init_governor(struct cpufreq_policy *policy);
72static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010073static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020074static void cpufreq_stop_governor(struct cpufreq_policy *policy);
75static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/**
Dave Jones32ee8c32006-02-28 00:43:23 -050078 * Two notifier lists: the "policy" list is involved in the
79 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * "transition" list for kernel code that needs to handle
81 * changes to devices when the CPU clock speed changes.
82 * The mutex locks both lists.
83 */
Alan Sterne041c682006-03-27 01:16:30 -080084static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Sebastian Andrzej Siewiorcc85de32018-05-25 12:19:58 +020085SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040087static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020088static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040089{
90 return off;
91}
92void disable_cpufreq(void)
93{
94 off = 1;
95}
Dave Jones29464f22009-01-18 01:37:11 -050096static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000098bool have_governor_per_policy(void)
99{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000101}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000102EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000103
Viresh Kumar944e9a02013-05-16 05:09:57 +0000104struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
105{
106 if (have_governor_per_policy())
107 return &policy->kobj;
108 else
109 return cpufreq_global_kobject;
110}
111EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
112
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000113static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
114{
115 u64 idle_time;
116 u64 cur_wall_time;
117 u64 busy_time;
118
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100119 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000120
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
127
128 idle_time = cur_wall_time - busy_time;
129 if (wall)
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100130 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000131
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100132 return div_u64(idle_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000133}
134
135u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
136{
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
138
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
141 else if (!io_busy)
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
143
144 return idle_time;
145}
146EXPORT_SYMBOL_GPL(get_cpu_idle_time);
147
Dietmar Eggemanne7d54592017-09-26 17:41:07 +0100148__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
149 unsigned long max_freq)
150{
151}
152EXPORT_SYMBOL_GPL(arch_set_freq_scale);
153
Viresh Kumar70e9e772013-10-03 20:29:07 +0530154/*
155 * This is a generic cpufreq init() routine which can be used by cpufreq
156 * drivers of SMP systems. It will do following:
157 * - validate & show freq table passed
158 * - set policies transition latency
159 * - policy->cpus with all possible CPUs
160 */
161int cpufreq_generic_init(struct cpufreq_policy *policy,
162 struct cpufreq_frequency_table *table,
163 unsigned int transition_latency)
164{
Viresh Kumar92c99d12018-02-26 10:38:45 +0530165 policy->freq_table = table;
Viresh Kumar70e9e772013-10-03 20:29:07 +0530166 policy->cpuinfo.transition_latency = transition_latency;
167
168 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530169 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530170 * share the clock and voltage and clock.
171 */
172 cpumask_setall(policy->cpus);
173
174 return 0;
175}
176EXPORT_SYMBOL_GPL(cpufreq_generic_init);
177
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200178struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530179{
180 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
181
Viresh Kumar988bed02015-05-08 11:53:45 +0530182 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
183}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200184EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530185
186unsigned int cpufreq_generic_get(unsigned int cpu)
187{
188 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
189
Viresh Kumar652ed952014-01-09 20:38:43 +0530190 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700191 pr_err("%s: No %s associated to cpu: %d\n",
192 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530193 return 0;
194 }
195
196 return clk_get_rate(policy->clk) / 1000;
197}
198EXPORT_SYMBOL_GPL(cpufreq_generic_get);
199
Viresh Kumar50e9c852015-02-19 17:02:03 +0530200/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100201 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
202 * @cpu: CPU to find the policy for.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530203 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100204 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
205 * the kobject reference counter of that policy. Return a valid policy on
206 * success or NULL on failure.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530207 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100208 * The policy returned by this function has to be released with the help of
209 * cpufreq_cpu_put() to balance its kobject reference counter properly.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530210 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530211struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530213 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 unsigned long flags;
215
Viresh Kumar1b947c902015-02-19 17:02:05 +0530216 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530217 return NULL;
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000220 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Viresh Kumar6eed9402013-08-06 22:53:11 +0530222 if (cpufreq_driver) {
223 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530224 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530225 if (policy)
226 kobject_get(&policy->kobj);
227 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200228
Viresh Kumar6eed9402013-08-06 22:53:11 +0530229 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530231 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
234
Viresh Kumar50e9c852015-02-19 17:02:03 +0530235/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100236 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
237 * @policy: cpufreq policy returned by cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530238 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530239void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530241 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
244
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100245/**
246 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
247 * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
248 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +0100249void cpufreq_cpu_release(struct cpufreq_policy *policy)
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100250{
251 if (WARN_ON(!policy))
252 return;
253
254 lockdep_assert_held(&policy->rwsem);
255
256 up_write(&policy->rwsem);
257
258 cpufreq_cpu_put(policy);
259}
260
261/**
262 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
263 * @cpu: CPU to find the policy for.
264 *
265 * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
266 * if the policy returned by it is not NULL, acquire its rwsem for writing.
267 * Return the policy if it is active or release it and return NULL otherwise.
268 *
269 * The policy returned by this function has to be released with the help of
270 * cpufreq_cpu_release() in order to release its rwsem and balance its usage
271 * counter properly.
272 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +0100273struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100274{
275 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
276
277 if (!policy)
278 return NULL;
279
280 down_write(&policy->rwsem);
281
282 if (policy_is_inactive(policy)) {
283 cpufreq_cpu_release(policy);
284 return NULL;
285 }
286
287 return policy;
288}
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
292 *********************************************************************/
293
294/**
295 * adjust_jiffies - adjust the system "loops_per_jiffy"
296 *
297 * This function alters the system "loops_per_jiffy" for the clock
298 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500299 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 * per-CPU loops_per_jiffy value wherever possible.
301 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800302static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530304#ifndef CONFIG_SMP
305 static unsigned long l_p_j_ref;
306 static unsigned int l_p_j_ref_freq;
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (ci->flags & CPUFREQ_CONST_LOOPS)
309 return;
310
311 if (!l_p_j_ref_freq) {
312 l_p_j_ref = loops_per_jiffy;
313 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700314 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
315 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530317 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530318 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
319 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700320 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
321 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Viresh Kumar20b53242018-05-10 15:00:29 +0530326/**
327 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
328 * @policy: cpufreq policy to enable fast frequency switching for.
329 * @freqs: contain details of the frequency update.
330 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
331 *
332 * This function calls the transition notifiers and the "adjust_jiffies"
333 * function. It is called twice on all CPU frequency changes that have
334 * external effects.
335 */
336static void cpufreq_notify_transition(struct cpufreq_policy *policy,
337 struct cpufreq_freqs *freqs,
338 unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Viresh Kumardf240142019-04-29 15:03:58 +0530340 int cpu;
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 BUG_ON(irqs_disabled());
343
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000344 if (cpufreq_disabled())
345 return;
346
Viresh Kumardf240142019-04-29 15:03:58 +0530347 freqs->policy = policy;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200348 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200349 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700350 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 switch (state) {
353 case CPUFREQ_PRECHANGE:
Viresh Kumar20b53242018-05-10 15:00:29 +0530354 /*
355 * Detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800356 * which is not equal to what the cpufreq core thinks is
357 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200359 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Viresh Kumar20b53242018-05-10 15:00:29 +0530360 if (policy->cur && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700361 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
362 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800363 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365 }
Viresh Kumar20b53242018-05-10 15:00:29 +0530366
Viresh Kumardf240142019-04-29 15:03:58 +0530367 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
368 CPUFREQ_PRECHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
371 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 case CPUFREQ_POSTCHANGE:
374 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530375 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
376 cpumask_pr_args(policy->cpus));
Viresh Kumarbb176f72013-06-19 14:19:33 +0530377
Viresh Kumardf240142019-04-29 15:03:58 +0530378 for_each_cpu(cpu, policy->cpus)
379 trace_cpu_frequency(freqs->new, cpu);
380
381 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
382 CPUFREQ_POSTCHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530383
384 cpufreq_stats_record_transition(policy, freqs->new);
385 policy->cur = freqs->new;
386 }
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530389/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530390static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530391 struct cpufreq_freqs *freqs, int transition_failed)
392{
393 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
394 if (!transition_failed)
395 return;
396
397 swap(freqs->old, freqs->new);
398 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
399 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
400}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530401
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530402void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
403 struct cpufreq_freqs *freqs)
404{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530405
406 /*
407 * Catch double invocations of _begin() which lead to self-deadlock.
408 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
409 * doesn't invoke _begin() on their behalf, and hence the chances of
410 * double invocations are very low. Moreover, there are scenarios
411 * where these checks can emit false-positive warnings in these
412 * drivers; so we avoid that by skipping them altogether.
413 */
414 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
415 && current == policy->transition_task);
416
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530417wait:
418 wait_event(policy->transition_wait, !policy->transition_ongoing);
419
420 spin_lock(&policy->transition_lock);
421
422 if (unlikely(policy->transition_ongoing)) {
423 spin_unlock(&policy->transition_lock);
424 goto wait;
425 }
426
427 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530428 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530429
430 spin_unlock(&policy->transition_lock);
431
432 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
433}
434EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
435
436void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
437 struct cpufreq_freqs *freqs, int transition_failed)
438{
Igor Stoppa0e7ea2f2018-09-07 19:09:55 +0300439 if (WARN_ON(!policy->transition_ongoing))
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530440 return;
441
442 cpufreq_notify_post_transition(policy, freqs, transition_failed);
443
444 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530445 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530446
447 wake_up(&policy->transition_wait);
448}
449EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
450
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200451/*
452 * Fast frequency switching status count. Positive means "enabled", negative
453 * means "disabled" and 0 means "not decided yet".
454 */
455static int cpufreq_fast_switch_count;
456static DEFINE_MUTEX(cpufreq_fast_switch_lock);
457
458static void cpufreq_list_transition_notifiers(void)
459{
460 struct notifier_block *nb;
461
462 pr_info("Registered transition notifiers:\n");
463
464 mutex_lock(&cpufreq_transition_notifier_list.mutex);
465
466 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
Sakari Ailusd75f7732019-03-25 21:32:28 +0200467 pr_info("%pS\n", nb->notifier_call);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200468
469 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
470}
471
472/**
473 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
474 * @policy: cpufreq policy to enable fast frequency switching for.
475 *
476 * Try to enable fast frequency switching for @policy.
477 *
478 * The attempt will fail if there is at least one transition notifier registered
479 * at this point, as fast frequency switching is quite fundamentally at odds
480 * with transition notifiers. Thus if successful, it will make registration of
481 * transition notifiers fail going forward.
482 */
483void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
484{
485 lockdep_assert_held(&policy->rwsem);
486
487 if (!policy->fast_switch_possible)
488 return;
489
490 mutex_lock(&cpufreq_fast_switch_lock);
491 if (cpufreq_fast_switch_count >= 0) {
492 cpufreq_fast_switch_count++;
493 policy->fast_switch_enabled = true;
494 } else {
495 pr_warn("CPU%u: Fast frequency switching not enabled\n",
496 policy->cpu);
497 cpufreq_list_transition_notifiers();
498 }
499 mutex_unlock(&cpufreq_fast_switch_lock);
500}
501EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
502
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200503/**
504 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
505 * @policy: cpufreq policy to disable fast frequency switching for.
506 */
507void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200508{
509 mutex_lock(&cpufreq_fast_switch_lock);
510 if (policy->fast_switch_enabled) {
511 policy->fast_switch_enabled = false;
512 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
513 cpufreq_fast_switch_count--;
514 }
515 mutex_unlock(&cpufreq_fast_switch_lock);
516}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200517EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Steve Mucklee3c06232016-07-13 13:25:25 -0700519/**
520 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
521 * one.
522 * @target_freq: target frequency to resolve.
523 *
524 * The target to driver frequency mapping is cached in the policy.
525 *
526 * Return: Lowest driver-supported frequency greater than or equal to the
527 * given target_freq, subject to policy (min/max) and driver limitations.
528 */
529unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
530 unsigned int target_freq)
531{
532 target_freq = clamp_val(target_freq, policy->min, policy->max);
533 policy->cached_target_freq = target_freq;
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700534
535 if (cpufreq_driver->target_index) {
536 int idx;
537
538 idx = cpufreq_frequency_table_target(policy, target_freq,
539 CPUFREQ_RELATION_L);
540 policy->cached_resolved_idx = idx;
541 return policy->freq_table[idx].frequency;
542 }
543
Steve Mucklee3c06232016-07-13 13:25:25 -0700544 if (cpufreq_driver->resolve_freq)
545 return cpufreq_driver->resolve_freq(policy, target_freq);
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700546
547 return target_freq;
Steve Mucklee3c06232016-07-13 13:25:25 -0700548}
Steve Muckleae2c1ca2016-07-21 19:24:38 -0700549EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
Steve Mucklee3c06232016-07-13 13:25:25 -0700550
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530551unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
552{
553 unsigned int latency;
554
555 if (policy->transition_delay_us)
556 return policy->transition_delay_us;
557
558 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Viresh Kumare948bc82017-08-17 09:12:27 +0530559 if (latency) {
560 /*
561 * For platforms that can change the frequency very fast (< 10
562 * us), the above formula gives a decent transition delay. But
563 * for platforms where transition_latency is in milliseconds, it
564 * ends up giving unrealistic values.
565 *
566 * Cap the default transition delay to 10 ms, which seems to be
567 * a reasonable amount of time after which we should reevaluate
568 * the frequency.
569 */
570 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
571 }
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530572
573 return LATENCY_MULTIPLIER;
574}
575EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*********************************************************************
578 * SYSFS INTERFACE *
579 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530580static ssize_t show_boost(struct kobject *kobj,
Viresh Kumar625c85a2019-01-25 12:53:07 +0530581 struct kobj_attribute *attr, char *buf)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100582{
583 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
584}
585
Viresh Kumar625c85a2019-01-25 12:53:07 +0530586static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
587 const char *buf, size_t count)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100588{
589 int ret, enable;
590
591 ret = sscanf(buf, "%d", &enable);
592 if (ret != 1 || enable < 0 || enable > 1)
593 return -EINVAL;
594
595 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700596 pr_err("%s: Cannot %s BOOST!\n",
597 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100598 return -EINVAL;
599 }
600
Joe Perchese837f9b2014-03-11 10:03:00 -0700601 pr_debug("%s: cpufreq BOOST %s\n",
602 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100603
604 return count;
605}
606define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530608static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700609{
610 struct cpufreq_governor *t;
611
Viresh Kumarf7b27062015-01-27 14:06:09 +0530612 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200613 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700614 return t;
615
616 return NULL;
617}
618
Yue Huab05d972019-04-29 15:24:18 +0800619static int cpufreq_parse_policy(char *str_governor,
620 struct cpufreq_policy *policy)
621{
622 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
623 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
624 return 0;
625 }
626 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
627 policy->policy = CPUFREQ_POLICY_POWERSAVE;
628 return 0;
629 }
630 return -EINVAL;
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/**
Yue Huab05d972019-04-29 15:24:18 +0800634 * cpufreq_parse_governor - parse a governor string only for !setpolicy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100636static int cpufreq_parse_governor(char *str_governor,
637 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Yue Huab05d972019-04-29 15:24:18 +0800639 struct cpufreq_governor *t;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100640
Yue Huab05d972019-04-29 15:24:18 +0800641 mutex_lock(&cpufreq_governor_mutex);
642
643 t = find_governor(str_governor);
644 if (!t) {
645 int ret;
646
647 mutex_unlock(&cpufreq_governor_mutex);
648
649 ret = request_module("cpufreq_%s", str_governor);
650 if (ret)
651 return -EINVAL;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700652
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800653 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700654
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530655 t = find_governor(str_governor);
Yue Huab05d972019-04-29 15:24:18 +0800656 }
657 if (t && !try_module_get(t->owner))
658 t = NULL;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700659
Yue Huab05d972019-04-29 15:24:18 +0800660 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100661
Yue Huab05d972019-04-29 15:24:18 +0800662 if (t) {
663 policy->governor = t;
664 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100666
667 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530671 * cpufreq_per_cpu_attr_read() / show_##file_name() -
672 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 *
674 * Write out information from cpufreq_driver->policy[cpu]; object must be
675 * "unsigned int".
676 */
677
Dave Jones32ee8c32006-02-28 00:43:23 -0500678#define show_one(file_name, object) \
679static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500680(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500681{ \
Dave Jones29464f22009-01-18 01:37:11 -0500682 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685show_one(cpuinfo_min_freq, cpuinfo.min_freq);
686show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100687show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688show_one(scaling_min_freq, min);
689show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700690
Len Brownf8475ce2017-06-23 22:11:52 -0700691__weak unsigned int arch_freq_get_on_cpu(int cpu)
692{
693 return 0;
694}
695
Viresh Kumar09347b22015-01-02 12:34:24 +0530696static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700697{
698 ssize_t ret;
Len Brownf8475ce2017-06-23 22:11:52 -0700699 unsigned int freq;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700700
Len Brownf8475ce2017-06-23 22:11:52 -0700701 freq = arch_freq_get_on_cpu(policy->cpu);
702 if (freq)
703 ret = sprintf(buf, "%u\n", freq);
704 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
705 cpufreq_driver->get)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700706 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
707 else
708 ret = sprintf(buf, "%u\n", policy->cur);
709 return ret;
710}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
712/**
713 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
714 */
715#define store_one(file_name, object) \
716static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500717(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800719 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 struct cpufreq_policy new_policy; \
721 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530722 memcpy(&new_policy, policy, sizeof(*policy)); \
Tao Wangc7d1f112018-05-26 15:16:48 +0800723 new_policy.min = policy->user_policy.min; \
724 new_policy.max = policy->user_policy.max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 \
Dave Jones29464f22009-01-18 01:37:11 -0500726 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (ret != 1) \
728 return -EINVAL; \
729 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800730 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530731 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800732 if (!ret) \
733 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 \
735 return ret ? ret : count; \
736}
737
Dave Jones29464f22009-01-18 01:37:11 -0500738store_one(scaling_min_freq, min);
739store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741/**
742 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
743 */
Dave Jones905d77c2008-03-05 14:28:32 -0500744static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
745 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530747 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysocki9b4f6032017-03-15 00:12:16 +0100748
749 if (cur_freq)
750 return sprintf(buf, "%u\n", cur_freq);
751
752 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/**
756 * show_scaling_governor - show the current policy for the specified CPU
757 */
Dave Jones905d77c2008-03-05 14:28:32 -0500758static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Dave Jones29464f22009-01-18 01:37:11 -0500760 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return sprintf(buf, "powersave\n");
762 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
763 return sprintf(buf, "performance\n");
764 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200765 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500766 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 return -EINVAL;
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/**
771 * store_scaling_governor - store policy for the specified CPU
772 */
Dave Jones905d77c2008-03-05 14:28:32 -0500773static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
774 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530776 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 char str_governor[16];
778 struct cpufreq_policy new_policy;
779
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530780 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Dave Jones29464f22009-01-18 01:37:11 -0500782 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (ret != 1)
784 return -EINVAL;
785
Yue Huab05d972019-04-29 15:24:18 +0800786 if (cpufreq_driver->setpolicy) {
787 if (cpufreq_parse_policy(str_governor, &new_policy))
788 return -EINVAL;
789 } else {
790 if (cpufreq_parse_governor(str_governor, &new_policy))
791 return -EINVAL;
792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Viresh Kumar037ce832013-10-02 14:13:16 +0530794 ret = cpufreq_set_policy(policy, &new_policy);
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100795
796 if (new_policy.governor)
797 module_put(new_policy.governor->owner);
798
Viresh Kumar88dc4382015-08-03 08:36:18 +0530799 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/**
803 * show_scaling_driver - show the cpufreq driver currently loaded
804 */
Dave Jones905d77c2008-03-05 14:28:32 -0500805static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200807 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
810/**
811 * show_scaling_available_governors - show the available CPUfreq governors
812 */
Dave Jones905d77c2008-03-05 14:28:32 -0500813static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
814 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 ssize_t i = 0;
817 struct cpufreq_governor *t;
818
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530819 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 i += sprintf(buf, "performance powersave");
821 goto out;
822 }
823
Viresh Kumarf7b27062015-01-27 14:06:09 +0530824 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500825 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
826 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200828 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500830out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 i += sprintf(&buf[i], "\n");
832 return i;
833}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700834
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800835ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 ssize_t i = 0;
838 unsigned int cpu;
839
Rusty Russell835481d2009-01-04 05:18:06 -0800840 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 if (i)
842 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
843 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
844 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500845 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847 i += sprintf(&buf[i], "\n");
848 return i;
849}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800850EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700852/**
853 * show_related_cpus - show the CPUs affected by each transition even if
854 * hw coordination is in use
855 */
856static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
857{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800858 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700859}
860
861/**
862 * show_affected_cpus - show the CPUs affected by each transition
863 */
864static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
865{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800866 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700867}
868
Venki Pallipadi9e769882007-10-26 10:18:21 -0700869static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500870 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700871{
872 unsigned int freq = 0;
873 unsigned int ret;
874
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700875 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700876 return -EINVAL;
877
878 ret = sscanf(buf, "%u", &freq);
879 if (ret != 1)
880 return -EINVAL;
881
882 policy->governor->store_setspeed(policy, freq);
883
884 return count;
885}
886
887static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
888{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700889 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700890 return sprintf(buf, "<unsupported>\n");
891
892 return policy->governor->show_setspeed(policy, buf);
893}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Thomas Renningere2f74f32009-11-19 12:31:01 +0100895/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200896 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100897 */
898static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
899{
900 unsigned int limit;
901 int ret;
Yue Hub23aa312019-04-16 10:40:27 +0800902 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
903 if (!ret)
904 return sprintf(buf, "%u\n", limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100905 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
906}
907
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200908cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
909cpufreq_freq_attr_ro(cpuinfo_min_freq);
910cpufreq_freq_attr_ro(cpuinfo_max_freq);
911cpufreq_freq_attr_ro(cpuinfo_transition_latency);
912cpufreq_freq_attr_ro(scaling_available_governors);
913cpufreq_freq_attr_ro(scaling_driver);
914cpufreq_freq_attr_ro(scaling_cur_freq);
915cpufreq_freq_attr_ro(bios_limit);
916cpufreq_freq_attr_ro(related_cpus);
917cpufreq_freq_attr_ro(affected_cpus);
918cpufreq_freq_attr_rw(scaling_min_freq);
919cpufreq_freq_attr_rw(scaling_max_freq);
920cpufreq_freq_attr_rw(scaling_governor);
921cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Dave Jones905d77c2008-03-05 14:28:32 -0500923static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 &cpuinfo_min_freq.attr,
925 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100926 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 &scaling_min_freq.attr,
928 &scaling_max_freq.attr,
929 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700930 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 &scaling_governor.attr,
932 &scaling_driver.attr,
933 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700934 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 NULL
936};
937
Dave Jones29464f22009-01-18 01:37:11 -0500938#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
939#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Dave Jones29464f22009-01-18 01:37:11 -0500941static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Dave Jones905d77c2008-03-05 14:28:32 -0500943 struct cpufreq_policy *policy = to_policy(kobj);
944 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530945 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530946
viresh kumarad7722d2013-10-18 19:10:15 +0530947 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100948 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530949 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return ret;
952}
953
Dave Jones905d77c2008-03-05 14:28:32 -0500954static ssize_t store(struct kobject *kobj, struct attribute *attr,
955 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Dave Jones905d77c2008-03-05 14:28:32 -0500957 struct cpufreq_policy *policy = to_policy(kobj);
958 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500959 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530960
Waiman Long9b3d9bb2018-07-24 14:26:05 -0400961 /*
962 * cpus_read_trylock() is used here to work around a circular lock
963 * dependency problem with respect to the cpufreq_register_driver().
964 */
965 if (!cpus_read_trylock())
966 return -EBUSY;
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530967
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100968 if (cpu_online(policy->cpu)) {
969 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530970 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100971 up_write(&policy->rwsem);
972 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530973
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200974 cpus_read_unlock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return ret;
977}
978
Dave Jones905d77c2008-03-05 14:28:32 -0500979static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Dave Jones905d77c2008-03-05 14:28:32 -0500981 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200982 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 complete(&policy->kobj_unregister);
984}
985
Emese Revfy52cf25d2010-01-19 02:58:23 +0100986static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 .show = show,
988 .store = store,
989};
990
991static struct kobj_type ktype_cpufreq = {
992 .sysfs_ops = &sysfs_ops,
993 .default_attrs = default_attrs,
994 .release = cpufreq_sysfs_release,
995};
996
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200997static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +0200998{
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +0200999 struct device *dev = get_cpu_device(cpu);
1000
1001 if (!dev)
1002 return;
1003
1004 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1005 return;
1006
Viresh Kumar266198042016-09-12 12:07:05 +05301007 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001008 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1009 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +02001010}
1011
Viresh Kumar266198042016-09-12 12:07:05 +05301012static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1013 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +02001014{
Viresh Kumar266198042016-09-12 12:07:05 +05301015 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1016 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +02001017}
1018
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001019static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -04001020{
1021 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -04001022 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -04001023
Dave Jones909a6942009-07-08 18:05:42 -04001024 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001025 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +05301026 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -04001027 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1028 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001029 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001030 drv_attr++;
1031 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001032 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001033 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1034 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001035 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001036 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001037
1038 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1039 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001040 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001041
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001042 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001043 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1044 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001045 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001046 }
Dave Jones909a6942009-07-08 18:05:42 -04001047
Viresh Kumar266198042016-09-12 12:07:05 +05301048 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301049}
1050
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001051__weak struct cpufreq_governor *cpufreq_default_governor(void)
1052{
1053 return NULL;
1054}
1055
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301056static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301057{
Yue Huab05d972019-04-29 15:24:18 +08001058 struct cpufreq_governor *gov = NULL, *def_gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301059 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301060
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301061 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001062
Yue Huab05d972019-04-29 15:24:18 +08001063 def_gov = cpufreq_default_governor();
1064
1065 if (has_target()) {
1066 /*
1067 * Update governor of new_policy to the governor used before
1068 * hotplug
1069 */
1070 gov = find_governor(policy->last_governor);
1071 if (gov) {
1072 pr_debug("Restoring governor %s for cpu %d\n",
viresh kumar6e2c89d2014-03-04 11:43:59 +08001073 policy->governor->name, policy->cpu);
Yue Huab05d972019-04-29 15:24:18 +08001074 } else {
1075 if (!def_gov)
1076 return -ENODATA;
1077 gov = def_gov;
1078 }
1079 new_policy.governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001080 } else {
Yue Huab05d972019-04-29 15:24:18 +08001081 /* Use the default policy if there is no last_policy. */
1082 if (policy->last_policy) {
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001083 new_policy.policy = policy->last_policy;
Yue Huab05d972019-04-29 15:24:18 +08001084 } else {
1085 if (!def_gov)
1086 return -ENODATA;
1087 cpufreq_parse_policy(def_gov->name, &new_policy);
1088 }
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001089 }
Yue Huab05d972019-04-29 15:24:18 +08001090
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301091 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001092}
1093
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001094static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001095{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301096 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001097
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301098 /* Has this CPU been taken care of already? */
1099 if (cpumask_test_cpu(cpu, policy->cpus))
1100 return 0;
1101
Viresh Kumar49f18562016-02-11 17:31:12 +05301102 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001103 if (has_target())
1104 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001105
Viresh Kumarfcf80582013-01-29 14:39:08 +00001106 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301107
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301108 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001109 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301110 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301111 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001112 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301113 up_write(&policy->rwsem);
1114 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301117static void handle_update(struct work_struct *work)
1118{
1119 struct cpufreq_policy *policy =
1120 container_of(work, struct cpufreq_policy, update);
1121 unsigned int cpu = policy->cpu;
1122 pr_debug("handle_update for cpu %u called\n", cpu);
1123 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001126static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301127{
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301128 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301129 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301130
1131 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1132 if (!policy)
1133 return NULL;
1134
1135 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1136 goto err_free_policy;
1137
1138 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1139 goto err_free_cpumask;
1140
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001141 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1142 goto err_free_rcpumask;
1143
Viresh Kumaredd4a892016-03-03 14:51:33 +05301144 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1145 cpufreq_global_kobject, "policy%u", cpu);
1146 if (ret) {
1147 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
Rafael J. Wysocki2acb9bd2019-05-10 12:35:53 +02001148 /*
1149 * The entire policy object will be freed below, but the extra
1150 * memory allocated for the kobject name needs to be freed by
1151 * releasing the kobject.
1152 */
Viresh Kumar4ebe36c2019-04-30 11:35:52 +05301153 kobject_put(&policy->kobj);
Viresh Kumaredd4a892016-03-03 14:51:33 +05301154 goto err_free_real_cpus;
1155 }
1156
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301157 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301158 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301159 spin_lock_init(&policy->transition_lock);
1160 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301161 init_completion(&policy->kobj_unregister);
1162 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301163
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001164 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301165 return policy;
1166
Viresh Kumaredd4a892016-03-03 14:51:33 +05301167err_free_real_cpus:
1168 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301169err_free_rcpumask:
1170 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301171err_free_cpumask:
1172 free_cpumask_var(policy->cpus);
1173err_free_policy:
1174 kfree(policy);
1175
1176 return NULL;
1177}
1178
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301179static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301180{
1181 struct kobject *kobj;
1182 struct completion *cmp;
1183
Viresh Kumar87549142015-06-10 02:13:21 +02001184 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001185 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301186 kobj = &policy->kobj;
1187 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001188 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301189 kobject_put(kobj);
1190
1191 /*
1192 * We need to make sure that the underlying kobj is
1193 * actually not referenced anymore by anybody before we
1194 * proceed with unloading.
1195 */
1196 pr_debug("waiting for dropping of refcount\n");
1197 wait_for_completion(cmp);
1198 pr_debug("wait complete\n");
1199}
1200
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301201static void cpufreq_policy_free(struct cpufreq_policy *policy)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301202{
Viresh Kumar988bed02015-05-08 11:53:45 +05301203 unsigned long flags;
1204 int cpu;
1205
1206 /* Remove policy from list */
1207 write_lock_irqsave(&cpufreq_driver_lock, flags);
1208 list_del(&policy->policy_list);
1209
1210 for_each_cpu(cpu, policy->related_cpus)
1211 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1212 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1213
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301214 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001215 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301216 free_cpumask_var(policy->related_cpus);
1217 free_cpumask_var(policy->cpus);
1218 kfree(policy);
1219}
1220
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001221static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301223 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001224 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001226 unsigned int j;
1227 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001228
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001229 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301230
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301231 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301232 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001233 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301234 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001235 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001236 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001238 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001239 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001240 down_write(&policy->rwsem);
1241 policy->cpu = cpu;
1242 policy->governor = NULL;
1243 up_write(&policy->rwsem);
1244 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001245 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001246 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001247 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001248 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001249 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301250
Viresh Kumar91a12e92019-02-12 16:36:04 +05301251 if (!new_policy && cpufreq_driver->online) {
1252 ret = cpufreq_driver->online(policy);
1253 if (ret) {
1254 pr_debug("%s: %d: initialization failed\n", __func__,
1255 __LINE__);
1256 goto out_exit_policy;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Viresh Kumar91a12e92019-02-12 16:36:04 +05301259 /* Recover policy->cpus using related_cpus */
1260 cpumask_copy(policy->cpus, policy->related_cpus);
1261 } else {
1262 cpumask_copy(policy->cpus, cpumask_of(cpu));
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001263
Viresh Kumar91a12e92019-02-12 16:36:04 +05301264 /*
1265 * Call driver. From then on the cpufreq must be able
1266 * to accept all calls to ->verify and ->setpolicy for this CPU.
1267 */
1268 ret = cpufreq_driver->init(policy);
1269 if (ret) {
1270 pr_debug("%s: %d: initialization failed\n", __func__,
1271 __LINE__);
1272 goto out_free_policy;
1273 }
Viresh Kumard417e062018-02-22 11:29:44 +05301274
Viresh Kumar91a12e92019-02-12 16:36:04 +05301275 ret = cpufreq_table_validate_and_sort(policy);
1276 if (ret)
1277 goto out_exit_policy;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001278
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001279 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301280 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001281 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001282
Viresh Kumar91a12e92019-02-12 16:36:04 +05301283 down_write(&policy->rwsem);
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001284 /*
1285 * affected cpus must always be the one, which are online. We aren't
1286 * managing offline cpus here.
1287 */
1288 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1289
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001290 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001291 policy->user_policy.min = policy->min;
1292 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001293
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001294 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301295 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001296 add_cpu_dev_symlink(policy, j);
1297 }
Viresh Kumarff010472017-03-21 11:36:06 +05301298 } else {
1299 policy->min = policy->user_policy.min;
1300 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301301 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301302
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001303 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301304 policy->cur = cpufreq_driver->get(policy->cpu);
1305 if (!policy->cur) {
1306 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumard417e062018-02-22 11:29:44 +05301307 goto out_destroy_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301308 }
1309 }
1310
Viresh Kumard3916692013-12-03 11:20:46 +05301311 /*
1312 * Sometimes boot loaders set CPU frequency to a value outside of
1313 * frequency table present with cpufreq core. In such cases CPU might be
1314 * unstable if it has to run on that frequency for long duration of time
1315 * and so its better to set it to a frequency which is specified in
1316 * freq-table. This also makes cpufreq stats inconsistent as
1317 * cpufreq-stats would fail to register because current frequency of CPU
1318 * isn't found in freq-table.
1319 *
1320 * Because we don't want this change to effect boot process badly, we go
1321 * for the next freq which is >= policy->cur ('cur' must be set by now,
1322 * otherwise we will end up setting freq to lowest of the table as 'cur'
1323 * is initialized to zero).
1324 *
1325 * We are passing target-freq as "policy->cur - 1" otherwise
1326 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1327 * equal to target-freq.
1328 */
1329 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1330 && has_target()) {
1331 /* Are we running at unknown frequency ? */
1332 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1333 if (ret == -EINVAL) {
1334 /* Warn user and fix it */
1335 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1336 __func__, policy->cpu, policy->cur);
1337 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1338 CPUFREQ_RELATION_L);
1339
1340 /*
1341 * Reaching here after boot in a few seconds may not
1342 * mean that system will remain stable at "unknown"
1343 * frequency for longer duration. Hence, a BUG_ON().
1344 */
1345 BUG_ON(ret);
1346 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1347 __func__, policy->cpu, policy->cur);
1348 }
1349 }
1350
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001351 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001352 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301353 if (ret)
Viresh Kumard417e062018-02-22 11:29:44 +05301354 goto out_destroy_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001355
1356 cpufreq_stats_create_table(policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001357
Viresh Kumar988bed02015-05-08 11:53:45 +05301358 write_lock_irqsave(&cpufreq_driver_lock, flags);
1359 list_add(&policy->policy_list, &cpufreq_policy_list);
1360 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1361 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301362
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301363 ret = cpufreq_init_policy(policy);
1364 if (ret) {
1365 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1366 __func__, cpu, ret);
Viresh Kumard417e062018-02-22 11:29:44 +05301367 goto out_destroy_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301368 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301369
Viresh Kumar4e97b632014-03-04 11:44:01 +08001370 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301371
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001372 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301373
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301374 /* Callback for handling stuff after policy is ready */
1375 if (cpufreq_driver->ready)
1376 cpufreq_driver->ready(policy);
1377
Amit Kucheria5c238a82019-01-30 10:52:01 +05301378 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1379 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV)
1380 policy->cdev = of_cpufreq_cooling_register(policy);
1381
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001382 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 return 0;
1385
Viresh Kumard417e062018-02-22 11:29:44 +05301386out_destroy_policy:
Viresh Kumarb24b6472018-02-22 11:29:43 +05301387 for_each_cpu(j, policy->real_cpus)
1388 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1389
Prarit Bhargava7106e022014-09-10 10:12:08 -04001390 up_write(&policy->rwsem);
1391
Viresh Kumard417e062018-02-22 11:29:44 +05301392out_exit_policy:
Viresh Kumarda60ce92013-10-03 20:28:30 +05301393 if (cpufreq_driver->exit)
1394 cpufreq_driver->exit(policy);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001395
Viresh Kumar8101f992015-07-08 15:12:15 +05301396out_free_policy:
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301397 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return ret;
1399}
1400
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001401/**
1402 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1403 * @dev: CPU device.
1404 * @sif: Subsystem interface structure pointer (not used)
1405 */
1406static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1407{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001408 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001409 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301410 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001411
1412 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1413
Viresh Kumar266198042016-09-12 12:07:05 +05301414 if (cpu_online(cpu)) {
1415 ret = cpufreq_online(cpu);
1416 if (ret)
1417 return ret;
1418 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001419
Viresh Kumar266198042016-09-12 12:07:05 +05301420 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001421 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001422 if (policy)
1423 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001428static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301430 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301431 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001433 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Viresh Kumar988bed02015-05-08 11:53:45 +05301435 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301436 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001437 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Viresh Kumar49f18562016-02-11 17:31:12 +05301441 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001442 if (has_target())
1443 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001444
Viresh Kumar9591bec2015-06-10 02:20:23 +02001445 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301446
Viresh Kumar9591bec2015-06-10 02:20:23 +02001447 if (policy_is_inactive(policy)) {
1448 if (has_target())
1449 strncpy(policy->last_governor, policy->governor->name,
1450 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001451 else
1452 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001453 } else if (cpu == policy->cpu) {
1454 /* Nominate new CPU */
1455 policy->cpu = cpumask_any(policy->cpus);
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Viresh Kumar9591bec2015-06-10 02:20:23 +02001458 /* Start governor again for active policy */
1459 if (!policy_is_inactive(policy)) {
1460 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001461 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001462 if (ret)
1463 pr_err("%s: Failed to start governor\n", __func__);
1464 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301465
Viresh Kumar49f18562016-02-11 17:31:12 +05301466 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301467 }
1468
Amit Kucheria5c238a82019-01-30 10:52:01 +05301469 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1470 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) {
1471 cpufreq_cooling_unregister(policy->cdev);
1472 policy->cdev = NULL;
1473 }
1474
Viresh Kumar69cee712016-02-11 17:31:11 +05301475 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001476 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001478 if (has_target())
1479 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001480
Viresh Kumar87549142015-06-10 02:13:21 +02001481 /*
Viresh Kumar91a12e92019-02-12 16:36:04 +05301482 * Perform the ->offline() during light-weight tear-down, as
1483 * that allows fast recovery when the CPU comes back.
Viresh Kumar87549142015-06-10 02:13:21 +02001484 */
Viresh Kumar91a12e92019-02-12 16:36:04 +05301485 if (cpufreq_driver->offline) {
1486 cpufreq_driver->offline(policy);
1487 } else if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001488 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001489 policy->freq_table = NULL;
1490 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301491
1492unlock:
1493 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001494 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301497/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301498 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301499 *
1500 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301501 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301502static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001503{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001504 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001505 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001506
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001507 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001508 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001509
Viresh Kumar69cee712016-02-11 17:31:11 +05301510 if (cpu_online(cpu))
1511 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001512
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001513 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301514 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301515
Viresh Kumar91a12e92019-02-12 16:36:04 +05301516 if (cpumask_empty(policy->real_cpus)) {
1517 /* We did light-weight exit earlier, do full tear down now */
1518 if (cpufreq_driver->offline)
1519 cpufreq_driver->exit(policy);
1520
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301521 cpufreq_policy_free(policy);
Viresh Kumar91a12e92019-02-12 16:36:04 +05301522 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001523}
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301526 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1527 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301528 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 * @new_freq: CPU frequency the CPU actually runs at
1530 *
Dave Jones29464f22009-01-18 01:37:11 -05001531 * We adjust to current frequency first, and need to clean up later.
1532 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301534static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301535 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
1537 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301538
Joe Perchese837f9b2014-03-11 10:03:00 -07001539 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 +05301540 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301542 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301544
Viresh Kumar8fec0512014-03-24 13:35:45 +05301545 cpufreq_freq_transition_begin(policy, &freqs);
1546 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547}
1548
Dave Jones32ee8c32006-02-28 00:43:23 -05001549/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301550 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001551 * @cpu: CPU number
1552 *
1553 * This is the last known freq, without actually getting it from the driver.
1554 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1555 */
1556unsigned int cpufreq_quick_get(unsigned int cpu)
1557{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001558 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301559 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001560 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001561
Richard Cochranc75361c2016-03-11 09:43:07 +01001562 read_lock_irqsave(&cpufreq_driver_lock, flags);
1563
1564 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1565 ret_freq = cpufreq_driver->get(cpu);
1566 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1567 return ret_freq;
1568 }
1569
1570 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001571
1572 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001573 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301574 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001575 cpufreq_cpu_put(policy);
1576 }
1577
Dave Jones4d34a672008-02-07 16:33:49 -05001578 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001579}
1580EXPORT_SYMBOL(cpufreq_quick_get);
1581
Jesse Barnes3d737102011-06-28 10:59:12 -07001582/**
1583 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1584 * @cpu: CPU number
1585 *
1586 * Just return the max possible frequency for a given CPU.
1587 */
1588unsigned int cpufreq_quick_get_max(unsigned int cpu)
1589{
1590 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1591 unsigned int ret_freq = 0;
1592
1593 if (policy) {
1594 ret_freq = policy->max;
1595 cpufreq_cpu_put(policy);
1596 }
1597
1598 return ret_freq;
1599}
1600EXPORT_SYMBOL(cpufreq_quick_get_max);
1601
Viresh Kumard92d50a2015-01-02 12:34:29 +05301602static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301604 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Yue Hu4db7c342019-04-19 14:27:58 +08001606 if (unlikely(policy_is_inactive(policy)))
Dave Jones4d34a672008-02-07 16:33:49 -05001607 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Viresh Kumard92d50a2015-01-02 12:34:29 +05301609 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001611 /*
Sudeep Holla2f661962019-01-07 18:51:53 +00001612 * If fast frequency switching is used with the given policy, the check
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001613 * against policy->cur is pointless, so skip it in that case too.
1614 */
Sudeep Holla2f661962019-01-07 18:51:53 +00001615 if (policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001616 return ret_freq;
1617
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301618 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001619 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301620 /* verify no discrepancy between actual and
1621 saved value exists */
1622 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301623 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 schedule_work(&policy->update);
1625 }
1626 }
1627
Dave Jones4d34a672008-02-07 16:33:49 -05001628 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001631/**
1632 * cpufreq_get - get the current CPU frequency (in kHz)
1633 * @cpu: CPU number
1634 *
1635 * Get the CPU current (static) CPU frequency
1636 */
1637unsigned int cpufreq_get(unsigned int cpu)
1638{
Aaron Plattner999976e2014-03-04 12:42:15 -08001639 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001640 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001641
Aaron Plattner999976e2014-03-04 12:42:15 -08001642 if (policy) {
1643 down_read(&policy->rwsem);
Yue Hu4db7c342019-04-19 14:27:58 +08001644 if (cpufreq_driver->get)
1645 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001646 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301647
Aaron Plattner999976e2014-03-04 12:42:15 -08001648 cpufreq_cpu_put(policy);
1649 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301650
Dave Jones4d34a672008-02-07 16:33:49 -05001651 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653EXPORT_SYMBOL(cpufreq_get);
1654
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001655static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1656{
1657 unsigned int new_freq;
1658
1659 new_freq = cpufreq_driver->get(policy->cpu);
1660 if (!new_freq)
1661 return 0;
1662
1663 if (!policy->cur) {
1664 pr_debug("cpufreq: Driver did not initialize current freq\n");
1665 policy->cur = new_freq;
1666 } else if (policy->cur != new_freq && has_target()) {
1667 cpufreq_out_of_sync(policy, new_freq);
1668 }
1669
1670 return new_freq;
1671}
1672
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001673static struct subsys_interface cpufreq_interface = {
1674 .name = "cpufreq",
1675 .subsys = &cpu_subsys,
1676 .add_dev = cpufreq_add_dev,
1677 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001678};
1679
Viresh Kumare28867e2014-03-04 11:00:27 +08001680/*
1681 * In case platform wants some specific frequency to be configured
1682 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001683 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001684int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001685{
Viresh Kumare28867e2014-03-04 11:00:27 +08001686 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001687
Viresh Kumare28867e2014-03-04 11:00:27 +08001688 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001689 pr_debug("%s: suspend_freq not defined\n", __func__);
1690 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001691 }
1692
Viresh Kumare28867e2014-03-04 11:00:27 +08001693 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1694 policy->suspend_freq);
1695
1696 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1697 CPUFREQ_RELATION_H);
1698 if (ret)
1699 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1700 __func__, policy->suspend_freq, ret);
1701
Dave Jonesc9060492008-02-07 16:32:18 -05001702 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001703}
Viresh Kumare28867e2014-03-04 11:00:27 +08001704EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001705
1706/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001707 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001709 * Called during system wide Suspend/Hibernate cycles for suspending governors
1710 * as some platforms can't change frequency after this point in suspend cycle.
1711 * Because some of the devices (like: i2c, regulators, etc) they use for
1712 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001714void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301716 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001718 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001719 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001721 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301722 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001724 pr_debug("%s: Suspending Governors\n", __func__);
1725
Viresh Kumarf9637352015-05-12 12:20:11 +05301726 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001727 if (has_target()) {
1728 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001729 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001730 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001731 }
1732
1733 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001734 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1735 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301737
1738suspend:
1739 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001743 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001745 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1746 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001748void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301751 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001753 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return;
1755
Bo Yan703cbaa2018-01-23 13:57:55 -08001756 if (unlikely(!cpufreq_suspended))
1757 return;
1758
Lan Tianyu8e304442014-09-18 15:03:07 +08001759 cpufreq_suspended = false;
1760
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001761 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001762 return;
1763
1764 pr_debug("%s: Resuming Governors\n", __func__);
1765
Viresh Kumarf9637352015-05-12 12:20:11 +05301766 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301767 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301768 pr_err("%s: Failed to resume driver: %p\n", __func__,
1769 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001770 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301771 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001772 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301773 up_write(&policy->rwsem);
1774
1775 if (ret)
1776 pr_err("%s: Failed to start governor for policy: %p\n",
1777 __func__, policy);
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Borislav Petkov9d950462013-01-20 10:24:28 +00001782/**
1783 * cpufreq_get_current_driver - return current driver's name
1784 *
1785 * Return the name string of the currently loaded cpufreq driver
1786 * or NULL, if none.
1787 */
1788const char *cpufreq_get_current_driver(void)
1789{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001790 if (cpufreq_driver)
1791 return cpufreq_driver->name;
1792
1793 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001794}
1795EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001797/**
1798 * cpufreq_get_driver_data - return current driver data
1799 *
1800 * Return the private data of the currently loaded cpufreq
1801 * driver, or NULL if no cpufreq driver is loaded.
1802 */
1803void *cpufreq_get_driver_data(void)
1804{
1805 if (cpufreq_driver)
1806 return cpufreq_driver->driver_data;
1807
1808 return NULL;
1809}
1810EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/*********************************************************************
1813 * NOTIFIER LISTS INTERFACE *
1814 *********************************************************************/
1815
1816/**
1817 * cpufreq_register_notifier - register a driver with cpufreq
1818 * @nb: notifier function to register
1819 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1820 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001821 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 * are notified about clock rate changes (once before and once after
1823 * the transition), or a list of drivers that are notified about
1824 * changes in cpufreq policy.
1825 *
1826 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001827 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 */
1829int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1830{
1831 int ret;
1832
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001833 if (cpufreq_disabled())
1834 return -EINVAL;
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 switch (list) {
1837 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001838 mutex_lock(&cpufreq_fast_switch_lock);
1839
1840 if (cpufreq_fast_switch_count > 0) {
1841 mutex_unlock(&cpufreq_fast_switch_lock);
1842 return -EBUSY;
1843 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001844 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001845 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001846 if (!ret)
1847 cpufreq_fast_switch_count--;
1848
1849 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 break;
1851 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001852 ret = blocking_notifier_chain_register(
1853 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
1855 default:
1856 ret = -EINVAL;
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 return ret;
1860}
1861EXPORT_SYMBOL(cpufreq_register_notifier);
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863/**
1864 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1865 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301866 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 *
1868 * Remove a driver from the CPU frequency notifier list.
1869 *
1870 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001871 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
1873int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1874{
1875 int ret;
1876
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001877 if (cpufreq_disabled())
1878 return -EINVAL;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 switch (list) {
1881 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001882 mutex_lock(&cpufreq_fast_switch_lock);
1883
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001884 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001885 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001886 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1887 cpufreq_fast_switch_count++;
1888
1889 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 break;
1891 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001892 ret = blocking_notifier_chain_unregister(
1893 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895 default:
1896 ret = -EINVAL;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 return ret;
1900}
1901EXPORT_SYMBOL(cpufreq_unregister_notifier);
1902
1903
1904/*********************************************************************
1905 * GOVERNORS *
1906 *********************************************************************/
1907
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001908/**
1909 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1910 * @policy: cpufreq policy to switch the frequency for.
1911 * @target_freq: New frequency to set (may be approximate).
1912 *
1913 * Carry out a fast frequency switch without sleeping.
1914 *
1915 * The driver's ->fast_switch() callback invoked by this function must be
1916 * suitable for being called from within RCU-sched read-side critical sections
1917 * and it is expected to select the minimum available frequency greater than or
1918 * equal to @target_freq (CPUFREQ_RELATION_L).
1919 *
1920 * This function must not be called if policy->fast_switch_enabled is unset.
1921 *
1922 * Governors calling this function must guarantee that it will never be invoked
1923 * twice in parallel for the same policy and that it will never be called in
1924 * parallel with either ->target() or ->target_index() for the same policy.
1925 *
Viresh Kumar209887e2017-08-09 10:21:46 +05301926 * Returns the actual frequency set for the CPU.
1927 *
1928 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1929 * error condition, the hardware configuration must be preserved.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001930 */
1931unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1932 unsigned int target_freq)
1933{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001934 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001935
1936 return cpufreq_driver->fast_switch(policy, target_freq);
1937}
1938EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1939
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301940/* Must set freqs->new to intermediate frequency */
1941static int __target_intermediate(struct cpufreq_policy *policy,
1942 struct cpufreq_freqs *freqs, int index)
1943{
1944 int ret;
1945
1946 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1947
1948 /* We don't need to switch to intermediate freq */
1949 if (!freqs->new)
1950 return 0;
1951
1952 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1953 __func__, policy->cpu, freqs->old, freqs->new);
1954
1955 cpufreq_freq_transition_begin(policy, freqs);
1956 ret = cpufreq_driver->target_intermediate(policy, index);
1957 cpufreq_freq_transition_end(policy, freqs, ret);
1958
1959 if (ret)
1960 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1961 __func__, ret);
1962
1963 return ret;
1964}
1965
Viresh Kumar23727842016-06-03 10:58:50 +05301966static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301967{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301968 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1969 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301970 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301971 int retval = -EINVAL;
1972 bool notify;
1973
Viresh Kumar23727842016-06-03 10:58:50 +05301974 if (newfreq == policy->cur)
1975 return 0;
1976
Viresh Kumar8d657752014-05-21 14:29:29 +05301977 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301978 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301979 /* Handle switching to intermediate frequency */
1980 if (cpufreq_driver->get_intermediate) {
1981 retval = __target_intermediate(policy, &freqs, index);
1982 if (retval)
1983 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301984
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301985 intermediate_freq = freqs.new;
1986 /* Set old freq to intermediate */
1987 if (intermediate_freq)
1988 freqs.old = freqs.new;
1989 }
1990
Viresh Kumar23727842016-06-03 10:58:50 +05301991 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301992 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1993 __func__, policy->cpu, freqs.old, freqs.new);
1994
1995 cpufreq_freq_transition_begin(policy, &freqs);
1996 }
1997
1998 retval = cpufreq_driver->target_index(policy, index);
1999 if (retval)
2000 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2001 retval);
2002
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302003 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05302004 cpufreq_freq_transition_end(policy, &freqs, retval);
2005
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302006 /*
2007 * Failed after setting to intermediate freq? Driver should have
2008 * reverted back to initial frequency and so should we. Check
2009 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302010 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302011 */
2012 if (unlikely(retval && intermediate_freq)) {
2013 freqs.old = intermediate_freq;
2014 freqs.new = policy->restore_freq;
2015 cpufreq_freq_transition_begin(policy, &freqs);
2016 cpufreq_freq_transition_end(policy, &freqs, 0);
2017 }
2018 }
2019
Viresh Kumar8d657752014-05-21 14:29:29 +05302020 return retval;
2021}
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023int __cpufreq_driver_target(struct cpufreq_policy *policy,
2024 unsigned int target_freq,
2025 unsigned int relation)
2026{
Viresh Kumar72499242012-10-31 01:28:21 +01002027 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302028 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002029
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002030 if (cpufreq_disabled())
2031 return -ENODEV;
2032
Viresh Kumar72499242012-10-31 01:28:21 +01002033 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302034 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002035
2036 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002037 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002038
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302039 /*
2040 * This might look like a redundant call as we are checking it again
2041 * after finding index. But it is left intentionally for cases where
2042 * exactly same freq is called again and so we can save on few function
2043 * calls.
2044 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002045 if (target_freq == policy->cur)
2046 return 0;
2047
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302048 /* Save last value to restore later on errors */
2049 policy->restore_freq = policy->cur;
2050
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002051 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002052 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002053
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002054 if (!cpufreq_driver->target_index)
2055 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302056
Viresh Kumard218ed72016-06-03 10:58:51 +05302057 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302058
Viresh Kumar23727842016-06-03 10:58:50 +05302059 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2062
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063int cpufreq_driver_target(struct cpufreq_policy *policy,
2064 unsigned int target_freq,
2065 unsigned int relation)
2066{
Julia Lawallf1829e42008-07-25 22:44:53 +02002067 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
viresh kumarad7722d2013-10-18 19:10:15 +05302069 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 ret = __cpufreq_driver_target(policy, target_freq, relation);
2072
viresh kumarad7722d2013-10-18 19:10:15 +05302073 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return ret;
2076}
2077EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2078
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002079__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2080{
2081 return NULL;
2082}
2083
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002084static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Dave Jonescc993ca2005-07-28 09:43:56 -07002086 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002087
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002088 /* Don't start any governor operations if we are entering suspend */
2089 if (cpufreq_suspended)
2090 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002091 /*
2092 * Governor might not be initiated here if ACPI _PPC changed
2093 * notification happened, so check it.
2094 */
2095 if (!policy->governor)
2096 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002097
Viresh Kumared4676e2017-07-19 15:42:46 +05302098 /* Platform doesn't want dynamic frequency switching ? */
2099 if (policy->governor->dynamic_switching &&
Viresh Kumarfc4c7092017-07-19 15:42:49 +05302100 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002101 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2102
2103 if (gov) {
Viresh Kumarfe829ed2017-07-19 15:42:48 +05302104 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002105 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002106 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002107 } else {
2108 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002109 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002112 if (!try_module_get(policy->governor->owner))
2113 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002115 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002116
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002117 if (policy->governor->init) {
2118 ret = policy->governor->init(policy);
2119 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002120 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002121 return ret;
2122 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002125 return 0;
2126}
2127
2128static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2129{
2130 if (cpufreq_suspended || !policy->governor)
2131 return;
2132
2133 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2134
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002135 if (policy->governor->exit)
2136 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002137
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002138 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002141static int cpufreq_start_governor(struct cpufreq_policy *policy)
2142{
2143 int ret;
2144
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002145 if (cpufreq_suspended)
2146 return 0;
2147
2148 if (!policy->governor)
2149 return -EINVAL;
2150
2151 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2152
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002153 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2154 cpufreq_update_current_freq(policy);
2155
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002156 if (policy->governor->start) {
2157 ret = policy->governor->start(policy);
2158 if (ret)
2159 return ret;
2160 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002161
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002162 if (policy->governor->limits)
2163 policy->governor->limits(policy);
2164
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002165 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002166}
2167
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002168static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2169{
2170 if (cpufreq_suspended || !policy->governor)
2171 return;
2172
2173 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2174
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002175 if (policy->governor->stop)
2176 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002177}
2178
2179static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2180{
2181 if (cpufreq_suspended || !policy->governor)
2182 return;
2183
2184 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2185
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002186 if (policy->governor->limits)
2187 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188}
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190int cpufreq_register_governor(struct cpufreq_governor *governor)
2191{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002192 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
2194 if (!governor)
2195 return -EINVAL;
2196
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002197 if (cpufreq_disabled())
2198 return -ENODEV;
2199
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002200 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002201
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002202 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302203 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002204 err = 0;
2205 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Dave Jones32ee8c32006-02-28 00:43:23 -05002208 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2214{
Viresh Kumar45732372015-05-12 12:22:34 +05302215 struct cpufreq_policy *policy;
2216 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (!governor)
2219 return;
2220
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002221 if (cpufreq_disabled())
2222 return;
2223
Viresh Kumar45732372015-05-12 12:22:34 +05302224 /* clear last_governor for all inactive policies */
2225 read_lock_irqsave(&cpufreq_driver_lock, flags);
2226 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302227 if (!strcmp(policy->last_governor, governor->name)) {
2228 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302229 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302230 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002231 }
Viresh Kumar45732372015-05-12 12:22:34 +05302232 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002233
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002234 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002236 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2239
2240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241/*********************************************************************
2242 * POLICY INTERFACE *
2243 *********************************************************************/
2244
2245/**
2246 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002247 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2248 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 *
2250 * Reads the current cpufreq policy.
2251 */
2252int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2253{
2254 struct cpufreq_policy *cpu_policy;
2255 if (!policy)
2256 return -EINVAL;
2257
2258 cpu_policy = cpufreq_cpu_get(cpu);
2259 if (!cpu_policy)
2260 return -EINVAL;
2261
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302262 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 return 0;
2266}
2267EXPORT_SYMBOL(cpufreq_get_policy);
2268
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002269/**
2270 * cpufreq_set_policy - Modify cpufreq policy parameters.
2271 * @policy: Policy object to modify.
2272 * @new_policy: New policy data.
2273 *
2274 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2275 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2276 * the driver's ->verify() callback again and run the notifiers for it again
2277 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2278 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2279 * callback (if present) or carry out a governor update for @policy. That is,
2280 * run the current governor's ->limits() callback (if the governor field in
2281 * @new_policy points to the same object as the one in @policy) or replace the
2282 * governor for @policy with the new one stored in @new_policy.
2283 *
2284 * The cpuinfo part of @policy is not updated by this function.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002285 */
Rafael J. Wysocki9083e492019-03-26 12:19:52 +01002286int cpufreq_set_policy(struct cpufreq_policy *policy,
2287 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002289 struct cpufreq_governor *old_gov;
2290 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
Joe Perchese837f9b2014-03-11 10:03:00 -07002292 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2293 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302295 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Pan Xinhuifba95732015-07-30 18:10:40 +08002297 /*
2298 * This check works well when we store new min/max freq attributes,
2299 * because new_policy is a copy of policy with one field updated.
2300 */
2301 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002302 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302305 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002307 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002310 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302311 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Viresh Kumarbb176f72013-06-19 14:19:33 +05302313 /*
2314 * verify the cpu speed can be set within this limit, which might be
2315 * different to the first one
2316 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302317 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002318 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002319 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002322 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302323 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302325 policy->min = new_policy->min;
2326 policy->max = new_policy->max;
Ruchi Kandoi601b2182018-07-24 10:35:44 -07002327 trace_cpu_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Steve Mucklee3c06232016-07-13 13:25:25 -07002329 policy->cached_target_freq = UINT_MAX;
2330
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002331 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002332 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002334 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302335 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002336 pr_debug("setting range\n");
Rafael J. Wysocki167a38d2019-02-20 00:26:30 +01002337 return cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002340 if (new_policy->governor == policy->governor) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002341 pr_debug("governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002342 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002343 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002344 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002345
2346 pr_debug("governor switch\n");
2347
2348 /* save old, working values */
2349 old_gov = policy->governor;
2350 /* end old governor */
2351 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002352 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002353 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002354 }
2355
2356 /* start new governor */
2357 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002358 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302359 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002360 ret = cpufreq_start_governor(policy);
2361 if (!ret) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002362 pr_debug("governor change\n");
Quentin Perret531b5c92018-12-03 09:56:21 +00002363 sched_cpufreq_governor_change(policy, old_gov);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002364 return 0;
2365 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002366 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002367 }
2368
2369 /* new governor failed, so re-start old one */
2370 pr_debug("starting governor %s failed\n", policy->governor->name);
2371 if (old_gov) {
2372 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002373 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302374 policy->governor = NULL;
2375 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002376 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002377 }
2378
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302379 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
2382/**
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002383 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2384 * @cpu: CPU to re-evaluate the policy for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 *
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002386 * Update the current frequency for the cpufreq policy of @cpu and use
2387 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2388 * user_policy sub-structure of that policy, which triggers the evaluation
2389 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2390 * policy in question, among other things.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 */
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002392void cpufreq_update_policy(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002394 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302395 struct cpufreq_policy new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002397 if (!policy)
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002398 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Viresh Kumarbb176f72013-06-19 14:19:33 +05302400 /*
2401 * BIOS might change freq behind our back
2402 * -> ask driver for current freq and notify governors about a change
2403 */
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002404 if (cpufreq_driver->get && !cpufreq_driver->setpolicy &&
2405 (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy))))
2406 goto unlock;
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002407
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002408 pr_debug("updating policy for CPU %u\n", cpu);
2409 memcpy(&new_policy, policy, sizeof(*policy));
2410 new_policy.min = policy->user_policy.min;
2411 new_policy.max = policy->user_policy.max;
Thomas Renninger0961dd02006-01-26 18:46:33 +01002412
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002413 cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002415unlock:
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002416 cpufreq_cpu_release(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417}
2418EXPORT_SYMBOL(cpufreq_update_policy);
2419
Rafael J. Wysocki5a25e3f2019-03-26 12:15:13 +01002420/**
2421 * cpufreq_update_limits - Update policy limits for a given CPU.
2422 * @cpu: CPU to update the policy limits for.
2423 *
2424 * Invoke the driver's ->update_limits callback if present or call
2425 * cpufreq_update_policy() for @cpu.
2426 */
2427void cpufreq_update_limits(unsigned int cpu)
2428{
2429 if (cpufreq_driver->update_limits)
2430 cpufreq_driver->update_limits(cpu);
2431 else
2432 cpufreq_update_policy(cpu);
2433}
2434EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002437 * BOOST *
2438 *********************************************************************/
2439static int cpufreq_boost_set_sw(int state)
2440{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002441 struct cpufreq_policy *policy;
2442 int ret = -EINVAL;
2443
Viresh Kumarf9637352015-05-12 12:20:11 +05302444 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302445 if (!policy->freq_table)
2446 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302447
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302448 ret = cpufreq_frequency_table_cpuinfo(policy,
2449 policy->freq_table);
2450 if (ret) {
2451 pr_err("%s: Policy frequency update failed\n",
2452 __func__);
2453 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002454 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302455
2456 down_write(&policy->rwsem);
2457 policy->user_policy.max = policy->max;
2458 cpufreq_governor_limits(policy);
2459 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002460 }
2461
2462 return ret;
2463}
2464
2465int cpufreq_boost_trigger_state(int state)
2466{
2467 unsigned long flags;
2468 int ret = 0;
2469
2470 if (cpufreq_driver->boost_enabled == state)
2471 return 0;
2472
2473 write_lock_irqsave(&cpufreq_driver_lock, flags);
2474 cpufreq_driver->boost_enabled = state;
2475 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2476
2477 ret = cpufreq_driver->set_boost(state);
2478 if (ret) {
2479 write_lock_irqsave(&cpufreq_driver_lock, flags);
2480 cpufreq_driver->boost_enabled = !state;
2481 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2482
Joe Perchese837f9b2014-03-11 10:03:00 -07002483 pr_err("%s: Cannot %s BOOST\n",
2484 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002485 }
2486
2487 return ret;
2488}
2489
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002490static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002491{
Yue Hu89f98d72019-04-09 10:25:36 +08002492 return cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002493}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002494
Viresh Kumar44139ed2015-07-29 16:23:09 +05302495static int create_boost_sysfs_file(void)
2496{
2497 int ret;
2498
Viresh Kumarc82bd442015-10-15 21:35:23 +05302499 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302500 if (ret)
2501 pr_err("%s: cannot register global BOOST sysfs file\n",
2502 __func__);
2503
2504 return ret;
2505}
2506
2507static void remove_boost_sysfs_file(void)
2508{
2509 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302510 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302511}
2512
2513int cpufreq_enable_boost_support(void)
2514{
2515 if (!cpufreq_driver)
2516 return -EINVAL;
2517
2518 if (cpufreq_boost_supported())
2519 return 0;
2520
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002521 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302522
2523 /* This will get removed on driver unregister */
2524 return create_boost_sysfs_file();
2525}
2526EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2527
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002528int cpufreq_boost_enabled(void)
2529{
2530 return cpufreq_driver->boost_enabled;
2531}
2532EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2533
2534/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2536 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002537static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Chen Yuc4a3fa22017-04-09 13:45:16 +08002539static int cpuhp_cpufreq_online(unsigned int cpu)
2540{
2541 cpufreq_online(cpu);
2542
2543 return 0;
2544}
2545
2546static int cpuhp_cpufreq_offline(unsigned int cpu)
2547{
2548 cpufreq_offline(cpu);
2549
2550 return 0;
2551}
2552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553/**
2554 * cpufreq_register_driver - register a CPU Frequency driver
2555 * @driver_data: A struct cpufreq_driver containing the values#
2556 * submitted by the CPU Frequency driver.
2557 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302558 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002559 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002560 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 *
2562 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002563int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 unsigned long flags;
2566 int ret;
2567
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002568 if (cpufreq_disabled())
2569 return -ENODEV;
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302572 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002573 driver_data->target) ||
2574 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302575 driver_data->target)) ||
Viresh Kumara9a22b52019-02-14 16:16:21 +05302576 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
Viresh Kumar91a12e92019-02-12 16:36:04 +05302577 (!driver_data->online != !driver_data->offline))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 return -EINVAL;
2579
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002580 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002582 /* Protect against concurrent CPU online/offline. */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002583 cpus_read_lock();
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002584
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002585 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002586 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002587 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002588 ret = -EEXIST;
2589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002591 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002592 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302594 if (driver_data->setpolicy)
2595 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2596
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002597 if (cpufreq_boost_supported()) {
2598 ret = create_boost_sysfs_file();
2599 if (ret)
2600 goto err_null_driver;
2601 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002602
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002603 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002604 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002605 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302607 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2608 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /* if all ->init() calls failed, unregister */
David Arcari6c770032017-05-26 11:37:31 -04002610 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302611 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2612 driver_data->name);
2613 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002616 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2617 "cpufreq:online",
2618 cpuhp_cpufreq_online,
2619 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002620 if (ret < 0)
2621 goto err_if_unreg;
2622 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002623 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002624
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002625 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002626 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002627
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002628err_if_unreg:
2629 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002630err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302631 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002632err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002633 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002634 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002635 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002636out:
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002637 cpus_read_unlock();
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002638 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639}
2640EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642/**
2643 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2644 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302645 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 * the right to do so, i.e. if you have succeeded in initialising before!
2647 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2648 * currently not initialised.
2649 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002650int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 unsigned long flags;
2653
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002654 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002657 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002659 /* Protect against concurrent cpu hotplug */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002660 cpus_read_lock();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002661 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302662 remove_boost_sysfs_file();
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002663 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002665 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302666
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002667 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302668
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002669 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002670 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
2672 return 0;
2673}
2674EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002675
Doug Anderson90de2a42014-12-23 22:09:48 -08002676/*
2677 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2678 * or mutexes when secondary CPUs are halted.
2679 */
2680static struct syscore_ops cpufreq_syscore_ops = {
2681 .shutdown = cpufreq_suspend,
2682};
2683
Viresh Kumarc82bd442015-10-15 21:35:23 +05302684struct kobject *cpufreq_global_kobject;
2685EXPORT_SYMBOL(cpufreq_global_kobject);
2686
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002687static int __init cpufreq_core_init(void)
2688{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002689 if (cpufreq_disabled())
2690 return -ENODEV;
2691
Viresh Kumar8eec1022015-10-15 21:35:22 +05302692 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002693 BUG_ON(!cpufreq_global_kobject);
2694
Doug Anderson90de2a42014-12-23 22:09:48 -08002695 register_syscore_ops(&cpufreq_syscore_ops);
2696
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002697 return 0;
2698}
Len Brownd82f2692017-02-28 16:44:16 -05002699module_param(off, int, 0444);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002700core_initcall(cpufreq_core_init);