blob: 727944891e3fd079d8d1cef7c40f2803252f5c8a [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
1509unlock:
1510 up_write(&policy->rwsem);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02001511 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301514/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301515 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301516 *
1517 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301518 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301519static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001520{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001521 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001522 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001523
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001524 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001525 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001526
Viresh Kumar69cee712016-02-11 17:31:11 +05301527 if (cpu_online(cpu))
1528 cpufreq_offline(cpu);
Viresh Kumar87549142015-06-10 02:13:21 +02001529
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001530 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar266198042016-09-12 12:07:05 +05301531 remove_cpu_dev_symlink(policy, dev);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301532
1533 if (cpumask_empty(policy->real_cpus))
1534 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001535}
1536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301538 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1539 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301540 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 * @new_freq: CPU frequency the CPU actually runs at
1542 *
Dave Jones29464f22009-01-18 01:37:11 -05001543 * We adjust to current frequency first, and need to clean up later.
1544 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301546static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301547 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
1549 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301550
Joe Perchese837f9b2014-03-11 10:03:00 -07001551 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 +05301552 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301554 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301556
Viresh Kumar8fec0512014-03-24 13:35:45 +05301557 cpufreq_freq_transition_begin(policy, &freqs);
1558 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Dave Jones32ee8c32006-02-28 00:43:23 -05001561/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301562 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001563 * @cpu: CPU number
1564 *
1565 * This is the last known freq, without actually getting it from the driver.
1566 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1567 */
1568unsigned int cpufreq_quick_get(unsigned int cpu)
1569{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001570 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301571 unsigned int ret_freq = 0;
Richard Cochranc75361c2016-03-11 09:43:07 +01001572 unsigned long flags;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001573
Richard Cochranc75361c2016-03-11 09:43:07 +01001574 read_lock_irqsave(&cpufreq_driver_lock, flags);
1575
1576 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1577 ret_freq = cpufreq_driver->get(cpu);
1578 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1579 return ret_freq;
1580 }
1581
1582 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001583
1584 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001585 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301586 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001587 cpufreq_cpu_put(policy);
1588 }
1589
Dave Jones4d34a672008-02-07 16:33:49 -05001590 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001591}
1592EXPORT_SYMBOL(cpufreq_quick_get);
1593
Jesse Barnes3d737102011-06-28 10:59:12 -07001594/**
1595 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1596 * @cpu: CPU number
1597 *
1598 * Just return the max possible frequency for a given CPU.
1599 */
1600unsigned int cpufreq_quick_get_max(unsigned int cpu)
1601{
1602 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1603 unsigned int ret_freq = 0;
1604
1605 if (policy) {
1606 ret_freq = policy->max;
1607 cpufreq_cpu_put(policy);
1608 }
1609
1610 return ret_freq;
1611}
1612EXPORT_SYMBOL(cpufreq_quick_get_max);
1613
Viresh Kumard92d50a2015-01-02 12:34:29 +05301614static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301616 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001618 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001619 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Viresh Kumard92d50a2015-01-02 12:34:29 +05301621 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001623 /*
1624 * Updating inactive policies is invalid, so avoid doing that. Also
1625 * if fast frequency switching is used with the given policy, the check
1626 * against policy->cur is pointless, so skip it in that case too.
1627 */
1628 if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
Viresh Kumar11e584c2015-06-10 02:11:45 +02001629 return ret_freq;
1630
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301631 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001632 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301633 /* verify no discrepancy between actual and
1634 saved value exists */
1635 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301636 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 schedule_work(&policy->update);
1638 }
1639 }
1640
Dave Jones4d34a672008-02-07 16:33:49 -05001641 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001642}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001644/**
1645 * cpufreq_get - get the current CPU frequency (in kHz)
1646 * @cpu: CPU number
1647 *
1648 * Get the CPU current (static) CPU frequency
1649 */
1650unsigned int cpufreq_get(unsigned int cpu)
1651{
Aaron Plattner999976e2014-03-04 12:42:15 -08001652 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001653 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001654
Aaron Plattner999976e2014-03-04 12:42:15 -08001655 if (policy) {
1656 down_read(&policy->rwsem);
Rafael J. Wysocki803ca5d2016-11-18 13:40:45 +01001657
1658 if (!policy_is_inactive(policy))
1659 ret_freq = __cpufreq_get(policy);
1660
Aaron Plattner999976e2014-03-04 12:42:15 -08001661 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301662
Aaron Plattner999976e2014-03-04 12:42:15 -08001663 cpufreq_cpu_put(policy);
1664 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301665
Dave Jones4d34a672008-02-07 16:33:49 -05001666 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668EXPORT_SYMBOL(cpufreq_get);
1669
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01001670static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1671{
1672 unsigned int new_freq;
1673
1674 new_freq = cpufreq_driver->get(policy->cpu);
1675 if (!new_freq)
1676 return 0;
1677
1678 if (!policy->cur) {
1679 pr_debug("cpufreq: Driver did not initialize current freq\n");
1680 policy->cur = new_freq;
1681 } else if (policy->cur != new_freq && has_target()) {
1682 cpufreq_out_of_sync(policy, new_freq);
1683 }
1684
1685 return new_freq;
1686}
1687
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001688static struct subsys_interface cpufreq_interface = {
1689 .name = "cpufreq",
1690 .subsys = &cpu_subsys,
1691 .add_dev = cpufreq_add_dev,
1692 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001693};
1694
Viresh Kumare28867e2014-03-04 11:00:27 +08001695/*
1696 * In case platform wants some specific frequency to be configured
1697 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001698 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001699int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001700{
Viresh Kumare28867e2014-03-04 11:00:27 +08001701 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001702
Viresh Kumare28867e2014-03-04 11:00:27 +08001703 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001704 pr_debug("%s: suspend_freq not defined\n", __func__);
1705 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001706 }
1707
Viresh Kumare28867e2014-03-04 11:00:27 +08001708 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1709 policy->suspend_freq);
1710
1711 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1712 CPUFREQ_RELATION_H);
1713 if (ret)
1714 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1715 __func__, policy->suspend_freq, ret);
1716
Dave Jonesc9060492008-02-07 16:32:18 -05001717 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001718}
Viresh Kumare28867e2014-03-04 11:00:27 +08001719EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001720
1721/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001722 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001724 * Called during system wide Suspend/Hibernate cycles for suspending governors
1725 * as some platforms can't change frequency after this point in suspend cycle.
1726 * Because some of the devices (like: i2c, regulators, etc) they use for
1727 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001729void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301731 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001733 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001734 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001736 if (!has_target() && !cpufreq_driver->suspend)
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301737 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001739 pr_debug("%s: Suspending Governors\n", __func__);
1740
Viresh Kumarf9637352015-05-12 12:20:11 +05301741 for_each_active_policy(policy) {
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001742 if (has_target()) {
1743 down_write(&policy->rwsem);
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02001744 cpufreq_stop_governor(policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001745 up_write(&policy->rwsem);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001746 }
1747
1748 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001749 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1750 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301752
1753suspend:
1754 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001758 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001760 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1761 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001763void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 struct cpufreq_policy *policy;
Viresh Kumar49f18562016-02-11 17:31:12 +05301766 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001768 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 return;
1770
Lan Tianyu8e304442014-09-18 15:03:07 +08001771 cpufreq_suspended = false;
1772
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001773 if (!has_target() && !cpufreq_driver->resume)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001774 return;
1775
1776 pr_debug("%s: Resuming Governors\n", __func__);
1777
Viresh Kumarf9637352015-05-12 12:20:11 +05301778 for_each_active_policy(policy) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301779 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301780 pr_err("%s: Failed to resume driver: %p\n", __func__,
1781 policy);
Rafael J. Wysockiba41e1b2016-05-02 02:27:19 +02001782 } else if (has_target()) {
Viresh Kumar49f18562016-02-11 17:31:12 +05301783 down_write(&policy->rwsem);
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01001784 ret = cpufreq_start_governor(policy);
Viresh Kumar49f18562016-02-11 17:31:12 +05301785 up_write(&policy->rwsem);
1786
1787 if (ret)
1788 pr_err("%s: Failed to start governor for policy: %p\n",
1789 __func__, policy);
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Borislav Petkov9d950462013-01-20 10:24:28 +00001794/**
1795 * cpufreq_get_current_driver - return current driver's name
1796 *
1797 * Return the name string of the currently loaded cpufreq driver
1798 * or NULL, if none.
1799 */
1800const char *cpufreq_get_current_driver(void)
1801{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001802 if (cpufreq_driver)
1803 return cpufreq_driver->name;
1804
1805 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001806}
1807EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001809/**
1810 * cpufreq_get_driver_data - return current driver data
1811 *
1812 * Return the private data of the currently loaded cpufreq
1813 * driver, or NULL if no cpufreq driver is loaded.
1814 */
1815void *cpufreq_get_driver_data(void)
1816{
1817 if (cpufreq_driver)
1818 return cpufreq_driver->driver_data;
1819
1820 return NULL;
1821}
1822EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824/*********************************************************************
1825 * NOTIFIER LISTS INTERFACE *
1826 *********************************************************************/
1827
1828/**
1829 * cpufreq_register_notifier - register a driver with cpufreq
1830 * @nb: notifier function to register
1831 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1832 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001833 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 * are notified about clock rate changes (once before and once after
1835 * the transition), or a list of drivers that are notified about
1836 * changes in cpufreq policy.
1837 *
1838 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001839 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 */
1841int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1842{
1843 int ret;
1844
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001845 if (cpufreq_disabled())
1846 return -EINVAL;
1847
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001848 WARN_ON(!init_cpufreq_transition_notifier_list_called ||
1849 !init_cpufreq_govinfo_notifier_list_called);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 switch (list) {
1852 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001853 mutex_lock(&cpufreq_fast_switch_lock);
1854
1855 if (cpufreq_fast_switch_count > 0) {
1856 mutex_unlock(&cpufreq_fast_switch_lock);
1857 return -EBUSY;
1858 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001859 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001860 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001861 if (!ret)
1862 cpufreq_fast_switch_count--;
1863
1864 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001867 ret = blocking_notifier_chain_register(
1868 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001870 case CPUFREQ_GOVINFO_NOTIFIER:
1871 ret = atomic_notifier_chain_register(
1872 &cpufreq_govinfo_notifier_list, nb);
1873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 default:
1875 ret = -EINVAL;
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 return ret;
1879}
1880EXPORT_SYMBOL(cpufreq_register_notifier);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882/**
1883 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1884 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301885 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 *
1887 * Remove a driver from the CPU frequency notifier list.
1888 *
1889 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001890 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
1892int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1893{
1894 int ret;
1895
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001896 if (cpufreq_disabled())
1897 return -EINVAL;
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 switch (list) {
1900 case CPUFREQ_TRANSITION_NOTIFIER:
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001901 mutex_lock(&cpufreq_fast_switch_lock);
1902
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001903 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001904 &cpufreq_transition_notifier_list, nb);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001905 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1906 cpufreq_fast_switch_count++;
1907
1908 mutex_unlock(&cpufreq_fast_switch_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001911 ret = blocking_notifier_chain_unregister(
1912 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
Rohit Gupta92e3ea92014-11-14 17:53:15 -08001914 case CPUFREQ_GOVINFO_NOTIFIER:
1915 ret = atomic_notifier_chain_unregister(
1916 &cpufreq_govinfo_notifier_list, nb);
1917 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 default:
1919 ret = -EINVAL;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 return ret;
1923}
1924EXPORT_SYMBOL(cpufreq_unregister_notifier);
1925
1926
1927/*********************************************************************
1928 * GOVERNORS *
1929 *********************************************************************/
1930
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001931/**
1932 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1933 * @policy: cpufreq policy to switch the frequency for.
1934 * @target_freq: New frequency to set (may be approximate).
1935 *
1936 * Carry out a fast frequency switch without sleeping.
1937 *
1938 * The driver's ->fast_switch() callback invoked by this function must be
1939 * suitable for being called from within RCU-sched read-side critical sections
1940 * and it is expected to select the minimum available frequency greater than or
1941 * equal to @target_freq (CPUFREQ_RELATION_L).
1942 *
1943 * This function must not be called if policy->fast_switch_enabled is unset.
1944 *
1945 * Governors calling this function must guarantee that it will never be invoked
1946 * twice in parallel for the same policy and that it will never be called in
1947 * parallel with either ->target() or ->target_index() for the same policy.
1948 *
1949 * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1950 * callback to indicate an error condition, the hardware configuration must be
1951 * preserved.
1952 */
1953unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1954 unsigned int target_freq)
1955{
Rafael J. Wysockib9af6942016-06-01 22:36:26 +02001956 target_freq = clamp_val(target_freq, policy->min, policy->max);
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02001957
1958 return cpufreq_driver->fast_switch(policy, target_freq);
1959}
1960EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1961
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301962/* Must set freqs->new to intermediate frequency */
1963static int __target_intermediate(struct cpufreq_policy *policy,
1964 struct cpufreq_freqs *freqs, int index)
1965{
1966 int ret;
1967
1968 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1969
1970 /* We don't need to switch to intermediate freq */
1971 if (!freqs->new)
1972 return 0;
1973
1974 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1975 __func__, policy->cpu, freqs->old, freqs->new);
1976
1977 cpufreq_freq_transition_begin(policy, freqs);
1978 ret = cpufreq_driver->target_intermediate(policy, index);
1979 cpufreq_freq_transition_end(policy, freqs, ret);
1980
1981 if (ret)
1982 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1983 __func__, ret);
1984
1985 return ret;
1986}
1987
Viresh Kumar23727842016-06-03 10:58:50 +05301988static int __target_index(struct cpufreq_policy *policy, int index)
Viresh Kumar8d657752014-05-21 14:29:29 +05301989{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301990 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1991 unsigned int intermediate_freq = 0;
Viresh Kumar23727842016-06-03 10:58:50 +05301992 unsigned int newfreq = policy->freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301993 int retval = -EINVAL;
1994 bool notify;
1995
Viresh Kumar23727842016-06-03 10:58:50 +05301996 if (newfreq == policy->cur)
1997 return 0;
1998
Viresh Kumar8d657752014-05-21 14:29:29 +05301999 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05302000 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302001 /* Handle switching to intermediate frequency */
2002 if (cpufreq_driver->get_intermediate) {
2003 retval = __target_intermediate(policy, &freqs, index);
2004 if (retval)
2005 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05302006
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302007 intermediate_freq = freqs.new;
2008 /* Set old freq to intermediate */
2009 if (intermediate_freq)
2010 freqs.old = freqs.new;
2011 }
2012
Viresh Kumar23727842016-06-03 10:58:50 +05302013 freqs.new = newfreq;
Viresh Kumar8d657752014-05-21 14:29:29 +05302014 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
2015 __func__, policy->cpu, freqs.old, freqs.new);
2016
2017 cpufreq_freq_transition_begin(policy, &freqs);
2018 }
2019
2020 retval = cpufreq_driver->target_index(policy, index);
2021 if (retval)
2022 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
2023 retval);
2024
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302025 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05302026 cpufreq_freq_transition_end(policy, &freqs, retval);
2027
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302028 /*
2029 * Failed after setting to intermediate freq? Driver should have
2030 * reverted back to initial frequency and so should we. Check
2031 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05302032 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302033 */
2034 if (unlikely(retval && intermediate_freq)) {
2035 freqs.old = intermediate_freq;
2036 freqs.new = policy->restore_freq;
2037 cpufreq_freq_transition_begin(policy, &freqs);
2038 cpufreq_freq_transition_end(policy, &freqs, 0);
2039 }
2040 }
2041
Viresh Kumar8d657752014-05-21 14:29:29 +05302042 return retval;
2043}
2044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045int __cpufreq_driver_target(struct cpufreq_policy *policy,
2046 unsigned int target_freq,
2047 unsigned int relation)
2048{
Viresh Kumar72499242012-10-31 01:28:21 +01002049 unsigned int old_target_freq = target_freq;
Viresh Kumard218ed72016-06-03 10:58:51 +05302050 int index;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002051
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002052 if (cpufreq_disabled())
2053 return -ENODEV;
2054
Viresh Kumar72499242012-10-31 01:28:21 +01002055 /* Make sure that target_freq is within supported range */
Viresh Kumar910c6e82016-05-26 11:27:24 +05302056 target_freq = clamp_val(target_freq, policy->min, policy->max);
Viresh Kumar72499242012-10-31 01:28:21 +01002057
2058 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002059 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002060
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302061 /* Save last value to restore later on errors */
2062 policy->restore_freq = policy->cur;
2063
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002064 if (cpufreq_driver->target)
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002065 return cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08002066
Rafael J. Wysocki6019d232016-02-26 00:03:01 +01002067 if (!cpufreq_driver->target_index)
2068 return -EINVAL;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302069
Viresh Kumard218ed72016-06-03 10:58:51 +05302070 index = cpufreq_frequency_table_target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302071
Viresh Kumar23727842016-06-03 10:58:50 +05302072 return __target_index(policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073}
2074EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076int cpufreq_driver_target(struct cpufreq_policy *policy,
2077 unsigned int target_freq,
2078 unsigned int relation)
2079{
Julia Lawallf1829e42008-07-25 22:44:53 +02002080 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
viresh kumarad7722d2013-10-18 19:10:15 +05302082 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
2084 ret = __cpufreq_driver_target(policy, target_freq, relation);
2085
viresh kumarad7722d2013-10-18 19:10:15 +05302086 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 return ret;
2089}
2090EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2091
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002092__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2093{
2094 return NULL;
2095}
2096
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002097static int cpufreq_init_governor(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Dave Jonescc993ca2005-07-28 09:43:56 -07002099 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002100
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002101 /* Don't start any governor operations if we are entering suspend */
2102 if (cpufreq_suspended)
2103 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09002104 /*
2105 * Governor might not be initiated here if ACPI _PPC changed
2106 * notification happened, so check it.
2107 */
2108 if (!policy->governor)
2109 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002110
Thomas Renninger1c256242007-10-02 13:28:12 -07002111 if (policy->governor->max_transition_latency &&
2112 policy->cpuinfo.transition_latency >
2113 policy->governor->max_transition_latency) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002114 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2115
2116 if (gov) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002117 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2118 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002119 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01002120 } else {
2121 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07002122 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002125 if (!try_module_get(policy->governor->owner))
2126 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002128 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002129
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002130 if (policy->governor->init) {
2131 ret = policy->governor->init(policy);
2132 if (ret) {
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002133 module_put(policy->governor->owner);
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002134 return ret;
2135 }
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002138 return 0;
2139}
2140
2141static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2142{
2143 if (cpufreq_suspended || !policy->governor)
2144 return;
2145
2146 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2147
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002148 if (policy->governor->exit)
2149 policy->governor->exit(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002150
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002151 module_put(policy->governor->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002154static int cpufreq_start_governor(struct cpufreq_policy *policy)
2155{
2156 int ret;
2157
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002158 if (cpufreq_suspended)
2159 return 0;
2160
2161 if (!policy->governor)
2162 return -EINVAL;
2163
2164 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2165
Rafael J. Wysocki3bbf8fe2016-03-21 15:47:48 +01002166 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2167 cpufreq_update_current_freq(policy);
2168
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002169 if (policy->governor->start) {
2170 ret = policy->governor->start(policy);
2171 if (ret)
2172 return ret;
2173 }
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002174
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002175 if (policy->governor->limits)
2176 policy->governor->limits(policy);
2177
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002178 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002179}
2180
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002181static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2182{
2183 if (cpufreq_suspended || !policy->governor)
2184 return;
2185
2186 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2187
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002188 if (policy->governor->stop)
2189 policy->governor->stop(policy);
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002190}
2191
2192static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2193{
2194 if (cpufreq_suspended || !policy->governor)
2195 return;
2196
2197 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2198
Rafael J. Wysockie7888922016-06-02 23:24:15 +02002199 if (policy->governor->limits)
2200 policy->governor->limits(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203int cpufreq_register_governor(struct cpufreq_governor *governor)
2204{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 if (!governor)
2208 return -EINVAL;
2209
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002210 if (cpufreq_disabled())
2211 return -ENODEV;
2212
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002213 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002214
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002215 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302216 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002217 err = 0;
2218 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Dave Jones32ee8c32006-02-28 00:43:23 -05002221 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002222 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2225
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2227{
Viresh Kumar45732372015-05-12 12:22:34 +05302228 struct cpufreq_policy *policy;
2229 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (!governor)
2232 return;
2233
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002234 if (cpufreq_disabled())
2235 return;
2236
Viresh Kumar45732372015-05-12 12:22:34 +05302237 /* clear last_governor for all inactive policies */
2238 read_lock_irqsave(&cpufreq_driver_lock, flags);
2239 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302240 if (!strcmp(policy->last_governor, governor->name)) {
2241 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302242 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302243 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002244 }
Viresh Kumar45732372015-05-12 12:22:34 +05302245 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002246
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002247 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002249 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return;
2251}
2252EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2253
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255/*********************************************************************
2256 * POLICY INTERFACE *
2257 *********************************************************************/
2258
2259/**
2260 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002261 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2262 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 *
2264 * Reads the current cpufreq policy.
2265 */
2266int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2267{
2268 struct cpufreq_policy *cpu_policy;
2269 if (!policy)
2270 return -EINVAL;
2271
2272 cpu_policy = cpufreq_cpu_get(cpu);
2273 if (!cpu_policy)
2274 return -EINVAL;
2275
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302276 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return 0;
2280}
2281EXPORT_SYMBOL(cpufreq_get_policy);
2282
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002283/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302284 * policy : current policy.
2285 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002286 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302287static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302288 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002290 struct cpufreq_governor *old_gov;
2291 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Joe Perchese837f9b2014-03-11 10:03:00 -07002293 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2294 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302296 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
Pan Xinhuifba95732015-07-30 18:10:40 +08002298 /*
2299 * This check works well when we store new min/max freq attributes,
2300 * because new_policy is a copy of policy with one field updated.
2301 */
2302 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002303 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302306 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002308 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002311 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302312 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
Viresh Kumarbb176f72013-06-19 14:19:33 +05302314 /*
2315 * verify the cpu speed can be set within this limit, which might be
2316 * different to the first one
2317 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302318 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002319 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002320 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002323 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302324 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Dietmar Eggemann3fb2d4b2017-07-13 12:11:11 +01002326 scale_max_freq_capacity(policy->cpus, policy->max);
Ionela Voinescu60813f12017-12-07 19:24:41 +00002327 scale_min_freq_capacity(policy->cpus, policy->min);
Dietmar Eggemannc33be5d2015-09-17 16:10:56 +01002328
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302329 policy->min = new_policy->min;
2330 policy->max = new_policy->max;
Ruchi Kandoi83707ea2015-11-19 16:07:19 -08002331 trace_cpu_frequency_limits(policy->max, policy->min, policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Steve Mucklee3c06232016-07-13 13:25:25 -07002333 policy->cached_target_freq = UINT_MAX;
2334
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002335 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002336 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002338 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302339 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002340 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002341 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 }
2343
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002344 if (new_policy->governor == policy->governor) {
2345 pr_debug("cpufreq: governor limits update\n");
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002346 cpufreq_governor_limits(policy);
Rafael J. Wysockid6ff44d2016-05-14 00:59:27 +02002347 return 0;
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002348 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002349
2350 pr_debug("governor switch\n");
2351
2352 /* save old, working values */
2353 old_gov = policy->governor;
2354 /* end old governor */
2355 if (old_gov) {
Rafael J. Wysocki45482c72016-05-12 15:14:12 +02002356 cpufreq_stop_governor(policy);
Rafael J. Wysocki36be3412016-05-12 15:13:35 +02002357 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002358 }
2359
2360 /* start new governor */
2361 policy->governor = new_policy->governor;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002362 ret = cpufreq_init_governor(policy);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302363 if (!ret) {
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002364 ret = cpufreq_start_governor(policy);
2365 if (!ret) {
2366 pr_debug("cpufreq: governor change\n");
2367 return 0;
2368 }
Rafael J. Wysockib7898fd2016-03-30 03:47:49 +02002369 cpufreq_exit_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002370 }
2371
2372 /* new governor failed, so re-start old one */
2373 pr_debug("starting governor %s failed\n", policy->governor->name);
2374 if (old_gov) {
2375 policy->governor = old_gov;
Rafael J. Wysockia92604b2016-05-14 01:01:46 +02002376 if (cpufreq_init_governor(policy))
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302377 policy->governor = NULL;
2378 else
Rafael J. Wysocki0a300762016-03-21 15:45:24 +01002379 cpufreq_start_governor(policy);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002380 }
2381
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383}
2384
2385/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2387 * @cpu: CPU which shall be re-evaluated
2388 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002389 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * at different times.
2391 */
2392int cpufreq_update_policy(unsigned int cpu)
2393{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302394 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2395 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002396 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002398 if (!policy)
2399 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
viresh kumarad7722d2013-10-18 19:10:15 +05302401 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Rafael J. Wysocki803ca5d2016-11-18 13:40:45 +01002403 if (policy_is_inactive(policy)) {
2404 ret = -ENODEV;
2405 goto unlock;
2406 }
2407
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002408 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302409 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302410 new_policy.min = policy->user_policy.min;
2411 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Viresh Kumarbb176f72013-06-19 14:19:33 +05302413 /*
2414 * BIOS might change freq behind our back
2415 * -> ask driver for current freq and notify governors about a change
2416 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002417 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Rafael J. Wysocki742c87b2016-06-28 03:29:29 +02002418 if (cpufreq_suspended) {
2419 ret = -EAGAIN;
2420 goto unlock;
2421 }
Rafael J. Wysocki999f5722016-03-21 15:46:25 +01002422 new_policy.cur = cpufreq_update_current_freq(policy);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302423 if (WARN_ON(!new_policy.cur)) {
2424 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002425 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302426 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002427 }
2428
Viresh Kumar037ce832013-10-02 14:13:16 +05302429 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002431unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302432 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002433
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302434 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 return ret;
2436}
2437EXPORT_SYMBOL(cpufreq_update_policy);
2438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002440 * BOOST *
2441 *********************************************************************/
2442static int cpufreq_boost_set_sw(int state)
2443{
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002444 struct cpufreq_policy *policy;
2445 int ret = -EINVAL;
2446
Viresh Kumarf9637352015-05-12 12:20:11 +05302447 for_each_active_policy(policy) {
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302448 if (!policy->freq_table)
2449 continue;
Viresh Kumar49f18562016-02-11 17:31:12 +05302450
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302451 ret = cpufreq_frequency_table_cpuinfo(policy,
2452 policy->freq_table);
2453 if (ret) {
2454 pr_err("%s: Policy frequency update failed\n",
2455 __func__);
2456 break;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002457 }
Viresh Kumarf8bfc112016-06-03 10:58:47 +05302458
2459 down_write(&policy->rwsem);
2460 policy->user_policy.max = policy->max;
2461 cpufreq_governor_limits(policy);
2462 up_write(&policy->rwsem);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002463 }
2464
2465 return ret;
2466}
2467
2468int cpufreq_boost_trigger_state(int state)
2469{
2470 unsigned long flags;
2471 int ret = 0;
2472
2473 if (cpufreq_driver->boost_enabled == state)
2474 return 0;
2475
2476 write_lock_irqsave(&cpufreq_driver_lock, flags);
2477 cpufreq_driver->boost_enabled = state;
2478 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2479
2480 ret = cpufreq_driver->set_boost(state);
2481 if (ret) {
2482 write_lock_irqsave(&cpufreq_driver_lock, flags);
2483 cpufreq_driver->boost_enabled = !state;
2484 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2485
Joe Perchese837f9b2014-03-11 10:03:00 -07002486 pr_err("%s: Cannot %s BOOST\n",
2487 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002488 }
2489
2490 return ret;
2491}
2492
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002493static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002494{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002495 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002496}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002497
Viresh Kumar44139ed2015-07-29 16:23:09 +05302498static int create_boost_sysfs_file(void)
2499{
2500 int ret;
2501
Viresh Kumarc82bd442015-10-15 21:35:23 +05302502 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302503 if (ret)
2504 pr_err("%s: cannot register global BOOST sysfs file\n",
2505 __func__);
2506
2507 return ret;
2508}
2509
2510static void remove_boost_sysfs_file(void)
2511{
2512 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302513 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302514}
2515
2516int cpufreq_enable_boost_support(void)
2517{
2518 if (!cpufreq_driver)
2519 return -EINVAL;
2520
2521 if (cpufreq_boost_supported())
2522 return 0;
2523
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002524 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302525
2526 /* This will get removed on driver unregister */
2527 return create_boost_sysfs_file();
2528}
2529EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2530
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002531int cpufreq_boost_enabled(void)
2532{
2533 return cpufreq_driver->boost_enabled;
2534}
2535EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2536
2537/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2539 *********************************************************************/
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002540static enum cpuhp_state hp_online;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Chen Yu5dda1572017-04-09 13:45:16 +08002542static int cpuhp_cpufreq_online(unsigned int cpu)
2543{
2544 cpufreq_online(cpu);
2545
2546 return 0;
2547}
2548
2549static int cpuhp_cpufreq_offline(unsigned int cpu)
2550{
2551 cpufreq_offline(cpu);
2552
2553 return 0;
2554}
2555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556/**
2557 * cpufreq_register_driver - register a CPU Frequency driver
2558 * @driver_data: A struct cpufreq_driver containing the values#
2559 * submitted by the CPU Frequency driver.
2560 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302561 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002562 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002563 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 *
2565 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002566int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567{
2568 unsigned long flags;
2569 int ret;
2570
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002571 if (cpufreq_disabled())
2572 return -ENODEV;
2573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302575 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002576 driver_data->target) ||
2577 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302578 driver_data->target)) ||
2579 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 return -EINVAL;
2581
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002582 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002584 /* Protect against concurrent CPU online/offline. */
2585 get_online_cpus();
2586
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002587 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002588 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002589 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002590 ret = -EEXIST;
2591 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002593 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002594 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302596 if (driver_data->setpolicy)
2597 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2598
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002599 if (cpufreq_boost_supported()) {
2600 ret = create_boost_sysfs_file();
2601 if (ret)
2602 goto err_null_driver;
2603 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002604
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002605 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002606 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002607 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302609 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2610 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* if all ->init() calls failed, unregister */
David Arcari96d7b43b2017-05-26 11:37:31 -04002612 ret = -ENODEV;
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302613 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2614 driver_data->name);
2615 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002618 ret = cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "cpufreq:online",
Chen Yu5dda1572017-04-09 13:45:16 +08002619 cpuhp_cpufreq_online,
2620 cpuhp_cpufreq_offline);
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002621 if (ret < 0)
2622 goto err_if_unreg;
2623 hp_online = ret;
Sebastian Andrzej Siewior5372e052016-09-20 16:56:28 +02002624 ret = 0;
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002625
Junjie Wua0bf0ee2015-02-10 14:54:36 -08002626 pr_info("driver %s up and running\n", driver_data->name);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002627 goto out;
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002628
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002629err_if_unreg:
2630 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002631err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302632 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002633err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002634 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002635 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002636 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Pankaj Gupta3834abb2016-05-16 11:07:19 +00002637out:
2638 put_online_cpus();
2639 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643/**
2644 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2645 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302646 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 * the right to do so, i.e. if you have succeeded in initialising before!
2648 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2649 * currently not initialised.
2650 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002651int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
2653 unsigned long flags;
2654
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002655 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Junjie Wua0bf0ee2015-02-10 14:54:36 -08002658 pr_info("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002660 /* Protect against concurrent cpu hotplug */
2661 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002662 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302663 remove_boost_sysfs_file();
Sebastian Andrzej Siewior27622b02016-09-06 19:04:48 +02002664 cpuhp_remove_state_nocalls(hp_online);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002666 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302667
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002668 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302669
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002670 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002671 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 return 0;
2674}
2675EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002676
Doug Anderson90de2a42014-12-23 22:09:48 -08002677/*
2678 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2679 * or mutexes when secondary CPUs are halted.
2680 */
2681static struct syscore_ops cpufreq_syscore_ops = {
2682 .shutdown = cpufreq_suspend,
2683};
2684
Viresh Kumarc82bd442015-10-15 21:35:23 +05302685struct kobject *cpufreq_global_kobject;
2686EXPORT_SYMBOL(cpufreq_global_kobject);
2687
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002688static int __init cpufreq_core_init(void)
2689{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002690 if (cpufreq_disabled())
2691 return -ENODEV;
2692
Viresh Kumar8eec1022015-10-15 21:35:22 +05302693 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002694 BUG_ON(!cpufreq_global_kobject);
2695
Doug Anderson90de2a42014-12-23 22:09:48 -08002696 register_syscore_ops(&cpufreq_syscore_ops);
2697
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002698 return 0;
2699}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002700core_initcall(cpufreq_core_init);