blob: 74d9fcbbe4f9367927285257623ca565f84909b2 [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
41static bool suitable_policy(struct cpufreq_policy *policy, bool active)
42{
43 return active == !policy_is_inactive(policy);
44}
45
46/* Finds Next Acive/Inactive policy */
47static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy,
48 bool active)
49{
50 do {
51 policy = list_next_entry(policy, policy_list);
52
53 /* No more policies in the list */
54 if (&policy->policy_list == &cpufreq_policy_list)
55 return NULL;
56 } while (!suitable_policy(policy, active));
57
58 return policy;
59}
60
61static struct cpufreq_policy *first_policy(bool active)
62{
63 struct cpufreq_policy *policy;
64
65 /* No policies in the list */
66 if (list_empty(&cpufreq_policy_list))
67 return NULL;
68
69 policy = list_first_entry(&cpufreq_policy_list, typeof(*policy),
70 policy_list);
71
72 if (!suitable_policy(policy, active))
73 policy = next_policy(policy, active);
74
75 return policy;
76}
77
78/* Macros to iterate over CPU policies */
79#define for_each_suitable_policy(__policy, __active) \
80 for (__policy = first_policy(__active); \
81 __policy; \
82 __policy = next_policy(__policy, __active))
83
84#define for_each_active_policy(__policy) \
85 for_each_suitable_policy(__policy, true)
86#define for_each_inactive_policy(__policy) \
87 for_each_suitable_policy(__policy, false)
88
89#define for_each_policy(__policy) \
Viresh Kumarb4f06762015-01-27 14:06:08 +053090 list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
91
Viresh Kumarf7b27062015-01-27 14:06:09 +053092/* Iterate over governors */
93static LIST_HEAD(cpufreq_governor_list);
94#define for_each_governor(__governor) \
95 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/**
Dave Jonescd878472006-08-11 17:59:28 -040098 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 * level driver of CPUFreq support, and its spinlock. This lock
100 * also protects the cpufreq_cpu_data array.
101 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200102static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -0700103static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530104static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +0530105static DEFINE_RWLOCK(cpufreq_driver_lock);
Jane Li6f1e4ef2014-01-03 17:17:41 +0800106DEFINE_MUTEX(cpufreq_governor_lock);
Viresh Kumarbb176f72013-06-19 14:19:33 +0530107
Thomas Renninger084f3492007-07-09 11:35:28 -0700108/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400109static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Viresh Kumar2f0aea92014-03-04 11:00:26 +0800111/* Flag to suspend/resume CPUFreq governors */
112static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530114static inline bool has_target(void)
115{
116 return cpufreq_driver->target_index || cpufreq_driver->target;
117}
118
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800119/*
Viresh Kumar6eed9402013-08-06 22:53:11 +0530120 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
121 * sections
122 */
123static DECLARE_RWSEM(cpufreq_rwsem);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500126static int __cpufreq_governor(struct cpufreq_policy *policy,
127 unsigned int event);
Viresh Kumard92d50a2015-01-02 12:34:29 +0530128static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
David Howells65f27f32006-11-22 14:55:48 +0000129static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500132 * Two notifier lists: the "policy" list is involved in the
133 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * "transition" list for kernel code that needs to handle
135 * changes to devices when the CPU clock speed changes.
136 * The mutex locks both lists.
137 */
Alan Sterne041c682006-03-27 01:16:30 -0800138static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700139static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200141static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700142static int __init init_cpufreq_transition_notifier_list(void)
143{
144 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200145 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700146 return 0;
147}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800148pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400150static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200151static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400152{
153 return off;
154}
155void disable_cpufreq(void)
156{
157 off = 1;
158}
Dave Jones29464f22009-01-18 01:37:11 -0500159static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000161bool have_governor_per_policy(void)
162{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530163 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000164}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000165EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000166
Viresh Kumar944e9a02013-05-16 05:09:57 +0000167struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
168{
169 if (have_governor_per_policy())
170 return &policy->kobj;
171 else
172 return cpufreq_global_kobject;
173}
174EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
175
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000176static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
177{
178 u64 idle_time;
179 u64 cur_wall_time;
180 u64 busy_time;
181
182 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
183
184 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
185 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
186 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
187 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
188 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
189 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
190
191 idle_time = cur_wall_time - busy_time;
192 if (wall)
193 *wall = cputime_to_usecs(cur_wall_time);
194
195 return cputime_to_usecs(idle_time);
196}
197
198u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
199{
200 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
201
202 if (idle_time == -1ULL)
203 return get_cpu_idle_time_jiffy(cpu, wall);
204 else if (!io_busy)
205 idle_time += get_cpu_iowait_time_us(cpu, wall);
206
207 return idle_time;
208}
209EXPORT_SYMBOL_GPL(get_cpu_idle_time);
210
Viresh Kumar70e9e772013-10-03 20:29:07 +0530211/*
212 * This is a generic cpufreq init() routine which can be used by cpufreq
213 * drivers of SMP systems. It will do following:
214 * - validate & show freq table passed
215 * - set policies transition latency
216 * - policy->cpus with all possible CPUs
217 */
218int cpufreq_generic_init(struct cpufreq_policy *policy,
219 struct cpufreq_frequency_table *table,
220 unsigned int transition_latency)
221{
222 int ret;
223
224 ret = cpufreq_table_validate_and_show(policy, table);
225 if (ret) {
226 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
227 return ret;
228 }
229
230 policy->cpuinfo.transition_latency = transition_latency;
231
232 /*
233 * The driver only supports the SMP configuartion where all processors
234 * share the clock and voltage and clock.
235 */
236 cpumask_setall(policy->cpus);
237
238 return 0;
239}
240EXPORT_SYMBOL_GPL(cpufreq_generic_init);
241
Viresh Kumar652ed952014-01-09 20:38:43 +0530242unsigned int cpufreq_generic_get(unsigned int cpu)
243{
244 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
245
246 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700247 pr_err("%s: No %s associated to cpu: %d\n",
248 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530249 return 0;
250 }
251
252 return clk_get_rate(policy->clk) / 1000;
253}
254EXPORT_SYMBOL_GPL(cpufreq_generic_get);
255
Viresh Kumare0b31652014-03-10 14:53:33 +0530256/* Only for cpufreq core internal use */
257struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
258{
259 return per_cpu(cpufreq_cpu_data, cpu);
260}
261
Viresh Kumar50e9c852015-02-19 17:02:03 +0530262/**
263 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
264 *
265 * @cpu: cpu to find policy for.
266 *
267 * This returns policy for 'cpu', returns NULL if it doesn't exist.
268 * It also increments the kobject reference count to mark it busy and so would
269 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
270 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
271 * freed as that depends on the kobj count.
272 *
273 * It also takes a read-lock of 'cpufreq_rwsem' and doesn't put it back if a
274 * valid policy is found. This is done to make sure the driver doesn't get
275 * unregistered while the policy is being used.
276 *
277 * Return: A valid policy on success, otherwise NULL on failure.
278 */
Viresh Kumar6eed9402013-08-06 22:53:11 +0530279struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530281 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 unsigned long flags;
283
Viresh Kumar1b947c902015-02-19 17:02:05 +0530284 if (WARN_ON(cpu >= nr_cpu_ids))
Viresh Kumar6eed9402013-08-06 22:53:11 +0530285 return NULL;
286
287 if (!down_read_trylock(&cpufreq_rwsem))
288 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000291 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Viresh Kumar6eed9402013-08-06 22:53:11 +0530293 if (cpufreq_driver) {
294 /* get the CPU */
295 policy = per_cpu(cpufreq_cpu_data, cpu);
296 if (policy)
297 kobject_get(&policy->kobj);
298 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200299
Viresh Kumar6eed9402013-08-06 22:53:11 +0530300 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530302 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530303 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530305 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000306}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
308
Viresh Kumar50e9c852015-02-19 17:02:03 +0530309/**
310 * cpufreq_cpu_put: Decrements the usage count of a policy
311 *
312 * @policy: policy earlier returned by cpufreq_cpu_get().
313 *
314 * This decrements the kobject reference count incremented earlier by calling
315 * cpufreq_cpu_get().
316 *
317 * It also drops the read-lock of 'cpufreq_rwsem' taken at cpufreq_cpu_get().
318 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530319void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530321 kobject_put(&policy->kobj);
322 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
328 *********************************************************************/
329
330/**
331 * adjust_jiffies - adjust the system "loops_per_jiffy"
332 *
333 * This function alters the system "loops_per_jiffy" for the clock
334 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500335 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * per-CPU loops_per_jiffy value wherever possible.
337 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800338static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530340#ifndef CONFIG_SMP
341 static unsigned long l_p_j_ref;
342 static unsigned int l_p_j_ref_freq;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (ci->flags & CPUFREQ_CONST_LOOPS)
345 return;
346
347 if (!l_p_j_ref_freq) {
348 l_p_j_ref = loops_per_jiffy;
349 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700350 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
351 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530353 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530354 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
355 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700356 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
357 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530362static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530363 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 BUG_ON(irqs_disabled());
366
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000367 if (cpufreq_disabled())
368 return;
369
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200370 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200371 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700372 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500377 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800378 * which is not equal to what the cpufreq core thinks is
379 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200381 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800382 if ((policy) && (policy->cpu == freqs->cpu) &&
383 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700384 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
385 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800386 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700389 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800390 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
392 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 case CPUFREQ_POSTCHANGE:
395 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700396 pr_debug("FREQ: %lu - CPU: %lu\n",
397 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100398 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700399 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800400 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800401 if (likely(policy) && likely(policy->cpu == freqs->cpu))
402 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530406
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530407/**
408 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
409 * on frequency transition.
410 *
411 * This function calls the transition notifiers and the "adjust_jiffies"
412 * function. It is called twice on all CPU frequency changes that have
413 * external effects.
414 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530415static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530416 struct cpufreq_freqs *freqs, unsigned int state)
417{
418 for_each_cpu(freqs->cpu, policy->cpus)
419 __cpufreq_notify_transition(policy, freqs, state);
420}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530422/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530423static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530424 struct cpufreq_freqs *freqs, int transition_failed)
425{
426 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
427 if (!transition_failed)
428 return;
429
430 swap(freqs->old, freqs->new);
431 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
432 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
433}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530434
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530435void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
436 struct cpufreq_freqs *freqs)
437{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530438
439 /*
440 * Catch double invocations of _begin() which lead to self-deadlock.
441 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
442 * doesn't invoke _begin() on their behalf, and hence the chances of
443 * double invocations are very low. Moreover, there are scenarios
444 * where these checks can emit false-positive warnings in these
445 * drivers; so we avoid that by skipping them altogether.
446 */
447 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
448 && current == policy->transition_task);
449
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530450wait:
451 wait_event(policy->transition_wait, !policy->transition_ongoing);
452
453 spin_lock(&policy->transition_lock);
454
455 if (unlikely(policy->transition_ongoing)) {
456 spin_unlock(&policy->transition_lock);
457 goto wait;
458 }
459
460 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530461 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530462
463 spin_unlock(&policy->transition_lock);
464
465 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
466}
467EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
468
469void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
470 struct cpufreq_freqs *freqs, int transition_failed)
471{
472 if (unlikely(WARN_ON(!policy->transition_ongoing)))
473 return;
474
475 cpufreq_notify_post_transition(policy, freqs, transition_failed);
476
477 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530478 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530479
480 wake_up(&policy->transition_wait);
481}
482EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/*********************************************************************
486 * SYSFS INTERFACE *
487 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530488static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100489 struct attribute *attr, char *buf)
490{
491 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
492}
493
494static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
495 const char *buf, size_t count)
496{
497 int ret, enable;
498
499 ret = sscanf(buf, "%d", &enable);
500 if (ret != 1 || enable < 0 || enable > 1)
501 return -EINVAL;
502
503 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700504 pr_err("%s: Cannot %s BOOST!\n",
505 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100506 return -EINVAL;
507 }
508
Joe Perchese837f9b2014-03-11 10:03:00 -0700509 pr_debug("%s: cpufreq BOOST %s\n",
510 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100511
512 return count;
513}
514define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530516static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700517{
518 struct cpufreq_governor *t;
519
Viresh Kumarf7b27062015-01-27 14:06:09 +0530520 for_each_governor(t)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200521 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700522 return t;
523
524 return NULL;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/**
528 * cpufreq_parse_governor - parse a governor string
529 */
Dave Jones905d77c2008-03-05 14:28:32 -0500530static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct cpufreq_governor **governor)
532{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700533 int err = -EINVAL;
534
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200535 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700536 goto out;
537
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200538 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200539 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700541 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200542 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530543 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700545 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530547 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700549
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800550 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700551
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530552 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700553
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700554 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700555 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700556
Kees Cook1a8e1462011-05-04 08:38:56 -0700557 mutex_unlock(&cpufreq_governor_mutex);
558 ret = request_module("cpufreq_%s", str_governor);
559 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700560
Kees Cook1a8e1462011-05-04 08:38:56 -0700561 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530562 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700563 }
564
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700565 if (t != NULL) {
566 *governor = t;
567 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700569
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800570 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Dave Jones29464f22009-01-18 01:37:11 -0500572out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700573 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530577 * cpufreq_per_cpu_attr_read() / show_##file_name() -
578 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *
580 * Write out information from cpufreq_driver->policy[cpu]; object must be
581 * "unsigned int".
582 */
583
Dave Jones32ee8c32006-02-28 00:43:23 -0500584#define show_one(file_name, object) \
585static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500586(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500587{ \
Dave Jones29464f22009-01-18 01:37:11 -0500588 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591show_one(cpuinfo_min_freq, cpuinfo.min_freq);
592show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100593show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594show_one(scaling_min_freq, min);
595show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700596
Viresh Kumar09347b22015-01-02 12:34:24 +0530597static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700598{
599 ssize_t ret;
600
601 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
602 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
603 else
604 ret = sprintf(buf, "%u\n", policy->cur);
605 return ret;
606}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Viresh Kumar037ce832013-10-02 14:13:16 +0530608static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530609 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
612 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
613 */
614#define store_one(file_name, object) \
615static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500616(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800618 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 struct cpufreq_policy new_policy; \
620 \
621 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
622 if (ret) \
623 return -EINVAL; \
624 \
Dave Jones29464f22009-01-18 01:37:11 -0500625 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (ret != 1) \
627 return -EINVAL; \
628 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800629 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530630 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800631 if (!ret) \
632 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 \
634 return ret ? ret : count; \
635}
636
Dave Jones29464f22009-01-18 01:37:11 -0500637store_one(scaling_min_freq, min);
638store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640/**
641 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
642 */
Dave Jones905d77c2008-03-05 14:28:32 -0500643static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
644 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530646 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (!cur_freq)
648 return sprintf(buf, "<unknown>");
649 return sprintf(buf, "%u\n", cur_freq);
650}
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652/**
653 * show_scaling_governor - show the current policy for the specified CPU
654 */
Dave Jones905d77c2008-03-05 14:28:32 -0500655static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dave Jones29464f22009-01-18 01:37:11 -0500657 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return sprintf(buf, "powersave\n");
659 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
660 return sprintf(buf, "performance\n");
661 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200662 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500663 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return -EINVAL;
665}
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/**
668 * store_scaling_governor - store policy for the specified CPU
669 */
Dave Jones905d77c2008-03-05 14:28:32 -0500670static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
671 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530673 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 char str_governor[16];
675 struct cpufreq_policy new_policy;
676
677 ret = cpufreq_get_policy(&new_policy, policy->cpu);
678 if (ret)
679 return ret;
680
Dave Jones29464f22009-01-18 01:37:11 -0500681 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (ret != 1)
683 return -EINVAL;
684
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530685 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
686 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return -EINVAL;
688
Viresh Kumar037ce832013-10-02 14:13:16 +0530689 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200690
691 policy->user_policy.policy = policy->policy;
692 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200693
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530694 if (ret)
695 return ret;
696 else
697 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
700/**
701 * show_scaling_driver - show the cpufreq driver currently loaded
702 */
Dave Jones905d77c2008-03-05 14:28:32 -0500703static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200705 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/**
709 * show_scaling_available_governors - show the available CPUfreq governors
710 */
Dave Jones905d77c2008-03-05 14:28:32 -0500711static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
712 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 ssize_t i = 0;
715 struct cpufreq_governor *t;
716
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530717 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 i += sprintf(buf, "performance powersave");
719 goto out;
720 }
721
Viresh Kumarf7b27062015-01-27 14:06:09 +0530722 for_each_governor(t) {
Dave Jones29464f22009-01-18 01:37:11 -0500723 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
724 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200726 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500728out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 i += sprintf(&buf[i], "\n");
730 return i;
731}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700732
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800733ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
735 ssize_t i = 0;
736 unsigned int cpu;
737
Rusty Russell835481d2009-01-04 05:18:06 -0800738 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 if (i)
740 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
741 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
742 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 i += sprintf(&buf[i], "\n");
746 return i;
747}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800748EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700750/**
751 * show_related_cpus - show the CPUs affected by each transition even if
752 * hw coordination is in use
753 */
754static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
755{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800756 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700757}
758
759/**
760 * show_affected_cpus - show the CPUs affected by each transition
761 */
762static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
763{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800764 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700765}
766
Venki Pallipadi9e769882007-10-26 10:18:21 -0700767static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500768 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700769{
770 unsigned int freq = 0;
771 unsigned int ret;
772
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700773 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700774 return -EINVAL;
775
776 ret = sscanf(buf, "%u", &freq);
777 if (ret != 1)
778 return -EINVAL;
779
780 policy->governor->store_setspeed(policy, freq);
781
782 return count;
783}
784
785static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
786{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700787 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700788 return sprintf(buf, "<unsupported>\n");
789
790 return policy->governor->show_setspeed(policy, buf);
791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Thomas Renningere2f74f32009-11-19 12:31:01 +0100793/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200794 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100795 */
796static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
797{
798 unsigned int limit;
799 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200800 if (cpufreq_driver->bios_limit) {
801 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100802 if (!ret)
803 return sprintf(buf, "%u\n", limit);
804 }
805 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
806}
807
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200808cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
809cpufreq_freq_attr_ro(cpuinfo_min_freq);
810cpufreq_freq_attr_ro(cpuinfo_max_freq);
811cpufreq_freq_attr_ro(cpuinfo_transition_latency);
812cpufreq_freq_attr_ro(scaling_available_governors);
813cpufreq_freq_attr_ro(scaling_driver);
814cpufreq_freq_attr_ro(scaling_cur_freq);
815cpufreq_freq_attr_ro(bios_limit);
816cpufreq_freq_attr_ro(related_cpus);
817cpufreq_freq_attr_ro(affected_cpus);
818cpufreq_freq_attr_rw(scaling_min_freq);
819cpufreq_freq_attr_rw(scaling_max_freq);
820cpufreq_freq_attr_rw(scaling_governor);
821cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Dave Jones905d77c2008-03-05 14:28:32 -0500823static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 &cpuinfo_min_freq.attr,
825 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100826 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 &scaling_min_freq.attr,
828 &scaling_max_freq.attr,
829 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700830 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 &scaling_governor.attr,
832 &scaling_driver.attr,
833 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700834 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 NULL
836};
837
Dave Jones29464f22009-01-18 01:37:11 -0500838#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
839#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Dave Jones29464f22009-01-18 01:37:11 -0500841static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Dave Jones905d77c2008-03-05 14:28:32 -0500843 struct cpufreq_policy *policy = to_policy(kobj);
844 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530845 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530846
847 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530848 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800849
viresh kumarad7722d2013-10-18 19:10:15 +0530850 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800851
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530852 if (fattr->show)
853 ret = fattr->show(policy, buf);
854 else
855 ret = -EIO;
856
viresh kumarad7722d2013-10-18 19:10:15 +0530857 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530858 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return ret;
861}
862
Dave Jones905d77c2008-03-05 14:28:32 -0500863static ssize_t store(struct kobject *kobj, struct attribute *attr,
864 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Dave Jones905d77c2008-03-05 14:28:32 -0500866 struct cpufreq_policy *policy = to_policy(kobj);
867 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500868 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530869
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530870 get_online_cpus();
871
872 if (!cpu_online(policy->cpu))
873 goto unlock;
874
Viresh Kumar6eed9402013-08-06 22:53:11 +0530875 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530876 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800877
viresh kumarad7722d2013-10-18 19:10:15 +0530878 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800879
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530880 if (fattr->store)
881 ret = fattr->store(policy, buf, count);
882 else
883 ret = -EIO;
884
viresh kumarad7722d2013-10-18 19:10:15 +0530885 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530886
Viresh Kumar6eed9402013-08-06 22:53:11 +0530887 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530888unlock:
889 put_online_cpus();
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return ret;
892}
893
Dave Jones905d77c2008-03-05 14:28:32 -0500894static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Dave Jones905d77c2008-03-05 14:28:32 -0500896 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200897 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 complete(&policy->kobj_unregister);
899}
900
Emese Revfy52cf25d2010-01-19 02:58:23 +0100901static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 .show = show,
903 .store = store,
904};
905
906static struct kobj_type ktype_cpufreq = {
907 .sysfs_ops = &sysfs_ops,
908 .default_attrs = default_attrs,
909 .release = cpufreq_sysfs_release,
910};
911
Viresh Kumar2361be22013-05-17 16:09:09 +0530912struct kobject *cpufreq_global_kobject;
913EXPORT_SYMBOL(cpufreq_global_kobject);
914
915static int cpufreq_global_kobject_usage;
916
917int cpufreq_get_global_kobject(void)
918{
919 if (!cpufreq_global_kobject_usage++)
920 return kobject_add(cpufreq_global_kobject,
921 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
922
923 return 0;
924}
925EXPORT_SYMBOL(cpufreq_get_global_kobject);
926
927void cpufreq_put_global_kobject(void)
928{
929 if (!--cpufreq_global_kobject_usage)
930 kobject_del(cpufreq_global_kobject);
931}
932EXPORT_SYMBOL(cpufreq_put_global_kobject);
933
934int cpufreq_sysfs_create_file(const struct attribute *attr)
935{
936 int ret = cpufreq_get_global_kobject();
937
938 if (!ret) {
939 ret = sysfs_create_file(cpufreq_global_kobject, attr);
940 if (ret)
941 cpufreq_put_global_kobject();
942 }
943
944 return ret;
945}
946EXPORT_SYMBOL(cpufreq_sysfs_create_file);
947
948void cpufreq_sysfs_remove_file(const struct attribute *attr)
949{
950 sysfs_remove_file(cpufreq_global_kobject, attr);
951 cpufreq_put_global_kobject();
952}
953EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
954
Dave Jones19d6f7e2009-07-08 17:35:39 -0400955/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200956static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400957{
958 unsigned int j;
959 int ret = 0;
960
961 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800962 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400963
Viresh Kumar308b60e2013-07-31 14:35:14 +0200964 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400965 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400966
Viresh Kumare8fdde12013-07-31 14:31:33 +0200967 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800968 cpu_dev = get_cpu_device(j);
969 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400970 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200971 if (ret)
972 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400973 }
974 return ret;
975}
976
Viresh Kumar308b60e2013-07-31 14:35:14 +0200977static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800978 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400979{
980 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400981 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400982
Dave Jones909a6942009-07-08 18:05:42 -0400983 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200984 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530985 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400986 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
987 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100988 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400989 drv_attr++;
990 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200991 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400992 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
993 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100994 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400995 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700996
997 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
998 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100999 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -07001000
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001001 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +01001002 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1003 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001004 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +01001005 }
Dave Jones909a6942009-07-08 18:05:42 -04001006
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001007 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301008}
1009
1010static void cpufreq_init_policy(struct cpufreq_policy *policy)
1011{
viresh kumar6e2c89d2014-03-04 11:43:59 +08001012 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301013 struct cpufreq_policy new_policy;
1014 int ret = 0;
1015
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301016 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +00001017
viresh kumar6e2c89d2014-03-04 11:43:59 +08001018 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar42f91fa2015-01-02 12:34:26 +05301019 gov = find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
viresh kumar6e2c89d2014-03-04 11:43:59 +08001020 if (gov)
1021 pr_debug("Restoring governor %s for cpu %d\n",
1022 policy->governor->name, policy->cpu);
1023 else
1024 gov = CPUFREQ_DEFAULT_GOVERNOR;
1025
1026 new_policy.governor = gov;
1027
Jason Barona27a9ab2013-12-19 22:50:50 +00001028 /* Use the default policy if its valid. */
1029 if (cpufreq_driver->setpolicy)
viresh kumar6e2c89d2014-03-04 11:43:59 +08001030 cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
Dave Jonesecf7e462009-07-08 18:48:47 -04001031
1032 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +05301033 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -04001034 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001035 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001036 if (cpufreq_driver->exit)
1037 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -04001038 }
Dave Jones909a6942009-07-08 18:05:42 -04001039}
1040
Viresh Kumard8d3b472013-08-04 01:20:07 +02001041static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301042 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +00001043{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301044 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001045 unsigned long flags;
1046
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301047 /* Has this CPU been taken care of already? */
1048 if (cpumask_test_cpu(cpu, policy->cpus))
1049 return 0;
1050
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301051 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301052 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1053 if (ret) {
1054 pr_err("%s: Failed to stop governor\n", __func__);
1055 return ret;
1056 }
1057 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001058
viresh kumarad7722d2013-10-18 19:10:15 +05301059 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301060
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001061 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301062
Viresh Kumarfcf80582013-01-29 14:39:08 +00001063 cpumask_set_cpu(cpu, policy->cpus);
1064 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001065 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001066
viresh kumarad7722d2013-10-18 19:10:15 +05301067 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301068
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301069 if (has_target()) {
Stratos Karafotise5c87b72014-03-19 23:29:17 +02001070 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1071 if (!ret)
1072 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1073
1074 if (ret) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301075 pr_err("%s: Failed to start governor\n", __func__);
1076 return ret;
1077 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +02001078 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001079
Viresh Kumar42f921a2013-12-20 21:26:02 +05301080 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +00001081}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301083static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
1084{
1085 struct cpufreq_policy *policy;
1086 unsigned long flags;
1087
Lan Tianyu44871c92013-09-11 15:05:05 +08001088 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301089
1090 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
1091
Lan Tianyu44871c92013-09-11 15:05:05 +08001092 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301093
Geert Uytterhoeven09712f52014-11-04 17:05:25 +01001094 if (policy)
1095 policy->governor = NULL;
viresh kumar6e2c89d2014-03-04 11:43:59 +08001096
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301097 return policy;
1098}
1099
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301100static struct cpufreq_policy *cpufreq_policy_alloc(void)
1101{
1102 struct cpufreq_policy *policy;
1103
1104 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1105 if (!policy)
1106 return NULL;
1107
1108 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1109 goto err_free_policy;
1110
1111 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1112 goto err_free_cpumask;
1113
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301114 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301115 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301116 spin_lock_init(&policy->transition_lock);
1117 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301118 init_completion(&policy->kobj_unregister);
1119 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301120
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301121 return policy;
1122
1123err_free_cpumask:
1124 free_cpumask_var(policy->cpus);
1125err_free_policy:
1126 kfree(policy);
1127
1128 return NULL;
1129}
1130
Viresh Kumar42f921a2013-12-20 21:26:02 +05301131static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1132{
1133 struct kobject *kobj;
1134 struct completion *cmp;
1135
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301136 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1137 CPUFREQ_REMOVE_POLICY, policy);
1138
Viresh Kumar42f921a2013-12-20 21:26:02 +05301139 down_read(&policy->rwsem);
1140 kobj = &policy->kobj;
1141 cmp = &policy->kobj_unregister;
1142 up_read(&policy->rwsem);
1143 kobject_put(kobj);
1144
1145 /*
1146 * We need to make sure that the underlying kobj is
1147 * actually not referenced anymore by anybody before we
1148 * proceed with unloading.
1149 */
1150 pr_debug("waiting for dropping of refcount\n");
1151 wait_for_completion(cmp);
1152 pr_debug("wait complete\n");
1153}
1154
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301155static void cpufreq_policy_free(struct cpufreq_policy *policy)
1156{
1157 free_cpumask_var(policy->related_cpus);
1158 free_cpumask_var(policy->cpus);
1159 kfree(policy);
1160}
1161
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301162static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu,
1163 struct device *cpu_dev)
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301164{
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301165 int ret;
1166
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301167 if (WARN_ON(cpu == policy->cpu))
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301168 return 0;
1169
1170 /* Move kobject to the new policy->cpu */
1171 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1172 if (ret) {
1173 pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
1174 return ret;
1175 }
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301176
viresh kumarad7722d2013-10-18 19:10:15 +05301177 down_write(&policy->rwsem);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301178 policy->cpu = cpu;
viresh kumarad7722d2013-10-18 19:10:15 +05301179 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301180
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301181 return 0;
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301182}
1183
Viresh Kumar23faf0b2015-02-19 17:02:04 +05301184/**
1185 * cpufreq_add_dev - add a CPU device
1186 *
1187 * Adds the cpufreq interface for a CPU device.
1188 *
1189 * The Oracle says: try running cpufreq registration/unregistration concurrently
1190 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1191 * mess up, but more thorough testing is needed. - Mathieu
1192 */
1193static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001195 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301196 int ret = -ENOMEM;
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301197 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 unsigned long flags;
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301199 bool recover_policy = cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Ashok Rajc32b6b82005-10-30 14:59:54 -08001201 if (cpu_is_offline(cpu))
1202 return 0;
1203
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001204 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Viresh Kumar6eed9402013-08-06 22:53:11 +05301206 if (!down_read_trylock(&cpufreq_rwsem))
1207 return 0;
1208
Viresh Kumarbb29ae12015-02-19 17:02:06 +05301209 /* Check if this CPU already has a policy to manage it */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001210 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumarf9637352015-05-12 12:20:11 +05301211 for_each_active_policy(policy) {
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301212 if (cpumask_test_cpu(cpu, policy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001213 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301214 ret = cpufreq_add_policy_cpu(policy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301215 up_read(&cpufreq_rwsem);
1216 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301217 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001218 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001219 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001221 /*
1222 * Restore the saved policy when doing light-weight init and fall back
1223 * to the full init if that fails.
1224 */
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301225 policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001226 if (!policy) {
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301227 recover_policy = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301228 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001229 if (!policy)
1230 goto nomem_out;
1231 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301232
1233 /*
1234 * In the resume path, since we restore a saved policy, the assignment
1235 * to policy->cpu is like an update of the existing policy, rather than
1236 * the creation of a brand new one. So we need to perform this update
1237 * by invoking update_policy_cpu().
1238 */
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301239 if (recover_policy && cpu != policy->cpu)
1240 WARN_ON(update_policy_cpu(policy, cpu, dev));
1241 else
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301242 policy->cpu = cpu;
1243
Rusty Russell835481d2009-01-04 05:18:06 -08001244 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /* call driver. From then on the cpufreq must be able
1247 * to accept all calls to ->verify and ->setpolicy for this CPU
1248 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001249 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001251 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301252 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001254
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001255 down_write(&policy->rwsem);
1256
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001257 /* related cpus should atleast have policy->cpus */
1258 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1259
1260 /*
1261 * affected cpus must always be the one, which are online. We aren't
1262 * managing offline cpus here.
1263 */
1264 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1265
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301266 if (!recover_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001267 policy->user_policy.min = policy->min;
1268 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001269
1270 /* prepare interface data */
1271 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1272 &dev->kobj, "cpufreq");
1273 if (ret) {
1274 pr_err("%s: failed to init policy->kobj: %d\n",
1275 __func__, ret);
1276 goto err_init_policy_kobj;
1277 }
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001278 }
1279
Viresh Kumar652ed952014-01-09 20:38:43 +05301280 write_lock_irqsave(&cpufreq_driver_lock, flags);
1281 for_each_cpu(j, policy->cpus)
1282 per_cpu(cpufreq_cpu_data, j) = policy;
1283 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1284
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001285 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301286 policy->cur = cpufreq_driver->get(policy->cpu);
1287 if (!policy->cur) {
1288 pr_err("%s: ->get() failed\n", __func__);
1289 goto err_get_freq;
1290 }
1291 }
1292
Viresh Kumard3916692013-12-03 11:20:46 +05301293 /*
1294 * Sometimes boot loaders set CPU frequency to a value outside of
1295 * frequency table present with cpufreq core. In such cases CPU might be
1296 * unstable if it has to run on that frequency for long duration of time
1297 * and so its better to set it to a frequency which is specified in
1298 * freq-table. This also makes cpufreq stats inconsistent as
1299 * cpufreq-stats would fail to register because current frequency of CPU
1300 * isn't found in freq-table.
1301 *
1302 * Because we don't want this change to effect boot process badly, we go
1303 * for the next freq which is >= policy->cur ('cur' must be set by now,
1304 * otherwise we will end up setting freq to lowest of the table as 'cur'
1305 * is initialized to zero).
1306 *
1307 * We are passing target-freq as "policy->cur - 1" otherwise
1308 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1309 * equal to target-freq.
1310 */
1311 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1312 && has_target()) {
1313 /* Are we running at unknown frequency ? */
1314 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1315 if (ret == -EINVAL) {
1316 /* Warn user and fix it */
1317 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1318 __func__, policy->cpu, policy->cur);
1319 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1320 CPUFREQ_RELATION_L);
1321
1322 /*
1323 * Reaching here after boot in a few seconds may not
1324 * mean that system will remain stable at "unknown"
1325 * frequency for longer duration. Hence, a BUG_ON().
1326 */
1327 BUG_ON(ret);
1328 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1329 __func__, policy->cpu, policy->cur);
1330 }
1331 }
1332
Thomas Renningera1531ac2008-07-29 22:32:58 -07001333 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1334 CPUFREQ_START, policy);
1335
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301336 if (!recover_policy) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001337 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301338 if (ret)
1339 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301340 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1341 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301342 }
Dave Jones8ff69732006-03-05 03:37:23 -05001343
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301344 write_lock_irqsave(&cpufreq_driver_lock, flags);
1345 list_add(&policy->policy_list, &cpufreq_policy_list);
1346 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1347
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301348 cpufreq_init_policy(policy);
1349
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301350 if (!recover_policy) {
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301351 policy->user_policy.policy = policy->policy;
1352 policy->user_policy.governor = policy->governor;
1353 }
Viresh Kumar4e97b632014-03-04 11:44:01 +08001354 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301355
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001356 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301357
Viresh Kumar6eed9402013-08-06 22:53:11 +05301358 up_read(&cpufreq_rwsem);
1359
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301360 /* Callback for handling stuff after policy is ready */
1361 if (cpufreq_driver->ready)
1362 cpufreq_driver->ready(policy);
1363
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001364 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return 0;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301369err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001370 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301371 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001372 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001373 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001375 if (!recover_policy) {
1376 kobject_put(&policy->kobj);
1377 wait_for_completion(&policy->kobj_unregister);
1378 }
1379err_init_policy_kobj:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001380 up_write(&policy->rwsem);
1381
Viresh Kumarda60ce92013-10-03 20:28:30 +05301382 if (cpufreq_driver->exit)
1383 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301384err_set_policy_cpu:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301385 if (recover_policy) {
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001386 /* Do not leave stale fallback data behind. */
1387 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301388 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001389 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301390 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301393 up_read(&cpufreq_rwsem);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return ret;
1396}
1397
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301398static int __cpufreq_remove_dev_prepare(struct device *dev,
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301399 struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301401 unsigned int cpu = dev->id, cpus;
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301402 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301404 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001406 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001408 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301410 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301412 /* Save the policy somewhere when doing a light-weight tear-down */
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301413 if (cpufreq_suspended)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301414 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301415
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001416 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301418 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001419 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301423 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301424 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1425 if (ret) {
1426 pr_err("%s: Failed to stop governor\n", __func__);
1427 return ret;
1428 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001429
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001430 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301431 policy->governor->name, CPUFREQ_NAME_LEN);
Viresh Kumardb5f2992015-01-02 12:34:25 +05301432 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001433
viresh kumarad7722d2013-10-18 19:10:15 +05301434 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301435 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301436 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301438 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301439 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001440 } else if (cpus > 1) {
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301441 /* Nominate new CPU */
1442 int new_cpu = cpumask_any_but(policy->cpus, cpu);
1443 struct device *cpu_dev = get_cpu_device(new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301444
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301445 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1446 ret = update_policy_cpu(policy, new_cpu, cpu_dev);
1447 if (ret) {
1448 if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1449 "cpufreq"))
1450 pr_err("%s: Failed to restore kobj link to cpu:%d\n",
1451 __func__, cpu_dev->id);
1452 return ret;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001453 }
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301454
1455 if (!cpufreq_suspended)
1456 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1457 __func__, new_cpu, cpu);
Preeti U Murthy789ca242014-09-29 15:47:12 +02001458 } else if (cpufreq_driver->stop_cpu) {
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001459 cpufreq_driver->stop_cpu(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001460 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001461
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301462 return 0;
1463}
1464
1465static int __cpufreq_remove_dev_finish(struct device *dev,
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301466 struct subsys_interface *sif)
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301467{
1468 unsigned int cpu = dev->id, cpus;
1469 int ret;
1470 unsigned long flags;
1471 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301472
Viresh Kumar6ffae8c2015-01-31 06:02:44 +05301473 write_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301474 policy = per_cpu(cpufreq_cpu_data, cpu);
Viresh Kumar6ffae8c2015-01-31 06:02:44 +05301475 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1476 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301477
1478 if (!policy) {
1479 pr_debug("%s: No cpu_data found\n", __func__);
1480 return -EINVAL;
1481 }
1482
viresh kumarad7722d2013-10-18 19:10:15 +05301483 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301484 cpus = cpumask_weight(policy->cpus);
Viresh Kumar303ae722015-02-19 17:02:07 +05301485 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301486 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301487
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001488 /* If cpu is last user of policy, free policy */
1489 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301490 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301491 ret = __cpufreq_governor(policy,
1492 CPUFREQ_GOV_POLICY_EXIT);
1493 if (ret) {
1494 pr_err("%s: Failed to exit governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001495 __func__);
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301496 return ret;
1497 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301498 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001499
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301500 if (!cpufreq_suspended)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301501 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301502
1503 /*
1504 * Perform the ->exit() even during light-weight tear-down,
1505 * since this is a core component, and is essential for the
1506 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001507 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001508 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301509 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001510
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301511 /* Remove policy from list of active policies */
1512 write_lock_irqsave(&cpufreq_driver_lock, flags);
1513 list_del(&policy->policy_list);
1514 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1515
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301516 if (!cpufreq_suspended)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301517 cpufreq_policy_free(policy);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02001518 } else if (has_target()) {
1519 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1520 if (!ret)
1521 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1522
1523 if (ret) {
1524 pr_err("%s: Failed to start governor\n", __func__);
1525 return ret;
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001526 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 return 0;
1530}
1531
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301532/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301533 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301534 *
1535 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301536 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001537static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001538{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001539 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301540 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001541
1542 if (cpu_is_offline(cpu))
1543 return 0;
1544
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301545 ret = __cpufreq_remove_dev_prepare(dev, sif);
Viresh Kumar27a862e2013-10-02 14:13:14 +05301546
1547 if (!ret)
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301548 ret = __cpufreq_remove_dev_finish(dev, sif);
Viresh Kumar27a862e2013-10-02 14:13:14 +05301549
1550 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001551}
1552
David Howells65f27f32006-11-22 14:55:48 +00001553static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
David Howells65f27f32006-11-22 14:55:48 +00001555 struct cpufreq_policy *policy =
1556 container_of(work, struct cpufreq_policy, update);
1557 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001558 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 cpufreq_update_policy(cpu);
1560}
1561
1562/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301563 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1564 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301565 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 * @new_freq: CPU frequency the CPU actually runs at
1567 *
Dave Jones29464f22009-01-18 01:37:11 -05001568 * We adjust to current frequency first, and need to clean up later.
1569 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301571static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301572 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301575
Joe Perchese837f9b2014-03-11 10:03:00 -07001576 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 +05301577 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301579 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301581
Viresh Kumar8fec0512014-03-24 13:35:45 +05301582 cpufreq_freq_transition_begin(policy, &freqs);
1583 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584}
1585
Dave Jones32ee8c32006-02-28 00:43:23 -05001586/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301587 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001588 * @cpu: CPU number
1589 *
1590 * This is the last known freq, without actually getting it from the driver.
1591 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1592 */
1593unsigned int cpufreq_quick_get(unsigned int cpu)
1594{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001595 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301596 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001597
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001598 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1599 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001600
1601 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001602 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301603 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001604 cpufreq_cpu_put(policy);
1605 }
1606
Dave Jones4d34a672008-02-07 16:33:49 -05001607 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001608}
1609EXPORT_SYMBOL(cpufreq_quick_get);
1610
Jesse Barnes3d737102011-06-28 10:59:12 -07001611/**
1612 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1613 * @cpu: CPU number
1614 *
1615 * Just return the max possible frequency for a given CPU.
1616 */
1617unsigned int cpufreq_quick_get_max(unsigned int cpu)
1618{
1619 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1620 unsigned int ret_freq = 0;
1621
1622 if (policy) {
1623 ret_freq = policy->max;
1624 cpufreq_cpu_put(policy);
1625 }
1626
1627 return ret_freq;
1628}
1629EXPORT_SYMBOL(cpufreq_quick_get_max);
1630
Viresh Kumard92d50a2015-01-02 12:34:29 +05301631static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301633 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001635 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001636 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Viresh Kumard92d50a2015-01-02 12:34:29 +05301638 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301640 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001641 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301642 /* verify no discrepancy between actual and
1643 saved value exists */
1644 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301645 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 schedule_work(&policy->update);
1647 }
1648 }
1649
Dave Jones4d34a672008-02-07 16:33:49 -05001650 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001651}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001653/**
1654 * cpufreq_get - get the current CPU frequency (in kHz)
1655 * @cpu: CPU number
1656 *
1657 * Get the CPU current (static) CPU frequency
1658 */
1659unsigned int cpufreq_get(unsigned int cpu)
1660{
Aaron Plattner999976e2014-03-04 12:42:15 -08001661 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001662 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001663
Aaron Plattner999976e2014-03-04 12:42:15 -08001664 if (policy) {
1665 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301666 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001667 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301668
Aaron Plattner999976e2014-03-04 12:42:15 -08001669 cpufreq_cpu_put(policy);
1670 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301671
Dave Jones4d34a672008-02-07 16:33:49 -05001672 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673}
1674EXPORT_SYMBOL(cpufreq_get);
1675
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001676static struct subsys_interface cpufreq_interface = {
1677 .name = "cpufreq",
1678 .subsys = &cpu_subsys,
1679 .add_dev = cpufreq_add_dev,
1680 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001681};
1682
Viresh Kumare28867e2014-03-04 11:00:27 +08001683/*
1684 * In case platform wants some specific frequency to be configured
1685 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001686 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001687int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001688{
Viresh Kumare28867e2014-03-04 11:00:27 +08001689 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001690
Viresh Kumare28867e2014-03-04 11:00:27 +08001691 if (!policy->suspend_freq) {
1692 pr_err("%s: suspend_freq can't be zero\n", __func__);
1693 return -EINVAL;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001694 }
1695
Viresh Kumare28867e2014-03-04 11:00:27 +08001696 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1697 policy->suspend_freq);
1698
1699 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1700 CPUFREQ_RELATION_H);
1701 if (ret)
1702 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1703 __func__, policy->suspend_freq, ret);
1704
Dave Jonesc9060492008-02-07 16:32:18 -05001705 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001706}
Viresh Kumare28867e2014-03-04 11:00:27 +08001707EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001708
1709/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001710 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001712 * Called during system wide Suspend/Hibernate cycles for suspending governors
1713 * as some platforms can't change frequency after this point in suspend cycle.
1714 * Because some of the devices (like: i2c, regulators, etc) they use for
1715 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001717void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301719 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001721 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001722 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001724 if (!has_target())
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301725 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001727 pr_debug("%s: Suspending Governors\n", __func__);
1728
Viresh Kumarf9637352015-05-12 12:20:11 +05301729 for_each_active_policy(policy) {
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001730 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1731 pr_err("%s: Failed to stop governor for policy: %p\n",
1732 __func__, policy);
1733 else if (cpufreq_driver->suspend
1734 && cpufreq_driver->suspend(policy))
1735 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1736 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
Viresh Kumarb1b12bab2014-09-30 09:33:17 +05301738
1739suspend:
1740 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741}
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001744 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001746 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1747 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001749void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 struct cpufreq_policy *policy;
1752
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001753 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return;
1755
Lan Tianyu8e304442014-09-18 15:03:07 +08001756 cpufreq_suspended = false;
1757
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001758 if (!has_target())
1759 return;
1760
1761 pr_debug("%s: Resuming Governors\n", __func__);
1762
Viresh Kumarf9637352015-05-12 12:20:11 +05301763 for_each_active_policy(policy) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301764 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1765 pr_err("%s: Failed to resume driver: %p\n", __func__,
1766 policy);
1767 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001768 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1769 pr_err("%s: Failed to start governor for policy: %p\n",
1770 __func__, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 }
Viresh Kumarc75de0a2015-04-02 10:21:33 +05301772
1773 /*
1774 * schedule call cpufreq_update_policy() for first-online CPU, as that
1775 * wouldn't be hotplugged-out on suspend. It will verify that the
1776 * current freq is in sync with what we believe it to be.
1777 */
1778 policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1779 if (WARN_ON(!policy))
1780 return;
1781
1782 schedule_work(&policy->update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Borislav Petkov9d950462013-01-20 10:24:28 +00001785/**
1786 * cpufreq_get_current_driver - return current driver's name
1787 *
1788 * Return the name string of the currently loaded cpufreq driver
1789 * or NULL, if none.
1790 */
1791const char *cpufreq_get_current_driver(void)
1792{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001793 if (cpufreq_driver)
1794 return cpufreq_driver->name;
1795
1796 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001797}
1798EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001800/**
1801 * cpufreq_get_driver_data - return current driver data
1802 *
1803 * Return the private data of the currently loaded cpufreq
1804 * driver, or NULL if no cpufreq driver is loaded.
1805 */
1806void *cpufreq_get_driver_data(void)
1807{
1808 if (cpufreq_driver)
1809 return cpufreq_driver->driver_data;
1810
1811 return NULL;
1812}
1813EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/*********************************************************************
1816 * NOTIFIER LISTS INTERFACE *
1817 *********************************************************************/
1818
1819/**
1820 * cpufreq_register_notifier - register a driver with cpufreq
1821 * @nb: notifier function to register
1822 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1823 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001824 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * are notified about clock rate changes (once before and once after
1826 * the transition), or a list of drivers that are notified about
1827 * changes in cpufreq policy.
1828 *
1829 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001830 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 */
1832int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1833{
1834 int ret;
1835
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001836 if (cpufreq_disabled())
1837 return -EINVAL;
1838
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001839 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 switch (list) {
1842 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001843 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001844 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 break;
1846 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001847 ret = blocking_notifier_chain_register(
1848 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 break;
1850 default:
1851 ret = -EINVAL;
1852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 return ret;
1855}
1856EXPORT_SYMBOL(cpufreq_register_notifier);
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/**
1859 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1860 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301861 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 *
1863 * Remove a driver from the CPU frequency notifier list.
1864 *
1865 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001866 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1869{
1870 int ret;
1871
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001872 if (cpufreq_disabled())
1873 return -EINVAL;
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 switch (list) {
1876 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001877 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001878 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 break;
1880 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001881 ret = blocking_notifier_chain_unregister(
1882 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
1884 default:
1885 ret = -EINVAL;
1886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 return ret;
1889}
1890EXPORT_SYMBOL(cpufreq_unregister_notifier);
1891
1892
1893/*********************************************************************
1894 * GOVERNORS *
1895 *********************************************************************/
1896
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301897/* Must set freqs->new to intermediate frequency */
1898static int __target_intermediate(struct cpufreq_policy *policy,
1899 struct cpufreq_freqs *freqs, int index)
1900{
1901 int ret;
1902
1903 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1904
1905 /* We don't need to switch to intermediate freq */
1906 if (!freqs->new)
1907 return 0;
1908
1909 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1910 __func__, policy->cpu, freqs->old, freqs->new);
1911
1912 cpufreq_freq_transition_begin(policy, freqs);
1913 ret = cpufreq_driver->target_intermediate(policy, index);
1914 cpufreq_freq_transition_end(policy, freqs, ret);
1915
1916 if (ret)
1917 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1918 __func__, ret);
1919
1920 return ret;
1921}
1922
Viresh Kumar8d657752014-05-21 14:29:29 +05301923static int __target_index(struct cpufreq_policy *policy,
1924 struct cpufreq_frequency_table *freq_table, int index)
1925{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301926 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1927 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301928 int retval = -EINVAL;
1929 bool notify;
1930
1931 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301932 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301933 /* Handle switching to intermediate frequency */
1934 if (cpufreq_driver->get_intermediate) {
1935 retval = __target_intermediate(policy, &freqs, index);
1936 if (retval)
1937 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301938
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301939 intermediate_freq = freqs.new;
1940 /* Set old freq to intermediate */
1941 if (intermediate_freq)
1942 freqs.old = freqs.new;
1943 }
1944
1945 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301946 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1947 __func__, policy->cpu, freqs.old, freqs.new);
1948
1949 cpufreq_freq_transition_begin(policy, &freqs);
1950 }
1951
1952 retval = cpufreq_driver->target_index(policy, index);
1953 if (retval)
1954 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1955 retval);
1956
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301957 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301958 cpufreq_freq_transition_end(policy, &freqs, retval);
1959
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301960 /*
1961 * Failed after setting to intermediate freq? Driver should have
1962 * reverted back to initial frequency and so should we. Check
1963 * here for intermediate_freq instead of get_intermediate, in
1964 * case we have't switched to intermediate freq at all.
1965 */
1966 if (unlikely(retval && intermediate_freq)) {
1967 freqs.old = intermediate_freq;
1968 freqs.new = policy->restore_freq;
1969 cpufreq_freq_transition_begin(policy, &freqs);
1970 cpufreq_freq_transition_end(policy, &freqs, 0);
1971 }
1972 }
1973
Viresh Kumar8d657752014-05-21 14:29:29 +05301974 return retval;
1975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977int __cpufreq_driver_target(struct cpufreq_policy *policy,
1978 unsigned int target_freq,
1979 unsigned int relation)
1980{
Viresh Kumar72499242012-10-31 01:28:21 +01001981 unsigned int old_target_freq = target_freq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301982 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001983
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001984 if (cpufreq_disabled())
1985 return -ENODEV;
1986
Viresh Kumar72499242012-10-31 01:28:21 +01001987 /* Make sure that target_freq is within supported range */
1988 if (target_freq > policy->max)
1989 target_freq = policy->max;
1990 if (target_freq < policy->min)
1991 target_freq = policy->min;
1992
1993 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001994 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001995
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301996 /*
1997 * This might look like a redundant call as we are checking it again
1998 * after finding index. But it is left intentionally for cases where
1999 * exactly same freq is called again and so we can save on few function
2000 * calls.
2001 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01002002 if (target_freq == policy->cur)
2003 return 0;
2004
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302005 /* Save last value to restore later on errors */
2006 policy->restore_freq = policy->cur;
2007
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002008 if (cpufreq_driver->target)
2009 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302010 else if (cpufreq_driver->target_index) {
2011 struct cpufreq_frequency_table *freq_table;
2012 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08002013
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302014 freq_table = cpufreq_frequency_get_table(policy->cpu);
2015 if (unlikely(!freq_table)) {
2016 pr_err("%s: Unable to find freq_table\n", __func__);
2017 goto out;
2018 }
2019
2020 retval = cpufreq_frequency_table_target(policy, freq_table,
2021 target_freq, relation, &index);
2022 if (unlikely(retval)) {
2023 pr_err("%s: Unable to find matching freq\n", __func__);
2024 goto out;
2025 }
2026
Viresh Kumard4019f02013-08-14 19:38:24 +05302027 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302028 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05302029 goto out;
2030 }
2031
Viresh Kumar8d657752014-05-21 14:29:29 +05302032 retval = __target_index(policy, freq_table, index);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302033 }
2034
2035out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return retval;
2037}
2038EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040int cpufreq_driver_target(struct cpufreq_policy *policy,
2041 unsigned int target_freq,
2042 unsigned int relation)
2043{
Julia Lawallf1829e42008-07-25 22:44:53 +02002044 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
viresh kumarad7722d2013-10-18 19:10:15 +05302046 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 ret = __cpufreq_driver_target(policy, target_freq, relation);
2049
viresh kumarad7722d2013-10-18 19:10:15 +05302050 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return ret;
2053}
2054EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2055
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302056static int __cpufreq_governor(struct cpufreq_policy *policy,
2057 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
Dave Jonescc993ca2005-07-28 09:43:56 -07002059 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002060
2061 /* Only must be defined when default governor is known to have latency
2062 restrictions, like e.g. conservative or ondemand.
2063 That this is the case is already ensured in Kconfig
2064 */
2065#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
2066 struct cpufreq_governor *gov = &cpufreq_gov_performance;
2067#else
2068 struct cpufreq_governor *gov = NULL;
2069#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07002070
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002071 /* Don't start any governor operations if we are entering suspend */
2072 if (cpufreq_suspended)
2073 return 0;
Ethan Zhaocb57720b2014-12-18 15:28:19 +09002074 /*
2075 * Governor might not be initiated here if ACPI _PPC changed
2076 * notification happened, so check it.
2077 */
2078 if (!policy->governor)
2079 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002080
Thomas Renninger1c256242007-10-02 13:28:12 -07002081 if (policy->governor->max_transition_latency &&
2082 policy->cpuinfo.transition_latency >
2083 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07002084 if (!gov)
2085 return -EINVAL;
2086 else {
Joe Perchese837f9b2014-03-11 10:03:00 -07002087 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2088 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002089 policy->governor = gov;
2090 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Viresh Kumarfe492f32013-08-06 22:53:10 +05302093 if (event == CPUFREQ_GOV_POLICY_INIT)
2094 if (!try_module_get(policy->governor->owner))
2095 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002097 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002098 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002099
2100 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05302101 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05302102 || (!policy->governor_enabled
2103 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002104 mutex_unlock(&cpufreq_governor_lock);
2105 return -EBUSY;
2106 }
2107
2108 if (event == CPUFREQ_GOV_STOP)
2109 policy->governor_enabled = false;
2110 else if (event == CPUFREQ_GOV_START)
2111 policy->governor_enabled = true;
2112
2113 mutex_unlock(&cpufreq_governor_lock);
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 ret = policy->governor->governor(policy, event);
2116
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002117 if (!ret) {
2118 if (event == CPUFREQ_GOV_POLICY_INIT)
2119 policy->governor->initialized++;
2120 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2121 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002122 } else {
2123 /* Restore original values */
2124 mutex_lock(&cpufreq_governor_lock);
2125 if (event == CPUFREQ_GOV_STOP)
2126 policy->governor_enabled = true;
2127 else if (event == CPUFREQ_GOV_START)
2128 policy->governor_enabled = false;
2129 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002130 }
Viresh Kumarb3940582013-02-01 05:42:58 +00002131
Viresh Kumarfe492f32013-08-06 22:53:10 +05302132 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2133 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 module_put(policy->governor->owner);
2135
2136 return ret;
2137}
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139int cpufreq_register_governor(struct cpufreq_governor *governor)
2140{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002141 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 if (!governor)
2144 return -EINVAL;
2145
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002146 if (cpufreq_disabled())
2147 return -ENODEV;
2148
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002149 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002150
Viresh Kumarb3940582013-02-01 05:42:58 +00002151 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002152 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302153 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002154 err = 0;
2155 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Dave Jones32ee8c32006-02-28 00:43:23 -05002158 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002159 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160}
2161EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2164{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002165 int cpu;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (!governor)
2168 return;
2169
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002170 if (cpufreq_disabled())
2171 return;
2172
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002173 for_each_present_cpu(cpu) {
2174 if (cpu_online(cpu))
2175 continue;
2176 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
2177 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
2178 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002179
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002180 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002182 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return;
2184}
2185EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2186
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188/*********************************************************************
2189 * POLICY INTERFACE *
2190 *********************************************************************/
2191
2192/**
2193 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002194 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2195 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 *
2197 * Reads the current cpufreq policy.
2198 */
2199int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2200{
2201 struct cpufreq_policy *cpu_policy;
2202 if (!policy)
2203 return -EINVAL;
2204
2205 cpu_policy = cpufreq_cpu_get(cpu);
2206 if (!cpu_policy)
2207 return -EINVAL;
2208
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302209 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
2211 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 return 0;
2213}
2214EXPORT_SYMBOL(cpufreq_get_policy);
2215
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002216/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302217 * policy : current policy.
2218 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002219 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302220static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302221 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002223 struct cpufreq_governor *old_gov;
2224 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Joe Perchese837f9b2014-03-11 10:03:00 -07002226 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2227 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302229 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002231 if (new_policy->min > policy->max || new_policy->max < policy->min)
2232 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302235 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002237 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002240 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302241 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
2243 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002244 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302245 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Viresh Kumarbb176f72013-06-19 14:19:33 +05302247 /*
2248 * verify the cpu speed can be set within this limit, which might be
2249 * different to the first one
2250 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302251 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002252 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002253 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
2255 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002256 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302257 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302259 policy->min = new_policy->min;
2260 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002262 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002263 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002265 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302266 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002267 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002268 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002271 if (new_policy->governor == policy->governor)
2272 goto out;
2273
2274 pr_debug("governor switch\n");
2275
2276 /* save old, working values */
2277 old_gov = policy->governor;
2278 /* end old governor */
2279 if (old_gov) {
2280 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2281 up_write(&policy->rwsem);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02002282 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002283 down_write(&policy->rwsem);
2284 }
2285
2286 /* start new governor */
2287 policy->governor = new_policy->governor;
2288 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2289 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2290 goto out;
2291
2292 up_write(&policy->rwsem);
2293 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2294 down_write(&policy->rwsem);
2295 }
2296
2297 /* new governor failed, so re-start old one */
2298 pr_debug("starting governor %s failed\n", policy->governor->name);
2299 if (old_gov) {
2300 policy->governor = old_gov;
2301 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2302 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2303 }
2304
2305 return -EINVAL;
2306
2307 out:
2308 pr_debug("governor: change or update limits\n");
2309 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310}
2311
2312/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2314 * @cpu: CPU which shall be re-evaluated
2315 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002316 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 * at different times.
2318 */
2319int cpufreq_update_policy(unsigned int cpu)
2320{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302321 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2322 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002323 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002325 if (!policy)
2326 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327
viresh kumarad7722d2013-10-18 19:10:15 +05302328 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002330 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302331 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302332 new_policy.min = policy->user_policy.min;
2333 new_policy.max = policy->user_policy.max;
2334 new_policy.policy = policy->user_policy.policy;
2335 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Viresh Kumarbb176f72013-06-19 14:19:33 +05302337 /*
2338 * BIOS might change freq behind our back
2339 * -> ask driver for current freq and notify governors about a change
2340 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002341 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302342 new_policy.cur = cpufreq_driver->get(cpu);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302343 if (WARN_ON(!new_policy.cur)) {
2344 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002345 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302346 }
2347
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302348 if (!policy->cur) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002349 pr_debug("Driver did not initialize current freq\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302350 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002351 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302352 if (policy->cur != new_policy.cur && has_target())
Viresh Kumara1e1dc42015-01-02 12:34:28 +05302353 cpufreq_out_of_sync(policy, new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002354 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002355 }
2356
Viresh Kumar037ce832013-10-02 14:13:16 +05302357 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002359unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302360 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002361
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302362 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return ret;
2364}
2365EXPORT_SYMBOL(cpufreq_update_policy);
2366
Paul Gortmaker27609842013-06-19 13:54:04 -04002367static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002368 unsigned long action, void *hcpu)
2369{
2370 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002371 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002372
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002373 dev = get_cpu_device(cpu);
2374 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302375 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002376 case CPU_ONLINE:
Viresh Kumar23faf0b2015-02-19 17:02:04 +05302377 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002378 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302379
Ashok Rajc32b6b82005-10-30 14:59:54 -08002380 case CPU_DOWN_PREPARE:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302381 __cpufreq_remove_dev_prepare(dev, NULL);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302382 break;
2383
2384 case CPU_POST_DEAD:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302385 __cpufreq_remove_dev_finish(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002386 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302387
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002388 case CPU_DOWN_FAILED:
Viresh Kumar23faf0b2015-02-19 17:02:04 +05302389 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002390 break;
2391 }
2392 }
2393 return NOTIFY_OK;
2394}
2395
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002396static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302397 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002398};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002401 * BOOST *
2402 *********************************************************************/
2403static int cpufreq_boost_set_sw(int state)
2404{
2405 struct cpufreq_frequency_table *freq_table;
2406 struct cpufreq_policy *policy;
2407 int ret = -EINVAL;
2408
Viresh Kumarf9637352015-05-12 12:20:11 +05302409 for_each_active_policy(policy) {
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002410 freq_table = cpufreq_frequency_get_table(policy->cpu);
2411 if (freq_table) {
2412 ret = cpufreq_frequency_table_cpuinfo(policy,
2413 freq_table);
2414 if (ret) {
2415 pr_err("%s: Policy frequency update failed\n",
2416 __func__);
2417 break;
2418 }
2419 policy->user_policy.max = policy->max;
2420 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2421 }
2422 }
2423
2424 return ret;
2425}
2426
2427int cpufreq_boost_trigger_state(int state)
2428{
2429 unsigned long flags;
2430 int ret = 0;
2431
2432 if (cpufreq_driver->boost_enabled == state)
2433 return 0;
2434
2435 write_lock_irqsave(&cpufreq_driver_lock, flags);
2436 cpufreq_driver->boost_enabled = state;
2437 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2438
2439 ret = cpufreq_driver->set_boost(state);
2440 if (ret) {
2441 write_lock_irqsave(&cpufreq_driver_lock, flags);
2442 cpufreq_driver->boost_enabled = !state;
2443 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2444
Joe Perchese837f9b2014-03-11 10:03:00 -07002445 pr_err("%s: Cannot %s BOOST\n",
2446 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002447 }
2448
2449 return ret;
2450}
2451
2452int cpufreq_boost_supported(void)
2453{
2454 if (likely(cpufreq_driver))
2455 return cpufreq_driver->boost_supported;
2456
2457 return 0;
2458}
2459EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2460
2461int cpufreq_boost_enabled(void)
2462{
2463 return cpufreq_driver->boost_enabled;
2464}
2465EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2466
2467/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2469 *********************************************************************/
2470
2471/**
2472 * cpufreq_register_driver - register a CPU Frequency driver
2473 * @driver_data: A struct cpufreq_driver containing the values#
2474 * submitted by the CPU Frequency driver.
2475 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302476 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002478 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 *
2480 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002481int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 unsigned long flags;
2484 int ret;
2485
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002486 if (cpufreq_disabled())
2487 return -ENODEV;
2488
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302490 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002491 driver_data->target) ||
2492 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302493 driver_data->target)) ||
2494 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return -EINVAL;
2496
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002497 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002499 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002500 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002501 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002502 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002504 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002505 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302507 if (driver_data->setpolicy)
2508 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2509
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002510 if (cpufreq_boost_supported()) {
2511 /*
2512 * Check if driver provides function to enable boost -
2513 * if not, use cpufreq_boost_set_sw as default
2514 */
2515 if (!cpufreq_driver->set_boost)
2516 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2517
2518 ret = cpufreq_sysfs_create_file(&boost.attr);
2519 if (ret) {
2520 pr_err("%s: cannot register global BOOST sysfs file\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002521 __func__);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002522 goto err_null_driver;
2523 }
2524 }
2525
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002526 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002527 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002528 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302530 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2531 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302533 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2534 driver_data->name);
2535 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002538 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002539 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002541 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002542err_if_unreg:
2543 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002544err_boost_unreg:
2545 if (cpufreq_boost_supported())
2546 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002547err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002548 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002549 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002550 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002551 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555/**
2556 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2557 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302558 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 * the right to do so, i.e. if you have succeeded in initialising before!
2560 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2561 * currently not initialised.
2562 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002563int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
2565 unsigned long flags;
2566
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002567 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002570 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002572 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002573 if (cpufreq_boost_supported())
2574 cpufreq_sysfs_remove_file(&boost.attr);
2575
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002576 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Viresh Kumar6eed9402013-08-06 22:53:11 +05302578 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002579 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302580
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002581 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302582
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002583 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302584 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
2586 return 0;
2587}
2588EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002589
Doug Anderson90de2a42014-12-23 22:09:48 -08002590/*
2591 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2592 * or mutexes when secondary CPUs are halted.
2593 */
2594static struct syscore_ops cpufreq_syscore_ops = {
2595 .shutdown = cpufreq_suspend,
2596};
2597
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002598static int __init cpufreq_core_init(void)
2599{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002600 if (cpufreq_disabled())
2601 return -ENODEV;
2602
Viresh Kumar2361be22013-05-17 16:09:09 +05302603 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002604 BUG_ON(!cpufreq_global_kobject);
2605
Doug Anderson90de2a42014-12-23 22:09:48 -08002606 register_syscore_ops(&cpufreq_syscore_ops);
2607
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002608 return 0;
2609}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002610core_initcall(cpufreq_core_init);