blob: 5474ae7bf5d578234c533ab68a1741fef4577b7a [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
155 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
156
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)
166 *wall = cputime_to_usecs(cur_wall_time);
167
168 return cputime_to_usecs(idle_time);
169}
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'Brien6e7b83d2018-01-31 18:11:57 -0800448 cpufreq_times_record_transition(freqs);
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,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100647 struct attribute *attr, char *buf)
648{
649 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
650}
651
652static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
653 const char *buf, size_t count)
654{
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)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 \
Taniya Dase0ed45f2014-07-31 11:05:51 +0530777 new_policy.min = new_policy.user_policy.min; \
778 new_policy.max = new_policy.user_policy.max; \
779 \
Dave Jones29464f22009-01-18 01:37:11 -0500780 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (ret != 1) \
782 return -EINVAL; \
783 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800784 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530785 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800786 if (!ret) \
787 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 \
789 return ret ? ret : count; \
790}
791
Dave Jones29464f22009-01-18 01:37:11 -0500792store_one(scaling_min_freq, min);
793store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795/**
796 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
797 */
Dave Jones905d77c2008-03-05 14:28:32 -0500798static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
799 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530801 unsigned int cur_freq = __cpufreq_get(policy);
Rafael J. Wysockibb8c61a2017-03-15 00:12:16 +0100802
803 if (cur_freq)
804 return sprintf(buf, "%u\n", cur_freq);
805
806 return sprintf(buf, "<unknown>\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/**
810 * show_scaling_governor - show the current policy for the specified CPU
811 */
Dave Jones905d77c2008-03-05 14:28:32 -0500812static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Dave Jones29464f22009-01-18 01:37:11 -0500814 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return sprintf(buf, "powersave\n");
816 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
817 return sprintf(buf, "performance\n");
818 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200819 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500820 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return -EINVAL;
822}
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/**
825 * store_scaling_governor - store policy for the specified CPU
826 */
Dave Jones905d77c2008-03-05 14:28:32 -0500827static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
828 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530830 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 char str_governor[16];
832 struct cpufreq_policy new_policy;
833
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530834 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
Dave Jones29464f22009-01-18 01:37:11 -0500836 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (ret != 1)
838 return -EINVAL;
839
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530840 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
841 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 return -EINVAL;
843
Viresh Kumar037ce832013-10-02 14:13:16 +0530844 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530845 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848/**
849 * show_scaling_driver - show the cpufreq driver currently loaded
850 */
Dave Jones905d77c2008-03-05 14:28:32 -0500851static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200853 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856/**
857 * show_scaling_available_governors - show the available CPUfreq governors
858 */
Dave Jones905d77c2008-03-05 14:28:32 -0500859static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
860 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 ssize_t i = 0;
863 struct cpufreq_governor *t;
864
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530865 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 i += sprintf(buf, "performance powersave");
867 goto out;
868 }
869
Viresh Kumarf7b27062015-01-27 14:06:09 +0530870 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500871 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
872 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200874 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500876out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 i += sprintf(&buf[i], "\n");
878 return i;
879}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700880
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800881ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
883 ssize_t i = 0;
884 unsigned int cpu;
885
Rusty Russell835481d2009-01-04 05:18:06 -0800886 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (i)
888 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
889 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
890 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500891 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 i += sprintf(&buf[i], "\n");
894 return i;
895}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800896EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700898/**
899 * show_related_cpus - show the CPUs affected by each transition even if
900 * hw coordination is in use
901 */
902static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
903{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800904 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700905}
906
907/**
908 * show_affected_cpus - show the CPUs affected by each transition
909 */
910static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
911{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800912 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700913}
914
Venki Pallipadi9e769882007-10-26 10:18:21 -0700915static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500916 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700917{
918 unsigned int freq = 0;
919 unsigned int ret;
920
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700921 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700922 return -EINVAL;
923
924 ret = sscanf(buf, "%u", &freq);
925 if (ret != 1)
926 return -EINVAL;
927
928 policy->governor->store_setspeed(policy, freq);
929
930 return count;
931}
932
933static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
934{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700935 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700936 return sprintf(buf, "<unsupported>\n");
937
938 return policy->governor->show_setspeed(policy, buf);
939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Thomas Renningere2f74f32009-11-19 12:31:01 +0100941/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200942 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100943 */
944static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
945{
946 unsigned int limit;
947 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200948 if (cpufreq_driver->bios_limit) {
949 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100950 if (!ret)
951 return sprintf(buf, "%u\n", limit);
952 }
953 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
954}
955
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200956cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
957cpufreq_freq_attr_ro(cpuinfo_min_freq);
958cpufreq_freq_attr_ro(cpuinfo_max_freq);
959cpufreq_freq_attr_ro(cpuinfo_transition_latency);
960cpufreq_freq_attr_ro(scaling_available_governors);
961cpufreq_freq_attr_ro(scaling_driver);
962cpufreq_freq_attr_ro(scaling_cur_freq);
963cpufreq_freq_attr_ro(bios_limit);
964cpufreq_freq_attr_ro(related_cpus);
965cpufreq_freq_attr_ro(affected_cpus);
966cpufreq_freq_attr_rw(scaling_min_freq);
967cpufreq_freq_attr_rw(scaling_max_freq);
968cpufreq_freq_attr_rw(scaling_governor);
969cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Dave Jones905d77c2008-03-05 14:28:32 -0500971static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 &cpuinfo_min_freq.attr,
973 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100974 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 &scaling_min_freq.attr,
976 &scaling_max_freq.attr,
977 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700978 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 &scaling_governor.attr,
980 &scaling_driver.attr,
981 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700982 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 NULL
984};
985
Dave Jones29464f22009-01-18 01:37:11 -0500986#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
987#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Dave Jones29464f22009-01-18 01:37:11 -0500989static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Dave Jones905d77c2008-03-05 14:28:32 -0500991 struct cpufreq_policy *policy = to_policy(kobj);
992 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530993 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530994
viresh kumarad7722d2013-10-18 19:10:15 +0530995 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100996 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530997 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return ret;
1000}
1001
Dave Jones905d77c2008-03-05 14:28:32 -05001002static ssize_t store(struct kobject *kobj, struct attribute *attr,
1003 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Dave Jones905d77c2008-03-05 14:28:32 -05001005 struct cpufreq_policy *policy = to_policy(kobj);
1006 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -05001007 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05301008
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +05301009 get_online_cpus();
1010
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +01001011 if (cpu_online(policy->cpu)) {
1012 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301013 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +01001014 up_write(&policy->rwsem);
1015 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301016
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +05301017 put_online_cpus();
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return ret;
1020}
1021
Dave Jones905d77c2008-03-05 14:28:32 -05001022static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023{
Dave Jones905d77c2008-03-05 14:28:32 -05001024 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001025 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 complete(&policy->kobj_unregister);
1027}
1028
Emese Revfy52cf25d2010-01-19 02:58:23 +01001029static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 .show = show,
1031 .store = store,
1032};
1033
1034static struct kobj_type ktype_cpufreq = {
1035 .sysfs_ops = &sysfs_ops,
1036 .default_attrs = default_attrs,
1037 .release = cpufreq_sysfs_release,
1038};
1039
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001040static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumar87549142015-06-10 02:13:21 +02001041{
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001042 struct device *dev = get_cpu_device(cpu);
1043
1044 if (!dev)
1045 return;
1046
1047 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1048 return;
1049
Viresh Kumar266198042016-09-12 12:07:05 +05301050 dev_dbg(dev, "%s: Adding symlink\n", __func__);
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001051 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1052 dev_err(dev, "cpufreq symlink creation failed\n");
Viresh Kumar87549142015-06-10 02:13:21 +02001053}
1054
Viresh Kumar266198042016-09-12 12:07:05 +05301055static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1056 struct device *dev)
Viresh Kumar87549142015-06-10 02:13:21 +02001057{
Viresh Kumar266198042016-09-12 12:07:05 +05301058 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1059 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar87549142015-06-10 02:13:21 +02001060}
1061
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001062static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -04001063{
1064 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -04001065 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -04001066
Dave Jones909a6942009-07-08 18:05:42 -04001067 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001068 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +05301069 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -04001070 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1071 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001072 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001073 drv_attr++;
1074 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001075 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -04001076 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1077 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001078 return ret;
Dave Jones909a6942009-07-08 18:05:42 -04001079 }
Dirk Brandewiec034b022014-10-13 08:37:40 -07001080
1081 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1082 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001083 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001084
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001085 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001086 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1087 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001088 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001089 }
Dave Jones909a6942009-07-08 18:05:42 -04001090
Viresh Kumar266198042016-09-12 12:07:05 +05301091 return 0;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301092}
1093
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001094__weak struct cpufreq_governor *cpufreq_default_governor(void)
1095{
1096 return NULL;
1097}
1098
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301099static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301100{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001101 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301102 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301103
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301104 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001105
viresh kumar6e2c89d2014-03-04 11:43:59 +08001106 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +05301107 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001108 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +08001109 pr_debug("Restoring governor %s for cpu %d\n",
1110 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001111 } else {
1112 gov = cpufreq_default_governor();
1113 if (!gov)
1114 return -ENODATA;
1115 }
viresh kumar6e2c89d2014-03-04 11:43:59 +08001116
1117 new_policy.governor = gov;
1118
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001119 /* Use the default policy if there is no last_policy. */
1120 if (cpufreq_driver->setpolicy) {
1121 if (policy->last_policy)
1122 new_policy.policy = policy->last_policy;
1123 else
1124 cpufreq_parse_governor(gov->name, &new_policy.policy,
1125 NULL);
1126 }
Dave Jonesecf7e462009-07-08 18:48:47 -04001127 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301128 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -04001129}
1130
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001131static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001132{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301133 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001134
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301135 /* Has this CPU been taken care of already? */
1136 if (cpumask_test_cpu(cpu, policy->cpus))
1137 return 0;
1138
Viresh Kumar49f18562016-02-11 17:31:12 +05301139 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001140 if (has_target())
1141 cpufreq_stop_governor(policy);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001142
Viresh Kumarfcf80582013-01-29 14:39:08 +00001143 cpumask_set_cpu(cpu, policy->cpus);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301144
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301145 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001146 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301147 if (ret)
Junjie Wu55f03862014-06-19 11:49:31 -07001148 pr_err("%s: Failed to start governor for CPU%u, policy CPU%u\n",
1149 __func__, cpu, policy->cpu);
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001150 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301151 up_write(&policy->rwsem);
1152 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001153}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Viresh Kumar11eb69b2016-02-22 16:36:42 +05301155static void handle_update(struct work_struct *work)
1156{
1157 struct cpufreq_policy *policy =
1158 container_of(work, struct cpufreq_policy, update);
1159 unsigned int cpu = policy->cpu;
1160 pr_debug("handle_update for cpu %u called\n", cpu);
1161 cpufreq_update_policy(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001164static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301165{
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301166 struct cpufreq_policy *policy;
Viresh Kumaredd4a892016-03-03 14:51:33 +05301167 int ret;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301168
1169 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1170 if (!policy)
1171 return NULL;
1172
1173 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1174 goto err_free_policy;
1175
1176 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1177 goto err_free_cpumask;
1178
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001179 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1180 goto err_free_rcpumask;
1181
Viresh Kumaredd4a892016-03-03 14:51:33 +05301182 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1183 cpufreq_global_kobject, "policy%u", cpu);
1184 if (ret) {
1185 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1186 goto err_free_real_cpus;
1187 }
1188
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301189 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301190 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301191 spin_lock_init(&policy->transition_lock);
1192 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301193 init_completion(&policy->kobj_unregister);
1194 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301195
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001196 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301197 return policy;
1198
Viresh Kumaredd4a892016-03-03 14:51:33 +05301199err_free_real_cpus:
1200 free_cpumask_var(policy->real_cpus);
Viresh Kumar2fc33842015-06-08 18:25:29 +05301201err_free_rcpumask:
1202 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301203err_free_cpumask:
1204 free_cpumask_var(policy->cpus);
1205err_free_policy:
1206 kfree(policy);
1207
1208 return NULL;
1209}
1210
Viresh Kumar2fc33842015-06-08 18:25:29 +05301211static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301212{
1213 struct kobject *kobj;
1214 struct completion *cmp;
1215
Viresh Kumar2fc33842015-06-08 18:25:29 +05301216 if (notify)
1217 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1218 CPUFREQ_REMOVE_POLICY, policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301219
Viresh Kumar87549142015-06-10 02:13:21 +02001220 down_write(&policy->rwsem);
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001221 cpufreq_stats_free_table(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301222 kobj = &policy->kobj;
1223 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001224 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301225 kobject_put(kobj);
1226
1227 /*
1228 * We need to make sure that the underlying kobj is
1229 * actually not referenced anymore by anybody before we
1230 * proceed with unloading.
1231 */
1232 pr_debug("waiting for dropping of refcount\n");
1233 wait_for_completion(cmp);
1234 pr_debug("wait complete\n");
1235}
1236
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301237static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301238{
Viresh Kumar988bed02015-05-08 11:53:45 +05301239 unsigned long flags;
1240 int cpu;
1241
1242 /* Remove policy from list */
1243 write_lock_irqsave(&cpufreq_driver_lock, flags);
1244 list_del(&policy->policy_list);
1245
1246 for_each_cpu(cpu, policy->related_cpus)
1247 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1248 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1249
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301250 cpufreq_policy_put_kobj(policy, notify);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001251 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301252 free_cpumask_var(policy->related_cpus);
1253 free_cpumask_var(policy->cpus);
1254 kfree(policy);
1255}
1256
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001257static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301259 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001260 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001262 unsigned int j;
1263 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001264
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001265 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301266
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301267 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301268 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001269 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301270 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001271 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001272 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001274 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001275 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001276 down_write(&policy->rwsem);
1277 policy->cpu = cpu;
1278 policy->governor = NULL;
1279 up_write(&policy->rwsem);
1280 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001281 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001282 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001283 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001284 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001285 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301286
Rusty Russell835481d2009-01-04 05:18:06 -08001287 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 /* call driver. From then on the cpufreq must be able
1290 * to accept all calls to ->verify and ->setpolicy for this CPU
1291 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001292 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001294 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301295 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001297
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001298 down_write(&policy->rwsem);
1299
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001300 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001301 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301302 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001303 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001304
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001305 /*
1306 * affected cpus must always be the one, which are online. We aren't
1307 * managing offline cpus here.
1308 */
1309 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1310
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001311 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001312 policy->user_policy.min = policy->min;
1313 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001314
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001315 for_each_cpu(j, policy->related_cpus) {
Viresh Kumar988bed02015-05-08 11:53:45 +05301316 per_cpu(cpufreq_cpu_data, j) = policy;
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001317 add_cpu_dev_symlink(policy, j);
1318 }
Santosh Mardi8a1c290f2017-01-18 17:43:23 +05301319 } else {
1320 policy->min = policy->user_policy.min;
1321 policy->max = policy->user_policy.max;
Viresh Kumar988bed02015-05-08 11:53:45 +05301322 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301323
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001324 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301325 policy->cur = cpufreq_driver->get(policy->cpu);
1326 if (!policy->cur) {
1327 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301328 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301329 }
1330 }
1331
Viresh Kumard3916692013-12-03 11:20:46 +05301332 /*
1333 * Sometimes boot loaders set CPU frequency to a value outside of
1334 * frequency table present with cpufreq core. In such cases CPU might be
1335 * unstable if it has to run on that frequency for long duration of time
1336 * and so its better to set it to a frequency which is specified in
1337 * freq-table. This also makes cpufreq stats inconsistent as
1338 * cpufreq-stats would fail to register because current frequency of CPU
1339 * isn't found in freq-table.
1340 *
1341 * Because we don't want this change to effect boot process badly, we go
1342 * for the next freq which is >= policy->cur ('cur' must be set by now,
1343 * otherwise we will end up setting freq to lowest of the table as 'cur'
1344 * is initialized to zero).
1345 *
1346 * We are passing target-freq as "policy->cur - 1" otherwise
1347 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1348 * equal to target-freq.
1349 */
1350 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1351 && has_target()) {
1352 /* Are we running at unknown frequency ? */
1353 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1354 if (ret == -EINVAL) {
1355 /* Warn user and fix it */
1356 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1357 __func__, policy->cpu, policy->cur);
1358 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1359 CPUFREQ_RELATION_L);
1360
1361 /*
1362 * Reaching here after boot in a few seconds may not
1363 * mean that system will remain stable at "unknown"
1364 * frequency for longer duration. Hence, a BUG_ON().
1365 */
1366 BUG_ON(ret);
1367 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1368 __func__, policy->cpu, policy->cur);
1369 }
1370 }
1371
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001372 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001373 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301374 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301375 goto out_exit_policy;
Rafael J. Wysocki1aefc752016-05-31 22:14:44 +02001376
1377 cpufreq_stats_create_table(policy);
Connor O'Brien6e7b83d2018-01-31 18:11:57 -08001378 cpufreq_times_create_policy(policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301379 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1380 CPUFREQ_CREATE_POLICY, policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001381
Viresh Kumar988bed02015-05-08 11:53:45 +05301382 write_lock_irqsave(&cpufreq_driver_lock, flags);
1383 list_add(&policy->policy_list, &cpufreq_policy_list);
1384 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1385 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301386
Viresh Kumar388612b2016-05-24 10:26:50 +05301387 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1388 CPUFREQ_START, policy);
1389
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301390 ret = cpufreq_init_policy(policy);
1391 if (ret) {
1392 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1393 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001394 /* cpufreq_policy_free() will notify based on this */
1395 new_policy = false;
1396 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301397 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301398
Viresh Kumar4e97b632014-03-04 11:44:01 +08001399 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301400
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001401 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301402
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301403 /* Callback for handling stuff after policy is ready */
1404 if (cpufreq_driver->ready)
1405 cpufreq_driver->ready(policy);
1406
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001407 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return 0;
1410
Viresh Kumar8101f992015-07-08 15:12:15 +05301411out_exit_policy:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001412 up_write(&policy->rwsem);
1413
Viresh Kumarda60ce92013-10-03 20:28:30 +05301414 if (cpufreq_driver->exit)
1415 cpufreq_driver->exit(policy);
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001416
1417 for_each_cpu(j, policy->real_cpus)
1418 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1419
Viresh Kumar8101f992015-07-08 15:12:15 +05301420out_free_policy:
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001421 cpufreq_policy_free(policy, !new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 return ret;
1423}
1424
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001425/**
1426 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1427 * @dev: CPU device.
1428 * @sif: Subsystem interface structure pointer (not used)
1429 */
1430static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1431{
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001432 struct cpufreq_policy *policy;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001433 unsigned cpu = dev->id;
Viresh Kumar266198042016-09-12 12:07:05 +05301434 int ret;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001435
1436 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1437
Viresh Kumar266198042016-09-12 12:07:05 +05301438 if (cpu_online(cpu)) {
1439 ret = cpufreq_online(cpu);
1440 if (ret)
1441 return ret;
1442 }
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001443
Viresh Kumar266198042016-09-12 12:07:05 +05301444 /* Create sysfs link on CPU registration */
Rafael J. Wysockia794d612016-04-07 03:31:57 +02001445 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001446 if (policy)
1447 add_cpu_dev_symlink(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Rafael J. Wysockie9a1ba22017-03-27 19:33:09 +02001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001452static int cpufreq_offline(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301454 struct cpufreq_policy *policy;
Viresh Kumar69cee712016-02-11 17:31:11 +05301455 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001457 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Viresh Kumar988bed02015-05-08 11:53:45 +05301459 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301460 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001461 pr_debug("%s: No cpu_data found\n", __func__);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Viresh Kumar49f18562016-02-11 17:31:12 +05301465 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001466 if (has_target())
1467 cpufreq_stop_governor(policy);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001468
Viresh Kumar9591bec2015-06-10 02:20:23 +02001469 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301470
Viresh Kumar9591bec2015-06-10 02:20:23 +02001471 if (policy_is_inactive(policy)) {
1472 if (has_target())
1473 strncpy(policy->last_governor, policy->governor->name,
1474 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001475 else
1476 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001477 } else if (cpu == policy->cpu) {
1478 /* Nominate new CPU */
1479 policy->cpu = cpumask_any(policy->cpus);
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Viresh Kumar9591bec2015-06-10 02:20:23 +02001482 /* Start governor again for active policy */
1483 if (!policy_is_inactive(policy)) {
1484 if (has_target()) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001485 ret = cpufreq_start_governor(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001486 if (ret)
1487 pr_err("%s: Failed to start governor\n", __func__);
1488 }
Viresh Kumar69cee712016-02-11 17:31:11 +05301489
Viresh Kumar49f18562016-02-11 17:31:12 +05301490 goto unlock;
Viresh Kumar69cee712016-02-11 17:31:11 +05301491 }
1492
1493 if (cpufreq_driver->stop_cpu)
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001494 cpufreq_driver->stop_cpu(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02001496 if (has_target())
1497 cpufreq_exit_governor(policy);
Viresh Kumar87549142015-06-10 02:13:21 +02001498
Viresh Kumar87549142015-06-10 02:13:21 +02001499 /*
1500 * Perform the ->exit() even during light-weight tear-down,
1501 * since this is a core component, and is essential for the
1502 * subsequent light-weight ->init() to succeed.
1503 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001504 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001505 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001506 policy->freq_table = NULL;
1507 }
Viresh Kumar49f18562016-02-11 17:31:12 +05301508
Maria Yu008aa442018-05-24 21:35:27 +08001509 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1510 CPUFREQ_STOP, policy);
1511
Viresh Kumar49f18562016-02-11 17:31:12 +05301512unlock:
1513 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301517/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301518 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301519 *
1520 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301521 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301522static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001523{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001524 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001525 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001526
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001527 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001528 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001529
Viresh Kumar69cee712016-02-11 17:31:11 +05301530 if (cpu_online(cpu))
1531 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001532
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001533 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301534 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301535
1536 if (cpumask_empty(policy->real_cpus))
1537 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301541 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1542 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301543 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 * @new_freq: CPU frequency the CPU actually runs at
1545 *
Dave Jones29464f22009-01-18 01:37:11 -05001546 * We adjust to current frequency first, and need to clean up later.
1547 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301549static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301550 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
1552 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301553
Joe Perchese837f9b2014-03-11 10:03:00 -07001554 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 +05301555 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301557 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301559
Viresh Kumar8fec0512014-03-24 13:35:45 +05301560 cpufreq_freq_transition_begin(policy, &freqs);
1561 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Dave Jones32ee8c32006-02-28 00:43:23 -05001564/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301565 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001566 * @cpu: CPU number
1567 *
1568 * This is the last known freq, without actually getting it from the driver.
1569 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1570 */
1571unsigned int cpufreq_quick_get(unsigned int cpu)
1572{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001573 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301574 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001575 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001576
Richard Cochranc75361c2016-03-11 09:43:07 +01001577 read_lock_irqsave(&cpufreq_driver_lock, flags);
1578
1579 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1580 ret_freq = cpufreq_driver->get(cpu);
1581 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1582 return ret_freq;
1583 }
1584
1585 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001586
1587 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001588 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301589 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001590 cpufreq_cpu_put(policy);
1591 }
1592
Dave Jones4d34a672008-02-07 16:33:49 -05001593 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001594}
1595EXPORT_SYMBOL(cpufreq_quick_get);
1596
Jesse Barnes3d737102011-06-28 10:59:12 -07001597/**
1598 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1599 * @cpu: CPU number
1600 *
1601 * Just return the max possible frequency for a given CPU.
1602 */
1603unsigned int cpufreq_quick_get_max(unsigned int cpu)
1604{
1605 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1606 unsigned int ret_freq = 0;
1607
1608 if (policy) {
1609 ret_freq = policy->max;
1610 cpufreq_cpu_put(policy);
1611 }
1612
1613 return ret_freq;
1614}
1615EXPORT_SYMBOL(cpufreq_quick_get_max);
1616
Viresh Kumard92d50a2015-01-02 12:34:29 +05301617static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301619 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001621 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001622 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Viresh Kumard92d50a2015-01-02 12:34:29 +05301624 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001626 /*
1627 * Updating inactive policies is invalid, so avoid doing that. Also
1628 * if fast frequency switching is used with the given policy, the check
1629 * against policy->cur is pointless, so skip it in that case too.
1630 */
1631 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001632 return ret_freq;
1633
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301634 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001635 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301636 /* verify no discrepancy between actual and
1637 saved value exists */
1638 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301639 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 schedule_work(&policy->update);
1641 }
1642 }
1643
Dave Jones4d34a672008-02-07 16:33:49 -05001644 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001645}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001647/**
1648 * cpufreq_get - get the current CPU frequency (in kHz)
1649 * @cpu: CPU number
1650 *
1651 * Get the CPU current (static) CPU frequency
1652 */
1653unsigned int cpufreq_get(unsigned int cpu)
1654{
Aaron Plattner999976e2014-03-04 12:42:15 -08001655 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001656 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001657
Aaron Plattner999976e2014-03-04 12:42:15 -08001658 if (policy) {
1659 down_read(&policy->rwsem);
Rafael J. Wysocki803ca5d2016-11-18 13:40:45 +01001660
1661 if (!policy_is_inactive(policy))
1662 ret_freq = __cpufreq_get(policy);
1663
Aaron Plattner999976e2014-03-04 12:42:15 -08001664 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301665
Aaron Plattner999976e2014-03-04 12:42:15 -08001666 cpufreq_cpu_put(policy);
1667 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301668
Dave Jones4d34a672008-02-07 16:33:49 -05001669 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671EXPORT_SYMBOL(cpufreq_get);
1672
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001673static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1674{
1675 unsigned int new_freq;
1676
1677 new_freq = cpufreq_driver->get(policy->cpu);
1678 if (!new_freq)
1679 return 0;
1680
1681 if (!policy->cur) {
1682 pr_debug("cpufreq: Driver did not initialize current freq\n");
1683 policy->cur = new_freq;
1684 } else if (policy->cur != new_freq && has_target()) {
1685 cpufreq_out_of_sync(policy, new_freq);
1686 }
1687
1688 return new_freq;
1689}
1690
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001691static struct subsys_interface cpufreq_interface = {
1692 .name = "cpufreq",
1693 .subsys = &cpu_subsys,
1694 .add_dev = cpufreq_add_dev,
1695 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001696};
1697
Viresh Kumare28867e2014-03-04 11:00:27 +08001698/*
1699 * In case platform wants some specific frequency to be configured
1700 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001701 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001702int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001703{
Viresh Kumare28867e2014-03-04 11:00:27 +08001704 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001705
Viresh Kumare28867e2014-03-04 11:00:27 +08001706 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001707 pr_debug("%s: suspend_freq not defined\n", __func__);
1708 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001709 }
1710
Viresh Kumare28867e2014-03-04 11:00:27 +08001711 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1712 policy->suspend_freq);
1713
1714 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1715 CPUFREQ_RELATION_H);
1716 if (ret)
1717 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1718 __func__, policy->suspend_freq, ret);
1719
Dave Jonesc9060492008-02-07 16:32:18 -05001720 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001721}
Viresh Kumare28867e2014-03-04 11:00:27 +08001722EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001723
1724/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001725 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001727 * Called during system wide Suspend/Hibernate cycles for suspending governors
1728 * as some platforms can't change frequency after this point in suspend cycle.
1729 * Because some of the devices (like: i2c, regulators, etc) they use for
1730 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001732void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301734 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001736 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001737 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001739 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301740 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001742 pr_debug("%s: Suspending Governors\n", __func__);
1743
Viresh Kumarf9637352015-05-12 12:20:11 +05301744 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001745 if (has_target()) {
1746 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001747 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001748 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001749 }
1750
1751 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001752 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1753 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301755
1756suspend:
1757 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001761 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001763 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1764 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001766void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301769 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001771 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return;
1773
Lan Tianyu8e304442014-09-18 15:03:07 +08001774 cpufreq_suspended = false;
1775
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001776 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001777 return;
1778
1779 pr_debug("%s: Resuming Governors\n", __func__);
1780
Viresh Kumarf9637352015-05-12 12:20:11 +05301781 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301782 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301783 pr_err("%s: Failed to resume driver: %p\n", __func__,
1784 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001785 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301786 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001787 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301788 up_write(&policy->rwsem);
1789
1790 if (ret)
1791 pr_err("%s: Failed to start governor for policy: %p\n",
1792 __func__, policy);
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Borislav Petkov9d950462013-01-20 10:24:28 +00001797/**
1798 * cpufreq_get_current_driver - return current driver's name
1799 *
1800 * Return the name string of the currently loaded cpufreq driver
1801 * or NULL, if none.
1802 */
1803const char *cpufreq_get_current_driver(void)
1804{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001805 if (cpufreq_driver)
1806 return cpufreq_driver->name;
1807
1808 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001809}
1810EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001812/**
1813 * cpufreq_get_driver_data - return current driver data
1814 *
1815 * Return the private data of the currently loaded cpufreq
1816 * driver, or NULL if no cpufreq driver is loaded.
1817 */
1818void *cpufreq_get_driver_data(void)
1819{
1820 if (cpufreq_driver)
1821 return cpufreq_driver->driver_data;
1822
1823 return NULL;
1824}
1825EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827/*********************************************************************
1828 * NOTIFIER LISTS INTERFACE *
1829 *********************************************************************/
1830
1831/**
1832 * cpufreq_register_notifier - register a driver with cpufreq
1833 * @nb: notifier function to register
1834 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1835 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001836 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 * are notified about clock rate changes (once before and once after
1838 * the transition), or a list of drivers that are notified about
1839 * changes in cpufreq policy.
1840 *
1841 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001842 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
1844int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1845{
1846 int ret;
1847
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001848 if (cpufreq_disabled())
1849 return -EINVAL;
1850
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001851 WARN_ON(!init_cpufreq_transition_notifier_list_called ||
1852 !init_cpufreq_govinfo_notifier_list_called);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 switch (list) {
1855 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001856 mutex_lock(&cpufreq_fast_switch_lock);
1857
1858 if (cpufreq_fast_switch_count > 0) {
1859 mutex_unlock(&cpufreq_fast_switch_lock);
1860 return -EBUSY;
1861 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001862 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001863 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001864 if (!ret)
1865 cpufreq_fast_switch_count--;
1866
1867 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001870 ret = blocking_notifier_chain_register(
1871 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001873 case CPUFREQ_GOVINFO_NOTIFIER:
1874 ret = atomic_notifier_chain_register(
1875 &cpufreq_govinfo_notifier_list, nb);
1876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 default:
1878 ret = -EINVAL;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 return ret;
1882}
1883EXPORT_SYMBOL(cpufreq_register_notifier);
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885/**
1886 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1887 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301888 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 *
1890 * Remove a driver from the CPU frequency notifier list.
1891 *
1892 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001893 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
1895int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1896{
1897 int ret;
1898
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001899 if (cpufreq_disabled())
1900 return -EINVAL;
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 switch (list) {
1903 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001904 mutex_lock(&cpufreq_fast_switch_lock);
1905
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001906 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001907 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001908 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1909 cpufreq_fast_switch_count++;
1910
1911 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 break;
1913 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001914 ret = blocking_notifier_chain_unregister(
1915 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 break;
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001917 case CPUFREQ_GOVINFO_NOTIFIER:
1918 ret = atomic_notifier_chain_unregister(
1919 &cpufreq_govinfo_notifier_list, nb);
1920 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 default:
1922 ret = -EINVAL;
1923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925 return ret;
1926}
1927EXPORT_SYMBOL(cpufreq_unregister_notifier);
1928
1929
1930/*********************************************************************
1931 * GOVERNORS *
1932 *********************************************************************/
1933
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001934/**
1935 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1936 * @policy: cpufreq policy to switch the frequency for.
1937 * @target_freq: New frequency to set (may be approximate).
1938 *
1939 * Carry out a fast frequency switch without sleeping.
1940 *
1941 * The driver's ->fast_switch() callback invoked by this function must be
1942 * suitable for being called from within RCU-sched read-side critical sections
1943 * and it is expected to select the minimum available frequency greater than or
1944 * equal to @target_freq (CPUFREQ_RELATION_L).
1945 *
1946 * This function must not be called if policy->fast_switch_enabled is unset.
1947 *
1948 * Governors calling this function must guarantee that it will never be invoked
1949 * twice in parallel for the same policy and that it will never be called in
1950 * parallel with either ->target() or ->target_index() for the same policy.
1951 *
1952 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1953 * callback to indicate an error condition, the hardware configuration must be
1954 * preserved.
1955 */
1956unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1957 unsigned int target_freq)
1958{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001959 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001960
1961 return cpufreq_driver->fast_switch(policy, target_freq);
1962}
1963EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1964
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301965/* Must set freqs->new to intermediate frequency */
1966static int __target_intermediate(struct cpufreq_policy *policy,
1967 struct cpufreq_freqs *freqs, int index)
1968{
1969 int ret;
1970
1971 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1972
1973 /* We don't need to switch to intermediate freq */
1974 if (!freqs->new)
1975 return 0;
1976
1977 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1978 __func__, policy->cpu, freqs->old, freqs->new);
1979
1980 cpufreq_freq_transition_begin(policy, freqs);
1981 ret = cpufreq_driver->target_intermediate(policy, index);
1982 cpufreq_freq_transition_end(policy, freqs, ret);
1983
1984 if (ret)
1985 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1986 __func__, ret);
1987
1988 return ret;
1989}
1990
Viresh Kumar23727842016-06-03 10:58:50 +05301991static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301992{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301993 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1994 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301995 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301996 int retval = -EINVAL;
1997 bool notify;
1998
Viresh Kumar23727842016-06-03 10:58:50 +05301999 if (newfreq == policy->cur)
2000 return 0;
2001
Viresh Kumar8d657752014-05-21 14:29:29 +05302002 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05302003 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302004 /* Handle switching to intermediate frequency */
2005 if (cpufreq_driver->get_intermediate) {
2006 retval = __target_intermediate(policy, &freqs, index);
2007 if (retval)
2008 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05302009
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302010 intermediate_freq = freqs.new;
2011 /* Set old freq to intermediate */
2012 if (intermediate_freq)
2013 freqs.old = freqs.new;
2014 }
2015
Viresh Kumar23727842016-06-03 10:58:50 +05302016 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05302017 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2018 __func__, policy->cpu, freqs.old, freqs.new);
2019
2020 cpufreq_freq_transition_begin(policy, &freqs);
2021 }
2022
2023 retval = cpufreq_driver->target_index(policy, index);
2024 if (retval)
2025 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2026 retval);
2027
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302028 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05302029 cpufreq_freq_transition_end(policy, &freqs, retval);
2030
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302031 /*
2032 * Failed after setting to intermediate freq? Driver should have
2033 * reverted back to initial frequency and so should we. Check
2034 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302035 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302036 */
2037 if (unlikely(retval && intermediate_freq)) {
2038 freqs.old = intermediate_freq;
2039 freqs.new = policy->restore_freq;
2040 cpufreq_freq_transition_begin(policy, &freqs);
2041 cpufreq_freq_transition_end(policy, &freqs, 0);
2042 }
2043 }
2044
Viresh Kumar8d657752014-05-21 14:29:29 +05302045 return retval;
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048int __cpufreq_driver_target(struct cpufreq_policy *policy,
2049 unsigned int target_freq,
2050 unsigned int relation)
2051{
Viresh Kumar72499242012-10-31 01:28:21 +01002052 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302053 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002054
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002055 if (cpufreq_disabled())
2056 return -ENODEV;
2057
Viresh Kumar72499242012-10-31 01:28:21 +01002058 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302059 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002060
2061 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002062 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002063
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302064 /* Save last value to restore later on errors */
2065 policy->restore_freq = policy->cur;
2066
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002067 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002068 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002069
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002070 if (!cpufreq_driver->target_index)
2071 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302072
Viresh Kumard218ed72016-06-03 10:58:51 +05302073 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302074
Viresh Kumar23727842016-06-03 10:58:50 +05302075 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079int cpufreq_driver_target(struct cpufreq_policy *policy,
2080 unsigned int target_freq,
2081 unsigned int relation)
2082{
Julia Lawallf1829e42008-07-25 22:44:53 +02002083 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
viresh kumarad7722d2013-10-18 19:10:15 +05302085 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 ret = __cpufreq_driver_target(policy, target_freq, relation);
2088
viresh kumarad7722d2013-10-18 19:10:15 +05302089 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return ret;
2092}
2093EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2094
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002095__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2096{
2097 return NULL;
2098}
2099
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002100static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
Dave Jonescc993ca2005-07-28 09:43:56 -07002102 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002103
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002104 /* Don't start any governor operations if we are entering suspend */
2105 if (cpufreq_suspended)
2106 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09002107 /*
2108 * Governor might not be initiated here if ACPI _PPC changed
2109 * notification happened, so check it.
2110 */
2111 if (!policy->governor)
2112 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002113
Thomas Renninger1c256242007-10-02 13:28:12 -07002114 if (policy->governor->max_transition_latency &&
2115 policy->cpuinfo.transition_latency >
2116 policy->governor->max_transition_latency) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002117 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2118
2119 if (gov) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002120 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2121 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002122 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002123 } else {
2124 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002125 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002128 if (!try_module_get(policy->governor->owner))
2129 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002131 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002132
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002133 if (policy->governor->init) {
2134 ret = policy->governor->init(policy);
2135 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002136 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002137 return ret;
2138 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002141 return 0;
2142}
2143
2144static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2145{
2146 if (cpufreq_suspended || !policy->governor)
2147 return;
2148
2149 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2150
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002151 if (policy->governor->exit)
2152 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002153
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002154 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002157static int cpufreq_start_governor(struct cpufreq_policy *policy)
2158{
2159 int ret;
2160
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002161 if (cpufreq_suspended)
2162 return 0;
2163
2164 if (!policy->governor)
2165 return -EINVAL;
2166
2167 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2168
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002169 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2170 cpufreq_update_current_freq(policy);
2171
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002172 if (policy->governor->start) {
2173 ret = policy->governor->start(policy);
2174 if (ret)
2175 return ret;
2176 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002177
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002178 if (policy->governor->limits)
2179 policy->governor->limits(policy);
2180
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002181 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002182}
2183
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002184static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2185{
2186 if (cpufreq_suspended || !policy->governor)
2187 return;
2188
2189 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2190
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002191 if (policy->governor->stop)
2192 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002193}
2194
2195static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2196{
2197 if (cpufreq_suspended || !policy->governor)
2198 return;
2199
2200 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2201
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002202 if (policy->governor->limits)
2203 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206int cpufreq_register_governor(struct cpufreq_governor *governor)
2207{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002208 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (!governor)
2211 return -EINVAL;
2212
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002213 if (cpufreq_disabled())
2214 return -ENODEV;
2215
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002216 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002217
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002218 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302219 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002220 err = 0;
2221 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Dave Jones32ee8c32006-02-28 00:43:23 -05002224 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002225 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226}
2227EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2230{
Viresh Kumar45732372015-05-12 12:22:34 +05302231 struct cpufreq_policy *policy;
2232 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (!governor)
2235 return;
2236
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002237 if (cpufreq_disabled())
2238 return;
2239
Viresh Kumar45732372015-05-12 12:22:34 +05302240 /* clear last_governor for all inactive policies */
2241 read_lock_irqsave(&cpufreq_driver_lock, flags);
2242 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302243 if (!strcmp(policy->last_governor, governor->name)) {
2244 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302245 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302246 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002247 }
Viresh Kumar45732372015-05-12 12:22:34 +05302248 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002249
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002250 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002252 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return;
2254}
2255EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2256
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258/*********************************************************************
2259 * POLICY INTERFACE *
2260 *********************************************************************/
2261
2262/**
2263 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002264 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2265 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 *
2267 * Reads the current cpufreq policy.
2268 */
2269int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2270{
2271 struct cpufreq_policy *cpu_policy;
2272 if (!policy)
2273 return -EINVAL;
2274
2275 cpu_policy = cpufreq_cpu_get(cpu);
2276 if (!cpu_policy)
2277 return -EINVAL;
2278
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302279 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return 0;
2283}
2284EXPORT_SYMBOL(cpufreq_get_policy);
2285
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002286/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302287 * policy : current policy.
2288 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002289 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302290static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302291 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002293 struct cpufreq_governor *old_gov;
2294 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Joe Perchese837f9b2014-03-11 10:03:00 -07002296 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2297 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302299 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Pan Xinhuifba95732015-07-30 18:10:40 +08002301 /*
2302 * This check works well when we store new min/max freq attributes,
2303 * because new_policy is a copy of policy with one field updated.
2304 */
2305 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002306 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302309 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002311 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002314 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302315 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Viresh Kumarbb176f72013-06-19 14:19:33 +05302317 /*
2318 * verify the cpu speed can be set within this limit, which might be
2319 * different to the first one
2320 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302321 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002322 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002323 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002326 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302327 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +01002329 scale_max_freq_capacity(policy->cpus, policy->max);
Ionela Voinescu60813f12017-12-07 19:24:41 +00002330 scale_min_freq_capacity(policy->cpus, policy->min);
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +01002331
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302332 policy->min = new_policy->min;
2333 policy->max = new_policy->max;
Ruchi Kandoi83707ea2015-11-19 16:07:19 -08002334 trace_cpu_frequency_limits(policy->max, policy->min, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Steve Mucklee3c06232016-07-13 13:25:25 -07002336 policy->cached_target_freq = UINT_MAX;
2337
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002338 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002339 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002341 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302342 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002343 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002344 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002347 if (new_policy->governor == policy->governor) {
2348 pr_debug("cpufreq: governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002349 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002350 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002351 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002352
2353 pr_debug("governor switch\n");
2354
2355 /* save old, working values */
2356 old_gov = policy->governor;
2357 /* end old governor */
2358 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002359 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002360 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002361 }
2362
2363 /* start new governor */
2364 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002365 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302366 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002367 ret = cpufreq_start_governor(policy);
2368 if (!ret) {
2369 pr_debug("cpufreq: governor change\n");
2370 return 0;
2371 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002372 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002373 }
2374
2375 /* new governor failed, so re-start old one */
2376 pr_debug("starting governor %s failed\n", policy->governor->name);
2377 if (old_gov) {
2378 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002379 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302380 policy->governor = NULL;
2381 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002382 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002383 }
2384
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302385 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
2388/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2390 * @cpu: CPU which shall be re-evaluated
2391 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002392 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 * at different times.
2394 */
2395int cpufreq_update_policy(unsigned int cpu)
2396{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302397 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2398 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002399 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002401 if (!policy)
2402 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
viresh kumarad7722d2013-10-18 19:10:15 +05302404 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Rafael J. Wysocki803ca5d2016-11-18 13:40:45 +01002406 if (policy_is_inactive(policy)) {
2407 ret = -ENODEV;
2408 goto unlock;
2409 }
2410
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002411 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302412 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302413 new_policy.min = policy->user_policy.min;
2414 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
Viresh Kumarbb176f72013-06-19 14:19:33 +05302416 /*
2417 * BIOS might change freq behind our back
2418 * -> ask driver for current freq and notify governors about a change
2419 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002420 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki742c87b2016-06-28 03:29:29 +02002421 if (cpufreq_suspended) {
2422 ret = -EAGAIN;
2423 goto unlock;
2424 }
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002425 new_policy.cur = cpufreq_update_current_freq(policy);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302426 if (WARN_ON(!new_policy.cur)) {
2427 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002428 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302429 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002430 }
2431
Viresh Kumar037ce832013-10-02 14:13:16 +05302432 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002434unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302435 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002436
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302437 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 return ret;
2439}
2440EXPORT_SYMBOL(cpufreq_update_policy);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002443 * BOOST *
2444 *********************************************************************/
2445static int cpufreq_boost_set_sw(int state)
2446{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002447 struct cpufreq_policy *policy;
2448 int ret = -EINVAL;
2449
Viresh Kumarf9637352015-05-12 12:20:11 +05302450 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302451 if (!policy->freq_table)
2452 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302453
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302454 ret = cpufreq_frequency_table_cpuinfo(policy,
2455 policy->freq_table);
2456 if (ret) {
2457 pr_err("%s: Policy frequency update failed\n",
2458 __func__);
2459 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002460 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302461
2462 down_write(&policy->rwsem);
2463 policy->user_policy.max = policy->max;
2464 cpufreq_governor_limits(policy);
2465 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002466 }
2467
2468 return ret;
2469}
2470
2471int cpufreq_boost_trigger_state(int state)
2472{
2473 unsigned long flags;
2474 int ret = 0;
2475
2476 if (cpufreq_driver->boost_enabled == state)
2477 return 0;
2478
2479 write_lock_irqsave(&cpufreq_driver_lock, flags);
2480 cpufreq_driver->boost_enabled = state;
2481 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2482
2483 ret = cpufreq_driver->set_boost(state);
2484 if (ret) {
2485 write_lock_irqsave(&cpufreq_driver_lock, flags);
2486 cpufreq_driver->boost_enabled = !state;
2487 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2488
Joe Perchese837f9b2014-03-11 10:03:00 -07002489 pr_err("%s: Cannot %s BOOST\n",
2490 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002491 }
2492
2493 return ret;
2494}
2495
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002496static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002497{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002498 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002499}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002500
Viresh Kumar44139ed2015-07-29 16:23:09 +05302501static int create_boost_sysfs_file(void)
2502{
2503 int ret;
2504
Viresh Kumarc82bd442015-10-15 21:35:23 +05302505 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302506 if (ret)
2507 pr_err("%s: cannot register global BOOST sysfs file\n",
2508 __func__);
2509
2510 return ret;
2511}
2512
2513static void remove_boost_sysfs_file(void)
2514{
2515 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302516 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302517}
2518
2519int cpufreq_enable_boost_support(void)
2520{
2521 if (!cpufreq_driver)
2522 return -EINVAL;
2523
2524 if (cpufreq_boost_supported())
2525 return 0;
2526
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002527 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302528
2529 /* This will get removed on driver unregister */
2530 return create_boost_sysfs_file();
2531}
2532EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2533
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002534int cpufreq_boost_enabled(void)
2535{
2536 return cpufreq_driver->boost_enabled;
2537}
2538EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2539
2540/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2542 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002543static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Chen Yu5dda1572017-04-09 13:45:16 +08002545static int cpuhp_cpufreq_online(unsigned int cpu)
2546{
2547 cpufreq_online(cpu);
2548
2549 return 0;
2550}
2551
2552static int cpuhp_cpufreq_offline(unsigned int cpu)
2553{
2554 cpufreq_offline(cpu);
2555
2556 return 0;
2557}
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559/**
2560 * cpufreq_register_driver - register a CPU Frequency driver
2561 * @driver_data: A struct cpufreq_driver containing the values#
2562 * submitted by the CPU Frequency driver.
2563 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302564 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002565 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002566 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 *
2568 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002569int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
2571 unsigned long flags;
2572 int ret;
2573
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002574 if (cpufreq_disabled())
2575 return -ENODEV;
2576
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302578 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002579 driver_data->target) ||
2580 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302581 driver_data->target)) ||
2582 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 return -EINVAL;
2584
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002585 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002587 /* Protect against concurrent CPU online/offline. */
2588 get_online_cpus();
2589
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002590 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002591 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002592 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002593 ret = -EEXIST;
2594 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002596 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002597 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302599 if (driver_data->setpolicy)
2600 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2601
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002602 if (cpufreq_boost_supported()) {
2603 ret = create_boost_sysfs_file();
2604 if (ret)
2605 goto err_null_driver;
2606 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002607
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002608 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002609 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002610 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302612 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2613 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* if all ->init() calls failed, unregister */
David Arcari96d7b43b2017-05-26 11:37:31 -04002615 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302616 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2617 driver_data->name);
2618 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 }
2620
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002621 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "cpufreq:online",
Chen Yu5dda1572017-04-09 13:45:16 +08002622 cpuhp_cpufreq_online,
2623 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002624 if (ret < 0)
2625 goto err_if_unreg;
2626 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002627 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002628
Junjie Wua0bf0ee2015-02-10 14:54:36 -08002629 pr_info("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002630 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002631
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002632err_if_unreg:
2633 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002634err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302635 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002636err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002637 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002638 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002639 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002640out:
2641 put_online_cpus();
2642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646/**
2647 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2648 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302649 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 * the right to do so, i.e. if you have succeeded in initialising before!
2651 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2652 * currently not initialised.
2653 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002654int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
2656 unsigned long flags;
2657
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002658 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Junjie Wua0bf0ee2015-02-10 14:54:36 -08002661 pr_info("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002663 /* Protect against concurrent cpu hotplug */
2664 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002665 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302666 remove_boost_sysfs_file();
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002667 cpuhp_remove_state_nocalls(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002669 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302670
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002671 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302672
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002673 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002674 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
2676 return 0;
2677}
2678EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002679
Doug Anderson90de2a42014-12-23 22:09:48 -08002680/*
2681 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2682 * or mutexes when secondary CPUs are halted.
2683 */
2684static struct syscore_ops cpufreq_syscore_ops = {
2685 .shutdown = cpufreq_suspend,
2686};
2687
Viresh Kumarc82bd442015-10-15 21:35:23 +05302688struct kobject *cpufreq_global_kobject;
2689EXPORT_SYMBOL(cpufreq_global_kobject);
2690
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002691static int __init cpufreq_core_init(void)
2692{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002693 if (cpufreq_disabled())
2694 return -ENODEV;
2695
Viresh Kumar8eec1022015-10-15 21:35:22 +05302696 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002697 BUG_ON(!cpufreq_global_kobject);
2698
Doug Anderson90de2a42014-12-23 22:09:48 -08002699 register_syscore_ops(&cpufreq_syscore_ops);
2700
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002701 return 0;
2702}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002703core_initcall(cpufreq_core_init);