blob: 9be654375657a197ddd65bab852476f6fbed3190 [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>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Viresh Kumar2f0aea92014-03-04 11:00:26 +080029#include <linux/suspend.h>
Doug Anderson90de2a42014-12-23 22:09:48 -080030#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053031#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020032#include <trace/events/power.h>
33
Viresh Kumarb4f06762015-01-27 14:06:08 +053034static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarf9637352015-05-12 12:20:11 +053035
36static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37{
38 return cpumask_empty(policy->cpus);
39}
40
Viresh Kumarf9637352015-05-12 12:20:11 +053041/* Macros to iterate over CPU policies */
Eric Biggersfd7dc7e2016-02-21 12:53:12 -060042#define for_each_suitable_policy(__policy, __active) \
43 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
44 if ((__active) == !policy_is_inactive(__policy))
Viresh Kumarf9637352015-05-12 12:20:11 +053045
46#define for_each_active_policy(__policy) \
47 for_each_suitable_policy(__policy, true)
48#define for_each_inactive_policy(__policy) \
49 for_each_suitable_policy(__policy, false)
50
51#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053052 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
53
Viresh Kumarf7b27062015-01-27 14:06:09 +053054/* Iterate over governors */
55static LIST_HEAD(cpufreq_governor_list);
56#define for_each_governor(__governor) \
57 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/**
Dave Jonescd878472006-08-11 17:59:28 -040060 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 * level driver of CPUFreq support, and its spinlock. This lock
62 * also protects the cpufreq_cpu_data array.
63 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020064static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070065static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Viresh Kumarbb176f72013-06-19 14:19:33 +053066static DEFINE_RWLOCK(cpufreq_driver_lock);
Jane Li6f1e4ef2014-01-03 17:17:41 +080067DEFINE_MUTEX(cpufreq_governor_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +053068
Viresh Kumar2f0aea92014-03-04 11:00:26 +080069/* Flag to suspend/resume CPUFreq governors */
70static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053072static inline bool has_target(void)
73{
74 return cpufreq_driver->target_index || cpufreq_driver->target;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050078static int __cpufreq_governor(struct cpufreq_policy *policy,
79 unsigned int event);
Viresh Kumard92d50a2015-01-02 12:34:29 +053080static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
David Howells65f27f32006-11-22 14:55:48 +000081static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/**
Dave Jones32ee8c32006-02-28 00:43:23 -050084 * Two notifier lists: the "policy" list is involved in the
85 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 * "transition" list for kernel code that needs to handle
87 * changes to devices when the CPU clock speed changes.
88 * The mutex locks both lists.
89 */
Alan Sterne041c682006-03-27 01:16:30 -080090static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070091static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020093static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070094static int __init init_cpufreq_transition_notifier_list(void)
95{
96 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020097 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070098 return 0;
99}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800100pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400102static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200103static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400104{
105 return off;
106}
107void disable_cpufreq(void)
108{
109 off = 1;
110}
Dave Jones29464f22009-01-18 01:37:11 -0500111static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000113bool have_governor_per_policy(void)
114{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530115 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000116}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000117EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000118
Viresh Kumar944e9a02013-05-16 05:09:57 +0000119struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
120{
121 if (have_governor_per_policy())
122 return &policy->kobj;
123 else
124 return cpufreq_global_kobject;
125}
126EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
127
Viresh Kumar5a31d592015-07-10 01:43:27 +0200128struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
129{
130 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
131
132 return policy && !policy_is_inactive(policy) ?
133 policy->freq_table : NULL;
134}
135EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
136
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000137static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
138{
139 u64 idle_time;
140 u64 cur_wall_time;
141 u64 busy_time;
142
143 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
144
145 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
146 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
147 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
148 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
149 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
150 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
151
152 idle_time = cur_wall_time - busy_time;
153 if (wall)
154 *wall = cputime_to_usecs(cur_wall_time);
155
156 return cputime_to_usecs(idle_time);
157}
158
159u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
160{
161 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
162
163 if (idle_time == -1ULL)
164 return get_cpu_idle_time_jiffy(cpu, wall);
165 else if (!io_busy)
166 idle_time += get_cpu_iowait_time_us(cpu, wall);
167
168 return idle_time;
169}
170EXPORT_SYMBOL_GPL(get_cpu_idle_time);
171
Viresh Kumar70e9e772013-10-03 20:29:07 +0530172/*
173 * This is a generic cpufreq init() routine which can be used by cpufreq
174 * drivers of SMP systems. It will do following:
175 * - validate & show freq table passed
176 * - set policies transition latency
177 * - policy->cpus with all possible CPUs
178 */
179int cpufreq_generic_init(struct cpufreq_policy *policy,
180 struct cpufreq_frequency_table *table,
181 unsigned int transition_latency)
182{
183 int ret;
184
185 ret = cpufreq_table_validate_and_show(policy, table);
186 if (ret) {
187 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
188 return ret;
189 }
190
191 policy->cpuinfo.transition_latency = transition_latency;
192
193 /*
Shailendra Verma58405af2015-05-22 22:48:22 +0530194 * The driver only supports the SMP configuration where all processors
Viresh Kumar70e9e772013-10-03 20:29:07 +0530195 * share the clock and voltage and clock.
196 */
197 cpumask_setall(policy->cpus);
198
199 return 0;
200}
201EXPORT_SYMBOL_GPL(cpufreq_generic_init);
202
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200203struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
Viresh Kumar652ed952014-01-09 20:38:43 +0530204{
205 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
206
Viresh Kumar988bed02015-05-08 11:53:45 +0530207 return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
208}
Rafael J. Wysocki1f0bd442015-09-16 02:17:49 +0200209EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
Viresh Kumar988bed02015-05-08 11:53:45 +0530210
211unsigned int cpufreq_generic_get(unsigned int cpu)
212{
213 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
214
Viresh Kumar652ed952014-01-09 20:38:43 +0530215 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700216 pr_err("%s: No %s associated to cpu: %d\n",
217 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530218 return 0;
219 }
220
221 return clk_get_rate(policy->clk) / 1000;
222}
223EXPORT_SYMBOL_GPL(cpufreq_generic_get);
224
Viresh Kumar50e9c852015-02-19 17:02:03 +0530225/**
226 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
227 *
228 * @cpu: cpu to find policy for.
229 *
230 * This returns policy for 'cpu', returns NULL if it doesn't exist.
231 * It also increments the kobject reference count to mark it busy and so would
232 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
233 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
234 * freed as that depends on the kobj count.
235 *
Viresh Kumar50e9c852015-02-19 17:02:03 +0530236 * Return: A valid policy on success, otherwise NULL on failure.
237 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530238struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530240 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 unsigned long flags;
242
Viresh Kumar1b947c92015-02-19 17:02:05 +0530243 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530244 return NULL;
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000247 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Viresh Kumar6eed9402013-08-06 22:53:11 +0530249 if (cpufreq_driver) {
250 /* get the CPU */
Viresh Kumar988bed02015-05-08 11:53:45 +0530251 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530252 if (policy)
253 kobject_get(&policy->kobj);
254 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200255
Viresh Kumar6eed9402013-08-06 22:53:11 +0530256 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530258 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
261
Viresh Kumar50e9c852015-02-19 17:02:03 +0530262/**
263 * cpufreq_cpu_put: Decrements the usage count of a policy
264 *
265 * @policy: policy earlier returned by cpufreq_cpu_get().
266 *
267 * This decrements the kobject reference count incremented earlier by calling
268 * cpufreq_cpu_get().
Viresh Kumar50e9c852015-02-19 17:02:03 +0530269 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530270void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530272 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
278 *********************************************************************/
279
280/**
281 * adjust_jiffies - adjust the system "loops_per_jiffy"
282 *
283 * This function alters the system "loops_per_jiffy" for the clock
284 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500285 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * per-CPU loops_per_jiffy value wherever possible.
287 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800288static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530290#ifndef CONFIG_SMP
291 static unsigned long l_p_j_ref;
292 static unsigned int l_p_j_ref_freq;
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (ci->flags & CPUFREQ_CONST_LOOPS)
295 return;
296
297 if (!l_p_j_ref_freq) {
298 l_p_j_ref = loops_per_jiffy;
299 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700300 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
301 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530303 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530304 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
305 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700306 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
307 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530312static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530313 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 BUG_ON(irqs_disabled());
316
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000317 if (cpufreq_disabled())
318 return;
319
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200320 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200321 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700322 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500327 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800328 * which is not equal to what the cpufreq core thinks is
329 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200331 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800332 if ((policy) && (policy->cpu == freqs->cpu) &&
333 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700334 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
335 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800336 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700339 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800340 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
342 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 case CPUFREQ_POSTCHANGE:
345 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700346 pr_debug("FREQ: %lu - CPU: %lu\n",
347 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100348 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700349 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800350 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800351 if (likely(policy) && likely(policy->cpu == freqs->cpu))
352 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 break;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530356
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530357/**
358 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
359 * on frequency transition.
360 *
361 * This function calls the transition notifiers and the "adjust_jiffies"
362 * function. It is called twice on all CPU frequency changes that have
363 * external effects.
364 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530365static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530366 struct cpufreq_freqs *freqs, unsigned int state)
367{
368 for_each_cpu(freqs->cpu, policy->cpus)
369 __cpufreq_notify_transition(policy, freqs, state);
370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530372/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530373static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530374 struct cpufreq_freqs *freqs, int transition_failed)
375{
376 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
377 if (!transition_failed)
378 return;
379
380 swap(freqs->old, freqs->new);
381 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
382 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
383}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530384
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530385void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
386 struct cpufreq_freqs *freqs)
387{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530388
389 /*
390 * Catch double invocations of _begin() which lead to self-deadlock.
391 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
392 * doesn't invoke _begin() on their behalf, and hence the chances of
393 * double invocations are very low. Moreover, there are scenarios
394 * where these checks can emit false-positive warnings in these
395 * drivers; so we avoid that by skipping them altogether.
396 */
397 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
398 && current == policy->transition_task);
399
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530400wait:
401 wait_event(policy->transition_wait, !policy->transition_ongoing);
402
403 spin_lock(&policy->transition_lock);
404
405 if (unlikely(policy->transition_ongoing)) {
406 spin_unlock(&policy->transition_lock);
407 goto wait;
408 }
409
410 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530411 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530412
413 spin_unlock(&policy->transition_lock);
414
415 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
416}
417EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
418
419void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
420 struct cpufreq_freqs *freqs, int transition_failed)
421{
422 if (unlikely(WARN_ON(!policy->transition_ongoing)))
423 return;
424
425 cpufreq_notify_post_transition(policy, freqs, transition_failed);
426
427 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530428 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530429
430 wake_up(&policy->transition_wait);
431}
432EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*********************************************************************
436 * SYSFS INTERFACE *
437 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530438static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100439 struct attribute *attr, char *buf)
440{
441 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
442}
443
444static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
445 const char *buf, size_t count)
446{
447 int ret, enable;
448
449 ret = sscanf(buf, "%d", &enable);
450 if (ret != 1 || enable < 0 || enable > 1)
451 return -EINVAL;
452
453 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700454 pr_err("%s: Cannot %s BOOST!\n",
455 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100456 return -EINVAL;
457 }
458
Joe Perchese837f9b2014-03-11 10:03:00 -0700459 pr_debug("%s: cpufreq BOOST %s\n",
460 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100461
462 return count;
463}
464define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530466static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700467{
468 struct cpufreq_governor *t;
469
Viresh Kumarf7b27062015-01-27 14:06:09 +0530470 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200471 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700472 return t;
473
474 return NULL;
475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/**
478 * cpufreq_parse_governor - parse a governor string
479 */
Dave Jones905d77c2008-03-05 14:28:32 -0500480static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 struct cpufreq_governor **governor)
482{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700483 int err = -EINVAL;
484
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200485 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200486 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700488 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200489 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530490 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700492 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530494 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700496
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800497 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700498
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530499 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700500
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700501 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700502 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700503
Kees Cook1a8e1462011-05-04 08:38:56 -0700504 mutex_unlock(&cpufreq_governor_mutex);
505 ret = request_module("cpufreq_%s", str_governor);
506 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700507
Kees Cook1a8e1462011-05-04 08:38:56 -0700508 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530509 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700510 }
511
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700512 if (t != NULL) {
513 *governor = t;
514 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700516
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800517 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530523 * cpufreq_per_cpu_attr_read() / show_##file_name() -
524 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *
526 * Write out information from cpufreq_driver->policy[cpu]; object must be
527 * "unsigned int".
528 */
529
Dave Jones32ee8c32006-02-28 00:43:23 -0500530#define show_one(file_name, object) \
531static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500532(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500533{ \
Dave Jones29464f22009-01-18 01:37:11 -0500534 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537show_one(cpuinfo_min_freq, cpuinfo.min_freq);
538show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100539show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540show_one(scaling_min_freq, min);
541show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700542
Viresh Kumar09347b22015-01-02 12:34:24 +0530543static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700544{
545 ssize_t ret;
546
547 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
548 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
549 else
550 ret = sprintf(buf, "%u\n", policy->cur);
551 return ret;
552}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Viresh Kumar037ce832013-10-02 14:13:16 +0530554static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530555 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557/**
558 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
559 */
560#define store_one(file_name, object) \
561static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500562(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800564 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct cpufreq_policy new_policy; \
566 \
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530567 memcpy(&new_policy, policy, sizeof(*policy)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 \
Dave Jones29464f22009-01-18 01:37:11 -0500569 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 if (ret != 1) \
571 return -EINVAL; \
572 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800573 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530574 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800575 if (!ret) \
576 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 \
578 return ret ? ret : count; \
579}
580
Dave Jones29464f22009-01-18 01:37:11 -0500581store_one(scaling_min_freq, min);
582store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584/**
585 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
586 */
Dave Jones905d77c2008-03-05 14:28:32 -0500587static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
588 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530590 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (!cur_freq)
592 return sprintf(buf, "<unknown>");
593 return sprintf(buf, "%u\n", cur_freq);
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/**
597 * show_scaling_governor - show the current policy for the specified CPU
598 */
Dave Jones905d77c2008-03-05 14:28:32 -0500599static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Dave Jones29464f22009-01-18 01:37:11 -0500601 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return sprintf(buf, "powersave\n");
603 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
604 return sprintf(buf, "performance\n");
605 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200606 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500607 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -EINVAL;
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
612 * store_scaling_governor - store policy for the specified CPU
613 */
Dave Jones905d77c2008-03-05 14:28:32 -0500614static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
615 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530617 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 char str_governor[16];
619 struct cpufreq_policy new_policy;
620
Viresh Kumar8fa5b632015-08-03 08:36:15 +0530621 memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Dave Jones29464f22009-01-18 01:37:11 -0500623 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (ret != 1)
625 return -EINVAL;
626
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530627 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
628 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return -EINVAL;
630
Viresh Kumar037ce832013-10-02 14:13:16 +0530631 ret = cpufreq_set_policy(policy, &new_policy);
Viresh Kumar88dc4382015-08-03 08:36:18 +0530632 return ret ? ret : count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/**
636 * show_scaling_driver - show the cpufreq driver currently loaded
637 */
Dave Jones905d77c2008-03-05 14:28:32 -0500638static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200640 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/**
644 * show_scaling_available_governors - show the available CPUfreq governors
645 */
Dave Jones905d77c2008-03-05 14:28:32 -0500646static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
647 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 ssize_t i = 0;
650 struct cpufreq_governor *t;
651
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530652 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 i += sprintf(buf, "performance powersave");
654 goto out;
655 }
656
Viresh Kumarf7b27062015-01-27 14:06:09 +0530657 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500658 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
659 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200661 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500663out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 i += sprintf(&buf[i], "\n");
665 return i;
666}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700667
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800668ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 ssize_t i = 0;
671 unsigned int cpu;
672
Rusty Russell835481d2009-01-04 05:18:06 -0800673 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (i)
675 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
676 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
677 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500678 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 i += sprintf(&buf[i], "\n");
681 return i;
682}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800683EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700685/**
686 * show_related_cpus - show the CPUs affected by each transition even if
687 * hw coordination is in use
688 */
689static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
690{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800691 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700692}
693
694/**
695 * show_affected_cpus - show the CPUs affected by each transition
696 */
697static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
698{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800699 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700700}
701
Venki Pallipadi9e769882007-10-26 10:18:21 -0700702static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500703 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700704{
705 unsigned int freq = 0;
706 unsigned int ret;
707
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700708 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700709 return -EINVAL;
710
711 ret = sscanf(buf, "%u", &freq);
712 if (ret != 1)
713 return -EINVAL;
714
715 policy->governor->store_setspeed(policy, freq);
716
717 return count;
718}
719
720static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
721{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700722 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700723 return sprintf(buf, "<unsupported>\n");
724
725 return policy->governor->show_setspeed(policy, buf);
726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Thomas Renningere2f74f32009-11-19 12:31:01 +0100728/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200729 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100730 */
731static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
732{
733 unsigned int limit;
734 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200735 if (cpufreq_driver->bios_limit) {
736 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100737 if (!ret)
738 return sprintf(buf, "%u\n", limit);
739 }
740 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
741}
742
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200743cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
744cpufreq_freq_attr_ro(cpuinfo_min_freq);
745cpufreq_freq_attr_ro(cpuinfo_max_freq);
746cpufreq_freq_attr_ro(cpuinfo_transition_latency);
747cpufreq_freq_attr_ro(scaling_available_governors);
748cpufreq_freq_attr_ro(scaling_driver);
749cpufreq_freq_attr_ro(scaling_cur_freq);
750cpufreq_freq_attr_ro(bios_limit);
751cpufreq_freq_attr_ro(related_cpus);
752cpufreq_freq_attr_ro(affected_cpus);
753cpufreq_freq_attr_rw(scaling_min_freq);
754cpufreq_freq_attr_rw(scaling_max_freq);
755cpufreq_freq_attr_rw(scaling_governor);
756cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dave Jones905d77c2008-03-05 14:28:32 -0500758static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 &cpuinfo_min_freq.attr,
760 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100761 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 &scaling_min_freq.attr,
763 &scaling_max_freq.attr,
764 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700765 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 &scaling_governor.attr,
767 &scaling_driver.attr,
768 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700769 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 NULL
771};
772
Dave Jones29464f22009-01-18 01:37:11 -0500773#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
774#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Dave Jones29464f22009-01-18 01:37:11 -0500776static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Dave Jones905d77c2008-03-05 14:28:32 -0500778 struct cpufreq_policy *policy = to_policy(kobj);
779 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530780 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530781
viresh kumarad7722d2013-10-18 19:10:15 +0530782 down_read(&policy->rwsem);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100783 ret = fattr->show(policy, buf);
viresh kumarad7722d2013-10-18 19:10:15 +0530784 up_read(&policy->rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return ret;
787}
788
Dave Jones905d77c2008-03-05 14:28:32 -0500789static ssize_t store(struct kobject *kobj, struct attribute *attr,
790 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Dave Jones905d77c2008-03-05 14:28:32 -0500792 struct cpufreq_policy *policy = to_policy(kobj);
793 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500794 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530795
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530796 get_online_cpus();
797
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100798 if (cpu_online(policy->cpu)) {
799 down_write(&policy->rwsem);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530800 ret = fattr->store(policy, buf, count);
Rafael J. Wysocki6541aef2016-02-12 23:56:21 +0100801 up_write(&policy->rwsem);
802 }
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530803
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530804 put_online_cpus();
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ret;
807}
808
Dave Jones905d77c2008-03-05 14:28:32 -0500809static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Dave Jones905d77c2008-03-05 14:28:32 -0500811 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200812 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 complete(&policy->kobj_unregister);
814}
815
Emese Revfy52cf25d2010-01-19 02:58:23 +0100816static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .show = show,
818 .store = store,
819};
820
821static struct kobj_type ktype_cpufreq = {
822 .sysfs_ops = &sysfs_ops,
823 .default_attrs = default_attrs,
824 .release = cpufreq_sysfs_release,
825};
826
Viresh Kumar87549142015-06-10 02:13:21 +0200827static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
828{
829 struct device *cpu_dev;
830
831 pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
832
833 if (!policy)
834 return 0;
835
836 cpu_dev = get_cpu_device(cpu);
837 if (WARN_ON(!cpu_dev))
838 return 0;
839
840 return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
841}
842
843static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
844{
845 struct device *cpu_dev;
846
847 pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
848
849 cpu_dev = get_cpu_device(cpu);
850 if (WARN_ON(!cpu_dev))
851 return;
852
853 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
854}
855
856/* Add/remove symlinks for all related CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200857static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400858{
859 unsigned int j;
860 int ret = 0;
861
Viresh Kumar87549142015-06-10 02:13:21 +0200862 /* Some related CPUs might not be present (physically hotplugged) */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +0200863 for_each_cpu(j, policy->real_cpus) {
Viresh Kumar87549142015-06-10 02:13:21 +0200864 ret = add_cpu_dev_symlink(policy, j);
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200865 if (ret)
866 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400867 }
Viresh Kumar87549142015-06-10 02:13:21 +0200868
Dave Jones19d6f7e2009-07-08 17:35:39 -0400869 return ret;
870}
871
Viresh Kumar87549142015-06-10 02:13:21 +0200872static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
873{
874 unsigned int j;
875
876 /* Some related CPUs might not be present (physically hotplugged) */
Viresh Kumar96bdda62015-10-15 21:35:24 +0530877 for_each_cpu(j, policy->real_cpus)
Viresh Kumar87549142015-06-10 02:13:21 +0200878 remove_cpu_dev_symlink(policy, j);
Viresh Kumar87549142015-06-10 02:13:21 +0200879}
880
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200881static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
Dave Jones909a6942009-07-08 18:05:42 -0400882{
883 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400884 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400885
Dave Jones909a6942009-07-08 18:05:42 -0400886 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200887 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530888 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400889 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
890 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100891 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400892 drv_attr++;
893 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200894 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400895 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
896 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100897 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400898 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700899
900 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
901 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100902 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700903
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200904 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100905 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
906 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100907 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100908 }
Dave Jones909a6942009-07-08 18:05:42 -0400909
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100910 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530911}
912
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100913__weak struct cpufreq_governor *cpufreq_default_governor(void)
914{
915 return NULL;
916}
917
Viresh Kumar7f0fa402015-07-08 15:12:16 +0530918static int cpufreq_init_policy(struct cpufreq_policy *policy)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530919{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800920 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530921 struct cpufreq_policy new_policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530922
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530923 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000924
viresh kumar6e2c89d2014-03-04 11:43:59 +0800925 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar45732372015-05-12 12:22:34 +0530926 gov = find_governor(policy->last_governor);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100927 if (gov) {
viresh kumar6e2c89d2014-03-04 11:43:59 +0800928 pr_debug("Restoring governor %s for cpu %d\n",
929 policy->governor->name, policy->cpu);
Rafael J. Wysockide1df262016-02-05 02:37:42 +0100930 } else {
931 gov = cpufreq_default_governor();
932 if (!gov)
933 return -ENODATA;
934 }
viresh kumar6e2c89d2014-03-04 11:43:59 +0800935
936 new_policy.governor = gov;
937
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -0800938 /* Use the default policy if there is no last_policy. */
939 if (cpufreq_driver->setpolicy) {
940 if (policy->last_policy)
941 new_policy.policy = policy->last_policy;
942 else
943 cpufreq_parse_governor(gov->name, &new_policy.policy,
944 NULL);
945 }
Dave Jonesecf7e462009-07-08 18:48:47 -0400946 /* set default policy */
Viresh Kumar7f0fa402015-07-08 15:12:16 +0530947 return cpufreq_set_policy(policy, &new_policy);
Dave Jones909a6942009-07-08 18:05:42 -0400948}
949
Rafael J. Wysockid9612a42015-07-27 23:11:37 +0200950static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000951{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530952 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000953
Viresh Kumarbb29ae12015-02-19 17:02:06 +0530954 /* Has this CPU been taken care of already? */
955 if (cpumask_test_cpu(cpu, policy->cpus))
956 return 0;
957
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530958 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530959 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
960 if (ret) {
961 pr_err("%s: Failed to stop governor\n", __func__);
962 return ret;
963 }
964 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000965
viresh kumarad7722d2013-10-18 19:10:15 +0530966 down_write(&policy->rwsem);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000967 cpumask_set_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +0530968 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530969
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530970 if (has_target()) {
Stratos Karafotise5c87b72014-03-19 23:29:17 +0200971 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
972 if (!ret)
973 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
974
975 if (ret) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530976 pr_err("%s: Failed to start governor\n", __func__);
977 return ret;
978 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200979 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000980
Viresh Kumar87549142015-06-10 02:13:21 +0200981 return 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000982}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +0200984static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530985{
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +0200986 struct device *dev = get_cpu_device(cpu);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530987 struct cpufreq_policy *policy;
988
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +0200989 if (WARN_ON(!dev))
990 return NULL;
991
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530992 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
993 if (!policy)
994 return NULL;
995
996 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
997 goto err_free_policy;
998
999 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1000 goto err_free_cpumask;
1001
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001002 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1003 goto err_free_rcpumask;
1004
Viresh Kumar3510fac2015-10-16 12:41:12 +05301005 kobject_init(&policy->kobj, &ktype_cpufreq);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301006 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301007 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301008 spin_lock_init(&policy->transition_lock);
1009 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301010 init_completion(&policy->kobj_unregister);
1011 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301012
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001013 policy->cpu = cpu;
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301014 return policy;
1015
Viresh Kumar2fc33842015-06-08 18:25:29 +05301016err_free_rcpumask:
1017 free_cpumask_var(policy->related_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301018err_free_cpumask:
1019 free_cpumask_var(policy->cpus);
1020err_free_policy:
1021 kfree(policy);
1022
1023 return NULL;
1024}
1025
Viresh Kumar2fc33842015-06-08 18:25:29 +05301026static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301027{
1028 struct kobject *kobj;
1029 struct completion *cmp;
1030
Viresh Kumar2fc33842015-06-08 18:25:29 +05301031 if (notify)
1032 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1033 CPUFREQ_REMOVE_POLICY, policy);
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301034
Viresh Kumar87549142015-06-10 02:13:21 +02001035 down_write(&policy->rwsem);
1036 cpufreq_remove_dev_symlink(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301037 kobj = &policy->kobj;
1038 cmp = &policy->kobj_unregister;
Viresh Kumar87549142015-06-10 02:13:21 +02001039 up_write(&policy->rwsem);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301040 kobject_put(kobj);
1041
1042 /*
1043 * We need to make sure that the underlying kobj is
1044 * actually not referenced anymore by anybody before we
1045 * proceed with unloading.
1046 */
1047 pr_debug("waiting for dropping of refcount\n");
1048 wait_for_completion(cmp);
1049 pr_debug("wait complete\n");
1050}
1051
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301052static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301053{
Viresh Kumar988bed02015-05-08 11:53:45 +05301054 unsigned long flags;
1055 int cpu;
1056
1057 /* Remove policy from list */
1058 write_lock_irqsave(&cpufreq_driver_lock, flags);
1059 list_del(&policy->policy_list);
1060
1061 for_each_cpu(cpu, policy->related_cpus)
1062 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1063 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1064
Viresh Kumar3654c5c2015-06-08 18:25:30 +05301065 cpufreq_policy_put_kobj(policy, notify);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001066 free_cpumask_var(policy->real_cpus);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301067 free_cpumask_var(policy->related_cpus);
1068 free_cpumask_var(policy->cpus);
1069 kfree(policy);
1070}
1071
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001072static int cpufreq_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301074 struct cpufreq_policy *policy;
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001075 bool new_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 unsigned long flags;
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001077 unsigned int j;
1078 int ret;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001079
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001080 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301081
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301082 /* Check if this CPU already has a policy to manage it */
Viresh Kumar9104bb22015-05-12 12:22:12 +05301083 policy = per_cpu(cpufreq_cpu_data, cpu);
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001084 if (policy) {
Viresh Kumar9104bb22015-05-12 12:22:12 +05301085 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001086 if (!policy_is_inactive(policy))
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001087 return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001089 /* This is the only online CPU for the policy. Start over. */
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001090 new_policy = false;
Rafael J. Wysocki11ce7072015-07-27 23:11:21 +02001091 down_write(&policy->rwsem);
1092 policy->cpu = cpu;
1093 policy->governor = NULL;
1094 up_write(&policy->rwsem);
1095 } else {
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001096 new_policy = true;
Rafael J. Wysockia34e63b2015-07-27 23:11:50 +02001097 policy = cpufreq_policy_alloc(cpu);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001098 if (!policy)
Rafael J. Wysockid4d854d2015-07-27 23:11:30 +02001099 return -ENOMEM;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001100 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301101
Rusty Russell835481d2009-01-04 05:18:06 -08001102 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /* call driver. From then on the cpufreq must be able
1105 * to accept all calls to ->verify and ->setpolicy for this CPU
1106 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001107 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001109 pr_debug("initialization failed\n");
Viresh Kumar8101f992015-07-08 15:12:15 +05301110 goto out_free_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001112
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001113 down_write(&policy->rwsem);
1114
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001115 if (new_policy) {
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001116 /* related_cpus should at least include policy->cpus. */
Viresh Kumar0998a032015-10-15 21:35:21 +05301117 cpumask_copy(policy->related_cpus, policy->cpus);
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001118 /* Remember CPUs present at the policy creation time. */
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001119 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
Viresh Kumar3510fac2015-10-16 12:41:12 +05301120
1121 /* Name and add the kobject */
1122 ret = kobject_add(&policy->kobj, cpufreq_global_kobject,
1123 "policy%u",
1124 cpumask_first(policy->related_cpus));
1125 if (ret) {
1126 pr_err("%s: failed to add policy->kobj: %d\n", __func__,
1127 ret);
1128 goto out_exit_policy;
1129 }
Rafael J. Wysocki4d1f3a52015-07-27 23:11:44 +02001130 }
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001131
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001132 /*
1133 * affected cpus must always be the one, which are online. We aren't
1134 * managing offline cpus here.
1135 */
1136 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1137
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001138 if (new_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001139 policy->user_policy.min = policy->min;
1140 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001141
Viresh Kumar988bed02015-05-08 11:53:45 +05301142 write_lock_irqsave(&cpufreq_driver_lock, flags);
1143 for_each_cpu(j, policy->related_cpus)
1144 per_cpu(cpufreq_cpu_data, j) = policy;
1145 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1146 }
Viresh Kumar652ed952014-01-09 20:38:43 +05301147
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001148 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301149 policy->cur = cpufreq_driver->get(policy->cpu);
1150 if (!policy->cur) {
1151 pr_err("%s: ->get() failed\n", __func__);
Viresh Kumar8101f992015-07-08 15:12:15 +05301152 goto out_exit_policy;
Viresh Kumarda60ce92013-10-03 20:28:30 +05301153 }
1154 }
1155
Viresh Kumard3916692013-12-03 11:20:46 +05301156 /*
1157 * Sometimes boot loaders set CPU frequency to a value outside of
1158 * frequency table present with cpufreq core. In such cases CPU might be
1159 * unstable if it has to run on that frequency for long duration of time
1160 * and so its better to set it to a frequency which is specified in
1161 * freq-table. This also makes cpufreq stats inconsistent as
1162 * cpufreq-stats would fail to register because current frequency of CPU
1163 * isn't found in freq-table.
1164 *
1165 * Because we don't want this change to effect boot process badly, we go
1166 * for the next freq which is >= policy->cur ('cur' must be set by now,
1167 * otherwise we will end up setting freq to lowest of the table as 'cur'
1168 * is initialized to zero).
1169 *
1170 * We are passing target-freq as "policy->cur - 1" otherwise
1171 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1172 * equal to target-freq.
1173 */
1174 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1175 && has_target()) {
1176 /* Are we running at unknown frequency ? */
1177 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1178 if (ret == -EINVAL) {
1179 /* Warn user and fix it */
1180 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1181 __func__, policy->cpu, policy->cur);
1182 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1183 CPUFREQ_RELATION_L);
1184
1185 /*
1186 * Reaching here after boot in a few seconds may not
1187 * mean that system will remain stable at "unknown"
1188 * frequency for longer duration. Hence, a BUG_ON().
1189 */
1190 BUG_ON(ret);
1191 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1192 __func__, policy->cpu, policy->cur);
1193 }
1194 }
1195
Thomas Renningera1531ac2008-07-29 22:32:58 -07001196 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1197 CPUFREQ_START, policy);
1198
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001199 if (new_policy) {
Rafael J. Wysockid9612a42015-07-27 23:11:37 +02001200 ret = cpufreq_add_dev_interface(policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301201 if (ret)
Viresh Kumar8101f992015-07-08 15:12:15 +05301202 goto out_exit_policy;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301203 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1204 CPUFREQ_CREATE_POLICY, policy);
Dave Jones8ff69732006-03-05 03:37:23 -05001205
Viresh Kumar988bed02015-05-08 11:53:45 +05301206 write_lock_irqsave(&cpufreq_driver_lock, flags);
1207 list_add(&policy->policy_list, &cpufreq_policy_list);
1208 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1209 }
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301210
Viresh Kumar7f0fa402015-07-08 15:12:16 +05301211 ret = cpufreq_init_policy(policy);
1212 if (ret) {
1213 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1214 __func__, cpu, ret);
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001215 /* cpufreq_policy_free() will notify based on this */
1216 new_policy = false;
1217 goto out_exit_policy;
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301218 }
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301219
Viresh Kumar4e97b632014-03-04 11:44:01 +08001220 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301221
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001222 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301223
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301224 /* Callback for handling stuff after policy is ready */
1225 if (cpufreq_driver->ready)
1226 cpufreq_driver->ready(policy);
1227
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001228 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 return 0;
1231
Viresh Kumar8101f992015-07-08 15:12:15 +05301232out_exit_policy:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001233 up_write(&policy->rwsem);
1234
Viresh Kumarda60ce92013-10-03 20:28:30 +05301235 if (cpufreq_driver->exit)
1236 cpufreq_driver->exit(policy);
Viresh Kumar8101f992015-07-08 15:12:15 +05301237out_free_policy:
Rafael J. Wysocki194d99c2015-07-29 03:08:57 +02001238 cpufreq_policy_free(policy, !new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return ret;
1240}
1241
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02001242/**
1243 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1244 * @dev: CPU device.
1245 * @sif: Subsystem interface structure pointer (not used)
1246 */
1247static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1248{
1249 unsigned cpu = dev->id;
1250 int ret;
1251
1252 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1253
1254 if (cpu_online(cpu)) {
1255 ret = cpufreq_online(cpu);
1256 } else {
1257 /*
1258 * A hotplug notifier will follow and we will handle it as CPU
1259 * online then. For now, just create the sysfs link, unless
1260 * there is no policy or the link is already present.
1261 */
1262 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1263
1264 ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus)
1265 ? add_cpu_dev_symlink(policy, cpu) : 0;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 return ret;
1269}
1270
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001271static void cpufreq_offline_prepare(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301273 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001275 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Viresh Kumar988bed02015-05-08 11:53:45 +05301277 policy = cpufreq_cpu_get_raw(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301278 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001279 pr_debug("%s: No cpu_data found\n", __func__);
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001280 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301283 if (has_target()) {
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001284 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001285 if (ret)
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301286 pr_err("%s: Failed to stop governor\n", __func__);
Viresh Kumardb5f2992015-01-02 12:34:25 +05301287 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001288
Viresh Kumar45732372015-05-12 12:22:34 +05301289 down_write(&policy->rwsem);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001290 cpumask_clear_cpu(cpu, policy->cpus);
Viresh Kumar45732372015-05-12 12:22:34 +05301291
Viresh Kumar9591bec2015-06-10 02:20:23 +02001292 if (policy_is_inactive(policy)) {
1293 if (has_target())
1294 strncpy(policy->last_governor, policy->governor->name,
1295 CPUFREQ_NAME_LEN);
Srinivas Pandruvada69030dd2015-12-01 16:52:14 -08001296 else
1297 policy->last_policy = policy->policy;
Viresh Kumar9591bec2015-06-10 02:20:23 +02001298 } else if (cpu == policy->cpu) {
1299 /* Nominate new CPU */
1300 policy->cpu = cpumask_any(policy->cpus);
1301 }
Viresh Kumar45732372015-05-12 12:22:34 +05301302 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Viresh Kumar9591bec2015-06-10 02:20:23 +02001304 /* Start governor again for active policy */
1305 if (!policy_is_inactive(policy)) {
1306 if (has_target()) {
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001307 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001308 if (!ret)
1309 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Viresh Kumar87549142015-06-10 02:13:21 +02001310
Viresh Kumar9591bec2015-06-10 02:20:23 +02001311 if (ret)
1312 pr_err("%s: Failed to start governor\n", __func__);
1313 }
1314 } else if (cpufreq_driver->stop_cpu) {
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001315 cpufreq_driver->stop_cpu(policy);
Viresh Kumar9591bec2015-06-10 02:20:23 +02001316 }
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301317}
1318
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001319static void cpufreq_offline_finish(unsigned int cpu)
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301320{
Viresh Kumar9591bec2015-06-10 02:20:23 +02001321 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301322
1323 if (!policy) {
1324 pr_debug("%s: No cpu_data found\n", __func__);
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001325 return;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301326 }
1327
Viresh Kumar9591bec2015-06-10 02:20:23 +02001328 /* Only proceed for inactive policies */
1329 if (!policy_is_inactive(policy))
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Viresh Kumar87549142015-06-10 02:13:21 +02001332 /* If cpu is last user of policy, free policy */
1333 if (has_target()) {
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001334 int ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001335 if (ret)
Viresh Kumar87549142015-06-10 02:13:21 +02001336 pr_err("%s: Failed to exit governor\n", __func__);
Viresh Kumar87549142015-06-10 02:13:21 +02001337 }
1338
Viresh Kumar87549142015-06-10 02:13:21 +02001339 /*
1340 * Perform the ->exit() even during light-weight tear-down,
1341 * since this is a core component, and is essential for the
1342 * subsequent light-weight ->init() to succeed.
1343 */
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001344 if (cpufreq_driver->exit) {
Viresh Kumar87549142015-06-10 02:13:21 +02001345 cpufreq_driver->exit(policy);
Srinivas Pandruvada55582bc2015-10-07 13:50:44 -07001346 policy->freq_table = NULL;
1347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301350/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301351 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301352 *
1353 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301354 */
Viresh Kumar71db87b2015-07-30 15:04:01 +05301355static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001356{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001357 unsigned int cpu = dev->id;
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001358 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venki Pallipadiec282972007-03-26 12:03:19 -07001359
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001360 if (!policy)
Linus Torvalds1af115d2015-08-31 08:47:40 -07001361 return;
Viresh Kumar87549142015-06-10 02:13:21 +02001362
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001363 if (cpu_online(cpu)) {
Rafael J. Wysocki15c0b4d2015-07-27 23:11:09 +02001364 cpufreq_offline_prepare(cpu);
1365 cpufreq_offline_finish(cpu);
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001366 }
Viresh Kumar87549142015-06-10 02:13:21 +02001367
Rafael J. Wysocki559ed402015-07-26 02:07:47 +02001368 cpumask_clear_cpu(cpu, policy->real_cpus);
Viresh Kumar96bdda62015-10-15 21:35:24 +05301369 remove_cpu_dev_symlink(policy, cpu);
Viresh Kumarf344dae2015-11-21 09:06:49 +05301370
1371 if (cpumask_empty(policy->real_cpus))
1372 cpufreq_policy_free(policy, true);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001373}
1374
David Howells65f27f32006-11-22 14:55:48 +00001375static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
David Howells65f27f32006-11-22 14:55:48 +00001377 struct cpufreq_policy *policy =
1378 container_of(work, struct cpufreq_policy, update);
1379 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001380 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 cpufreq_update_policy(cpu);
1382}
1383
1384/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301385 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1386 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301387 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 * @new_freq: CPU frequency the CPU actually runs at
1389 *
Dave Jones29464f22009-01-18 01:37:11 -05001390 * We adjust to current frequency first, and need to clean up later.
1391 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301393static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301394 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301397
Joe Perchese837f9b2014-03-11 10:03:00 -07001398 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 +05301399 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301401 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301403
Viresh Kumar8fec0512014-03-24 13:35:45 +05301404 cpufreq_freq_transition_begin(policy, &freqs);
1405 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Dave Jones32ee8c32006-02-28 00:43:23 -05001408/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301409 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001410 * @cpu: CPU number
1411 *
1412 * This is the last known freq, without actually getting it from the driver.
1413 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1414 */
1415unsigned int cpufreq_quick_get(unsigned int cpu)
1416{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001417 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301418 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001419
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001420 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1421 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001422
1423 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001424 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301425 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001426 cpufreq_cpu_put(policy);
1427 }
1428
Dave Jones4d34a672008-02-07 16:33:49 -05001429 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001430}
1431EXPORT_SYMBOL(cpufreq_quick_get);
1432
Jesse Barnes3d737102011-06-28 10:59:12 -07001433/**
1434 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1435 * @cpu: CPU number
1436 *
1437 * Just return the max possible frequency for a given CPU.
1438 */
1439unsigned int cpufreq_quick_get_max(unsigned int cpu)
1440{
1441 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1442 unsigned int ret_freq = 0;
1443
1444 if (policy) {
1445 ret_freq = policy->max;
1446 cpufreq_cpu_put(policy);
1447 }
1448
1449 return ret_freq;
1450}
1451EXPORT_SYMBOL(cpufreq_quick_get_max);
1452
Viresh Kumard92d50a2015-01-02 12:34:29 +05301453static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301455 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001457 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001458 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Viresh Kumard92d50a2015-01-02 12:34:29 +05301460 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Viresh Kumar11e584c2015-06-10 02:11:45 +02001462 /* Updating inactive policies is invalid, so avoid doing that. */
1463 if (unlikely(policy_is_inactive(policy)))
1464 return ret_freq;
1465
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301466 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001467 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301468 /* verify no discrepancy between actual and
1469 saved value exists */
1470 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301471 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 schedule_work(&policy->update);
1473 }
1474 }
1475
Dave Jones4d34a672008-02-07 16:33:49 -05001476 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001477}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001479/**
1480 * cpufreq_get - get the current CPU frequency (in kHz)
1481 * @cpu: CPU number
1482 *
1483 * Get the CPU current (static) CPU frequency
1484 */
1485unsigned int cpufreq_get(unsigned int cpu)
1486{
Aaron Plattner999976e2014-03-04 12:42:15 -08001487 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001488 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001489
Aaron Plattner999976e2014-03-04 12:42:15 -08001490 if (policy) {
1491 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301492 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001493 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301494
Aaron Plattner999976e2014-03-04 12:42:15 -08001495 cpufreq_cpu_put(policy);
1496 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301497
Dave Jones4d34a672008-02-07 16:33:49 -05001498 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500EXPORT_SYMBOL(cpufreq_get);
1501
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001502static struct subsys_interface cpufreq_interface = {
1503 .name = "cpufreq",
1504 .subsys = &cpu_subsys,
1505 .add_dev = cpufreq_add_dev,
1506 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001507};
1508
Viresh Kumare28867e2014-03-04 11:00:27 +08001509/*
1510 * In case platform wants some specific frequency to be configured
1511 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001512 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001513int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001514{
Viresh Kumare28867e2014-03-04 11:00:27 +08001515 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001516
Viresh Kumare28867e2014-03-04 11:00:27 +08001517 if (!policy->suspend_freq) {
Bartlomiej Zolnierkiewicz201f3712015-09-08 18:41:02 +02001518 pr_debug("%s: suspend_freq not defined\n", __func__);
1519 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001520 }
1521
Viresh Kumare28867e2014-03-04 11:00:27 +08001522 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1523 policy->suspend_freq);
1524
1525 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1526 CPUFREQ_RELATION_H);
1527 if (ret)
1528 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1529 __func__, policy->suspend_freq, ret);
1530
Dave Jonesc9060492008-02-07 16:32:18 -05001531 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001532}
Viresh Kumare28867e2014-03-04 11:00:27 +08001533EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001534
1535/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001536 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001538 * Called during system wide Suspend/Hibernate cycles for suspending governors
1539 * as some platforms can't change frequency after this point in suspend cycle.
1540 * Because some of the devices (like: i2c, regulators, etc) they use for
1541 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001543void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301545 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001547 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001548 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001550 if (!has_target())
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301551 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001553 pr_debug("%s: Suspending Governors\n", __func__);
1554
Viresh Kumarf9637352015-05-12 12:20:11 +05301555 for_each_active_policy(policy) {
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001556 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1557 pr_err("%s: Failed to stop governor for policy: %p\n",
1558 __func__, policy);
1559 else if (cpufreq_driver->suspend
1560 && cpufreq_driver->suspend(policy))
1561 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1562 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301564
1565suspend:
1566 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001570 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001572 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1573 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001575void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 struct cpufreq_policy *policy;
1578
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001579 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 return;
1581
Lan Tianyu8e304442014-09-18 15:03:07 +08001582 cpufreq_suspended = false;
1583
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001584 if (!has_target())
1585 return;
1586
1587 pr_debug("%s: Resuming Governors\n", __func__);
1588
Viresh Kumarf9637352015-05-12 12:20:11 +05301589 for_each_active_policy(policy) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301590 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1591 pr_err("%s: Failed to resume driver: %p\n", __func__,
1592 policy);
1593 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001594 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1595 pr_err("%s: Failed to start governor for policy: %p\n",
1596 __func__, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Viresh Kumarc75de0a2015-04-02 10:21:33 +05301598
1599 /*
1600 * schedule call cpufreq_update_policy() for first-online CPU, as that
1601 * wouldn't be hotplugged-out on suspend. It will verify that the
1602 * current freq is in sync with what we believe it to be.
1603 */
1604 policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1605 if (WARN_ON(!policy))
1606 return;
1607
1608 schedule_work(&policy->update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Borislav Petkov9d950462013-01-20 10:24:28 +00001611/**
1612 * cpufreq_get_current_driver - return current driver's name
1613 *
1614 * Return the name string of the currently loaded cpufreq driver
1615 * or NULL, if none.
1616 */
1617const char *cpufreq_get_current_driver(void)
1618{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001619 if (cpufreq_driver)
1620 return cpufreq_driver->name;
1621
1622 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001623}
1624EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001626/**
1627 * cpufreq_get_driver_data - return current driver data
1628 *
1629 * Return the private data of the currently loaded cpufreq
1630 * driver, or NULL if no cpufreq driver is loaded.
1631 */
1632void *cpufreq_get_driver_data(void)
1633{
1634 if (cpufreq_driver)
1635 return cpufreq_driver->driver_data;
1636
1637 return NULL;
1638}
1639EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/*********************************************************************
1642 * NOTIFIER LISTS INTERFACE *
1643 *********************************************************************/
1644
1645/**
1646 * cpufreq_register_notifier - register a driver with cpufreq
1647 * @nb: notifier function to register
1648 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1649 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001650 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 * are notified about clock rate changes (once before and once after
1652 * the transition), or a list of drivers that are notified about
1653 * changes in cpufreq policy.
1654 *
1655 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001656 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 */
1658int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1659{
1660 int ret;
1661
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001662 if (cpufreq_disabled())
1663 return -EINVAL;
1664
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001665 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 switch (list) {
1668 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001669 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001670 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 break;
1672 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001673 ret = blocking_notifier_chain_register(
1674 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 break;
1676 default:
1677 ret = -EINVAL;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 return ret;
1681}
1682EXPORT_SYMBOL(cpufreq_register_notifier);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/**
1685 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1686 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301687 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 *
1689 * Remove a driver from the CPU frequency notifier list.
1690 *
1691 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001692 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
1694int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1695{
1696 int ret;
1697
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001698 if (cpufreq_disabled())
1699 return -EINVAL;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 switch (list) {
1702 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001703 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001704 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 break;
1706 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001707 ret = blocking_notifier_chain_unregister(
1708 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 break;
1710 default:
1711 ret = -EINVAL;
1712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 return ret;
1715}
1716EXPORT_SYMBOL(cpufreq_unregister_notifier);
1717
1718
1719/*********************************************************************
1720 * GOVERNORS *
1721 *********************************************************************/
1722
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301723/* Must set freqs->new to intermediate frequency */
1724static int __target_intermediate(struct cpufreq_policy *policy,
1725 struct cpufreq_freqs *freqs, int index)
1726{
1727 int ret;
1728
1729 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1730
1731 /* We don't need to switch to intermediate freq */
1732 if (!freqs->new)
1733 return 0;
1734
1735 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1736 __func__, policy->cpu, freqs->old, freqs->new);
1737
1738 cpufreq_freq_transition_begin(policy, freqs);
1739 ret = cpufreq_driver->target_intermediate(policy, index);
1740 cpufreq_freq_transition_end(policy, freqs, ret);
1741
1742 if (ret)
1743 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1744 __func__, ret);
1745
1746 return ret;
1747}
1748
Viresh Kumar8d657752014-05-21 14:29:29 +05301749static int __target_index(struct cpufreq_policy *policy,
1750 struct cpufreq_frequency_table *freq_table, int index)
1751{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301752 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1753 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301754 int retval = -EINVAL;
1755 bool notify;
1756
1757 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301758 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301759 /* Handle switching to intermediate frequency */
1760 if (cpufreq_driver->get_intermediate) {
1761 retval = __target_intermediate(policy, &freqs, index);
1762 if (retval)
1763 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301764
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301765 intermediate_freq = freqs.new;
1766 /* Set old freq to intermediate */
1767 if (intermediate_freq)
1768 freqs.old = freqs.new;
1769 }
1770
1771 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301772 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1773 __func__, policy->cpu, freqs.old, freqs.new);
1774
1775 cpufreq_freq_transition_begin(policy, &freqs);
1776 }
1777
1778 retval = cpufreq_driver->target_index(policy, index);
1779 if (retval)
1780 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1781 retval);
1782
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301783 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301784 cpufreq_freq_transition_end(policy, &freqs, retval);
1785
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301786 /*
1787 * Failed after setting to intermediate freq? Driver should have
1788 * reverted back to initial frequency and so should we. Check
1789 * here for intermediate_freq instead of get_intermediate, in
Shailendra Verma58405af2015-05-22 22:48:22 +05301790 * case we haven't switched to intermediate freq at all.
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301791 */
1792 if (unlikely(retval && intermediate_freq)) {
1793 freqs.old = intermediate_freq;
1794 freqs.new = policy->restore_freq;
1795 cpufreq_freq_transition_begin(policy, &freqs);
1796 cpufreq_freq_transition_end(policy, &freqs, 0);
1797 }
1798 }
1799
Viresh Kumar8d657752014-05-21 14:29:29 +05301800 return retval;
1801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803int __cpufreq_driver_target(struct cpufreq_policy *policy,
1804 unsigned int target_freq,
1805 unsigned int relation)
1806{
Viresh Kumar72499242012-10-31 01:28:21 +01001807 unsigned int old_target_freq = target_freq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301808 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001809
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001810 if (cpufreq_disabled())
1811 return -ENODEV;
1812
Viresh Kumar72499242012-10-31 01:28:21 +01001813 /* Make sure that target_freq is within supported range */
1814 if (target_freq > policy->max)
1815 target_freq = policy->max;
1816 if (target_freq < policy->min)
1817 target_freq = policy->min;
1818
1819 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001820 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001821
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301822 /*
1823 * This might look like a redundant call as we are checking it again
1824 * after finding index. But it is left intentionally for cases where
1825 * exactly same freq is called again and so we can save on few function
1826 * calls.
1827 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001828 if (target_freq == policy->cur)
1829 return 0;
1830
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301831 /* Save last value to restore later on errors */
1832 policy->restore_freq = policy->cur;
1833
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001834 if (cpufreq_driver->target)
1835 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301836 else if (cpufreq_driver->target_index) {
1837 struct cpufreq_frequency_table *freq_table;
1838 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001839
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301840 freq_table = cpufreq_frequency_get_table(policy->cpu);
1841 if (unlikely(!freq_table)) {
1842 pr_err("%s: Unable to find freq_table\n", __func__);
1843 goto out;
1844 }
1845
1846 retval = cpufreq_frequency_table_target(policy, freq_table,
1847 target_freq, relation, &index);
1848 if (unlikely(retval)) {
1849 pr_err("%s: Unable to find matching freq\n", __func__);
1850 goto out;
1851 }
1852
Viresh Kumard4019f02013-08-14 19:38:24 +05301853 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301854 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301855 goto out;
1856 }
1857
Viresh Kumar8d657752014-05-21 14:29:29 +05301858 retval = __target_index(policy, freq_table, index);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301859 }
1860
1861out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 return retval;
1863}
1864EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866int cpufreq_driver_target(struct cpufreq_policy *policy,
1867 unsigned int target_freq,
1868 unsigned int relation)
1869{
Julia Lawallf1829e42008-07-25 22:44:53 +02001870 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
viresh kumarad7722d2013-10-18 19:10:15 +05301872 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 ret = __cpufreq_driver_target(policy, target_freq, relation);
1875
viresh kumarad7722d2013-10-18 19:10:15 +05301876 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return ret;
1879}
1880EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1881
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001882__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1883{
1884 return NULL;
1885}
1886
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301887static int __cpufreq_governor(struct cpufreq_policy *policy,
1888 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Dave Jonescc993ca2005-07-28 09:43:56 -07001890 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001891
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001892 /* Don't start any governor operations if we are entering suspend */
1893 if (cpufreq_suspended)
1894 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09001895 /*
1896 * Governor might not be initiated here if ACPI _PPC changed
1897 * notification happened, so check it.
1898 */
1899 if (!policy->governor)
1900 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001901
Thomas Renninger1c256242007-10-02 13:28:12 -07001902 if (policy->governor->max_transition_latency &&
1903 policy->cpuinfo.transition_latency >
1904 policy->governor->max_transition_latency) {
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001905 struct cpufreq_governor *gov = cpufreq_fallback_governor();
1906
1907 if (gov) {
Joe Perchese837f9b2014-03-11 10:03:00 -07001908 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
1909 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07001910 policy->governor = gov;
Rafael J. Wysockide1df262016-02-05 02:37:42 +01001911 } else {
1912 return -EINVAL;
Thomas Renninger6afde102007-10-02 13:28:13 -07001913 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Viresh Kumarfe492f32013-08-06 22:53:10 +05301916 if (event == CPUFREQ_GOV_POLICY_INIT)
1917 if (!try_module_get(policy->governor->owner))
1918 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Viresh Kumar63431f72015-07-27 17:58:06 +05301920 pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001921
1922 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301923 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301924 || (!policy->governor_enabled
1925 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001926 mutex_unlock(&cpufreq_governor_lock);
1927 return -EBUSY;
1928 }
1929
1930 if (event == CPUFREQ_GOV_STOP)
1931 policy->governor_enabled = false;
1932 else if (event == CPUFREQ_GOV_START)
1933 policy->governor_enabled = true;
1934
1935 mutex_unlock(&cpufreq_governor_lock);
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 ret = policy->governor->governor(policy, event);
1938
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001939 if (!ret) {
1940 if (event == CPUFREQ_GOV_POLICY_INIT)
1941 policy->governor->initialized++;
1942 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1943 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001944 } else {
1945 /* Restore original values */
1946 mutex_lock(&cpufreq_governor_lock);
1947 if (event == CPUFREQ_GOV_STOP)
1948 policy->governor_enabled = true;
1949 else if (event == CPUFREQ_GOV_START)
1950 policy->governor_enabled = false;
1951 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001952 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001953
Viresh Kumarfe492f32013-08-06 22:53:10 +05301954 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1955 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 module_put(policy->governor->owner);
1957
1958 return ret;
1959}
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961int cpufreq_register_governor(struct cpufreq_governor *governor)
1962{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001963 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 if (!governor)
1966 return -EINVAL;
1967
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001968 if (cpufreq_disabled())
1969 return -ENODEV;
1970
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001971 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001972
Viresh Kumarb3940582013-02-01 05:42:58 +00001973 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001974 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05301975 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001976 err = 0;
1977 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Dave Jones32ee8c32006-02-28 00:43:23 -05001980 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001981 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1986{
Viresh Kumar45732372015-05-12 12:22:34 +05301987 struct cpufreq_policy *policy;
1988 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (!governor)
1991 return;
1992
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001993 if (cpufreq_disabled())
1994 return;
1995
Viresh Kumar45732372015-05-12 12:22:34 +05301996 /* clear last_governor for all inactive policies */
1997 read_lock_irqsave(&cpufreq_driver_lock, flags);
1998 for_each_inactive_policy(policy) {
Viresh Kumar18bf3a12015-05-12 12:22:51 +05301999 if (!strcmp(policy->last_governor, governor->name)) {
2000 policy->governor = NULL;
Viresh Kumar45732372015-05-12 12:22:34 +05302001 strcpy(policy->last_governor, "\0");
Viresh Kumar18bf3a12015-05-12 12:22:51 +05302002 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002003 }
Viresh Kumar45732372015-05-12 12:22:34 +05302004 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002005
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002006 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002008 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return;
2010}
2011EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2012
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/*********************************************************************
2015 * POLICY INTERFACE *
2016 *********************************************************************/
2017
2018/**
2019 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002020 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2021 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 *
2023 * Reads the current cpufreq policy.
2024 */
2025int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2026{
2027 struct cpufreq_policy *cpu_policy;
2028 if (!policy)
2029 return -EINVAL;
2030
2031 cpu_policy = cpufreq_cpu_get(cpu);
2032 if (!cpu_policy)
2033 return -EINVAL;
2034
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302035 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 return 0;
2039}
2040EXPORT_SYMBOL(cpufreq_get_policy);
2041
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002042/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302043 * policy : current policy.
2044 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002045 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302046static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302047 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002049 struct cpufreq_governor *old_gov;
2050 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Joe Perchese837f9b2014-03-11 10:03:00 -07002052 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2053 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302055 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Pan Xinhuifba95732015-07-30 18:10:40 +08002057 /*
2058 * This check works well when we store new min/max freq attributes,
2059 * because new_policy is a copy of policy with one field updated.
2060 */
2061 if (new_policy->min > new_policy->max)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002062 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302065 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002067 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002070 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302071 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Viresh Kumarbb176f72013-06-19 14:19:33 +05302073 /*
2074 * verify the cpu speed can be set within this limit, which might be
2075 * different to the first one
2076 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302077 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002078 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002079 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
2081 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002082 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302083 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302085 policy->min = new_policy->min;
2086 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002088 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002089 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002091 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302092 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002093 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002094 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
2096
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002097 if (new_policy->governor == policy->governor)
2098 goto out;
2099
2100 pr_debug("governor switch\n");
2101
2102 /* save old, working values */
2103 old_gov = policy->governor;
2104 /* end old governor */
2105 if (old_gov) {
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302106 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2107 if (ret) {
2108 /* This can happen due to race with other operations */
2109 pr_debug("%s: Failed to Stop Governor: %s (%d)\n",
2110 __func__, old_gov->name, ret);
2111 return ret;
2112 }
2113
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002114 up_write(&policy->rwsem);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302115 ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002116 down_write(&policy->rwsem);
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302117
2118 if (ret) {
2119 pr_err("%s: Failed to Exit Governor: %s (%d)\n",
2120 __func__, old_gov->name, ret);
2121 return ret;
2122 }
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002123 }
2124
2125 /* start new governor */
2126 policy->governor = new_policy->governor;
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302127 ret = __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2128 if (!ret) {
2129 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
2130 if (!ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002131 goto out;
2132
2133 up_write(&policy->rwsem);
2134 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2135 down_write(&policy->rwsem);
2136 }
2137
2138 /* new governor failed, so re-start old one */
2139 pr_debug("starting governor %s failed\n", policy->governor->name);
2140 if (old_gov) {
2141 policy->governor = old_gov;
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302142 if (__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
2143 policy->governor = NULL;
2144 else
2145 __cpufreq_governor(policy, CPUFREQ_GOV_START);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002146 }
2147
Viresh Kumar4bc384a2015-07-18 11:31:03 +05302148 return ret;
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002149
2150 out:
2151 pr_debug("governor: change or update limits\n");
2152 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
2155/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2157 * @cpu: CPU which shall be re-evaluated
2158 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002159 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 * at different times.
2161 */
2162int cpufreq_update_policy(unsigned int cpu)
2163{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302164 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2165 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002166 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002168 if (!policy)
2169 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
viresh kumarad7722d2013-10-18 19:10:15 +05302171 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002173 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302174 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302175 new_policy.min = policy->user_policy.min;
2176 new_policy.max = policy->user_policy.max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Viresh Kumarbb176f72013-06-19 14:19:33 +05302178 /*
2179 * BIOS might change freq behind our back
2180 * -> ask driver for current freq and notify governors about a change
2181 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002182 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302183 new_policy.cur = cpufreq_driver->get(cpu);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302184 if (WARN_ON(!new_policy.cur)) {
2185 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002186 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302187 }
2188
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302189 if (!policy->cur) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002190 pr_debug("Driver did not initialize current freq\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302191 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002192 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302193 if (policy->cur != new_policy.cur && has_target())
Viresh Kumara1e1dc42015-01-02 12:34:28 +05302194 cpufreq_out_of_sync(policy, new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002195 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002196 }
2197
Viresh Kumar037ce832013-10-02 14:13:16 +05302198 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002200unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302201 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002202
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302203 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 return ret;
2205}
2206EXPORT_SYMBOL(cpufreq_update_policy);
2207
Paul Gortmaker27609842013-06-19 13:54:04 -04002208static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002209 unsigned long action, void *hcpu)
2210{
2211 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002212
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002213 switch (action & ~CPU_TASKS_FROZEN) {
2214 case CPU_ONLINE:
2215 cpufreq_online(cpu);
2216 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302217
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002218 case CPU_DOWN_PREPARE:
2219 cpufreq_offline_prepare(cpu);
2220 break;
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302221
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002222 case CPU_POST_DEAD:
2223 cpufreq_offline_finish(cpu);
2224 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302225
Rafael J. Wysocki0b275352015-07-29 03:03:44 +02002226 case CPU_DOWN_FAILED:
2227 cpufreq_online(cpu);
2228 break;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002229 }
2230 return NOTIFY_OK;
2231}
2232
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002233static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302234 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002235};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002238 * BOOST *
2239 *********************************************************************/
2240static int cpufreq_boost_set_sw(int state)
2241{
2242 struct cpufreq_frequency_table *freq_table;
2243 struct cpufreq_policy *policy;
2244 int ret = -EINVAL;
2245
Viresh Kumarf9637352015-05-12 12:20:11 +05302246 for_each_active_policy(policy) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002247 freq_table = cpufreq_frequency_get_table(policy->cpu);
2248 if (freq_table) {
2249 ret = cpufreq_frequency_table_cpuinfo(policy,
2250 freq_table);
2251 if (ret) {
2252 pr_err("%s: Policy frequency update failed\n",
2253 __func__);
2254 break;
2255 }
2256 policy->user_policy.max = policy->max;
2257 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2258 }
2259 }
2260
2261 return ret;
2262}
2263
2264int cpufreq_boost_trigger_state(int state)
2265{
2266 unsigned long flags;
2267 int ret = 0;
2268
2269 if (cpufreq_driver->boost_enabled == state)
2270 return 0;
2271
2272 write_lock_irqsave(&cpufreq_driver_lock, flags);
2273 cpufreq_driver->boost_enabled = state;
2274 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2275
2276 ret = cpufreq_driver->set_boost(state);
2277 if (ret) {
2278 write_lock_irqsave(&cpufreq_driver_lock, flags);
2279 cpufreq_driver->boost_enabled = !state;
2280 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2281
Joe Perchese837f9b2014-03-11 10:03:00 -07002282 pr_err("%s: Cannot %s BOOST\n",
2283 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002284 }
2285
2286 return ret;
2287}
2288
Rafael J. Wysocki41669da2015-12-27 00:23:48 +01002289static bool cpufreq_boost_supported(void)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002290{
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002291 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002292}
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002293
Viresh Kumar44139ed2015-07-29 16:23:09 +05302294static int create_boost_sysfs_file(void)
2295{
2296 int ret;
2297
Viresh Kumarc82bd442015-10-15 21:35:23 +05302298 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302299 if (ret)
2300 pr_err("%s: cannot register global BOOST sysfs file\n",
2301 __func__);
2302
2303 return ret;
2304}
2305
2306static void remove_boost_sysfs_file(void)
2307{
2308 if (cpufreq_boost_supported())
Viresh Kumarc82bd442015-10-15 21:35:23 +05302309 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302310}
2311
2312int cpufreq_enable_boost_support(void)
2313{
2314 if (!cpufreq_driver)
2315 return -EINVAL;
2316
2317 if (cpufreq_boost_supported())
2318 return 0;
2319
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002320 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
Viresh Kumar44139ed2015-07-29 16:23:09 +05302321
2322 /* This will get removed on driver unregister */
2323 return create_boost_sysfs_file();
2324}
2325EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2326
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002327int cpufreq_boost_enabled(void)
2328{
2329 return cpufreq_driver->boost_enabled;
2330}
2331EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2332
2333/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2335 *********************************************************************/
2336
2337/**
2338 * cpufreq_register_driver - register a CPU Frequency driver
2339 * @driver_data: A struct cpufreq_driver containing the values#
2340 * submitted by the CPU Frequency driver.
2341 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302342 * Registers a CPU Frequency driver to this core code. This code
Eric Biggers63af4052016-02-20 21:50:01 -06002343 * returns zero on success, -EEXIST when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002344 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *
2346 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002347int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348{
2349 unsigned long flags;
2350 int ret;
2351
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002352 if (cpufreq_disabled())
2353 return -ENODEV;
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302356 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002357 driver_data->target) ||
2358 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302359 driver_data->target)) ||
2360 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return -EINVAL;
2362
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002363 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002365 /* Protect against concurrent CPU online/offline. */
2366 get_online_cpus();
2367
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002368 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002369 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002370 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002371 ret = -EEXIST;
2372 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002374 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002375 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302377 if (driver_data->setpolicy)
2378 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2379
Rafael J. Wysocki7a6c79f2015-12-27 00:27:38 +01002380 if (cpufreq_boost_supported()) {
2381 ret = create_boost_sysfs_file();
2382 if (ret)
2383 goto err_null_driver;
2384 }
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002385
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002386 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002387 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002388 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302390 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2391 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302393 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2394 driver_data->name);
2395 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002398 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002399 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002401out:
2402 put_online_cpus();
2403 return ret;
2404
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002405err_if_unreg:
2406 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002407err_boost_unreg:
Viresh Kumar44139ed2015-07-29 16:23:09 +05302408 remove_boost_sysfs_file();
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002409err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002410 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002411 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002412 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Rafael J. Wysockifdd320d2015-07-30 01:45:07 +02002413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414}
2415EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2416
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417/**
2418 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2419 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302420 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 * the right to do so, i.e. if you have succeeded in initialising before!
2422 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2423 * currently not initialised.
2424 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002425int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
2427 unsigned long flags;
2428
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002429 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002432 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002434 /* Protect against concurrent cpu hotplug */
2435 get_online_cpus();
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002436 subsys_interface_unregister(&cpufreq_interface);
Viresh Kumar44139ed2015-07-29 16:23:09 +05302437 remove_boost_sysfs_file();
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002438 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002440 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302441
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002442 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302443
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002444 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Sebastian Andrzej Siewior454d3a22015-07-22 17:59:11 +02002445 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 return 0;
2448}
2449EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002450
Doug Anderson90de2a42014-12-23 22:09:48 -08002451/*
2452 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2453 * or mutexes when secondary CPUs are halted.
2454 */
2455static struct syscore_ops cpufreq_syscore_ops = {
2456 .shutdown = cpufreq_suspend,
2457};
2458
Viresh Kumarc82bd442015-10-15 21:35:23 +05302459struct kobject *cpufreq_global_kobject;
2460EXPORT_SYMBOL(cpufreq_global_kobject);
2461
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002462static int __init cpufreq_core_init(void)
2463{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002464 if (cpufreq_disabled())
2465 return -ENODEV;
2466
Viresh Kumar8eec1022015-10-15 21:35:22 +05302467 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002468 BUG_ON(!cpufreq_global_kobject);
2469
Doug Anderson90de2a42014-12-23 22:09:48 -08002470 register_syscore_ops(&cpufreq_syscore_ops);
2471
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002472 return 0;
2473}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002474core_initcall(cpufreq_core_init);