blob: d8fc395af7737af030a4043b9025f25d9539092c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
Amit Kucheria5c238a82019-01-30 10:52:01 +053022#include <linux/cpu_cooling.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053025#include <linux/init.h>
26#include <linux/kernel_stat.h>
27#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080028#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053029#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080030#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080031#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053032#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020033#include <trace/events/power.h>
34
Viresh Kumarb4f06762015-01-27 14:06:08 +053035static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053036
37static inline bool policy_is_inactive(struct cpufreq_policy *policy)
38{
39 return cpumask_empty(policy->cpus);
40}
41
Viresh Kumarf9637352015-05-12 12:20:11 +053042/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060043#define for_each_suitable_policy(__policy, __active) \
44 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
45 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053046
47#define for_each_active_policy(__policy) \
48 for_each_suitable_policy(__policy, true)
49#define for_each_inactive_policy(__policy) \
50 for_each_suitable_policy(__policy, false)
51
52#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053053 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
54
Viresh Kumarf7b27062015-01-27 14:06:09 +053055/* Iterate over governors */
56static LIST_HEAD(cpufreq_governor_list);
57#define for_each_governor(__governor) \
58 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/**
Dave Jonescd878472006-08-11 17:59:28 -040061 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 * level driver of CPUFreq support, and its spinlock. This lock
63 * also protects the cpufreq_cpu_data array.
64 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020065static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070066static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053067static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053068
Viresh Kumar2f0aea92014-03-04 11:00:26 +080069/* Flag to suspend/resume CPUFreq governors */
70static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053072static inline bool has_target(void)
73{
74 return cpufreq_driver->target_index || cpufreq_driver->target;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053078static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020079static int cpufreq_init_governor(struct cpufreq_policy *policy);
80static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010081static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020082static void cpufreq_stop_governor(struct cpufreq_policy *policy);
83static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
Dave Jones32ee8c32006-02-28 00:43:23 -050086 * Two notifier lists: the "policy" list is involved in the
87 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * "transition" list for kernel code that needs to handle
89 * changes to devices when the CPU clock speed changes.
90 * The mutex locks both lists.
91 */
Alan Sterne041c682006-03-27 01:16:30 -080092static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Sebastian Andrzej Siewiorcc85de32018-05-25 12:19:58 +020093SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040095static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020096static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040097{
98 return off;
99}
100void disable_cpufreq(void)
101{
102 off = 1;
103}
Dave Jones29464f22009-01-18 01:37:11 -0500104static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000106bool have_governor_per_policy(void)
107{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530108 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000109}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000110EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000111
Viresh Kumar944e9a02013-05-16 05:09:57 +0000112struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
113{
114 if (have_governor_per_policy())
115 return &policy->kobj;
116 else
117 return cpufreq_global_kobject;
118}
119EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
120
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000121static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
122{
123 u64 idle_time;
124 u64 cur_wall_time;
125 u64 busy_time;
126
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100127 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000128
129 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
130 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
131 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
132 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
133 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
134 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
135
136 idle_time = cur_wall_time - busy_time;
137 if (wall)
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100138 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000139
Frederic Weisbecker7fb13272017-01-31 04:09:19 +0100140 return div_u64(idle_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000141}
142
143u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
144{
145 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
146
147 if (idle_time == -1ULL)
148 return get_cpu_idle_time_jiffy(cpu, wall);
149 else if (!io_busy)
150 idle_time += get_cpu_iowait_time_us(cpu, wall);
151
152 return idle_time;
153}
154EXPORT_SYMBOL_GPL(get_cpu_idle_time);
155
Dietmar Eggemanne7d54592017-09-26 17:41:07 +0100156__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
157 unsigned long max_freq)
158{
159}
160EXPORT_SYMBOL_GPL(arch_set_freq_scale);
161
Viresh Kumar70e9e772013-10-03 20:29:07 +0530162/*
163 * This is a generic cpufreq init() routine which can be used by cpufreq
164 * drivers of SMP systems. It will do following:
165 * - validate & show freq table passed
166 * - set policies transition latency
167 * - policy->cpus with all possible CPUs
168 */
169int cpufreq_generic_init(struct cpufreq_policy *policy,
170 struct cpufreq_frequency_table *table,
171 unsigned int transition_latency)
172{
Viresh Kumar92c99d12018-02-26 10:38:45 +0530173 policy->freq_table = table;
Viresh Kumar70e9e772013-10-03 20:29:07 +0530174 policy->cpuinfo.transition_latency = transition_latency;
175
176 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530177 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530178 * share the clock and voltage and clock.
179 */
180 cpumask_setall(policy->cpus);
181
182 return 0;
183}
184EXPORT_SYMBOL_GPL(cpufreq_generic_init);
185
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200186struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530187{
188 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
189
Viresh Kumar988bed02015-05-08 11:53:45 +0530190 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
191}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200192EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530193
194unsigned int cpufreq_generic_get(unsigned int cpu)
195{
196 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
197
Viresh Kumar652ed952014-01-09 20:38:43 +0530198 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700199 pr_err("%s: No %s associated to cpu: %d\n",
200 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530201 return 0;
202 }
203
204 return clk_get_rate(policy->clk) / 1000;
205}
206EXPORT_SYMBOL_GPL(cpufreq_generic_get);
207
Viresh Kumar50e9c852015-02-19 17:02:03 +0530208/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100209 * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
210 * @cpu: CPU to find the policy for.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530211 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100212 * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
213 * the kobject reference counter of that policy. Return a valid policy on
214 * success or NULL on failure.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530215 *
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100216 * The policy returned by this function has to be released with the help of
217 * cpufreq_cpu_put() to balance its kobject reference counter properly.
Viresh Kumar50e9c852015-02-19 17:02:03 +0530218 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530219struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530221 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 unsigned long flags;
223
Viresh Kumar1b947c902015-02-19 17:02:05 +0530224 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530225 return NULL;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000228 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Viresh Kumar6eed9402013-08-06 22:53:11 +0530230 if (cpufreq_driver) {
231 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530232 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530233 if (policy)
234 kobject_get(&policy->kobj);
235 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200236
Viresh Kumar6eed9402013-08-06 22:53:11 +0530237 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530239 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000240}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
242
Viresh Kumar50e9c852015-02-19 17:02:03 +0530243/**
Rafael J. Wysocki5d094fe2019-03-05 11:44:04 +0100244 * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
245 * @policy: cpufreq policy returned by cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530246 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530247void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530249 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
252
Rafael J. Wysocki540a37582019-03-26 12:16:58 +0100253/**
254 * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
255 * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
256 */
257static void cpufreq_cpu_release(struct cpufreq_policy *policy)
258{
259 if (WARN_ON(!policy))
260 return;
261
262 lockdep_assert_held(&policy->rwsem);
263
264 up_write(&policy->rwsem);
265
266 cpufreq_cpu_put(policy);
267}
268
269/**
270 * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
271 * @cpu: CPU to find the policy for.
272 *
273 * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
274 * if the policy returned by it is not NULL, acquire its rwsem for writing.
275 * Return the policy if it is active or release it and return NULL otherwise.
276 *
277 * The policy returned by this function has to be released with the help of
278 * cpufreq_cpu_release() in order to release its rwsem and balance its usage
279 * counter properly.
280 */
281static struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
282{
283 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
284
285 if (!policy)
286 return NULL;
287
288 down_write(&policy->rwsem);
289
290 if (policy_is_inactive(policy)) {
291 cpufreq_cpu_release(policy);
292 return NULL;
293 }
294
295 return policy;
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
300 *********************************************************************/
301
302/**
303 * adjust_jiffies - adjust the system "loops_per_jiffy"
304 *
305 * This function alters the system "loops_per_jiffy" for the clock
306 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500307 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 * per-CPU loops_per_jiffy value wherever possible.
309 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800310static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530312#ifndef CONFIG_SMP
313 static unsigned long l_p_j_ref;
314 static unsigned int l_p_j_ref_freq;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (ci->flags & CPUFREQ_CONST_LOOPS)
317 return;
318
319 if (!l_p_j_ref_freq) {
320 l_p_j_ref = loops_per_jiffy;
321 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700322 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
323 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530325 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530326 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
327 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700328 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
329 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Viresh Kumar20b53242018-05-10 15:00:29 +0530334/**
335 * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
336 * @policy: cpufreq policy to enable fast frequency switching for.
337 * @freqs: contain details of the frequency update.
338 * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
339 *
340 * This function calls the transition notifiers and the "adjust_jiffies"
341 * function. It is called twice on all CPU frequency changes that have
342 * external effects.
343 */
344static void cpufreq_notify_transition(struct cpufreq_policy *policy,
345 struct cpufreq_freqs *freqs,
346 unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
348 BUG_ON(irqs_disabled());
349
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000350 if (cpufreq_disabled())
351 return;
352
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200353 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200354 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700355 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 switch (state) {
358 case CPUFREQ_PRECHANGE:
Viresh Kumar20b53242018-05-10 15:00:29 +0530359 /*
360 * Detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800361 * which is not equal to what the cpufreq core thinks is
362 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200364 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Viresh Kumar20b53242018-05-10 15:00:29 +0530365 if (policy->cur && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700366 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
367 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800368 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370 }
Viresh Kumar20b53242018-05-10 15:00:29 +0530371
372 for_each_cpu(freqs->cpu, policy->cpus) {
373 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
374 CPUFREQ_PRECHANGE, freqs);
375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
378 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 case CPUFREQ_POSTCHANGE:
381 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Viresh Kumar20b53242018-05-10 15:00:29 +0530382 pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
383 cpumask_pr_args(policy->cpus));
Viresh Kumarbb176f72013-06-19 14:19:33 +0530384
Viresh Kumar20b53242018-05-10 15:00:29 +0530385 for_each_cpu(freqs->cpu, policy->cpus) {
386 trace_cpu_frequency(freqs->new, freqs->cpu);
387 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
388 CPUFREQ_POSTCHANGE, freqs);
389 }
390
391 cpufreq_stats_record_transition(policy, freqs->new);
392 policy->cur = freqs->new;
393 }
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530394}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530396/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530397static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530398 struct cpufreq_freqs *freqs, int transition_failed)
399{
400 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
401 if (!transition_failed)
402 return;
403
404 swap(freqs->old, freqs->new);
405 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
406 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
407}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530408
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530409void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
410 struct cpufreq_freqs *freqs)
411{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530412
413 /*
414 * Catch double invocations of _begin() which lead to self-deadlock.
415 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
416 * doesn't invoke _begin() on their behalf, and hence the chances of
417 * double invocations are very low. Moreover, there are scenarios
418 * where these checks can emit false-positive warnings in these
419 * drivers; so we avoid that by skipping them altogether.
420 */
421 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
422 && current == policy->transition_task);
423
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530424wait:
425 wait_event(policy->transition_wait, !policy->transition_ongoing);
426
427 spin_lock(&policy->transition_lock);
428
429 if (unlikely(policy->transition_ongoing)) {
430 spin_unlock(&policy->transition_lock);
431 goto wait;
432 }
433
434 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530435 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530436
437 spin_unlock(&policy->transition_lock);
438
439 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
440}
441EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
442
443void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
444 struct cpufreq_freqs *freqs, int transition_failed)
445{
Igor Stoppa0e7ea2f2018-09-07 19:09:55 +0300446 if (WARN_ON(!policy->transition_ongoing))
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530447 return;
448
449 cpufreq_notify_post_transition(policy, freqs, transition_failed);
450
451 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530452 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530453
454 wake_up(&policy->transition_wait);
455}
456EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
457
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200458/*
459 * Fast frequency switching status count. Positive means "enabled", negative
460 * means "disabled" and 0 means "not decided yet".
461 */
462static int cpufreq_fast_switch_count;
463static DEFINE_MUTEX(cpufreq_fast_switch_lock);
464
465static void cpufreq_list_transition_notifiers(void)
466{
467 struct notifier_block *nb;
468
469 pr_info("Registered transition notifiers:\n");
470
471 mutex_lock(&cpufreq_transition_notifier_list.mutex);
472
473 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
474 pr_info("%pF\n", nb->notifier_call);
475
476 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
477}
478
479/**
480 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
481 * @policy: cpufreq policy to enable fast frequency switching for.
482 *
483 * Try to enable fast frequency switching for @policy.
484 *
485 * The attempt will fail if there is at least one transition notifier registered
486 * at this point, as fast frequency switching is quite fundamentally at odds
487 * with transition notifiers. Thus if successful, it will make registration of
488 * transition notifiers fail going forward.
489 */
490void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
491{
492 lockdep_assert_held(&policy->rwsem);
493
494 if (!policy->fast_switch_possible)
495 return;
496
497 mutex_lock(&cpufreq_fast_switch_lock);
498 if (cpufreq_fast_switch_count >= 0) {
499 cpufreq_fast_switch_count++;
500 policy->fast_switch_enabled = true;
501 } else {
502 pr_warn("CPU%u: Fast frequency switching not enabled\n",
503 policy->cpu);
504 cpufreq_list_transition_notifiers();
505 }
506 mutex_unlock(&cpufreq_fast_switch_lock);
507}
508EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
509
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200510/**
511 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
512 * @policy: cpufreq policy to disable fast frequency switching for.
513 */
514void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200515{
516 mutex_lock(&cpufreq_fast_switch_lock);
517 if (policy->fast_switch_enabled) {
518 policy->fast_switch_enabled = false;
519 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
520 cpufreq_fast_switch_count--;
521 }
522 mutex_unlock(&cpufreq_fast_switch_lock);
523}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200524EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Steve Mucklee3c06232016-07-13 13:25:25 -0700526/**
527 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
528 * one.
529 * @target_freq: target frequency to resolve.
530 *
531 * The target to driver frequency mapping is cached in the policy.
532 *
533 * Return: Lowest driver-supported frequency greater than or equal to the
534 * given target_freq, subject to policy (min/max) and driver limitations.
535 */
536unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
537 unsigned int target_freq)
538{
539 target_freq = clamp_val(target_freq, policy->min, policy->max);
540 policy->cached_target_freq = target_freq;
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700541
542 if (cpufreq_driver->target_index) {
543 int idx;
544
545 idx = cpufreq_frequency_table_target(policy, target_freq,
546 CPUFREQ_RELATION_L);
547 policy->cached_resolved_idx = idx;
548 return policy->freq_table[idx].frequency;
549 }
550
Steve Mucklee3c06232016-07-13 13:25:25 -0700551 if (cpufreq_driver->resolve_freq)
552 return cpufreq_driver->resolve_freq(policy, target_freq);
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700553
554 return target_freq;
Steve Mucklee3c06232016-07-13 13:25:25 -0700555}
Steve Muckleae2c1ca2016-07-21 19:24:38 -0700556EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
Steve Mucklee3c06232016-07-13 13:25:25 -0700557
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530558unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
559{
560 unsigned int latency;
561
562 if (policy->transition_delay_us)
563 return policy->transition_delay_us;
564
565 latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
Viresh Kumare948bc82017-08-17 09:12:27 +0530566 if (latency) {
567 /*
568 * For platforms that can change the frequency very fast (< 10
569 * us), the above formula gives a decent transition delay. But
570 * for platforms where transition_latency is in milliseconds, it
571 * ends up giving unrealistic values.
572 *
573 * Cap the default transition delay to 10 ms, which seems to be
574 * a reasonable amount of time after which we should reevaluate
575 * the frequency.
576 */
577 return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
578 }
Viresh Kumaraa7519a2017-07-19 15:42:42 +0530579
580 return LATENCY_MULTIPLIER;
581}
582EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/*********************************************************************
585 * SYSFS INTERFACE *
586 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530587static ssize_t show_boost(struct kobject *kobj,
Viresh Kumar625c85a2019-01-25 12:53:07 +0530588 struct kobj_attribute *attr, char *buf)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100589{
590 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
591}
592
Viresh Kumar625c85a2019-01-25 12:53:07 +0530593static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
594 const char *buf, size_t count)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100595{
596 int ret, enable;
597
598 ret = sscanf(buf, "%d", &enable);
599 if (ret != 1 || enable < 0 || enable > 1)
600 return -EINVAL;
601
602 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700603 pr_err("%s: Cannot %s BOOST!\n",
604 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100605 return -EINVAL;
606 }
607
Joe Perchese837f9b2014-03-11 10:03:00 -0700608 pr_debug("%s: cpufreq BOOST %s\n",
609 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100610
611 return count;
612}
613define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530615static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700616{
617 struct cpufreq_governor *t;
618
Viresh Kumarf7b27062015-01-27 14:06:09 +0530619 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200620 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700621 return t;
622
623 return NULL;
624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/**
627 * cpufreq_parse_governor - parse a governor string
628 */
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100629static int cpufreq_parse_governor(char *str_governor,
630 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200632 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200633 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100634 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100635 return 0;
636 }
637
638 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100639 policy->policy = CPUFREQ_POLICY_POWERSAVE;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100640 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530642 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700644
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800645 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700646
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530647 t = find_governor(str_governor);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100648 if (!t) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700649 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700650
Kees Cook1a8e1462011-05-04 08:38:56 -0700651 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100652
Kees Cook1a8e1462011-05-04 08:38:56 -0700653 ret = request_module("cpufreq_%s", str_governor);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100654 if (ret)
655 return -EINVAL;
656
Kees Cook1a8e1462011-05-04 08:38:56 -0700657 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700658
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100659 t = find_governor(str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100661 if (t && !try_module_get(t->owner))
662 t = NULL;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700663
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800664 mutex_unlock(&cpufreq_governor_mutex);
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100665
666 if (t) {
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100667 policy->governor = t;
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100668 return 0;
669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Rafael J. Wysocki045149e2017-11-23 01:23:16 +0100671
672 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530676 * cpufreq_per_cpu_attr_read() / show_##file_name() -
677 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 *
679 * Write out information from cpufreq_driver->policy[cpu]; object must be
680 * "unsigned int".
681 */
682
Dave Jones32ee8c32006-02-28 00:43:23 -0500683#define show_one(file_name, object) \
684static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500685(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500686{ \
Dave Jones29464f22009-01-18 01:37:11 -0500687 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690show_one(cpuinfo_min_freq, cpuinfo.min_freq);
691show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100692show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693show_one(scaling_min_freq, min);
694show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700695
Len Brownf8475ce2017-06-23 22:11:52 -0700696__weak unsigned int arch_freq_get_on_cpu(int cpu)
697{
698 return 0;
699}
700
Viresh Kumar09347b22015-01-02 12:34:24 +0530701static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700702{
703 ssize_t ret;
Len Brownf8475ce2017-06-23 22:11:52 -0700704 unsigned int freq;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700705
Len Brownf8475ce2017-06-23 22:11:52 -0700706 freq = arch_freq_get_on_cpu(policy->cpu);
707 if (freq)
708 ret = sprintf(buf, "%u\n", freq);
709 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
710 cpufreq_driver->get)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700711 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
712 else
713 ret = sprintf(buf, "%u\n", policy->cur);
714 return ret;
715}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Viresh Kumar037ce832013-10-02 14:13:16 +0530717static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530718 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/**
721 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
722 */
723#define store_one(file_name, object) \
724static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500725(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800727 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 struct cpufreq_policy new_policy; \
729 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530730 memcpy(&new_policy, policy, sizeof(*policy)); \
Tao Wangc7d1f112018-05-26 15:16:48 +0800731 new_policy.min = policy->user_policy.min; \
732 new_policy.max = policy->user_policy.max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 \
Dave Jones29464f22009-01-18 01:37:11 -0500734 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (ret != 1) \
736 return -EINVAL; \
737 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800738 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530739 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800740 if (!ret) \
741 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 \
743 return ret ? ret : count; \
744}
745
Dave Jones29464f22009-01-18 01:37:11 -0500746store_one(scaling_min_freq, min);
747store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/**
750 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
751 */
Dave Jones905d77c2008-03-05 14:28:32 -0500752static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
753 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530755 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysocki9b4f6032017-03-15 00:12:16 +0100756
757 if (cur_freq)
758 return sprintf(buf, "%u\n", cur_freq);
759
760 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/**
764 * show_scaling_governor - show the current policy for the specified CPU
765 */
Dave Jones905d77c2008-03-05 14:28:32 -0500766static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Dave Jones29464f22009-01-18 01:37:11 -0500768 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return sprintf(buf, "powersave\n");
770 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
771 return sprintf(buf, "performance\n");
772 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200773 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500774 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return -EINVAL;
776}
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/**
779 * store_scaling_governor - store policy for the specified CPU
780 */
Dave Jones905d77c2008-03-05 14:28:32 -0500781static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
782 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530784 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 char str_governor[16];
786 struct cpufreq_policy new_policy;
787
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530788 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Jones29464f22009-01-18 01:37:11 -0500790 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (ret != 1)
792 return -EINVAL;
793
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +0100794 if (cpufreq_parse_governor(str_governor, &new_policy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EINVAL;
796
Viresh Kumar037ce832013-10-02 14:13:16 +0530797 ret = cpufreq_set_policy(policy, &new_policy);
Rafael J. Wysockia8b149d2017-11-23 14:27:07 +0100798
799 if (new_policy.governor)
800 module_put(new_policy.governor->owner);
801
Viresh Kumar88dc4382015-08-03 08:36:18 +0530802 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805/**
806 * show_scaling_driver - show the cpufreq driver currently loaded
807 */
Dave Jones905d77c2008-03-05 14:28:32 -0500808static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200810 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
812
813/**
814 * show_scaling_available_governors - show the available CPUfreq governors
815 */
Dave Jones905d77c2008-03-05 14:28:32 -0500816static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
817 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 ssize_t i = 0;
820 struct cpufreq_governor *t;
821
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530822 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 i += sprintf(buf, "performance powersave");
824 goto out;
825 }
826
Viresh Kumarf7b27062015-01-27 14:06:09 +0530827 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500828 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
829 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200831 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500833out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 i += sprintf(&buf[i], "\n");
835 return i;
836}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700837
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800838ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 ssize_t i = 0;
841 unsigned int cpu;
842
Rusty Russell835481d2009-01-04 05:18:06 -0800843 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (i)
845 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
846 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
847 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500848 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850 i += sprintf(&buf[i], "\n");
851 return i;
852}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800853EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700855/**
856 * show_related_cpus - show the CPUs affected by each transition even if
857 * hw coordination is in use
858 */
859static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
860{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800861 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700862}
863
864/**
865 * show_affected_cpus - show the CPUs affected by each transition
866 */
867static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
868{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800869 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700870}
871
Venki Pallipadi9e769882007-10-26 10:18:21 -0700872static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500873 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700874{
875 unsigned int freq = 0;
876 unsigned int ret;
877
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700878 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700879 return -EINVAL;
880
881 ret = sscanf(buf, "%u", &freq);
882 if (ret != 1)
883 return -EINVAL;
884
885 policy->governor->store_setspeed(policy, freq);
886
887 return count;
888}
889
890static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
891{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700892 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700893 return sprintf(buf, "<unsupported>\n");
894
895 return policy->governor->show_setspeed(policy, buf);
896}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Thomas Renningere2f74f32009-11-19 12:31:01 +0100898/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200899 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100900 */
901static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
902{
903 unsigned int limit;
904 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200905 if (cpufreq_driver->bios_limit) {
906 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100907 if (!ret)
908 return sprintf(buf, "%u\n", limit);
909 }
910 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
911}
912
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200913cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
914cpufreq_freq_attr_ro(cpuinfo_min_freq);
915cpufreq_freq_attr_ro(cpuinfo_max_freq);
916cpufreq_freq_attr_ro(cpuinfo_transition_latency);
917cpufreq_freq_attr_ro(scaling_available_governors);
918cpufreq_freq_attr_ro(scaling_driver);
919cpufreq_freq_attr_ro(scaling_cur_freq);
920cpufreq_freq_attr_ro(bios_limit);
921cpufreq_freq_attr_ro(related_cpus);
922cpufreq_freq_attr_ro(affected_cpus);
923cpufreq_freq_attr_rw(scaling_min_freq);
924cpufreq_freq_attr_rw(scaling_max_freq);
925cpufreq_freq_attr_rw(scaling_governor);
926cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Dave Jones905d77c2008-03-05 14:28:32 -0500928static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 &cpuinfo_min_freq.attr,
930 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100931 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 &scaling_min_freq.attr,
933 &scaling_max_freq.attr,
934 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700935 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 &scaling_governor.attr,
937 &scaling_driver.attr,
938 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700939 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 NULL
941};
942
Dave Jones29464f22009-01-18 01:37:11 -0500943#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
944#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dave Jones29464f22009-01-18 01:37:11 -0500946static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Dave Jones905d77c2008-03-05 14:28:32 -0500948 struct cpufreq_policy *policy = to_policy(kobj);
949 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530950 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530951
viresh kumarad7722d2013-10-18 19:10:15 +0530952 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100953 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530954 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return ret;
957}
958
Dave Jones905d77c2008-03-05 14:28:32 -0500959static ssize_t store(struct kobject *kobj, struct attribute *attr,
960 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Dave Jones905d77c2008-03-05 14:28:32 -0500962 struct cpufreq_policy *policy = to_policy(kobj);
963 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500964 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530965
Waiman Long9b3d9bb2018-07-24 14:26:05 -0400966 /*
967 * cpus_read_trylock() is used here to work around a circular lock
968 * dependency problem with respect to the cpufreq_register_driver().
969 */
970 if (!cpus_read_trylock())
971 return -EBUSY;
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530972
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100973 if (cpu_online(policy->cpu)) {
974 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530975 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100976 up_write(&policy->rwsem);
977 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530978
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +0200979 cpus_read_unlock();
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 return ret;
982}
983
Dave Jones905d77c2008-03-05 14:28:32 -0500984static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Dave Jones905d77c2008-03-05 14:28:32 -0500986 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200987 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 complete(&policy->kobj_unregister);
989}
990
Emese Revfy52cf25d2010-01-19 02:58:23 +0100991static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 .show = show,
993 .store = store,
994};
995
996static struct kobj_type ktype_cpufreq = {
997 .sysfs_ops = &sysfs_ops,
998 .default_attrs = default_attrs,
999 .release = cpufreq_sysfs_release,
1000};
1001
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001002static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +02001003{
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001004 struct device *dev = get_cpu_device(cpu);
1005
1006 if (!dev)
1007 return;
1008
1009 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1010 return;
1011
Viresh Kumar266198042016-09-12 12:07:05 +05301012 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001013 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1014 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +02001015}
1016
Viresh Kumar266198042016-09-12 12:07:05 +05301017static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1018 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +02001019{
Viresh Kumar266198042016-09-12 12:07:05 +05301020 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1021 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +02001022}
1023
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001024static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -04001025{
1026 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -04001027 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -04001028
Dave Jones909a6942009-07-08 18:05:42 -04001029 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001030 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +05301031 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -04001032 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1033 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001034 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001035 drv_attr++;
1036 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001037 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001038 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1039 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001040 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001041 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001042
1043 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1044 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001045 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001046
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001047 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001048 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1049 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001050 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001051 }
Dave Jones909a6942009-07-08 18:05:42 -04001052
Viresh Kumar266198042016-09-12 12:07:05 +05301053 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301054}
1055
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001056__weak struct cpufreq_governor *cpufreq_default_governor(void)
1057{
1058 return NULL;
1059}
1060
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301061static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301062{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001063 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301064 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301065
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301066 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001067
viresh kumar6e2c89d2014-03-04 11:43:59 +08001068 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +05301069 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001070 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +08001071 pr_debug("Restoring governor %s for cpu %d\n",
1072 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001073 } else {
1074 gov = cpufreq_default_governor();
1075 if (!gov)
1076 return -ENODATA;
1077 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001078
1079 new_policy.governor = gov;
1080
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001081 /* Use the default policy if there is no last_policy. */
1082 if (cpufreq_driver->setpolicy) {
1083 if (policy->last_policy)
1084 new_policy.policy = policy->last_policy;
1085 else
Rafael J. Wysockiae0ff892017-11-23 01:24:05 +01001086 cpufreq_parse_governor(gov->name, &new_policy);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001087 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001088 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301089 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001090}
1091
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001092static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001093{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301094 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001095
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301096 /* Has this CPU been taken care of already? */
1097 if (cpumask_test_cpu(cpu, policy->cpus))
1098 return 0;
1099
Viresh Kumar49f18562016-02-11 17:31:12 +05301100 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001101 if (has_target())
1102 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001103
Viresh Kumarfcf80582013-01-29 14:39:08 +00001104 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301105
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301106 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001107 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301108 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301109 pr_err("%s: Failed to start governor\n", __func__);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001110 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301111 up_write(&policy->rwsem);
1112 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001113}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301115static void handle_update(struct work_struct *work)
1116{
1117 struct cpufreq_policy *policy =
1118 container_of(work, struct cpufreq_policy, update);
1119 unsigned int cpu = policy->cpu;
1120 pr_debug("handle_update for cpu %u called\n", cpu);
1121 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001124static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301125{
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301126 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301127 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301128
1129 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1130 if (!policy)
1131 return NULL;
1132
1133 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1134 goto err_free_policy;
1135
1136 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1137 goto err_free_cpumask;
1138
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001139 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1140 goto err_free_rcpumask;
1141
Viresh Kumaredd4a892016-03-03 14:51:33 +05301142 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1143 cpufreq_global_kobject, "policy%u", cpu);
1144 if (ret) {
1145 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1146 goto err_free_real_cpus;
1147 }
1148
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301149 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301150 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301151 spin_lock_init(&policy->transition_lock);
1152 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301153 init_completion(&policy->kobj_unregister);
1154 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301155
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001156 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301157 return policy;
1158
Viresh Kumaredd4a892016-03-03 14:51:33 +05301159err_free_real_cpus:
1160 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301161err_free_rcpumask:
1162 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301163err_free_cpumask:
1164 free_cpumask_var(policy->cpus);
1165err_free_policy:
1166 kfree(policy);
1167
1168 return NULL;
1169}
1170
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301171static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301172{
1173 struct kobject *kobj;
1174 struct completion *cmp;
1175
Viresh Kumar87549142015-06-10 02:13:21 +02001176 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001177 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301178 kobj = &policy->kobj;
1179 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001180 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301181 kobject_put(kobj);
1182
1183 /*
1184 * We need to make sure that the underlying kobj is
1185 * actually not referenced anymore by anybody before we
1186 * proceed with unloading.
1187 */
1188 pr_debug("waiting for dropping of refcount\n");
1189 wait_for_completion(cmp);
1190 pr_debug("wait complete\n");
1191}
1192
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301193static void cpufreq_policy_free(struct cpufreq_policy *policy)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301194{
Viresh Kumar988bed02015-05-08 11:53:45 +05301195 unsigned long flags;
1196 int cpu;
1197
1198 /* Remove policy from list */
1199 write_lock_irqsave(&cpufreq_driver_lock, flags);
1200 list_del(&policy->policy_list);
1201
1202 for_each_cpu(cpu, policy->related_cpus)
1203 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1204 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1205
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301206 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001207 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301208 free_cpumask_var(policy->related_cpus);
1209 free_cpumask_var(policy->cpus);
1210 kfree(policy);
1211}
1212
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001213static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301215 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001216 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001218 unsigned int j;
1219 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001220
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001221 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301222
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301223 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301224 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001225 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301226 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001227 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001228 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001230 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001231 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001232 down_write(&policy->rwsem);
1233 policy->cpu = cpu;
1234 policy->governor = NULL;
1235 up_write(&policy->rwsem);
1236 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001237 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001238 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001239 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001240 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001241 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301242
Viresh Kumar91a12e92019-02-12 16:36:04 +05301243 if (!new_policy && cpufreq_driver->online) {
1244 ret = cpufreq_driver->online(policy);
1245 if (ret) {
1246 pr_debug("%s: %d: initialization failed\n", __func__,
1247 __LINE__);
1248 goto out_exit_policy;
1249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Viresh Kumar91a12e92019-02-12 16:36:04 +05301251 /* Recover policy->cpus using related_cpus */
1252 cpumask_copy(policy->cpus, policy->related_cpus);
1253 } else {
1254 cpumask_copy(policy->cpus, cpumask_of(cpu));
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001255
Viresh Kumar91a12e92019-02-12 16:36:04 +05301256 /*
1257 * Call driver. From then on the cpufreq must be able
1258 * to accept all calls to ->verify and ->setpolicy for this CPU.
1259 */
1260 ret = cpufreq_driver->init(policy);
1261 if (ret) {
1262 pr_debug("%s: %d: initialization failed\n", __func__,
1263 __LINE__);
1264 goto out_free_policy;
1265 }
Viresh Kumard417e062018-02-22 11:29:44 +05301266
Viresh Kumar91a12e92019-02-12 16:36:04 +05301267 ret = cpufreq_table_validate_and_sort(policy);
1268 if (ret)
1269 goto out_exit_policy;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001270
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001271 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301272 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001273 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001274
Viresh Kumar91a12e92019-02-12 16:36:04 +05301275 down_write(&policy->rwsem);
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001276 /*
1277 * affected cpus must always be the one, which are online. We aren't
1278 * managing offline cpus here.
1279 */
1280 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1281
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001282 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001283 policy->user_policy.min = policy->min;
1284 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001285
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001286 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301287 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001288 add_cpu_dev_symlink(policy, j);
1289 }
Viresh Kumarff010472017-03-21 11:36:06 +05301290 } else {
1291 policy->min = policy->user_policy.min;
1292 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301293 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301294
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001295 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301296 policy->cur = cpufreq_driver->get(policy->cpu);
1297 if (!policy->cur) {
1298 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumard417e062018-02-22 11:29:44 +05301299 goto out_destroy_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301300 }
1301 }
1302
Viresh Kumard3916692013-12-03 11:20:46 +05301303 /*
1304 * Sometimes boot loaders set CPU frequency to a value outside of
1305 * frequency table present with cpufreq core. In such cases CPU might be
1306 * unstable if it has to run on that frequency for long duration of time
1307 * and so its better to set it to a frequency which is specified in
1308 * freq-table. This also makes cpufreq stats inconsistent as
1309 * cpufreq-stats would fail to register because current frequency of CPU
1310 * isn't found in freq-table.
1311 *
1312 * Because we don't want this change to effect boot process badly, we go
1313 * for the next freq which is >= policy->cur ('cur' must be set by now,
1314 * otherwise we will end up setting freq to lowest of the table as 'cur'
1315 * is initialized to zero).
1316 *
1317 * We are passing target-freq as "policy->cur - 1" otherwise
1318 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1319 * equal to target-freq.
1320 */
1321 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1322 && has_target()) {
1323 /* Are we running at unknown frequency ? */
1324 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1325 if (ret == -EINVAL) {
1326 /* Warn user and fix it */
1327 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1328 __func__, policy->cpu, policy->cur);
1329 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1330 CPUFREQ_RELATION_L);
1331
1332 /*
1333 * Reaching here after boot in a few seconds may not
1334 * mean that system will remain stable at "unknown"
1335 * frequency for longer duration. Hence, a BUG_ON().
1336 */
1337 BUG_ON(ret);
1338 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1339 __func__, policy->cpu, policy->cur);
1340 }
1341 }
1342
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001343 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001344 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301345 if (ret)
Viresh Kumard417e062018-02-22 11:29:44 +05301346 goto out_destroy_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001347
1348 cpufreq_stats_create_table(policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001349
Viresh Kumar988bed02015-05-08 11:53:45 +05301350 write_lock_irqsave(&cpufreq_driver_lock, flags);
1351 list_add(&policy->policy_list, &cpufreq_policy_list);
1352 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1353 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301354
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301355 ret = cpufreq_init_policy(policy);
1356 if (ret) {
1357 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1358 __func__, cpu, ret);
Viresh Kumard417e062018-02-22 11:29:44 +05301359 goto out_destroy_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301360 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301361
Viresh Kumar4e97b632014-03-04 11:44:01 +08001362 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301363
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001364 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301365
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301366 /* Callback for handling stuff after policy is ready */
1367 if (cpufreq_driver->ready)
1368 cpufreq_driver->ready(policy);
1369
Amit Kucheria5c238a82019-01-30 10:52:01 +05301370 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1371 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV)
1372 policy->cdev = of_cpufreq_cooling_register(policy);
1373
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001374 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return 0;
1377
Viresh Kumard417e062018-02-22 11:29:44 +05301378out_destroy_policy:
Viresh Kumarb24b6472018-02-22 11:29:43 +05301379 for_each_cpu(j, policy->real_cpus)
1380 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1381
Prarit Bhargava7106e022014-09-10 10:12:08 -04001382 up_write(&policy->rwsem);
1383
Viresh Kumard417e062018-02-22 11:29:44 +05301384out_exit_policy:
Viresh Kumarda60ce92013-10-03 20:28:30 +05301385 if (cpufreq_driver->exit)
1386 cpufreq_driver->exit(policy);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001387
Viresh Kumar8101f992015-07-08 15:12:15 +05301388out_free_policy:
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301389 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 return ret;
1391}
1392
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001393/**
1394 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1395 * @dev: CPU device.
1396 * @sif: Subsystem interface structure pointer (not used)
1397 */
1398static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1399{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001400 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001401 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301402 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001403
1404 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1405
Viresh Kumar266198042016-09-12 12:07:05 +05301406 if (cpu_online(cpu)) {
1407 ret = cpufreq_online(cpu);
1408 if (ret)
1409 return ret;
1410 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001411
Viresh Kumar266198042016-09-12 12:07:05 +05301412 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001413 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001414 if (policy)
1415 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Rafael J. Wysocki2f0ba792017-03-27 19:33:09 +02001417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418}
1419
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001420static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301422 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301423 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001425 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Viresh Kumar988bed02015-05-08 11:53:45 +05301427 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301428 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001429 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Viresh Kumar49f18562016-02-11 17:31:12 +05301433 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001434 if (has_target())
1435 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001436
Viresh Kumar9591bec2015-06-10 02:20:23 +02001437 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301438
Viresh Kumar9591bec2015-06-10 02:20:23 +02001439 if (policy_is_inactive(policy)) {
1440 if (has_target())
1441 strncpy(policy->last_governor, policy->governor->name,
1442 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd12015-12-01 16:52:14 -08001443 else
1444 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001445 } else if (cpu == policy->cpu) {
1446 /* Nominate new CPU */
1447 policy->cpu = cpumask_any(policy->cpus);
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Viresh Kumar9591bec2015-06-10 02:20:23 +02001450 /* Start governor again for active policy */
1451 if (!policy_is_inactive(policy)) {
1452 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001453 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001454 if (ret)
1455 pr_err("%s: Failed to start governor\n", __func__);
1456 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301457
Viresh Kumar49f18562016-02-11 17:31:12 +05301458 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301459 }
1460
Amit Kucheria5c238a82019-01-30 10:52:01 +05301461 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1462 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) {
1463 cpufreq_cooling_unregister(policy->cdev);
1464 policy->cdev = NULL;
1465 }
1466
Viresh Kumar69cee712016-02-11 17:31:11 +05301467 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001468 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001470 if (has_target())
1471 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001472
Viresh Kumar87549142015-06-10 02:13:21 +02001473 /*
Viresh Kumar91a12e92019-02-12 16:36:04 +05301474 * Perform the ->offline() during light-weight tear-down, as
1475 * that allows fast recovery when the CPU comes back.
Viresh Kumar87549142015-06-10 02:13:21 +02001476 */
Viresh Kumar91a12e92019-02-12 16:36:04 +05301477 if (cpufreq_driver->offline) {
1478 cpufreq_driver->offline(policy);
1479 } else if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001480 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001481 policy->freq_table = NULL;
1482 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301483
1484unlock:
1485 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301489/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301490 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301491 *
1492 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301493 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301494static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001495{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001496 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001497 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001498
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001499 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001500 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001501
Viresh Kumar69cee712016-02-11 17:31:11 +05301502 if (cpu_online(cpu))
1503 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001504
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001505 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301506 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301507
Viresh Kumar91a12e92019-02-12 16:36:04 +05301508 if (cpumask_empty(policy->real_cpus)) {
1509 /* We did light-weight exit earlier, do full tear down now */
1510 if (cpufreq_driver->offline)
1511 cpufreq_driver->exit(policy);
1512
Viresh Kumarf9f41e3e2017-01-05 10:17:27 +05301513 cpufreq_policy_free(policy);
Viresh Kumar91a12e92019-02-12 16:36:04 +05301514 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301518 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1519 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301520 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 * @new_freq: CPU frequency the CPU actually runs at
1522 *
Dave Jones29464f22009-01-18 01:37:11 -05001523 * We adjust to current frequency first, and need to clean up later.
1524 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301526static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301527 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528{
1529 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301530
Joe Perchese837f9b2014-03-11 10:03:00 -07001531 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 +05301532 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301534 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301536
Viresh Kumar8fec0512014-03-24 13:35:45 +05301537 cpufreq_freq_transition_begin(policy, &freqs);
1538 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Dave Jones32ee8c32006-02-28 00:43:23 -05001541/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301542 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001543 * @cpu: CPU number
1544 *
1545 * This is the last known freq, without actually getting it from the driver.
1546 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1547 */
1548unsigned int cpufreq_quick_get(unsigned int cpu)
1549{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001550 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301551 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001552 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001553
Richard Cochranc75361c2016-03-11 09:43:07 +01001554 read_lock_irqsave(&cpufreq_driver_lock, flags);
1555
1556 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1557 ret_freq = cpufreq_driver->get(cpu);
1558 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1559 return ret_freq;
1560 }
1561
1562 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001563
1564 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001565 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301566 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001567 cpufreq_cpu_put(policy);
1568 }
1569
Dave Jones4d34a672008-02-07 16:33:49 -05001570 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001571}
1572EXPORT_SYMBOL(cpufreq_quick_get);
1573
Jesse Barnes3d737102011-06-28 10:59:12 -07001574/**
1575 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1576 * @cpu: CPU number
1577 *
1578 * Just return the max possible frequency for a given CPU.
1579 */
1580unsigned int cpufreq_quick_get_max(unsigned int cpu)
1581{
1582 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1583 unsigned int ret_freq = 0;
1584
1585 if (policy) {
1586 ret_freq = policy->max;
1587 cpufreq_cpu_put(policy);
1588 }
1589
1590 return ret_freq;
1591}
1592EXPORT_SYMBOL(cpufreq_quick_get_max);
1593
Viresh Kumard92d50a2015-01-02 12:34:29 +05301594static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301596 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Sudeep Holla2f661962019-01-07 18:51:53 +00001598 if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001599 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Viresh Kumard92d50a2015-01-02 12:34:29 +05301601 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001603 /*
Sudeep Holla2f661962019-01-07 18:51:53 +00001604 * If fast frequency switching is used with the given policy, the check
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001605 * against policy->cur is pointless, so skip it in that case too.
1606 */
Sudeep Holla2f661962019-01-07 18:51:53 +00001607 if (policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001608 return ret_freq;
1609
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301610 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001611 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301612 /* verify no discrepancy between actual and
1613 saved value exists */
1614 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301615 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 schedule_work(&policy->update);
1617 }
1618 }
1619
Dave Jones4d34a672008-02-07 16:33:49 -05001620 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001621}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001623/**
1624 * cpufreq_get - get the current CPU frequency (in kHz)
1625 * @cpu: CPU number
1626 *
1627 * Get the CPU current (static) CPU frequency
1628 */
1629unsigned int cpufreq_get(unsigned int cpu)
1630{
Aaron Plattner999976e2014-03-04 12:42:15 -08001631 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001632 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001633
Aaron Plattner999976e2014-03-04 12:42:15 -08001634 if (policy) {
1635 down_read(&policy->rwsem);
Sudeep Holla2f661962019-01-07 18:51:53 +00001636 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001637 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301638
Aaron Plattner999976e2014-03-04 12:42:15 -08001639 cpufreq_cpu_put(policy);
1640 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301641
Dave Jones4d34a672008-02-07 16:33:49 -05001642 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644EXPORT_SYMBOL(cpufreq_get);
1645
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001646static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1647{
1648 unsigned int new_freq;
1649
1650 new_freq = cpufreq_driver->get(policy->cpu);
1651 if (!new_freq)
1652 return 0;
1653
1654 if (!policy->cur) {
1655 pr_debug("cpufreq: Driver did not initialize current freq\n");
1656 policy->cur = new_freq;
1657 } else if (policy->cur != new_freq && has_target()) {
1658 cpufreq_out_of_sync(policy, new_freq);
1659 }
1660
1661 return new_freq;
1662}
1663
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001664static struct subsys_interface cpufreq_interface = {
1665 .name = "cpufreq",
1666 .subsys = &cpu_subsys,
1667 .add_dev = cpufreq_add_dev,
1668 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001669};
1670
Viresh Kumare28867e2014-03-04 11:00:27 +08001671/*
1672 * In case platform wants some specific frequency to be configured
1673 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001674 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001675int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001676{
Viresh Kumare28867e2014-03-04 11:00:27 +08001677 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001678
Viresh Kumare28867e2014-03-04 11:00:27 +08001679 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001680 pr_debug("%s: suspend_freq not defined\n", __func__);
1681 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001682 }
1683
Viresh Kumare28867e2014-03-04 11:00:27 +08001684 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1685 policy->suspend_freq);
1686
1687 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1688 CPUFREQ_RELATION_H);
1689 if (ret)
1690 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1691 __func__, policy->suspend_freq, ret);
1692
Dave Jonesc9060492008-02-07 16:32:18 -05001693 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001694}
Viresh Kumare28867e2014-03-04 11:00:27 +08001695EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001696
1697/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001698 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001700 * Called during system wide Suspend/Hibernate cycles for suspending governors
1701 * as some platforms can't change frequency after this point in suspend cycle.
1702 * Because some of the devices (like: i2c, regulators, etc) they use for
1703 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001705void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301707 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001709 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001712 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301713 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001715 pr_debug("%s: Suspending Governors\n", __func__);
1716
Viresh Kumarf9637352015-05-12 12:20:11 +05301717 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001718 if (has_target()) {
1719 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001720 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001721 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001722 }
1723
1724 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001725 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1726 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301728
1729suspend:
1730 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001734 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001736 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1737 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001739void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301742 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001744 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return;
1746
Bo Yan703cbaa2018-01-23 13:57:55 -08001747 if (unlikely(!cpufreq_suspended))
1748 return;
1749
Lan Tianyu8e304442014-09-18 15:03:07 +08001750 cpufreq_suspended = false;
1751
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001752 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001753 return;
1754
1755 pr_debug("%s: Resuming Governors\n", __func__);
1756
Viresh Kumarf9637352015-05-12 12:20:11 +05301757 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301758 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301759 pr_err("%s: Failed to resume driver: %p\n", __func__,
1760 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001761 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301762 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001763 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301764 up_write(&policy->rwsem);
1765
1766 if (ret)
1767 pr_err("%s: Failed to start governor for policy: %p\n",
1768 __func__, policy);
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Borislav Petkov9d950462013-01-20 10:24:28 +00001773/**
1774 * cpufreq_get_current_driver - return current driver's name
1775 *
1776 * Return the name string of the currently loaded cpufreq driver
1777 * or NULL, if none.
1778 */
1779const char *cpufreq_get_current_driver(void)
1780{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001781 if (cpufreq_driver)
1782 return cpufreq_driver->name;
1783
1784 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001785}
1786EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001788/**
1789 * cpufreq_get_driver_data - return current driver data
1790 *
1791 * Return the private data of the currently loaded cpufreq
1792 * driver, or NULL if no cpufreq driver is loaded.
1793 */
1794void *cpufreq_get_driver_data(void)
1795{
1796 if (cpufreq_driver)
1797 return cpufreq_driver->driver_data;
1798
1799 return NULL;
1800}
1801EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*********************************************************************
1804 * NOTIFIER LISTS INTERFACE *
1805 *********************************************************************/
1806
1807/**
1808 * cpufreq_register_notifier - register a driver with cpufreq
1809 * @nb: notifier function to register
1810 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1811 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001812 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * are notified about clock rate changes (once before and once after
1814 * the transition), or a list of drivers that are notified about
1815 * changes in cpufreq policy.
1816 *
1817 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001818 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 */
1820int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1821{
1822 int ret;
1823
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001824 if (cpufreq_disabled())
1825 return -EINVAL;
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 switch (list) {
1828 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001829 mutex_lock(&cpufreq_fast_switch_lock);
1830
1831 if (cpufreq_fast_switch_count > 0) {
1832 mutex_unlock(&cpufreq_fast_switch_lock);
1833 return -EBUSY;
1834 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001835 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001836 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001837 if (!ret)
1838 cpufreq_fast_switch_count--;
1839
1840 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 break;
1842 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001843 ret = blocking_notifier_chain_register(
1844 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
1846 default:
1847 ret = -EINVAL;
1848 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 return ret;
1851}
1852EXPORT_SYMBOL(cpufreq_register_notifier);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854/**
1855 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1856 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301857 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *
1859 * Remove a driver from the CPU frequency notifier list.
1860 *
1861 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001862 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
1864int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1865{
1866 int ret;
1867
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001868 if (cpufreq_disabled())
1869 return -EINVAL;
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 switch (list) {
1872 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001873 mutex_lock(&cpufreq_fast_switch_lock);
1874
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001875 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001876 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001877 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1878 cpufreq_fast_switch_count++;
1879
1880 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 break;
1882 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001883 ret = blocking_notifier_chain_unregister(
1884 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 break;
1886 default:
1887 ret = -EINVAL;
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
1890 return ret;
1891}
1892EXPORT_SYMBOL(cpufreq_unregister_notifier);
1893
1894
1895/*********************************************************************
1896 * GOVERNORS *
1897 *********************************************************************/
1898
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001899/**
1900 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1901 * @policy: cpufreq policy to switch the frequency for.
1902 * @target_freq: New frequency to set (may be approximate).
1903 *
1904 * Carry out a fast frequency switch without sleeping.
1905 *
1906 * The driver's ->fast_switch() callback invoked by this function must be
1907 * suitable for being called from within RCU-sched read-side critical sections
1908 * and it is expected to select the minimum available frequency greater than or
1909 * equal to @target_freq (CPUFREQ_RELATION_L).
1910 *
1911 * This function must not be called if policy->fast_switch_enabled is unset.
1912 *
1913 * Governors calling this function must guarantee that it will never be invoked
1914 * twice in parallel for the same policy and that it will never be called in
1915 * parallel with either ->target() or ->target_index() for the same policy.
1916 *
Viresh Kumar209887e2017-08-09 10:21:46 +05301917 * Returns the actual frequency set for the CPU.
1918 *
1919 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1920 * error condition, the hardware configuration must be preserved.
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001921 */
1922unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1923 unsigned int target_freq)
1924{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001925 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001926
1927 return cpufreq_driver->fast_switch(policy, target_freq);
1928}
1929EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1930
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301931/* Must set freqs->new to intermediate frequency */
1932static int __target_intermediate(struct cpufreq_policy *policy,
1933 struct cpufreq_freqs *freqs, int index)
1934{
1935 int ret;
1936
1937 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1938
1939 /* We don't need to switch to intermediate freq */
1940 if (!freqs->new)
1941 return 0;
1942
1943 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1944 __func__, policy->cpu, freqs->old, freqs->new);
1945
1946 cpufreq_freq_transition_begin(policy, freqs);
1947 ret = cpufreq_driver->target_intermediate(policy, index);
1948 cpufreq_freq_transition_end(policy, freqs, ret);
1949
1950 if (ret)
1951 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1952 __func__, ret);
1953
1954 return ret;
1955}
1956
Viresh Kumar23727842016-06-03 10:58:50 +05301957static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301958{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301959 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1960 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301961 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301962 int retval = -EINVAL;
1963 bool notify;
1964
Viresh Kumar23727842016-06-03 10:58:50 +05301965 if (newfreq == policy->cur)
1966 return 0;
1967
Viresh Kumar8d657752014-05-21 14:29:29 +05301968 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301969 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301970 /* Handle switching to intermediate frequency */
1971 if (cpufreq_driver->get_intermediate) {
1972 retval = __target_intermediate(policy, &freqs, index);
1973 if (retval)
1974 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301975
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301976 intermediate_freq = freqs.new;
1977 /* Set old freq to intermediate */
1978 if (intermediate_freq)
1979 freqs.old = freqs.new;
1980 }
1981
Viresh Kumar23727842016-06-03 10:58:50 +05301982 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301983 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1984 __func__, policy->cpu, freqs.old, freqs.new);
1985
1986 cpufreq_freq_transition_begin(policy, &freqs);
1987 }
1988
1989 retval = cpufreq_driver->target_index(policy, index);
1990 if (retval)
1991 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1992 retval);
1993
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301994 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301995 cpufreq_freq_transition_end(policy, &freqs, retval);
1996
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301997 /*
1998 * Failed after setting to intermediate freq? Driver should have
1999 * reverted back to initial frequency and so should we. Check
2000 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302001 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302002 */
2003 if (unlikely(retval && intermediate_freq)) {
2004 freqs.old = intermediate_freq;
2005 freqs.new = policy->restore_freq;
2006 cpufreq_freq_transition_begin(policy, &freqs);
2007 cpufreq_freq_transition_end(policy, &freqs, 0);
2008 }
2009 }
2010
Viresh Kumar8d657752014-05-21 14:29:29 +05302011 return retval;
2012}
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014int __cpufreq_driver_target(struct cpufreq_policy *policy,
2015 unsigned int target_freq,
2016 unsigned int relation)
2017{
Viresh Kumar72499242012-10-31 01:28:21 +01002018 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302019 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002020
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002021 if (cpufreq_disabled())
2022 return -ENODEV;
2023
Viresh Kumar72499242012-10-31 01:28:21 +01002024 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302025 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002026
2027 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002028 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002029
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302030 /*
2031 * This might look like a redundant call as we are checking it again
2032 * after finding index. But it is left intentionally for cases where
2033 * exactly same freq is called again and so we can save on few function
2034 * calls.
2035 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002036 if (target_freq == policy->cur)
2037 return 0;
2038
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302039 /* Save last value to restore later on errors */
2040 policy->restore_freq = policy->cur;
2041
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002042 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002043 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002044
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002045 if (!cpufreq_driver->target_index)
2046 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302047
Viresh Kumard218ed72016-06-03 10:58:51 +05302048 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302049
Viresh Kumar23727842016-06-03 10:58:50 +05302050 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054int cpufreq_driver_target(struct cpufreq_policy *policy,
2055 unsigned int target_freq,
2056 unsigned int relation)
2057{
Julia Lawallf1829e42008-07-25 22:44:53 +02002058 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
viresh kumarad7722d2013-10-18 19:10:15 +05302060 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
2062 ret = __cpufreq_driver_target(policy, target_freq, relation);
2063
viresh kumarad7722d2013-10-18 19:10:15 +05302064 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return ret;
2067}
2068EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2069
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002070__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2071{
2072 return NULL;
2073}
2074
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002075static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Dave Jonescc993ca2005-07-28 09:43:56 -07002077 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002078
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002079 /* Don't start any governor operations if we are entering suspend */
2080 if (cpufreq_suspended)
2081 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002082 /*
2083 * Governor might not be initiated here if ACPI _PPC changed
2084 * notification happened, so check it.
2085 */
2086 if (!policy->governor)
2087 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002088
Viresh Kumared4676e2017-07-19 15:42:46 +05302089 /* Platform doesn't want dynamic frequency switching ? */
2090 if (policy->governor->dynamic_switching &&
Viresh Kumarfc4c7092017-07-19 15:42:49 +05302091 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002092 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2093
2094 if (gov) {
Viresh Kumarfe829ed2017-07-19 15:42:48 +05302095 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002096 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002097 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002098 } else {
2099 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002100 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002101 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002103 if (!try_module_get(policy->governor->owner))
2104 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002106 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002107
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002108 if (policy->governor->init) {
2109 ret = policy->governor->init(policy);
2110 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002111 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002112 return ret;
2113 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002116 return 0;
2117}
2118
2119static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2120{
2121 if (cpufreq_suspended || !policy->governor)
2122 return;
2123
2124 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2125
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002126 if (policy->governor->exit)
2127 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002128
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002129 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002132static int cpufreq_start_governor(struct cpufreq_policy *policy)
2133{
2134 int ret;
2135
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002136 if (cpufreq_suspended)
2137 return 0;
2138
2139 if (!policy->governor)
2140 return -EINVAL;
2141
2142 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2143
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002144 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2145 cpufreq_update_current_freq(policy);
2146
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002147 if (policy->governor->start) {
2148 ret = policy->governor->start(policy);
2149 if (ret)
2150 return ret;
2151 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002152
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002153 if (policy->governor->limits)
2154 policy->governor->limits(policy);
2155
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002156 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002157}
2158
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002159static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2160{
2161 if (cpufreq_suspended || !policy->governor)
2162 return;
2163
2164 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2165
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002166 if (policy->governor->stop)
2167 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002168}
2169
2170static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2171{
2172 if (cpufreq_suspended || !policy->governor)
2173 return;
2174
2175 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2176
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002177 if (policy->governor->limits)
2178 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181int cpufreq_register_governor(struct cpufreq_governor *governor)
2182{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 if (!governor)
2186 return -EINVAL;
2187
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002188 if (cpufreq_disabled())
2189 return -ENODEV;
2190
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002191 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002192
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002193 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302194 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002195 err = 0;
2196 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Dave Jones32ee8c32006-02-28 00:43:23 -05002199 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002200 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2205{
Viresh Kumar45732372015-05-12 12:22:34 +05302206 struct cpufreq_policy *policy;
2207 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (!governor)
2210 return;
2211
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002212 if (cpufreq_disabled())
2213 return;
2214
Viresh Kumar45732372015-05-12 12:22:34 +05302215 /* clear last_governor for all inactive policies */
2216 read_lock_irqsave(&cpufreq_driver_lock, flags);
2217 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302218 if (!strcmp(policy->last_governor, governor->name)) {
2219 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302220 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302221 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002222 }
Viresh Kumar45732372015-05-12 12:22:34 +05302223 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002224
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002225 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002227 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2230
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232/*********************************************************************
2233 * POLICY INTERFACE *
2234 *********************************************************************/
2235
2236/**
2237 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002238 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2239 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 *
2241 * Reads the current cpufreq policy.
2242 */
2243int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2244{
2245 struct cpufreq_policy *cpu_policy;
2246 if (!policy)
2247 return -EINVAL;
2248
2249 cpu_policy = cpufreq_cpu_get(cpu);
2250 if (!cpu_policy)
2251 return -EINVAL;
2252
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302253 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return 0;
2257}
2258EXPORT_SYMBOL(cpufreq_get_policy);
2259
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002260/**
2261 * cpufreq_set_policy - Modify cpufreq policy parameters.
2262 * @policy: Policy object to modify.
2263 * @new_policy: New policy data.
2264 *
2265 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2266 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2267 * the driver's ->verify() callback again and run the notifiers for it again
2268 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2269 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2270 * callback (if present) or carry out a governor update for @policy. That is,
2271 * run the current governor's ->limits() callback (if the governor field in
2272 * @new_policy points to the same object as the one in @policy) or replace the
2273 * governor for @policy with the new one stored in @new_policy.
2274 *
2275 * The cpuinfo part of @policy is not updated by this function.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002276 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302277static int cpufreq_set_policy(struct cpufreq_policy *policy,
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002278 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002280 struct cpufreq_governor *old_gov;
2281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Joe Perchese837f9b2014-03-11 10:03:00 -07002283 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2284 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302286 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Pan Xinhuifba95732015-07-30 18:10:40 +08002288 /*
2289 * This check works well when we store new min/max freq attributes,
2290 * because new_policy is a copy of policy with one field updated.
2291 */
2292 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002293 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302296 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002301 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302302 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Viresh Kumarbb176f72013-06-19 14:19:33 +05302304 /*
2305 * verify the cpu speed can be set within this limit, which might be
2306 * different to the first one
2307 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302308 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002309 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002310 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002313 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302314 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302316 policy->min = new_policy->min;
2317 policy->max = new_policy->max;
Ruchi Kandoi601b2182018-07-24 10:35:44 -07002318 trace_cpu_frequency_limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Steve Mucklee3c06232016-07-13 13:25:25 -07002320 policy->cached_target_freq = UINT_MAX;
2321
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002322 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002323 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002325 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302326 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002327 pr_debug("setting range\n");
Rafael J. Wysocki167a38d2019-02-20 00:26:30 +01002328 return cpufreq_driver->setpolicy(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
2330
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002331 if (new_policy->governor == policy->governor) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002332 pr_debug("governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002333 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002334 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002335 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002336
2337 pr_debug("governor switch\n");
2338
2339 /* save old, working values */
2340 old_gov = policy->governor;
2341 /* end old governor */
2342 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002343 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002344 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002345 }
2346
2347 /* start new governor */
2348 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002349 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302350 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002351 ret = cpufreq_start_governor(policy);
2352 if (!ret) {
Rafael J. Wysocki2bb40592019-02-20 00:25:18 +01002353 pr_debug("governor change\n");
Quentin Perret531b5c92018-12-03 09:56:21 +00002354 sched_cpufreq_governor_change(policy, old_gov);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002355 return 0;
2356 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002357 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002358 }
2359
2360 /* new governor failed, so re-start old one */
2361 pr_debug("starting governor %s failed\n", policy->governor->name);
2362 if (old_gov) {
2363 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002364 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302365 policy->governor = NULL;
2366 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002367 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002368 }
2369
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302370 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373/**
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002374 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2375 * @cpu: CPU to re-evaluate the policy for.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 *
Rafael J. Wysockia0dbb812019-02-20 00:22:51 +01002377 * Update the current frequency for the cpufreq policy of @cpu and use
2378 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2379 * user_policy sub-structure of that policy, which triggers the evaluation
2380 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2381 * policy in question, among other things.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 */
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002383void cpufreq_update_policy(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002385 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302386 struct cpufreq_policy new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002388 if (!policy)
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002389 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Viresh Kumarbb176f72013-06-19 14:19:33 +05302391 /*
2392 * BIOS might change freq behind our back
2393 * -> ask driver for current freq and notify governors about a change
2394 */
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002395 if (cpufreq_driver->get && !cpufreq_driver->setpolicy &&
2396 (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy))))
2397 goto unlock;
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002398
Rafael J. Wysocki348a2ec2019-02-20 00:24:25 +01002399 pr_debug("updating policy for CPU %u\n", cpu);
2400 memcpy(&new_policy, policy, sizeof(*policy));
2401 new_policy.min = policy->user_policy.min;
2402 new_policy.max = policy->user_policy.max;
Thomas Renninger0961dd02006-01-26 18:46:33 +01002403
Rafael J. Wysocki30248fe2016-11-18 13:59:21 +01002404 cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002406unlock:
Rafael J. Wysocki540a37582019-03-26 12:16:58 +01002407 cpufreq_cpu_release(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408}
2409EXPORT_SYMBOL(cpufreq_update_policy);
2410
Rafael J. Wysocki5a25e3f2019-03-26 12:15:13 +01002411/**
2412 * cpufreq_update_limits - Update policy limits for a given CPU.
2413 * @cpu: CPU to update the policy limits for.
2414 *
2415 * Invoke the driver's ->update_limits callback if present or call
2416 * cpufreq_update_policy() for @cpu.
2417 */
2418void cpufreq_update_limits(unsigned int cpu)
2419{
2420 if (cpufreq_driver->update_limits)
2421 cpufreq_driver->update_limits(cpu);
2422 else
2423 cpufreq_update_policy(cpu);
2424}
2425EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002428 * BOOST *
2429 *********************************************************************/
2430static int cpufreq_boost_set_sw(int state)
2431{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002432 struct cpufreq_policy *policy;
2433 int ret = -EINVAL;
2434
Viresh Kumarf9637352015-05-12 12:20:11 +05302435 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302436 if (!policy->freq_table)
2437 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302438
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302439 ret = cpufreq_frequency_table_cpuinfo(policy,
2440 policy->freq_table);
2441 if (ret) {
2442 pr_err("%s: Policy frequency update failed\n",
2443 __func__);
2444 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002445 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302446
2447 down_write(&policy->rwsem);
2448 policy->user_policy.max = policy->max;
2449 cpufreq_governor_limits(policy);
2450 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002451 }
2452
2453 return ret;
2454}
2455
2456int cpufreq_boost_trigger_state(int state)
2457{
2458 unsigned long flags;
2459 int ret = 0;
2460
2461 if (cpufreq_driver->boost_enabled == state)
2462 return 0;
2463
2464 write_lock_irqsave(&cpufreq_driver_lock, flags);
2465 cpufreq_driver->boost_enabled = state;
2466 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2467
2468 ret = cpufreq_driver->set_boost(state);
2469 if (ret) {
2470 write_lock_irqsave(&cpufreq_driver_lock, flags);
2471 cpufreq_driver->boost_enabled = !state;
2472 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2473
Joe Perchese837f9b2014-03-11 10:03:00 -07002474 pr_err("%s: Cannot %s BOOST\n",
2475 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002476 }
2477
2478 return ret;
2479}
2480
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002481static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002482{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002483 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002484}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002485
Viresh Kumar44139ed2015-07-29 16:23:09 +05302486static int create_boost_sysfs_file(void)
2487{
2488 int ret;
2489
Viresh Kumarc82bd442015-10-15 21:35:23 +05302490 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302491 if (ret)
2492 pr_err("%s: cannot register global BOOST sysfs file\n",
2493 __func__);
2494
2495 return ret;
2496}
2497
2498static void remove_boost_sysfs_file(void)
2499{
2500 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302501 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302502}
2503
2504int cpufreq_enable_boost_support(void)
2505{
2506 if (!cpufreq_driver)
2507 return -EINVAL;
2508
2509 if (cpufreq_boost_supported())
2510 return 0;
2511
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002512 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302513
2514 /* This will get removed on driver unregister */
2515 return create_boost_sysfs_file();
2516}
2517EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2518
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002519int cpufreq_boost_enabled(void)
2520{
2521 return cpufreq_driver->boost_enabled;
2522}
2523EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2524
2525/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2527 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002528static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Chen Yuc4a3fa22017-04-09 13:45:16 +08002530static int cpuhp_cpufreq_online(unsigned int cpu)
2531{
2532 cpufreq_online(cpu);
2533
2534 return 0;
2535}
2536
2537static int cpuhp_cpufreq_offline(unsigned int cpu)
2538{
2539 cpufreq_offline(cpu);
2540
2541 return 0;
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544/**
2545 * cpufreq_register_driver - register a CPU Frequency driver
2546 * @driver_data: A struct cpufreq_driver containing the values#
2547 * submitted by the CPU Frequency driver.
2548 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302549 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002550 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002551 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *
2553 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002554int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 unsigned long flags;
2557 int ret;
2558
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002559 if (cpufreq_disabled())
2560 return -ENODEV;
2561
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302563 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002564 driver_data->target) ||
2565 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302566 driver_data->target)) ||
Viresh Kumara9a22b52019-02-14 16:16:21 +05302567 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
Viresh Kumar91a12e92019-02-12 16:36:04 +05302568 (!driver_data->online != !driver_data->offline))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 return -EINVAL;
2570
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002571 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002573 /* Protect against concurrent CPU online/offline. */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002574 cpus_read_lock();
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002575
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002576 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002577 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002578 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002579 ret = -EEXIST;
2580 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002582 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002583 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302585 if (driver_data->setpolicy)
2586 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2587
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002588 if (cpufreq_boost_supported()) {
2589 ret = create_boost_sysfs_file();
2590 if (ret)
2591 goto err_null_driver;
2592 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002593
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002594 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002595 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002596 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302598 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2599 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* if all ->init() calls failed, unregister */
David Arcari6c770032017-05-26 11:37:31 -04002601 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302602 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2603 driver_data->name);
2604 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002607 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2608 "cpufreq:online",
2609 cpuhp_cpufreq_online,
2610 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002611 if (ret < 0)
2612 goto err_if_unreg;
2613 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002614 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002615
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002616 pr_debug("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002617 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002618
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002619err_if_unreg:
2620 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002621err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302622 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002623err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002624 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002625 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002626 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002627out:
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002628 cpus_read_unlock();
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002629 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633/**
2634 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2635 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302636 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * the right to do so, i.e. if you have succeeded in initialising before!
2638 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2639 * currently not initialised.
2640 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002641int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642{
2643 unsigned long flags;
2644
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002645 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002648 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002650 /* Protect against concurrent cpu hotplug */
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002651 cpus_read_lock();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002652 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302653 remove_boost_sysfs_file();
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002654 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002656 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302657
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002658 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302659
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002660 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewiora92551e2017-05-24 10:15:20 +02002661 cpus_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
2663 return 0;
2664}
2665EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002666
Doug Anderson90de2a42014-12-23 22:09:48 -08002667/*
2668 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2669 * or mutexes when secondary CPUs are halted.
2670 */
2671static struct syscore_ops cpufreq_syscore_ops = {
2672 .shutdown = cpufreq_suspend,
2673};
2674
Viresh Kumarc82bd442015-10-15 21:35:23 +05302675struct kobject *cpufreq_global_kobject;
2676EXPORT_SYMBOL(cpufreq_global_kobject);
2677
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002678static int __init cpufreq_core_init(void)
2679{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002680 if (cpufreq_disabled())
2681 return -ENODEV;
2682
Viresh Kumar8eec1022015-10-15 21:35:22 +05302683 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002684 BUG_ON(!cpufreq_global_kobject);
2685
Doug Anderson90de2a42014-12-23 22:09:48 -08002686 register_syscore_ops(&cpufreq_syscore_ops);
2687
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002688 return 0;
2689}
Len Brownd82f2692017-02-28 16:44:16 -05002690module_param(off, int, 0444);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002691core_initcall(cpufreq_core_init);