blob: 1667bc0e7b50562e8c0e6147a605eba78711c081 [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>
Connor O'Brien6e7b83d2018-01-31 18:11:57 -080022#include <linux/cpufreq_times.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>
Amit Pundir8aab20c2016-08-29 19:48:17 +053033#ifdef CONFIG_SMP
Juri Lelli2f8ed122015-04-30 17:35:23 +010034#include <linux/sched.h>
Amit Pundir8aab20c2016-08-29 19:48:17 +053035#endif
Thomas Renninger6f4f2722010-04-20 13:17:36 +020036#include <trace/events/power.h>
37
Viresh Kumarb4f06762015-01-27 14:06:08 +053038static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053039
40static inline bool policy_is_inactive(struct cpufreq_policy *policy)
41{
42 return cpumask_empty(policy->cpus);
43}
44
Viresh Kumarf9637352015-05-12 12:20:11 +053045/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060046#define for_each_suitable_policy(__policy, __active) \
47 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
48 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053049
50#define for_each_active_policy(__policy) \
51 for_each_suitable_policy(__policy, true)
52#define for_each_inactive_policy(__policy) \
53 for_each_suitable_policy(__policy, false)
54
55#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053056 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
57
Viresh Kumarf7b27062015-01-27 14:06:09 +053058/* Iterate over governors */
59static LIST_HEAD(cpufreq_governor_list);
60#define for_each_governor(__governor) \
61 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/**
Dave Jonescd878472006-08-11 17:59:28 -040064 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * level driver of CPUFreq support, and its spinlock. This lock
66 * also protects the cpufreq_cpu_data array.
67 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020068static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070069static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053070static DEFINE_RWLOCK(cpufreq_driver_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053071
Viresh Kumar2f0aea92014-03-04 11:00:26 +080072/* Flag to suspend/resume CPUFreq governors */
73static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053075static inline bool has_target(void)
76{
77 return cpufreq_driver->target_index || cpufreq_driver->target;
78}
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* internal prototypes */
Viresh Kumard92d50a2015-01-02 12:34:29 +053081static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020082static int cpufreq_init_governor(struct cpufreq_policy *policy);
83static void cpufreq_exit_governor(struct cpufreq_policy *policy);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +010084static int cpufreq_start_governor(struct cpufreq_policy *policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +020085static void cpufreq_stop_governor(struct cpufreq_policy *policy);
86static void cpufreq_governor_limits(struct cpufreq_policy *policy);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +020087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/**
Dave Jones32ee8c32006-02-28 00:43:23 -050089 * Two notifier lists: the "policy" list is involved in the
90 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * "transition" list for kernel code that needs to handle
92 * changes to devices when the CPU clock speed changes.
93 * The mutex locks both lists.
94 */
Alan Sterne041c682006-03-27 01:16:30 -080095static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070096static struct srcu_notifier_head cpufreq_transition_notifier_list;
Rohit Gupta92e3ea92014-11-14 17:53:15 -080097struct atomic_notifier_head cpufreq_govinfo_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020099static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700100static int __init init_cpufreq_transition_notifier_list(void)
101{
102 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200103 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700104 return 0;
105}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800106pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Rohit Gupta92e3ea92014-11-14 17:53:15 -0800108static bool init_cpufreq_govinfo_notifier_list_called;
109static int __init init_cpufreq_govinfo_notifier_list(void)
110{
111 ATOMIC_INIT_NOTIFIER_HEAD(&cpufreq_govinfo_notifier_list);
112 init_cpufreq_govinfo_notifier_list_called = true;
113 return 0;
114}
115pure_initcall(init_cpufreq_govinfo_notifier_list);
116
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400117static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200118static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400119{
120 return off;
121}
122void disable_cpufreq(void)
123{
124 off = 1;
125}
Dave Jones29464f22009-01-18 01:37:11 -0500126static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000128bool have_governor_per_policy(void)
129{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530130 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000131}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000132EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000133
Michael Turquettedabce2b2015-06-30 12:45:27 +0100134bool cpufreq_driver_is_slow(void)
135{
136 return !(cpufreq_driver->flags & CPUFREQ_DRIVER_FAST);
137}
138EXPORT_SYMBOL_GPL(cpufreq_driver_is_slow);
139
Viresh Kumar944e9a02013-05-16 05:09:57 +0000140struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
141{
142 if (have_governor_per_policy())
143 return &policy->kobj;
144 else
145 return cpufreq_global_kobject;
146}
147EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
148
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000149static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
150{
151 u64 idle_time;
152 u64 cur_wall_time;
153 u64 busy_time;
154
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100155 cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000156
157 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
158 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
159 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
163
164 idle_time = cur_wall_time - busy_time;
165 if (wall)
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100166 *wall = div_u64(cur_wall_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000167
Frederic Weisbeckerdbf9a052017-01-31 04:09:19 +0100168 return div_u64(idle_time, NSEC_PER_USEC);
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000169}
170
171u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
172{
173 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
174
175 if (idle_time == -1ULL)
176 return get_cpu_idle_time_jiffy(cpu, wall);
177 else if (!io_busy)
178 idle_time += get_cpu_iowait_time_us(cpu, wall);
179
180 return idle_time;
181}
182EXPORT_SYMBOL_GPL(get_cpu_idle_time);
183
Viresh Kumar70e9e772013-10-03 20:29:07 +0530184/*
185 * This is a generic cpufreq init() routine which can be used by cpufreq
186 * drivers of SMP systems. It will do following:
187 * - validate & show freq table passed
188 * - set policies transition latency
189 * - policy->cpus with all possible CPUs
190 */
191int cpufreq_generic_init(struct cpufreq_policy *policy,
192 struct cpufreq_frequency_table *table,
193 unsigned int transition_latency)
194{
195 int ret;
196
197 ret = cpufreq_table_validate_and_show(policy, table);
198 if (ret) {
199 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
200 return ret;
201 }
202
203 policy->cpuinfo.transition_latency = transition_latency;
204
205 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530206 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530207 * share the clock and voltage and clock.
208 */
209 cpumask_setall(policy->cpus);
210
211 return 0;
212}
213EXPORT_SYMBOL_GPL(cpufreq_generic_init);
214
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200215struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530216{
217 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
218
Viresh Kumar988bed02015-05-08 11:53:45 +0530219 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
220}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200221EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530222
223unsigned int cpufreq_generic_get(unsigned int cpu)
224{
225 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
226
Viresh Kumar652ed952014-01-09 20:38:43 +0530227 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700228 pr_err("%s: No %s associated to cpu: %d\n",
229 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530230 return 0;
231 }
232
233 return clk_get_rate(policy->clk) / 1000;
234}
235EXPORT_SYMBOL_GPL(cpufreq_generic_get);
236
Viresh Kumar50e9c852015-02-19 17:02:03 +0530237/**
238 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
239 *
240 * @cpu: cpu to find policy for.
241 *
242 * This returns policy for 'cpu', returns NULL if it doesn't exist.
243 * It also increments the kobject reference count to mark it busy and so would
244 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
245 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
246 * freed as that depends on the kobj count.
247 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530248 * Return: A valid policy on success, otherwise NULL on failure.
249 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530250struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530252 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 unsigned long flags;
254
Viresh Kumar1b947c92015-02-19 17:02:05 +0530255 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530256 return NULL;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000259 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Viresh Kumar6eed9402013-08-06 22:53:11 +0530261 if (cpufreq_driver) {
262 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530263 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530264 if (policy)
265 kobject_get(&policy->kobj);
266 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200267
Viresh Kumar6eed9402013-08-06 22:53:11 +0530268 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530270 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000271}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
273
Viresh Kumar50e9c852015-02-19 17:02:03 +0530274/**
275 * cpufreq_cpu_put: Decrements the usage count of a policy
276 *
277 * @policy: policy earlier returned by cpufreq_cpu_get().
278 *
279 * This decrements the kobject reference count incremented earlier by calling
280 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530281 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530282void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530284 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
290 *********************************************************************/
291
292/**
293 * adjust_jiffies - adjust the system "loops_per_jiffy"
294 *
295 * This function alters the system "loops_per_jiffy" for the clock
296 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500297 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * per-CPU loops_per_jiffy value wherever possible.
299 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800300static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530302#ifndef CONFIG_SMP
303 static unsigned long l_p_j_ref;
304 static unsigned int l_p_j_ref_freq;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (ci->flags & CPUFREQ_CONST_LOOPS)
307 return;
308
309 if (!l_p_j_ref_freq) {
310 l_p_j_ref = loops_per_jiffy;
311 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700312 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
313 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530315 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530316 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
317 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700318 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
319 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100324/*********************************************************************
325 * FREQUENCY INVARIANT CPU CAPACITY *
326 *********************************************************************/
327
328static DEFINE_PER_CPU(unsigned long, freq_scale) = SCHED_CAPACITY_SCALE;
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +0100329static DEFINE_PER_CPU(unsigned long, max_freq_cpu);
Dietmar Eggemann4eb92972015-09-22 16:47:48 +0100330static DEFINE_PER_CPU(unsigned long, max_freq_scale) = SCHED_CAPACITY_SCALE;
Ionela Voinescu60813f12017-12-07 19:24:41 +0000331static DEFINE_PER_CPU(unsigned long, min_freq_scale);
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100332
333static void
Dietmar Eggemann9dd5d552017-07-12 14:09:50 +0100334scale_freq_capacity(const cpumask_t *cpus, unsigned long cur_freq,
335 unsigned long max_freq)
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100336{
Dietmar Eggemann9dd5d552017-07-12 14:09:50 +0100337 unsigned long scale = (cur_freq << SCHED_CAPACITY_SHIFT) / max_freq;
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100338 int cpu;
339
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +0100340 for_each_cpu(cpu, cpus) {
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100341 per_cpu(freq_scale, cpu) = scale;
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +0100342 per_cpu(max_freq_cpu, cpu) = max_freq;
343 }
Dietmar Eggemann4eb92972015-09-22 16:47:48 +0100344
Dietmar Eggemann9dd5d552017-07-12 14:09:50 +0100345 pr_debug("cpus %*pbl cur freq/max freq %lu/%lu kHz freq scale %lu\n",
346 cpumask_pr_args(cpus), cur_freq, max_freq, scale);
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100347}
348
349unsigned long cpufreq_scale_freq_capacity(struct sched_domain *sd, int cpu)
350{
351 return per_cpu(freq_scale, cpu);
352}
353
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +0100354static void
355scale_max_freq_capacity(const cpumask_t *cpus, unsigned long policy_max_freq)
356{
357 unsigned long scale, max_freq;
358 int cpu = cpumask_first(cpus);
359
360 if (cpu >= nr_cpu_ids)
361 return;
362
363 max_freq = per_cpu(max_freq_cpu, cpu);
364
365 if (!max_freq)
366 return;
367
368 scale = (policy_max_freq << SCHED_CAPACITY_SHIFT) / max_freq;
369
370 for_each_cpu(cpu, cpus)
371 per_cpu(max_freq_scale, cpu) = scale;
372
373 pr_debug("cpus %*pbl policy max freq/max freq %lu/%lu kHz max freq scale %lu\n",
374 cpumask_pr_args(cpus), policy_max_freq, max_freq, scale);
375}
376
377unsigned long cpufreq_scale_max_freq_capacity(struct sched_domain *sd, int cpu)
Dietmar Eggemann4eb92972015-09-22 16:47:48 +0100378{
379 return per_cpu(max_freq_scale, cpu);
380}
381
Ionela Voinescu60813f12017-12-07 19:24:41 +0000382static void
383scale_min_freq_capacity(const cpumask_t *cpus, unsigned long policy_min_freq)
384{
385 unsigned long scale, max_freq;
386 int cpu = cpumask_first(cpus);
387
388 if (cpu >= nr_cpu_ids)
389 return;
390
391 max_freq = per_cpu(max_freq_cpu, cpu);
392
393 if (!max_freq)
394 return;
395
396 scale = (policy_min_freq << SCHED_CAPACITY_SHIFT) / max_freq;
397
398 for_each_cpu(cpu, cpus)
399 per_cpu(min_freq_scale, cpu) = scale;
400
401 pr_debug("cpus %*pbl policy min freq/max freq %lu/%lu kHz min freq scale %lu\n",
402 cpumask_pr_args(cpus), policy_min_freq, max_freq, scale);
403}
404
405unsigned long cpufreq_scale_min_freq_capacity(struct sched_domain *sd, int cpu)
406{
407 return per_cpu(min_freq_scale, cpu);
408}
409
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530410static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530411 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 BUG_ON(irqs_disabled());
414
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000415 if (cpufreq_disabled())
416 return;
417
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200418 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200419 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700420 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500425 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800426 * which is not equal to what the cpufreq core thinks is
427 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200429 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800430 if ((policy) && (policy->cpu == freqs->cpu) &&
431 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700432 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
433 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800434 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700437 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800438 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
440 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 case CPUFREQ_POSTCHANGE:
443 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700444 pr_debug("FREQ: %lu - CPU: %lu\n",
445 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100446 trace_cpu_frequency(freqs->new, freqs->cpu);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +0200447 cpufreq_stats_record_transition(policy, freqs->new);
Connor O'Brien2f0bc4b2019-02-08 12:30:41 -0800448 cpufreq_times_record_transition(policy, freqs->new);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700449 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800450 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800451 if (likely(policy) && likely(policy->cpu == freqs->cpu))
452 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 break;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530456
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530457/**
458 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
459 * on frequency transition.
460 *
461 * This function calls the transition notifiers and the "adjust_jiffies"
462 * function. It is called twice on all CPU frequency changes that have
463 * external effects.
464 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530465static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530466 struct cpufreq_freqs *freqs, unsigned int state)
467{
468 for_each_cpu(freqs->cpu, policy->cpus)
469 __cpufreq_notify_transition(policy, freqs, state);
470}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530472/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530473static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530474 struct cpufreq_freqs *freqs, int transition_failed)
475{
476 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
477 if (!transition_failed)
478 return;
479
480 swap(freqs->old, freqs->new);
481 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
482 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
483}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530484
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530485void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
486 struct cpufreq_freqs *freqs)
487{
Amit Pundir8aab20c2016-08-29 19:48:17 +0530488#ifdef CONFIG_SMP
Juri Lelli2f8ed122015-04-30 17:35:23 +0100489 int cpu;
Amit Pundir8aab20c2016-08-29 19:48:17 +0530490#endif
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530491
492 /*
493 * Catch double invocations of _begin() which lead to self-deadlock.
494 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
495 * doesn't invoke _begin() on their behalf, and hence the chances of
496 * double invocations are very low. Moreover, there are scenarios
497 * where these checks can emit false-positive warnings in these
498 * drivers; so we avoid that by skipping them altogether.
499 */
500 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
501 && current == policy->transition_task);
502
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530503wait:
504 wait_event(policy->transition_wait, !policy->transition_ongoing);
505
506 spin_lock(&policy->transition_lock);
507
508 if (unlikely(policy->transition_ongoing)) {
509 spin_unlock(&policy->transition_lock);
510 goto wait;
511 }
512
513 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530514 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530515
516 spin_unlock(&policy->transition_lock);
517
Dietmar Eggemann9dd5d552017-07-12 14:09:50 +0100518 scale_freq_capacity(policy->cpus, freqs->new, policy->cpuinfo.max_freq);
Amit Pundir8aab20c2016-08-29 19:48:17 +0530519#ifdef CONFIG_SMP
Juri Lelli2f8ed122015-04-30 17:35:23 +0100520 for_each_cpu(cpu, policy->cpus)
521 trace_cpu_capacity(capacity_curr_of(cpu), cpu);
Amit Pundir8aab20c2016-08-29 19:48:17 +0530522#endif
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +0100523
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530524 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
525}
526EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
527
528void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
529 struct cpufreq_freqs *freqs, int transition_failed)
530{
531 if (unlikely(WARN_ON(!policy->transition_ongoing)))
532 return;
533
534 cpufreq_notify_post_transition(policy, freqs, transition_failed);
535
536 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530537 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530538
539 wake_up(&policy->transition_wait);
540}
541EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
542
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200543/*
544 * Fast frequency switching status count. Positive means "enabled", negative
545 * means "disabled" and 0 means "not decided yet".
546 */
547static int cpufreq_fast_switch_count;
548static DEFINE_MUTEX(cpufreq_fast_switch_lock);
549
550static void cpufreq_list_transition_notifiers(void)
551{
552 struct notifier_block *nb;
553
554 pr_info("Registered transition notifiers:\n");
555
556 mutex_lock(&cpufreq_transition_notifier_list.mutex);
557
558 for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
559 pr_info("%pF\n", nb->notifier_call);
560
561 mutex_unlock(&cpufreq_transition_notifier_list.mutex);
562}
563
564/**
565 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
566 * @policy: cpufreq policy to enable fast frequency switching for.
567 *
568 * Try to enable fast frequency switching for @policy.
569 *
570 * The attempt will fail if there is at least one transition notifier registered
571 * at this point, as fast frequency switching is quite fundamentally at odds
572 * with transition notifiers. Thus if successful, it will make registration of
573 * transition notifiers fail going forward.
574 */
575void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
576{
577 lockdep_assert_held(&policy->rwsem);
578
579 if (!policy->fast_switch_possible)
580 return;
581
582 mutex_lock(&cpufreq_fast_switch_lock);
583 if (cpufreq_fast_switch_count >= 0) {
584 cpufreq_fast_switch_count++;
585 policy->fast_switch_enabled = true;
586 } else {
587 pr_warn("CPU%u: Fast frequency switching not enabled\n",
588 policy->cpu);
589 cpufreq_list_transition_notifiers();
590 }
591 mutex_unlock(&cpufreq_fast_switch_lock);
592}
593EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
594
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200595/**
596 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
597 * @policy: cpufreq policy to disable fast frequency switching for.
598 */
599void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +0200600{
601 mutex_lock(&cpufreq_fast_switch_lock);
602 if (policy->fast_switch_enabled) {
603 policy->fast_switch_enabled = false;
604 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
605 cpufreq_fast_switch_count--;
606 }
607 mutex_unlock(&cpufreq_fast_switch_lock);
608}
Rafael J. Wysocki6c9d9c82016-04-07 23:38:46 +0200609EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Steve Mucklee3c06232016-07-13 13:25:25 -0700611/**
612 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
613 * one.
614 * @target_freq: target frequency to resolve.
615 *
616 * The target to driver frequency mapping is cached in the policy.
617 *
618 * Return: Lowest driver-supported frequency greater than or equal to the
619 * given target_freq, subject to policy (min/max) and driver limitations.
620 */
621unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
622 unsigned int target_freq)
623{
624 target_freq = clamp_val(target_freq, policy->min, policy->max);
625 policy->cached_target_freq = target_freq;
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700626
627 if (cpufreq_driver->target_index) {
628 int idx;
629
630 idx = cpufreq_frequency_table_target(policy, target_freq,
631 CPUFREQ_RELATION_L);
632 policy->cached_resolved_idx = idx;
633 return policy->freq_table[idx].frequency;
634 }
635
Steve Mucklee3c06232016-07-13 13:25:25 -0700636 if (cpufreq_driver->resolve_freq)
637 return cpufreq_driver->resolve_freq(policy, target_freq);
Viresh Kumarabe8bd02016-07-21 14:39:26 -0700638
639 return target_freq;
Steve Mucklee3c06232016-07-13 13:25:25 -0700640}
Steve Muckleae2c1ca2016-07-21 19:24:38 -0700641EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
Steve Mucklee3c06232016-07-13 13:25:25 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643/*********************************************************************
644 * SYSFS INTERFACE *
645 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530646static ssize_t show_boost(struct kobject *kobj,
Viresh Kumar8c09b192019-01-25 12:53:07 +0530647 struct kobj_attribute *attr, char *buf)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100648{
649 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
650}
651
Viresh Kumar8c09b192019-01-25 12:53:07 +0530652static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
653 const char *buf, size_t count)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100654{
655 int ret, enable;
656
657 ret = sscanf(buf, "%d", &enable);
658 if (ret != 1 || enable < 0 || enable > 1)
659 return -EINVAL;
660
661 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700662 pr_err("%s: Cannot %s BOOST!\n",
663 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100664 return -EINVAL;
665 }
666
Joe Perchese837f9b2014-03-11 10:03:00 -0700667 pr_debug("%s: cpufreq BOOST %s\n",
668 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100669
670 return count;
671}
672define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530674static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700675{
676 struct cpufreq_governor *t;
677
Viresh Kumarf7b27062015-01-27 14:06:09 +0530678 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200679 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700680 return t;
681
682 return NULL;
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/**
686 * cpufreq_parse_governor - parse a governor string
687 */
Dave Jones905d77c2008-03-05 14:28:32 -0500688static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 struct cpufreq_governor **governor)
690{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700691 int err = -EINVAL;
692
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200693 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200694 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700696 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200697 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530698 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700700 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530702 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700704
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800705 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700706
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530707 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700708
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700709 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700710 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700711
Kees Cook1a8e1462011-05-04 08:38:56 -0700712 mutex_unlock(&cpufreq_governor_mutex);
713 ret = request_module("cpufreq_%s", str_governor);
714 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700715
Kees Cook1a8e1462011-05-04 08:38:56 -0700716 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530717 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700718 }
719
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700720 if (t != NULL) {
721 *governor = t;
722 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700724
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800725 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700727 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530731 * cpufreq_per_cpu_attr_read() / show_##file_name() -
732 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 *
734 * Write out information from cpufreq_driver->policy[cpu]; object must be
735 * "unsigned int".
736 */
737
Dave Jones32ee8c32006-02-28 00:43:23 -0500738#define show_one(file_name, object) \
739static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500740(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500741{ \
Dave Jones29464f22009-01-18 01:37:11 -0500742 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745show_one(cpuinfo_min_freq, cpuinfo.min_freq);
746show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100747show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748show_one(scaling_min_freq, min);
749show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700750
Viresh Kumar09347b22015-01-02 12:34:24 +0530751static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700752{
753 ssize_t ret;
754
755 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
756 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
757 else
758 ret = sprintf(buf, "%u\n", policy->cur);
759 return ret;
760}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Viresh Kumar037ce832013-10-02 14:13:16 +0530762static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530763 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/**
766 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
767 */
768#define store_one(file_name, object) \
769static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500770(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800772 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 struct cpufreq_policy new_policy; \
774 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530775 memcpy(&new_policy, policy, sizeof(*policy)); \
Tao Wang59305892018-05-26 15:16:48 +0800776 new_policy.min = policy->user_policy.min; \
777 new_policy.max = policy->user_policy.max; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 \
Taniya Dase0ed45f2014-07-31 11:05:51 +0530779 new_policy.min = new_policy.user_policy.min; \
780 new_policy.max = new_policy.user_policy.max; \
781 \
Dave Jones29464f22009-01-18 01:37:11 -0500782 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (ret != 1) \
784 return -EINVAL; \
785 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800786 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530787 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800788 if (!ret) \
789 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 \
791 return ret ? ret : count; \
792}
793
Dave Jones29464f22009-01-18 01:37:11 -0500794store_one(scaling_min_freq, min);
795store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797/**
798 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
799 */
Dave Jones905d77c2008-03-05 14:28:32 -0500800static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
801 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530803 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysockibb8c61a2017-03-15 00:12:16 +0100804
805 if (cur_freq)
806 return sprintf(buf, "%u\n", cur_freq);
807
808 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811/**
812 * show_scaling_governor - show the current policy for the specified CPU
813 */
Dave Jones905d77c2008-03-05 14:28:32 -0500814static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
Dave Jones29464f22009-01-18 01:37:11 -0500816 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return sprintf(buf, "powersave\n");
818 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
819 return sprintf(buf, "performance\n");
820 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200821 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500822 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return -EINVAL;
824}
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/**
827 * store_scaling_governor - store policy for the specified CPU
828 */
Dave Jones905d77c2008-03-05 14:28:32 -0500829static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
830 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530832 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 char str_governor[16];
834 struct cpufreq_policy new_policy;
835
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530836 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Dave Jones29464f22009-01-18 01:37:11 -0500838 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (ret != 1)
840 return -EINVAL;
841
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530842 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
843 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EINVAL;
845
Viresh Kumar037ce832013-10-02 14:13:16 +0530846 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530847 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
850/**
851 * show_scaling_driver - show the cpufreq driver currently loaded
852 */
Dave Jones905d77c2008-03-05 14:28:32 -0500853static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200855 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856}
857
858/**
859 * show_scaling_available_governors - show the available CPUfreq governors
860 */
Dave Jones905d77c2008-03-05 14:28:32 -0500861static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
862 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
864 ssize_t i = 0;
865 struct cpufreq_governor *t;
866
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530867 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 i += sprintf(buf, "performance powersave");
869 goto out;
870 }
871
Viresh Kumarf7b27062015-01-27 14:06:09 +0530872 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500873 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
874 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200876 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500878out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 i += sprintf(&buf[i], "\n");
880 return i;
881}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700882
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800883ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
885 ssize_t i = 0;
886 unsigned int cpu;
887
Rusty Russell835481d2009-01-04 05:18:06 -0800888 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (i)
890 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
891 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
892 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500893 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895 i += sprintf(&buf[i], "\n");
896 return i;
897}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800898EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700900/**
901 * show_related_cpus - show the CPUs affected by each transition even if
902 * hw coordination is in use
903 */
904static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
905{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800906 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700907}
908
909/**
910 * show_affected_cpus - show the CPUs affected by each transition
911 */
912static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
913{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800914 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700915}
916
Venki Pallipadi9e769882007-10-26 10:18:21 -0700917static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500918 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700919{
920 unsigned int freq = 0;
921 unsigned int ret;
922
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700923 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700924 return -EINVAL;
925
926 ret = sscanf(buf, "%u", &freq);
927 if (ret != 1)
928 return -EINVAL;
929
930 policy->governor->store_setspeed(policy, freq);
931
932 return count;
933}
934
935static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
936{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700937 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700938 return sprintf(buf, "<unsupported>\n");
939
940 return policy->governor->show_setspeed(policy, buf);
941}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Thomas Renningere2f74f32009-11-19 12:31:01 +0100943/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200944 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100945 */
946static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
947{
948 unsigned int limit;
949 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200950 if (cpufreq_driver->bios_limit) {
951 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100952 if (!ret)
953 return sprintf(buf, "%u\n", limit);
954 }
955 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
956}
957
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200958cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
959cpufreq_freq_attr_ro(cpuinfo_min_freq);
960cpufreq_freq_attr_ro(cpuinfo_max_freq);
961cpufreq_freq_attr_ro(cpuinfo_transition_latency);
962cpufreq_freq_attr_ro(scaling_available_governors);
963cpufreq_freq_attr_ro(scaling_driver);
964cpufreq_freq_attr_ro(scaling_cur_freq);
965cpufreq_freq_attr_ro(bios_limit);
966cpufreq_freq_attr_ro(related_cpus);
967cpufreq_freq_attr_ro(affected_cpus);
968cpufreq_freq_attr_rw(scaling_min_freq);
969cpufreq_freq_attr_rw(scaling_max_freq);
970cpufreq_freq_attr_rw(scaling_governor);
971cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Dave Jones905d77c2008-03-05 14:28:32 -0500973static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 &cpuinfo_min_freq.attr,
975 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100976 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 &scaling_min_freq.attr,
978 &scaling_max_freq.attr,
979 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700980 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 &scaling_governor.attr,
982 &scaling_driver.attr,
983 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700984 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 NULL
986};
987
Dave Jones29464f22009-01-18 01:37:11 -0500988#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
989#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Dave Jones29464f22009-01-18 01:37:11 -0500991static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Dave Jones905d77c2008-03-05 14:28:32 -0500993 struct cpufreq_policy *policy = to_policy(kobj);
994 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530995 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530996
Kai Shen42b4bc32019-11-07 05:08:17 +0000997 if (!fattr->show)
998 return -EIO;
999
viresh kumarad7722d2013-10-18 19:10:15 +05301000 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +01001001 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +05301002 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +05301003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return ret;
1005}
1006
Dave Jones905d77c2008-03-05 14:28:32 -05001007static ssize_t store(struct kobject *kobj, struct attribute *attr,
1008 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
Dave Jones905d77c2008-03-05 14:28:32 -05001010 struct cpufreq_policy *policy = to_policy(kobj);
1011 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -05001012 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05301013
Kai Shen42b4bc32019-11-07 05:08:17 +00001014 if (!fattr->store)
1015 return -EIO;
1016
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +05301017 get_online_cpus();
1018
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +01001019 if (cpu_online(policy->cpu)) {
1020 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301021 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +01001022 up_write(&policy->rwsem);
1023 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301024
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +05301025 put_online_cpus();
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return ret;
1028}
1029
Dave Jones905d77c2008-03-05 14:28:32 -05001030static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Dave Jones905d77c2008-03-05 14:28:32 -05001032 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001033 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 complete(&policy->kobj_unregister);
1035}
1036
Emese Revfy52cf25d2010-01-19 02:58:23 +01001037static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 .show = show,
1039 .store = store,
1040};
1041
1042static struct kobj_type ktype_cpufreq = {
1043 .sysfs_ops = &sysfs_ops,
1044 .default_attrs = default_attrs,
1045 .release = cpufreq_sysfs_release,
1046};
1047
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001048static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +02001049{
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001050 struct device *dev = get_cpu_device(cpu);
1051
1052 if (!dev)
1053 return;
1054
1055 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1056 return;
1057
Viresh Kumar266198042016-09-12 12:07:05 +05301058 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001059 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1060 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +02001061}
1062
Viresh Kumar266198042016-09-12 12:07:05 +05301063static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1064 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +02001065{
Viresh Kumar266198042016-09-12 12:07:05 +05301066 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1067 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +02001068}
1069
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001070static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -04001071{
1072 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -04001073 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -04001074
Dave Jones909a6942009-07-08 18:05:42 -04001075 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001076 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +05301077 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -04001078 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1079 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001080 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001081 drv_attr++;
1082 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001083 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001084 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1085 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001086 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001087 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001088
1089 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1090 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001091 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001092
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001093 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001094 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1095 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001096 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001097 }
Dave Jones909a6942009-07-08 18:05:42 -04001098
Viresh Kumar266198042016-09-12 12:07:05 +05301099 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301100}
1101
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001102__weak struct cpufreq_governor *cpufreq_default_governor(void)
1103{
1104 return NULL;
1105}
1106
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301107static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301108{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001109 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301110 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301111
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301112 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001113
viresh kumar6e2c89d2014-03-04 11:43:59 +08001114 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +05301115 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001116 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +08001117 pr_debug("Restoring governor %s for cpu %d\n",
1118 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001119 } else {
1120 gov = cpufreq_default_governor();
1121 if (!gov)
1122 return -ENODATA;
1123 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001124
1125 new_policy.governor = gov;
1126
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001127 /* Use the default policy if there is no last_policy. */
1128 if (cpufreq_driver->setpolicy) {
1129 if (policy->last_policy)
1130 new_policy.policy = policy->last_policy;
1131 else
1132 cpufreq_parse_governor(gov->name, &new_policy.policy,
1133 NULL);
1134 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001135 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301136 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001137}
1138
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001139static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001140{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301141 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001142
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301143 /* Has this CPU been taken care of already? */
1144 if (cpumask_test_cpu(cpu, policy->cpus))
1145 return 0;
1146
Viresh Kumar49f18562016-02-11 17:31:12 +05301147 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001148 if (has_target())
1149 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001150
Viresh Kumarfcf80582013-01-29 14:39:08 +00001151 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301152
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301153 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001154 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301155 if (ret)
Junjie Wu55f03862014-06-19 11:49:31 -07001156 pr_err("%s: Failed to start governor for CPU%u, policy CPU%u\n",
1157 __func__, cpu, policy->cpu);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001158 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301159 up_write(&policy->rwsem);
1160 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301163static void handle_update(struct work_struct *work)
1164{
1165 struct cpufreq_policy *policy =
1166 container_of(work, struct cpufreq_policy, update);
1167 unsigned int cpu = policy->cpu;
1168 pr_debug("handle_update for cpu %u called\n", cpu);
1169 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001172static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301173{
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301174 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301175 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301176
1177 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1178 if (!policy)
1179 return NULL;
1180
1181 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1182 goto err_free_policy;
1183
1184 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1185 goto err_free_cpumask;
1186
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001187 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1188 goto err_free_rcpumask;
1189
Viresh Kumaredd4a892016-03-03 14:51:33 +05301190 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1191 cpufreq_global_kobject, "policy%u", cpu);
1192 if (ret) {
1193 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
Viresh Kumar4e0c06e2019-04-30 11:35:52 +05301194 kobject_put(&policy->kobj);
Viresh Kumaredd4a892016-03-03 14:51:33 +05301195 goto err_free_real_cpus;
1196 }
1197
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301198 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301199 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301200 spin_lock_init(&policy->transition_lock);
1201 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301202 init_completion(&policy->kobj_unregister);
1203 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301204
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001205 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301206 return policy;
1207
Viresh Kumaredd4a892016-03-03 14:51:33 +05301208err_free_real_cpus:
1209 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301210err_free_rcpumask:
1211 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301212err_free_cpumask:
1213 free_cpumask_var(policy->cpus);
1214err_free_policy:
1215 kfree(policy);
1216
1217 return NULL;
1218}
1219
Viresh Kumar2fc33842015-06-08 18:25:29 +05301220static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301221{
1222 struct kobject *kobj;
1223 struct completion *cmp;
1224
Viresh Kumar2fc33842015-06-08 18:25:29 +05301225 if (notify)
1226 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1227 CPUFREQ_REMOVE_POLICY, policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301228
Viresh Kumar87549142015-06-10 02:13:21 +02001229 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001230 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301231 kobj = &policy->kobj;
1232 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001233 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301234 kobject_put(kobj);
1235
1236 /*
1237 * We need to make sure that the underlying kobj is
1238 * actually not referenced anymore by anybody before we
1239 * proceed with unloading.
1240 */
1241 pr_debug("waiting for dropping of refcount\n");
1242 wait_for_completion(cmp);
1243 pr_debug("wait complete\n");
1244}
1245
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301246static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301247{
Viresh Kumar988bed02015-05-08 11:53:45 +05301248 unsigned long flags;
1249 int cpu;
1250
1251 /* Remove policy from list */
1252 write_lock_irqsave(&cpufreq_driver_lock, flags);
1253 list_del(&policy->policy_list);
1254
1255 for_each_cpu(cpu, policy->related_cpus)
1256 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1257 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1258
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301259 cpufreq_policy_put_kobj(policy, notify);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001260 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301261 free_cpumask_var(policy->related_cpus);
1262 free_cpumask_var(policy->cpus);
1263 kfree(policy);
1264}
1265
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001266static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301268 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001269 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001271 unsigned int j;
1272 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001273
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001274 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301275
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301276 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301277 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001278 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301279 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001280 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001281 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001283 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001284 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001285 down_write(&policy->rwsem);
1286 policy->cpu = cpu;
1287 policy->governor = NULL;
1288 up_write(&policy->rwsem);
1289 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001290 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001291 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001292 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001293 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001294 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301295
Rusty Russell835481d2009-01-04 05:18:06 -08001296 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* call driver. From then on the cpufreq must be able
1299 * to accept all calls to ->verify and ->setpolicy for this CPU
1300 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001301 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001303 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301304 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001306
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001307 down_write(&policy->rwsem);
1308
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001309 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001310 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301311 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001312 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001313
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001314 /*
1315 * affected cpus must always be the one, which are online. We aren't
1316 * managing offline cpus here.
1317 */
1318 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1319
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001320 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001321 policy->user_policy.min = policy->min;
1322 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001323
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001324 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301325 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001326 add_cpu_dev_symlink(policy, j);
1327 }
Santosh Mardi8a1c290f2017-01-18 17:43:23 +05301328 } else {
1329 policy->min = policy->user_policy.min;
1330 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301331 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301332
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001333 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301334 policy->cur = cpufreq_driver->get(policy->cpu);
1335 if (!policy->cur) {
1336 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301337 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301338 }
1339 }
1340
Viresh Kumard3916692013-12-03 11:20:46 +05301341 /*
1342 * Sometimes boot loaders set CPU frequency to a value outside of
1343 * frequency table present with cpufreq core. In such cases CPU might be
1344 * unstable if it has to run on that frequency for long duration of time
1345 * and so its better to set it to a frequency which is specified in
1346 * freq-table. This also makes cpufreq stats inconsistent as
1347 * cpufreq-stats would fail to register because current frequency of CPU
1348 * isn't found in freq-table.
1349 *
1350 * Because we don't want this change to effect boot process badly, we go
1351 * for the next freq which is >= policy->cur ('cur' must be set by now,
1352 * otherwise we will end up setting freq to lowest of the table as 'cur'
1353 * is initialized to zero).
1354 *
1355 * We are passing target-freq as "policy->cur - 1" otherwise
1356 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1357 * equal to target-freq.
1358 */
1359 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1360 && has_target()) {
1361 /* Are we running at unknown frequency ? */
1362 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1363 if (ret == -EINVAL) {
1364 /* Warn user and fix it */
1365 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1366 __func__, policy->cpu, policy->cur);
1367 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1368 CPUFREQ_RELATION_L);
1369
1370 /*
1371 * Reaching here after boot in a few seconds may not
1372 * mean that system will remain stable at "unknown"
1373 * frequency for longer duration. Hence, a BUG_ON().
1374 */
1375 BUG_ON(ret);
1376 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1377 __func__, policy->cpu, policy->cur);
1378 }
1379 }
1380
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001381 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001382 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301383 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301384 goto out_exit_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001385
1386 cpufreq_stats_create_table(policy);
Connor O'Brien6e7b83d2018-01-31 18:11:57 -08001387 cpufreq_times_create_policy(policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301388 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1389 CPUFREQ_CREATE_POLICY, policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001390
Viresh Kumar988bed02015-05-08 11:53:45 +05301391 write_lock_irqsave(&cpufreq_driver_lock, flags);
1392 list_add(&policy->policy_list, &cpufreq_policy_list);
1393 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1394 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301395
Viresh Kumar388612b2016-05-24 10:26:50 +05301396 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1397 CPUFREQ_START, policy);
1398
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301399 ret = cpufreq_init_policy(policy);
1400 if (ret) {
1401 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1402 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001403 /* cpufreq_policy_free() will notify based on this */
1404 new_policy = false;
1405 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301406 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301407
Viresh Kumar4e97b632014-03-04 11:44:01 +08001408 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301409
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001410 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301411
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301412 /* Callback for handling stuff after policy is ready */
1413 if (cpufreq_driver->ready)
1414 cpufreq_driver->ready(policy);
1415
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001416 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 return 0;
1419
Viresh Kumar8101f992015-07-08 15:12:15 +05301420out_exit_policy:
Viresh Kumar0fba88e2018-02-22 11:29:43 +05301421 for_each_cpu(j, policy->real_cpus)
1422 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1423
Prarit Bhargava7106e022014-09-10 10:12:08 -04001424 up_write(&policy->rwsem);
1425
Viresh Kumarda60ce92013-10-03 20:28:30 +05301426 if (cpufreq_driver->exit)
1427 cpufreq_driver->exit(policy);
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001428
Viresh Kumar8101f992015-07-08 15:12:15 +05301429out_free_policy:
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001430 cpufreq_policy_free(policy, !new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return ret;
1432}
1433
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001434/**
1435 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1436 * @dev: CPU device.
1437 * @sif: Subsystem interface structure pointer (not used)
1438 */
1439static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1440{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001441 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001442 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301443 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001444
1445 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1446
Viresh Kumar266198042016-09-12 12:07:05 +05301447 if (cpu_online(cpu)) {
1448 ret = cpufreq_online(cpu);
1449 if (ret)
1450 return ret;
1451 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001452
Viresh Kumar266198042016-09-12 12:07:05 +05301453 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001454 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001455 if (policy)
1456 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001458 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001461static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301463 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301464 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001466 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Viresh Kumar988bed02015-05-08 11:53:45 +05301468 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301469 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001470 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Viresh Kumar49f18562016-02-11 17:31:12 +05301474 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001475 if (has_target())
1476 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001477
Viresh Kumar9591bec2015-06-10 02:20:23 +02001478 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301479
Viresh Kumar9591bec2015-06-10 02:20:23 +02001480 if (policy_is_inactive(policy)) {
1481 if (has_target())
1482 strncpy(policy->last_governor, policy->governor->name,
1483 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001484 else
1485 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001486 } else if (cpu == policy->cpu) {
1487 /* Nominate new CPU */
1488 policy->cpu = cpumask_any(policy->cpus);
1489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Viresh Kumar9591bec2015-06-10 02:20:23 +02001491 /* Start governor again for active policy */
1492 if (!policy_is_inactive(policy)) {
1493 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001494 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001495 if (ret)
1496 pr_err("%s: Failed to start governor\n", __func__);
1497 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301498
Viresh Kumar49f18562016-02-11 17:31:12 +05301499 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301500 }
1501
1502 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001503 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001505 if (has_target())
1506 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001507
Viresh Kumar87549142015-06-10 02:13:21 +02001508 /*
1509 * Perform the ->exit() even during light-weight tear-down,
1510 * since this is a core component, and is essential for the
1511 * subsequent light-weight ->init() to succeed.
1512 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001513 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001514 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001515 policy->freq_table = NULL;
1516 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301517
Maria Yu008aa442018-05-24 21:35:27 +08001518 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1519 CPUFREQ_STOP, policy);
1520
Viresh Kumar49f18562016-02-11 17:31:12 +05301521unlock:
1522 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301526/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301527 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301528 *
1529 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301530 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301531static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001532{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001533 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001534 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001535
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001536 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001537 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001538
Viresh Kumar69cee712016-02-11 17:31:11 +05301539 if (cpu_online(cpu))
1540 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001541
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001542 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301543 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301544
1545 if (cpumask_empty(policy->real_cpus))
1546 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301550 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1551 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301552 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * @new_freq: CPU frequency the CPU actually runs at
1554 *
Dave Jones29464f22009-01-18 01:37:11 -05001555 * We adjust to current frequency first, and need to clean up later.
1556 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301558static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301559 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301562
Joe Perchese837f9b2014-03-11 10:03:00 -07001563 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 +05301564 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301566 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301568
Viresh Kumar8fec0512014-03-24 13:35:45 +05301569 cpufreq_freq_transition_begin(policy, &freqs);
1570 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
Dave Jones32ee8c32006-02-28 00:43:23 -05001573/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301574 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001575 * @cpu: CPU number
1576 *
1577 * This is the last known freq, without actually getting it from the driver.
1578 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1579 */
1580unsigned int cpufreq_quick_get(unsigned int cpu)
1581{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001582 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301583 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001584 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001585
Richard Cochranc75361c2016-03-11 09:43:07 +01001586 read_lock_irqsave(&cpufreq_driver_lock, flags);
1587
1588 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1589 ret_freq = cpufreq_driver->get(cpu);
1590 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1591 return ret_freq;
1592 }
1593
1594 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001595
1596 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001597 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301598 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001599 cpufreq_cpu_put(policy);
1600 }
1601
Dave Jones4d34a672008-02-07 16:33:49 -05001602 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001603}
1604EXPORT_SYMBOL(cpufreq_quick_get);
1605
Jesse Barnes3d737102011-06-28 10:59:12 -07001606/**
1607 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1608 * @cpu: CPU number
1609 *
1610 * Just return the max possible frequency for a given CPU.
1611 */
1612unsigned int cpufreq_quick_get_max(unsigned int cpu)
1613{
1614 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1615 unsigned int ret_freq = 0;
1616
1617 if (policy) {
1618 ret_freq = policy->max;
1619 cpufreq_cpu_put(policy);
1620 }
1621
1622 return ret_freq;
1623}
1624EXPORT_SYMBOL(cpufreq_quick_get_max);
1625
Viresh Kumard92d50a2015-01-02 12:34:29 +05301626static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301628 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Sudeep Holladbb43fb2019-01-07 18:51:53 +00001630 if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001631 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Viresh Kumard92d50a2015-01-02 12:34:29 +05301633 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001635 /*
Sudeep Holladbb43fb2019-01-07 18:51:53 +00001636 * If fast frequency switching is used with the given policy, the check
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001637 * against policy->cur is pointless, so skip it in that case too.
1638 */
Sudeep Holladbb43fb2019-01-07 18:51:53 +00001639 if (policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001640 return ret_freq;
1641
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301642 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001643 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301644 /* verify no discrepancy between actual and
1645 saved value exists */
1646 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301647 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 schedule_work(&policy->update);
1649 }
1650 }
1651
Dave Jones4d34a672008-02-07 16:33:49 -05001652 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001653}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001655/**
1656 * cpufreq_get - get the current CPU frequency (in kHz)
1657 * @cpu: CPU number
1658 *
1659 * Get the CPU current (static) CPU frequency
1660 */
1661unsigned int cpufreq_get(unsigned int cpu)
1662{
Aaron Plattner999976e2014-03-04 12:42:15 -08001663 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001664 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001665
Aaron Plattner999976e2014-03-04 12:42:15 -08001666 if (policy) {
1667 down_read(&policy->rwsem);
Rafael J. Wysocki803ca5d2016-11-18 13:40:45 +01001668
1669 if (!policy_is_inactive(policy))
1670 ret_freq = __cpufreq_get(policy);
1671
Aaron Plattner999976e2014-03-04 12:42:15 -08001672 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301673
Aaron Plattner999976e2014-03-04 12:42:15 -08001674 cpufreq_cpu_put(policy);
1675 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301676
Dave Jones4d34a672008-02-07 16:33:49 -05001677 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678}
1679EXPORT_SYMBOL(cpufreq_get);
1680
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001681static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1682{
1683 unsigned int new_freq;
1684
1685 new_freq = cpufreq_driver->get(policy->cpu);
1686 if (!new_freq)
1687 return 0;
1688
1689 if (!policy->cur) {
1690 pr_debug("cpufreq: Driver did not initialize current freq\n");
1691 policy->cur = new_freq;
1692 } else if (policy->cur != new_freq && has_target()) {
1693 cpufreq_out_of_sync(policy, new_freq);
1694 }
1695
1696 return new_freq;
1697}
1698
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001699static struct subsys_interface cpufreq_interface = {
1700 .name = "cpufreq",
1701 .subsys = &cpu_subsys,
1702 .add_dev = cpufreq_add_dev,
1703 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001704};
1705
Viresh Kumare28867e2014-03-04 11:00:27 +08001706/*
1707 * In case platform wants some specific frequency to be configured
1708 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001709 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001710int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001711{
Viresh Kumare28867e2014-03-04 11:00:27 +08001712 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001713
Viresh Kumare28867e2014-03-04 11:00:27 +08001714 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001715 pr_debug("%s: suspend_freq not defined\n", __func__);
1716 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001717 }
1718
Viresh Kumare28867e2014-03-04 11:00:27 +08001719 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1720 policy->suspend_freq);
1721
1722 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1723 CPUFREQ_RELATION_H);
1724 if (ret)
1725 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1726 __func__, policy->suspend_freq, ret);
1727
Dave Jonesc9060492008-02-07 16:32:18 -05001728 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001729}
Viresh Kumare28867e2014-03-04 11:00:27 +08001730EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001731
1732/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001733 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001735 * Called during system wide Suspend/Hibernate cycles for suspending governors
1736 * as some platforms can't change frequency after this point in suspend cycle.
1737 * Because some of the devices (like: i2c, regulators, etc) they use for
1738 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001740void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301742 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001744 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001745 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001747 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301748 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001750 pr_debug("%s: Suspending Governors\n", __func__);
1751
Viresh Kumarf9637352015-05-12 12:20:11 +05301752 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001753 if (has_target()) {
1754 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001755 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001756 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001757 }
1758
1759 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001760 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1761 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301763
1764suspend:
1765 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001769 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001771 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1772 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001774void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301777 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001779 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 return;
1781
Bo Yan7ca04ac2018-01-23 13:57:55 -08001782 if (unlikely(!cpufreq_suspended))
1783 return;
1784
Lan Tianyu8e304442014-09-18 15:03:07 +08001785 cpufreq_suspended = false;
1786
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001787 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001788 return;
1789
1790 pr_debug("%s: Resuming Governors\n", __func__);
1791
Viresh Kumarf9637352015-05-12 12:20:11 +05301792 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301793 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301794 pr_err("%s: Failed to resume driver: %p\n", __func__,
1795 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001796 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301797 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001798 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301799 up_write(&policy->rwsem);
1800
1801 if (ret)
1802 pr_err("%s: Failed to start governor for policy: %p\n",
1803 __func__, policy);
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Borislav Petkov9d950462013-01-20 10:24:28 +00001808/**
1809 * cpufreq_get_current_driver - return current driver's name
1810 *
1811 * Return the name string of the currently loaded cpufreq driver
1812 * or NULL, if none.
1813 */
1814const char *cpufreq_get_current_driver(void)
1815{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001816 if (cpufreq_driver)
1817 return cpufreq_driver->name;
1818
1819 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001820}
1821EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001823/**
1824 * cpufreq_get_driver_data - return current driver data
1825 *
1826 * Return the private data of the currently loaded cpufreq
1827 * driver, or NULL if no cpufreq driver is loaded.
1828 */
1829void *cpufreq_get_driver_data(void)
1830{
1831 if (cpufreq_driver)
1832 return cpufreq_driver->driver_data;
1833
1834 return NULL;
1835}
1836EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/*********************************************************************
1839 * NOTIFIER LISTS INTERFACE *
1840 *********************************************************************/
1841
1842/**
1843 * cpufreq_register_notifier - register a driver with cpufreq
1844 * @nb: notifier function to register
1845 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1846 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001847 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 * are notified about clock rate changes (once before and once after
1849 * the transition), or a list of drivers that are notified about
1850 * changes in cpufreq policy.
1851 *
1852 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001853 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 */
1855int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1856{
1857 int ret;
1858
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001859 if (cpufreq_disabled())
1860 return -EINVAL;
1861
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001862 WARN_ON(!init_cpufreq_transition_notifier_list_called ||
1863 !init_cpufreq_govinfo_notifier_list_called);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 switch (list) {
1866 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001867 mutex_lock(&cpufreq_fast_switch_lock);
1868
1869 if (cpufreq_fast_switch_count > 0) {
1870 mutex_unlock(&cpufreq_fast_switch_lock);
1871 return -EBUSY;
1872 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001873 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001874 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001875 if (!ret)
1876 cpufreq_fast_switch_count--;
1877
1878 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break;
1880 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001881 ret = blocking_notifier_chain_register(
1882 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001884 case CPUFREQ_GOVINFO_NOTIFIER:
1885 ret = atomic_notifier_chain_register(
1886 &cpufreq_govinfo_notifier_list, nb);
1887 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 default:
1889 ret = -EINVAL;
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 return ret;
1893}
1894EXPORT_SYMBOL(cpufreq_register_notifier);
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/**
1897 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1898 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301899 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 *
1901 * Remove a driver from the CPU frequency notifier list.
1902 *
1903 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001904 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
1906int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1907{
1908 int ret;
1909
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001910 if (cpufreq_disabled())
1911 return -EINVAL;
1912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 switch (list) {
1914 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001915 mutex_lock(&cpufreq_fast_switch_lock);
1916
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001917 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001918 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001919 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1920 cpufreq_fast_switch_count++;
1921
1922 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 break;
1924 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001925 ret = blocking_notifier_chain_unregister(
1926 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001928 case CPUFREQ_GOVINFO_NOTIFIER:
1929 ret = atomic_notifier_chain_unregister(
1930 &cpufreq_govinfo_notifier_list, nb);
1931 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 default:
1933 ret = -EINVAL;
1934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 return ret;
1937}
1938EXPORT_SYMBOL(cpufreq_unregister_notifier);
1939
1940
1941/*********************************************************************
1942 * GOVERNORS *
1943 *********************************************************************/
1944
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001945/**
1946 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1947 * @policy: cpufreq policy to switch the frequency for.
1948 * @target_freq: New frequency to set (may be approximate).
1949 *
1950 * Carry out a fast frequency switch without sleeping.
1951 *
1952 * The driver's ->fast_switch() callback invoked by this function must be
1953 * suitable for being called from within RCU-sched read-side critical sections
1954 * and it is expected to select the minimum available frequency greater than or
1955 * equal to @target_freq (CPUFREQ_RELATION_L).
1956 *
1957 * This function must not be called if policy->fast_switch_enabled is unset.
1958 *
1959 * Governors calling this function must guarantee that it will never be invoked
1960 * twice in parallel for the same policy and that it will never be called in
1961 * parallel with either ->target() or ->target_index() for the same policy.
1962 *
1963 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1964 * callback to indicate an error condition, the hardware configuration must be
1965 * preserved.
1966 */
1967unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1968 unsigned int target_freq)
1969{
Connor O'Brien2f0bc4b2019-02-08 12:30:41 -08001970 int ret;
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001971 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001972
Connor O'Brien2f0bc4b2019-02-08 12:30:41 -08001973 ret = cpufreq_driver->fast_switch(policy, target_freq);
1974 if (ret)
1975 cpufreq_times_record_transition(policy, ret);
1976
1977 return ret;
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001978}
1979EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1980
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301981/* Must set freqs->new to intermediate frequency */
1982static int __target_intermediate(struct cpufreq_policy *policy,
1983 struct cpufreq_freqs *freqs, int index)
1984{
1985 int ret;
1986
1987 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1988
1989 /* We don't need to switch to intermediate freq */
1990 if (!freqs->new)
1991 return 0;
1992
1993 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1994 __func__, policy->cpu, freqs->old, freqs->new);
1995
1996 cpufreq_freq_transition_begin(policy, freqs);
1997 ret = cpufreq_driver->target_intermediate(policy, index);
1998 cpufreq_freq_transition_end(policy, freqs, ret);
1999
2000 if (ret)
2001 pr_err("%s: Failed to change to intermediate frequency: %d\n",
2002 __func__, ret);
2003
2004 return ret;
2005}
2006
Viresh Kumar23727842016-06-03 10:58:50 +05302007static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05302008{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302009 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
2010 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05302011 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05302012 int retval = -EINVAL;
2013 bool notify;
2014
Viresh Kumar23727842016-06-03 10:58:50 +05302015 if (newfreq == policy->cur)
2016 return 0;
2017
Viresh Kumar8d657752014-05-21 14:29:29 +05302018 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05302019 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302020 /* Handle switching to intermediate frequency */
2021 if (cpufreq_driver->get_intermediate) {
2022 retval = __target_intermediate(policy, &freqs, index);
2023 if (retval)
2024 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05302025
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302026 intermediate_freq = freqs.new;
2027 /* Set old freq to intermediate */
2028 if (intermediate_freq)
2029 freqs.old = freqs.new;
2030 }
2031
Viresh Kumar23727842016-06-03 10:58:50 +05302032 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05302033 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2034 __func__, policy->cpu, freqs.old, freqs.new);
2035
2036 cpufreq_freq_transition_begin(policy, &freqs);
2037 }
2038
2039 retval = cpufreq_driver->target_index(policy, index);
2040 if (retval)
2041 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2042 retval);
2043
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302044 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05302045 cpufreq_freq_transition_end(policy, &freqs, retval);
2046
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302047 /*
2048 * Failed after setting to intermediate freq? Driver should have
2049 * reverted back to initial frequency and so should we. Check
2050 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302051 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302052 */
2053 if (unlikely(retval && intermediate_freq)) {
2054 freqs.old = intermediate_freq;
2055 freqs.new = policy->restore_freq;
2056 cpufreq_freq_transition_begin(policy, &freqs);
2057 cpufreq_freq_transition_end(policy, &freqs, 0);
2058 }
2059 }
2060
Viresh Kumar8d657752014-05-21 14:29:29 +05302061 return retval;
2062}
2063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064int __cpufreq_driver_target(struct cpufreq_policy *policy,
2065 unsigned int target_freq,
2066 unsigned int relation)
2067{
Viresh Kumar72499242012-10-31 01:28:21 +01002068 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302069 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002070
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002071 if (cpufreq_disabled())
2072 return -ENODEV;
2073
Viresh Kumar72499242012-10-31 01:28:21 +01002074 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302075 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002076
2077 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002078 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002079
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302080 /* Save last value to restore later on errors */
2081 policy->restore_freq = policy->cur;
2082
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002083 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002084 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002085
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002086 if (!cpufreq_driver->target_index)
2087 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302088
Viresh Kumard218ed72016-06-03 10:58:51 +05302089 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302090
Viresh Kumar23727842016-06-03 10:58:50 +05302091 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095int cpufreq_driver_target(struct cpufreq_policy *policy,
2096 unsigned int target_freq,
2097 unsigned int relation)
2098{
Julia Lawallf1829e42008-07-25 22:44:53 +02002099 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
viresh kumarad7722d2013-10-18 19:10:15 +05302101 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 ret = __cpufreq_driver_target(policy, target_freq, relation);
2104
viresh kumarad7722d2013-10-18 19:10:15 +05302105 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return ret;
2108}
2109EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2110
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002111__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2112{
2113 return NULL;
2114}
2115
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002116static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Dave Jonescc993ca2005-07-28 09:43:56 -07002118 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002119
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002120 /* Don't start any governor operations if we are entering suspend */
2121 if (cpufreq_suspended)
2122 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09002123 /*
2124 * Governor might not be initiated here if ACPI _PPC changed
2125 * notification happened, so check it.
2126 */
2127 if (!policy->governor)
2128 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002129
Thomas Renninger1c256242007-10-02 13:28:12 -07002130 if (policy->governor->max_transition_latency &&
2131 policy->cpuinfo.transition_latency >
2132 policy->governor->max_transition_latency) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002133 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2134
2135 if (gov) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002136 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2137 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002138 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002139 } else {
2140 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002141 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002144 if (!try_module_get(policy->governor->owner))
2145 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002147 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002148
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002149 if (policy->governor->init) {
2150 ret = policy->governor->init(policy);
2151 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002152 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002153 return ret;
2154 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002157 return 0;
2158}
2159
2160static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2161{
2162 if (cpufreq_suspended || !policy->governor)
2163 return;
2164
2165 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2166
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002167 if (policy->governor->exit)
2168 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002169
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002170 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002173static int cpufreq_start_governor(struct cpufreq_policy *policy)
2174{
2175 int ret;
2176
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002177 if (cpufreq_suspended)
2178 return 0;
2179
2180 if (!policy->governor)
2181 return -EINVAL;
2182
2183 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2184
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002185 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2186 cpufreq_update_current_freq(policy);
2187
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002188 if (policy->governor->start) {
2189 ret = policy->governor->start(policy);
2190 if (ret)
2191 return ret;
2192 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002193
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002194 if (policy->governor->limits)
2195 policy->governor->limits(policy);
2196
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002197 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002198}
2199
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002200static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2201{
2202 if (cpufreq_suspended || !policy->governor)
2203 return;
2204
2205 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2206
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002207 if (policy->governor->stop)
2208 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002209}
2210
2211static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2212{
2213 if (cpufreq_suspended || !policy->governor)
2214 return;
2215
2216 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2217
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002218 if (policy->governor->limits)
2219 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222int cpufreq_register_governor(struct cpufreq_governor *governor)
2223{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002224 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 if (!governor)
2227 return -EINVAL;
2228
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002229 if (cpufreq_disabled())
2230 return -ENODEV;
2231
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002232 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002233
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002234 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302235 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002236 err = 0;
2237 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Dave Jones32ee8c32006-02-28 00:43:23 -05002240 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002241 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2246{
Viresh Kumar45732372015-05-12 12:22:34 +05302247 struct cpufreq_policy *policy;
2248 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (!governor)
2251 return;
2252
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002253 if (cpufreq_disabled())
2254 return;
2255
Viresh Kumar45732372015-05-12 12:22:34 +05302256 /* clear last_governor for all inactive policies */
2257 read_lock_irqsave(&cpufreq_driver_lock, flags);
2258 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302259 if (!strcmp(policy->last_governor, governor->name)) {
2260 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302261 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302262 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002263 }
Viresh Kumar45732372015-05-12 12:22:34 +05302264 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002265
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002266 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002268 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 return;
2270}
2271EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2272
2273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274/*********************************************************************
2275 * POLICY INTERFACE *
2276 *********************************************************************/
2277
2278/**
2279 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002280 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2281 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 *
2283 * Reads the current cpufreq policy.
2284 */
2285int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2286{
2287 struct cpufreq_policy *cpu_policy;
2288 if (!policy)
2289 return -EINVAL;
2290
2291 cpu_policy = cpufreq_cpu_get(cpu);
2292 if (!cpu_policy)
2293 return -EINVAL;
2294
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302295 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299}
2300EXPORT_SYMBOL(cpufreq_get_policy);
2301
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002302/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302303 * policy : current policy.
2304 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002305 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302306static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302307 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002309 struct cpufreq_governor *old_gov;
2310 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Joe Perchese837f9b2014-03-11 10:03:00 -07002312 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2313 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302315 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Pan Xinhuifba95732015-07-30 18:10:40 +08002317 /*
2318 * This check works well when we store new min/max freq attributes,
2319 * because new_policy is a copy of policy with one field updated.
2320 */
2321 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002322 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302325 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002327 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002330 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302331 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Santosh Mardi5ccbfa32018-11-28 14:59:28 +05302333 /* adjust if necessary - hardware incompatibility */
2334 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2335 CPUFREQ_INCOMPATIBLE, new_policy);
2336
2337
Viresh Kumarbb176f72013-06-19 14:19:33 +05302338 /*
2339 * verify the cpu speed can be set within this limit, which might be
2340 * different to the first one
2341 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302342 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002343 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002344 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
2346 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002347 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302348 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +01002350 scale_max_freq_capacity(policy->cpus, policy->max);
Ionela Voinescu60813f12017-12-07 19:24:41 +00002351 scale_min_freq_capacity(policy->cpus, policy->min);
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +01002352
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302353 policy->min = new_policy->min;
2354 policy->max = new_policy->max;
Ruchi Kandoi83707ea2015-11-19 16:07:19 -08002355 trace_cpu_frequency_limits(policy->max, policy->min, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Steve Mucklee3c06232016-07-13 13:25:25 -07002357 policy->cached_target_freq = UINT_MAX;
2358
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002359 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002360 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002362 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302363 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002364 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002365 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 }
2367
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002368 if (new_policy->governor == policy->governor) {
2369 pr_debug("cpufreq: governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002370 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002371 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002372 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002373
2374 pr_debug("governor switch\n");
2375
2376 /* save old, working values */
2377 old_gov = policy->governor;
2378 /* end old governor */
2379 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002380 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002381 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002382 }
2383
2384 /* start new governor */
2385 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002386 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302387 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002388 ret = cpufreq_start_governor(policy);
2389 if (!ret) {
2390 pr_debug("cpufreq: governor change\n");
2391 return 0;
2392 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002393 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002394 }
2395
2396 /* new governor failed, so re-start old one */
2397 pr_debug("starting governor %s failed\n", policy->governor->name);
2398 if (old_gov) {
2399 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002400 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302401 policy->governor = NULL;
2402 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002403 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002404 }
2405
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302406 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407}
2408
2409/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2411 * @cpu: CPU which shall be re-evaluated
2412 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002413 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 * at different times.
2415 */
2416int cpufreq_update_policy(unsigned int cpu)
2417{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302418 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2419 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002420 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002422 if (!policy)
2423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
viresh kumarad7722d2013-10-18 19:10:15 +05302425 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Rafael J. Wysocki803ca5d2016-11-18 13:40:45 +01002427 if (policy_is_inactive(policy)) {
2428 ret = -ENODEV;
2429 goto unlock;
2430 }
2431
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002432 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302433 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302434 new_policy.min = policy->user_policy.min;
2435 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Viresh Kumarbb176f72013-06-19 14:19:33 +05302437 /*
2438 * BIOS might change freq behind our back
2439 * -> ask driver for current freq and notify governors about a change
2440 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002441 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki742c87b2016-06-28 03:29:29 +02002442 if (cpufreq_suspended) {
2443 ret = -EAGAIN;
2444 goto unlock;
2445 }
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002446 new_policy.cur = cpufreq_update_current_freq(policy);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302447 if (WARN_ON(!new_policy.cur)) {
2448 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002449 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302450 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002451 }
2452
Viresh Kumar037ce832013-10-02 14:13:16 +05302453 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002455unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302456 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002457
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302458 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 return ret;
2460}
2461EXPORT_SYMBOL(cpufreq_update_policy);
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002464 * BOOST *
2465 *********************************************************************/
2466static int cpufreq_boost_set_sw(int state)
2467{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002468 struct cpufreq_policy *policy;
2469 int ret = -EINVAL;
2470
Viresh Kumarf9637352015-05-12 12:20:11 +05302471 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302472 if (!policy->freq_table)
2473 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302474
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302475 ret = cpufreq_frequency_table_cpuinfo(policy,
2476 policy->freq_table);
2477 if (ret) {
2478 pr_err("%s: Policy frequency update failed\n",
2479 __func__);
2480 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002481 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302482
2483 down_write(&policy->rwsem);
2484 policy->user_policy.max = policy->max;
2485 cpufreq_governor_limits(policy);
2486 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002487 }
2488
2489 return ret;
2490}
2491
2492int cpufreq_boost_trigger_state(int state)
2493{
2494 unsigned long flags;
2495 int ret = 0;
2496
2497 if (cpufreq_driver->boost_enabled == state)
2498 return 0;
2499
2500 write_lock_irqsave(&cpufreq_driver_lock, flags);
2501 cpufreq_driver->boost_enabled = state;
2502 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2503
2504 ret = cpufreq_driver->set_boost(state);
2505 if (ret) {
2506 write_lock_irqsave(&cpufreq_driver_lock, flags);
2507 cpufreq_driver->boost_enabled = !state;
2508 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2509
Joe Perchese837f9b2014-03-11 10:03:00 -07002510 pr_err("%s: Cannot %s BOOST\n",
2511 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002512 }
2513
2514 return ret;
2515}
2516
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002517static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002518{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002519 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002520}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002521
Viresh Kumar44139ed2015-07-29 16:23:09 +05302522static int create_boost_sysfs_file(void)
2523{
2524 int ret;
2525
Viresh Kumarc82bd442015-10-15 21:35:23 +05302526 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302527 if (ret)
2528 pr_err("%s: cannot register global BOOST sysfs file\n",
2529 __func__);
2530
2531 return ret;
2532}
2533
2534static void remove_boost_sysfs_file(void)
2535{
2536 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302537 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302538}
2539
2540int cpufreq_enable_boost_support(void)
2541{
2542 if (!cpufreq_driver)
2543 return -EINVAL;
2544
2545 if (cpufreq_boost_supported())
2546 return 0;
2547
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002548 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302549
2550 /* This will get removed on driver unregister */
2551 return create_boost_sysfs_file();
2552}
2553EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2554
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002555int cpufreq_boost_enabled(void)
2556{
2557 return cpufreq_driver->boost_enabled;
2558}
2559EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2560
2561/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2563 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002564static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Chen Yu5dda1572017-04-09 13:45:16 +08002566static int cpuhp_cpufreq_online(unsigned int cpu)
2567{
2568 cpufreq_online(cpu);
2569
2570 return 0;
2571}
2572
2573static int cpuhp_cpufreq_offline(unsigned int cpu)
2574{
2575 cpufreq_offline(cpu);
2576
2577 return 0;
2578}
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580/**
2581 * cpufreq_register_driver - register a CPU Frequency driver
2582 * @driver_data: A struct cpufreq_driver containing the values#
2583 * submitted by the CPU Frequency driver.
2584 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302585 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002586 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002587 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 *
2589 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002590int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 unsigned long flags;
2593 int ret;
2594
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002595 if (cpufreq_disabled())
2596 return -ENODEV;
2597
Viresh Kumarb5a67ca2019-11-14 09:06:17 +05302598 /*
2599 * The cpufreq core depends heavily on the availability of device
2600 * structure, make sure they are available before proceeding further.
2601 */
2602 if (!get_cpu_device(0))
2603 return -EPROBE_DEFER;
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302606 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002607 driver_data->target) ||
2608 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302609 driver_data->target)) ||
2610 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return -EINVAL;
2612
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002613 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002615 /* Protect against concurrent CPU online/offline. */
2616 get_online_cpus();
2617
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002618 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002619 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002620 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002621 ret = -EEXIST;
2622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002624 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002625 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302627 if (driver_data->setpolicy)
2628 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2629
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002630 if (cpufreq_boost_supported()) {
2631 ret = create_boost_sysfs_file();
2632 if (ret)
2633 goto err_null_driver;
2634 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002635
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002636 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002637 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002638 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302640 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2641 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 /* if all ->init() calls failed, unregister */
David Arcari96d7b43b2017-05-26 11:37:31 -04002643 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302644 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2645 driver_data->name);
2646 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002649 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "cpufreq:online",
Chen Yu5dda1572017-04-09 13:45:16 +08002650 cpuhp_cpufreq_online,
2651 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002652 if (ret < 0)
2653 goto err_if_unreg;
2654 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002655 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002656
Junjie Wua0bf0ee2015-02-10 14:54:36 -08002657 pr_info("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002658 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002659
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002660err_if_unreg:
2661 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002662err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302663 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002664err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002665 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002666 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002667 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002668out:
2669 put_online_cpus();
2670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671}
2672EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2673
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674/**
2675 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2676 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302677 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 * the right to do so, i.e. if you have succeeded in initialising before!
2679 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2680 * currently not initialised.
2681 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002682int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683{
2684 unsigned long flags;
2685
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002686 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Junjie Wua0bf0ee2015-02-10 14:54:36 -08002689 pr_info("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002691 /* Protect against concurrent cpu hotplug */
2692 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002693 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302694 remove_boost_sysfs_file();
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002695 cpuhp_remove_state_nocalls(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002697 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302698
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002699 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302700
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002701 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002702 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
2704 return 0;
2705}
2706EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002707
Viresh Kumarc82bd442015-10-15 21:35:23 +05302708struct kobject *cpufreq_global_kobject;
2709EXPORT_SYMBOL(cpufreq_global_kobject);
2710
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002711static int __init cpufreq_core_init(void)
2712{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002713 if (cpufreq_disabled())
2714 return -ENODEV;
2715
Viresh Kumar8eec1022015-10-15 21:35:22 +05302716 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002717 BUG_ON(!cpufreq_global_kobject);
2718
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002719 return 0;
2720}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002721core_initcall(cpufreq_core_init);