blob: 70b568a253e978c4d503cc3cc2c7cac83b635a43 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/**
Dave Jonescd878472006-08-11 17:59:28 -040035 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
38 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020039static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053041static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053042static DEFINE_RWLOCK(cpufreq_driver_lock);
Jane Li6f1e4ef2014-01-03 17:17:41 +080043DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053044static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053045
Thomas Renninger084f3492007-07-09 11:35:28 -070046/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040047static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Viresh Kumar2f0aea92014-03-04 11:00:26 +080049/* Flag to suspend/resume CPUFreq governors */
50static bool cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053052static inline bool has_target(void)
53{
54 return cpufreq_driver->target_index || cpufreq_driver->target;
55}
56
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080057/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053058 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
59 * sections
60 */
61static DECLARE_RWSEM(cpufreq_rwsem);
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050064static int __cpufreq_governor(struct cpufreq_policy *policy,
65 unsigned int event);
Viresh Kumard92d50a2015-01-02 12:34:29 +053066static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
David Howells65f27f32006-11-22 14:55:48 +000067static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/**
Dave Jones32ee8c32006-02-28 00:43:23 -050070 * Two notifier lists: the "policy" list is involved in the
71 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * "transition" list for kernel code that needs to handle
73 * changes to devices when the CPU clock speed changes.
74 * The mutex locks both lists.
75 */
Alan Sterne041c682006-03-27 01:16:30 -080076static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070077static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020079static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070080static int __init init_cpufreq_transition_notifier_list(void)
81{
82 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020083 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070084 return 0;
85}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080086pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040088static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020089static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040090{
91 return off;
92}
93void disable_cpufreq(void)
94{
95 off = 1;
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050098static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000100bool have_governor_per_policy(void)
101{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530102 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000103}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000104EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000105
Viresh Kumar944e9a02013-05-16 05:09:57 +0000106struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
107{
108 if (have_governor_per_policy())
109 return &policy->kobj;
110 else
111 return cpufreq_global_kobject;
112}
113EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
114
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000115static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
116{
117 u64 idle_time;
118 u64 cur_wall_time;
119 u64 busy_time;
120
121 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
122
123 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
127 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
128 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
129
130 idle_time = cur_wall_time - busy_time;
131 if (wall)
132 *wall = cputime_to_usecs(cur_wall_time);
133
134 return cputime_to_usecs(idle_time);
135}
136
137u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
138{
139 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
140
141 if (idle_time == -1ULL)
142 return get_cpu_idle_time_jiffy(cpu, wall);
143 else if (!io_busy)
144 idle_time += get_cpu_iowait_time_us(cpu, wall);
145
146 return idle_time;
147}
148EXPORT_SYMBOL_GPL(get_cpu_idle_time);
149
Viresh Kumar70e9e772013-10-03 20:29:07 +0530150/*
151 * This is a generic cpufreq init() routine which can be used by cpufreq
152 * drivers of SMP systems. It will do following:
153 * - validate & show freq table passed
154 * - set policies transition latency
155 * - policy->cpus with all possible CPUs
156 */
157int cpufreq_generic_init(struct cpufreq_policy *policy,
158 struct cpufreq_frequency_table *table,
159 unsigned int transition_latency)
160{
161 int ret;
162
163 ret = cpufreq_table_validate_and_show(policy, table);
164 if (ret) {
165 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
166 return ret;
167 }
168
169 policy->cpuinfo.transition_latency = transition_latency;
170
171 /*
172 * The driver only supports the SMP configuartion where all processors
173 * share the clock and voltage and clock.
174 */
175 cpumask_setall(policy->cpus);
176
177 return 0;
178}
179EXPORT_SYMBOL_GPL(cpufreq_generic_init);
180
Viresh Kumar652ed952014-01-09 20:38:43 +0530181unsigned int cpufreq_generic_get(unsigned int cpu)
182{
183 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
184
185 if (!policy || IS_ERR(policy->clk)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700186 pr_err("%s: No %s associated to cpu: %d\n",
187 __func__, policy ? "clk" : "policy", cpu);
Viresh Kumar652ed952014-01-09 20:38:43 +0530188 return 0;
189 }
190
191 return clk_get_rate(policy->clk) / 1000;
192}
193EXPORT_SYMBOL_GPL(cpufreq_generic_get);
194
Viresh Kumare0b31652014-03-10 14:53:33 +0530195/* Only for cpufreq core internal use */
196struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
197{
198 return per_cpu(cpufreq_cpu_data, cpu);
199}
200
Viresh Kumar6eed9402013-08-06 22:53:11 +0530201struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530203 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 unsigned long flags;
205
Viresh Kumar6eed9402013-08-06 22:53:11 +0530206 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
207 return NULL;
208
209 if (!down_read_trylock(&cpufreq_rwsem))
210 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000213 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Viresh Kumar6eed9402013-08-06 22:53:11 +0530215 if (cpufreq_driver) {
216 /* get the CPU */
217 policy = per_cpu(cpufreq_cpu_data, cpu);
218 if (policy)
219 kobject_get(&policy->kobj);
220 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200221
Viresh Kumar6eed9402013-08-06 22:53:11 +0530222 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530224 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530225 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530227 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
230
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530231void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000233 if (cpufreq_disabled())
234 return;
235
Viresh Kumar6eed9402013-08-06 22:53:11 +0530236 kobject_put(&policy->kobj);
237 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
243 *********************************************************************/
244
245/**
246 * adjust_jiffies - adjust the system "loops_per_jiffy"
247 *
248 * This function alters the system "loops_per_jiffy" for the clock
249 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500250 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * per-CPU loops_per_jiffy value wherever possible.
252 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800253static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Viresh Kumar39c132e2015-01-02 12:34:34 +0530255#ifndef CONFIG_SMP
256 static unsigned long l_p_j_ref;
257 static unsigned int l_p_j_ref_freq;
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ci->flags & CPUFREQ_CONST_LOOPS)
260 return;
261
262 if (!l_p_j_ref_freq) {
263 l_p_j_ref = loops_per_jiffy;
264 l_p_j_ref_freq = ci->old;
Joe Perchese837f9b2014-03-11 10:03:00 -0700265 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
266 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
Viresh Kumar0b443ea2014-03-19 11:24:58 +0530268 if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530269 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
270 ci->new);
Joe Perchese837f9b2014-03-11 10:03:00 -0700271 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
272 loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#endif
Viresh Kumar39c132e2015-01-02 12:34:34 +0530275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530277static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530278 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 BUG_ON(irqs_disabled());
281
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000282 if (cpufreq_disabled())
283 return;
284
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200285 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200286 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700287 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500292 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800293 * which is not equal to what the cpufreq core thinks is
294 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200296 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800297 if ((policy) && (policy->cpu == freqs->cpu) &&
298 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700299 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
300 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800301 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 }
303 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700304 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800305 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
307 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 case CPUFREQ_POSTCHANGE:
310 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700311 pr_debug("FREQ: %lu - CPU: %lu\n",
312 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100313 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700314 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800315 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800316 if (likely(policy) && likely(policy->cpu == freqs->cpu))
317 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530321
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530322/**
323 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
324 * on frequency transition.
325 *
326 * This function calls the transition notifiers and the "adjust_jiffies"
327 * function. It is called twice on all CPU frequency changes that have
328 * external effects.
329 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530330static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530331 struct cpufreq_freqs *freqs, unsigned int state)
332{
333 for_each_cpu(freqs->cpu, policy->cpus)
334 __cpufreq_notify_transition(policy, freqs, state);
335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530337/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530338static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530339 struct cpufreq_freqs *freqs, int transition_failed)
340{
341 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
342 if (!transition_failed)
343 return;
344
345 swap(freqs->old, freqs->new);
346 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
347 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530349
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530350void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
351 struct cpufreq_freqs *freqs)
352{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530353
354 /*
355 * Catch double invocations of _begin() which lead to self-deadlock.
356 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
357 * doesn't invoke _begin() on their behalf, and hence the chances of
358 * double invocations are very low. Moreover, there are scenarios
359 * where these checks can emit false-positive warnings in these
360 * drivers; so we avoid that by skipping them altogether.
361 */
362 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
363 && current == policy->transition_task);
364
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530365wait:
366 wait_event(policy->transition_wait, !policy->transition_ongoing);
367
368 spin_lock(&policy->transition_lock);
369
370 if (unlikely(policy->transition_ongoing)) {
371 spin_unlock(&policy->transition_lock);
372 goto wait;
373 }
374
375 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530376 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530377
378 spin_unlock(&policy->transition_lock);
379
380 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
381}
382EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
383
384void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
385 struct cpufreq_freqs *freqs, int transition_failed)
386{
387 if (unlikely(WARN_ON(!policy->transition_ongoing)))
388 return;
389
390 cpufreq_notify_post_transition(policy, freqs, transition_failed);
391
392 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530393 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530394
395 wake_up(&policy->transition_wait);
396}
397EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
398
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/*********************************************************************
401 * SYSFS INTERFACE *
402 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530403static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100404 struct attribute *attr, char *buf)
405{
406 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
407}
408
409static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
410 const char *buf, size_t count)
411{
412 int ret, enable;
413
414 ret = sscanf(buf, "%d", &enable);
415 if (ret != 1 || enable < 0 || enable > 1)
416 return -EINVAL;
417
418 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700419 pr_err("%s: Cannot %s BOOST!\n",
420 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100421 return -EINVAL;
422 }
423
Joe Perchese837f9b2014-03-11 10:03:00 -0700424 pr_debug("%s: cpufreq BOOST %s\n",
425 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100426
427 return count;
428}
429define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530431static struct cpufreq_governor *find_governor(const char *str_governor)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700432{
433 struct cpufreq_governor *t;
434
435 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200436 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700437 return t;
438
439 return NULL;
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/**
443 * cpufreq_parse_governor - parse a governor string
444 */
Dave Jones905d77c2008-03-05 14:28:32 -0500445static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 struct cpufreq_governor **governor)
447{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700448 int err = -EINVAL;
449
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200450 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700451 goto out;
452
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200453 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200454 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700456 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200457 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530458 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700460 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Viresh Kumar2e1cc3a2015-01-02 12:34:27 +0530462 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700464
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800465 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700466
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530467 t = find_governor(str_governor);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700468
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700469 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700470 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700471
Kees Cook1a8e1462011-05-04 08:38:56 -0700472 mutex_unlock(&cpufreq_governor_mutex);
473 ret = request_module("cpufreq_%s", str_governor);
474 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700475
Kees Cook1a8e1462011-05-04 08:38:56 -0700476 if (ret == 0)
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530477 t = find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700478 }
479
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700480 if (t != NULL) {
481 *governor = t;
482 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700484
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800485 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Dave Jones29464f22009-01-18 01:37:11 -0500487out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700488 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530492 * cpufreq_per_cpu_attr_read() / show_##file_name() -
493 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 *
495 * Write out information from cpufreq_driver->policy[cpu]; object must be
496 * "unsigned int".
497 */
498
Dave Jones32ee8c32006-02-28 00:43:23 -0500499#define show_one(file_name, object) \
500static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500501(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500502{ \
Dave Jones29464f22009-01-18 01:37:11 -0500503 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506show_one(cpuinfo_min_freq, cpuinfo.min_freq);
507show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100508show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509show_one(scaling_min_freq, min);
510show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700511
Viresh Kumar09347b22015-01-02 12:34:24 +0530512static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
Dirk Brandewiec034b022014-10-13 08:37:40 -0700513{
514 ssize_t ret;
515
516 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
517 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
518 else
519 ret = sprintf(buf, "%u\n", policy->cur);
520 return ret;
521}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Viresh Kumar037ce832013-10-02 14:13:16 +0530523static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530524 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526/**
527 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
528 */
529#define store_one(file_name, object) \
530static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500531(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800533 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct cpufreq_policy new_policy; \
535 \
536 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
537 if (ret) \
538 return -EINVAL; \
539 \
Dave Jones29464f22009-01-18 01:37:11 -0500540 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (ret != 1) \
542 return -EINVAL; \
543 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800544 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530545 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800546 if (!ret) \
547 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 \
549 return ret ? ret : count; \
550}
551
Dave Jones29464f22009-01-18 01:37:11 -0500552store_one(scaling_min_freq, min);
553store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/**
556 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
557 */
Dave Jones905d77c2008-03-05 14:28:32 -0500558static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
559 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Viresh Kumard92d50a2015-01-02 12:34:29 +0530561 unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (!cur_freq)
563 return sprintf(buf, "<unknown>");
564 return sprintf(buf, "%u\n", cur_freq);
565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/**
568 * show_scaling_governor - show the current policy for the specified CPU
569 */
Dave Jones905d77c2008-03-05 14:28:32 -0500570static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Dave Jones29464f22009-01-18 01:37:11 -0500572 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return sprintf(buf, "powersave\n");
574 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
575 return sprintf(buf, "performance\n");
576 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200577 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500578 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return -EINVAL;
580}
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/**
583 * store_scaling_governor - store policy for the specified CPU
584 */
Dave Jones905d77c2008-03-05 14:28:32 -0500585static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
586 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530588 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 char str_governor[16];
590 struct cpufreq_policy new_policy;
591
592 ret = cpufreq_get_policy(&new_policy, policy->cpu);
593 if (ret)
594 return ret;
595
Dave Jones29464f22009-01-18 01:37:11 -0500596 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (ret != 1)
598 return -EINVAL;
599
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530600 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
601 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return -EINVAL;
603
Viresh Kumar037ce832013-10-02 14:13:16 +0530604 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200605
606 policy->user_policy.policy = policy->policy;
607 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200608
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530609 if (ret)
610 return ret;
611 else
612 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615/**
616 * show_scaling_driver - show the cpufreq driver currently loaded
617 */
Dave Jones905d77c2008-03-05 14:28:32 -0500618static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200620 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623/**
624 * show_scaling_available_governors - show the available CPUfreq governors
625 */
Dave Jones905d77c2008-03-05 14:28:32 -0500626static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
627 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 ssize_t i = 0;
630 struct cpufreq_governor *t;
631
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530632 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 i += sprintf(buf, "performance powersave");
634 goto out;
635 }
636
637 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500638 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
639 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200641 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500643out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 i += sprintf(&buf[i], "\n");
645 return i;
646}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700647
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800648ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
650 ssize_t i = 0;
651 unsigned int cpu;
652
Rusty Russell835481d2009-01-04 05:18:06 -0800653 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (i)
655 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
656 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
657 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 i += sprintf(&buf[i], "\n");
661 return i;
662}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800663EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700665/**
666 * show_related_cpus - show the CPUs affected by each transition even if
667 * hw coordination is in use
668 */
669static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
670{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800671 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700672}
673
674/**
675 * show_affected_cpus - show the CPUs affected by each transition
676 */
677static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
678{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800679 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700680}
681
Venki Pallipadi9e769882007-10-26 10:18:21 -0700682static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500683 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700684{
685 unsigned int freq = 0;
686 unsigned int ret;
687
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700688 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700689 return -EINVAL;
690
691 ret = sscanf(buf, "%u", &freq);
692 if (ret != 1)
693 return -EINVAL;
694
695 policy->governor->store_setspeed(policy, freq);
696
697 return count;
698}
699
700static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
701{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700702 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700703 return sprintf(buf, "<unsupported>\n");
704
705 return policy->governor->show_setspeed(policy, buf);
706}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Thomas Renningere2f74f32009-11-19 12:31:01 +0100708/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200709 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100710 */
711static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
712{
713 unsigned int limit;
714 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200715 if (cpufreq_driver->bios_limit) {
716 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100717 if (!ret)
718 return sprintf(buf, "%u\n", limit);
719 }
720 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
721}
722
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200723cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
724cpufreq_freq_attr_ro(cpuinfo_min_freq);
725cpufreq_freq_attr_ro(cpuinfo_max_freq);
726cpufreq_freq_attr_ro(cpuinfo_transition_latency);
727cpufreq_freq_attr_ro(scaling_available_governors);
728cpufreq_freq_attr_ro(scaling_driver);
729cpufreq_freq_attr_ro(scaling_cur_freq);
730cpufreq_freq_attr_ro(bios_limit);
731cpufreq_freq_attr_ro(related_cpus);
732cpufreq_freq_attr_ro(affected_cpus);
733cpufreq_freq_attr_rw(scaling_min_freq);
734cpufreq_freq_attr_rw(scaling_max_freq);
735cpufreq_freq_attr_rw(scaling_governor);
736cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Dave Jones905d77c2008-03-05 14:28:32 -0500738static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 &cpuinfo_min_freq.attr,
740 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100741 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 &scaling_min_freq.attr,
743 &scaling_max_freq.attr,
744 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700745 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 &scaling_governor.attr,
747 &scaling_driver.attr,
748 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700749 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 NULL
751};
752
Dave Jones29464f22009-01-18 01:37:11 -0500753#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
754#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Jones29464f22009-01-18 01:37:11 -0500756static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
Dave Jones905d77c2008-03-05 14:28:32 -0500758 struct cpufreq_policy *policy = to_policy(kobj);
759 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530760 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530761
762 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530763 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800764
viresh kumarad7722d2013-10-18 19:10:15 +0530765 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800766
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530767 if (fattr->show)
768 ret = fattr->show(policy, buf);
769 else
770 ret = -EIO;
771
viresh kumarad7722d2013-10-18 19:10:15 +0530772 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530773 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return ret;
776}
777
Dave Jones905d77c2008-03-05 14:28:32 -0500778static ssize_t store(struct kobject *kobj, struct attribute *attr,
779 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Dave Jones905d77c2008-03-05 14:28:32 -0500781 struct cpufreq_policy *policy = to_policy(kobj);
782 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500783 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530784
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530785 get_online_cpus();
786
787 if (!cpu_online(policy->cpu))
788 goto unlock;
789
Viresh Kumar6eed9402013-08-06 22:53:11 +0530790 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530791 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800792
viresh kumarad7722d2013-10-18 19:10:15 +0530793 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800794
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530795 if (fattr->store)
796 ret = fattr->store(policy, buf, count);
797 else
798 ret = -EIO;
799
viresh kumarad7722d2013-10-18 19:10:15 +0530800 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530801
Viresh Kumar6eed9402013-08-06 22:53:11 +0530802 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530803unlock:
804 put_online_cpus();
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return ret;
807}
808
Dave Jones905d77c2008-03-05 14:28:32 -0500809static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Dave Jones905d77c2008-03-05 14:28:32 -0500811 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200812 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 complete(&policy->kobj_unregister);
814}
815
Emese Revfy52cf25d2010-01-19 02:58:23 +0100816static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 .show = show,
818 .store = store,
819};
820
821static struct kobj_type ktype_cpufreq = {
822 .sysfs_ops = &sysfs_ops,
823 .default_attrs = default_attrs,
824 .release = cpufreq_sysfs_release,
825};
826
Viresh Kumar2361be22013-05-17 16:09:09 +0530827struct kobject *cpufreq_global_kobject;
828EXPORT_SYMBOL(cpufreq_global_kobject);
829
830static int cpufreq_global_kobject_usage;
831
832int cpufreq_get_global_kobject(void)
833{
834 if (!cpufreq_global_kobject_usage++)
835 return kobject_add(cpufreq_global_kobject,
836 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
837
838 return 0;
839}
840EXPORT_SYMBOL(cpufreq_get_global_kobject);
841
842void cpufreq_put_global_kobject(void)
843{
844 if (!--cpufreq_global_kobject_usage)
845 kobject_del(cpufreq_global_kobject);
846}
847EXPORT_SYMBOL(cpufreq_put_global_kobject);
848
849int cpufreq_sysfs_create_file(const struct attribute *attr)
850{
851 int ret = cpufreq_get_global_kobject();
852
853 if (!ret) {
854 ret = sysfs_create_file(cpufreq_global_kobject, attr);
855 if (ret)
856 cpufreq_put_global_kobject();
857 }
858
859 return ret;
860}
861EXPORT_SYMBOL(cpufreq_sysfs_create_file);
862
863void cpufreq_sysfs_remove_file(const struct attribute *attr)
864{
865 sysfs_remove_file(cpufreq_global_kobject, attr);
866 cpufreq_put_global_kobject();
867}
868EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
869
Dave Jones19d6f7e2009-07-08 17:35:39 -0400870/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200871static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400872{
873 unsigned int j;
874 int ret = 0;
875
876 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800877 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400878
Viresh Kumar308b60e2013-07-31 14:35:14 +0200879 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400880 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400881
Viresh Kumare8fdde12013-07-31 14:31:33 +0200882 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800883 cpu_dev = get_cpu_device(j);
884 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400885 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200886 if (ret)
887 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400888 }
889 return ret;
890}
891
Viresh Kumar308b60e2013-07-31 14:35:14 +0200892static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800893 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400894{
895 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400896 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400897
Dave Jones909a6942009-07-08 18:05:42 -0400898 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200899 drv_attr = cpufreq_driver->attr;
Viresh Kumarf13f1182015-01-02 12:34:23 +0530900 while (drv_attr && *drv_attr) {
Dave Jones909a6942009-07-08 18:05:42 -0400901 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
902 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100903 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400904 drv_attr++;
905 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200906 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400907 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
908 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100909 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400910 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700911
912 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
913 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100914 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700915
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200916 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100917 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
918 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100919 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100920 }
Dave Jones909a6942009-07-08 18:05:42 -0400921
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100922 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530923}
924
925static void cpufreq_init_policy(struct cpufreq_policy *policy)
926{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800927 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530928 struct cpufreq_policy new_policy;
929 int ret = 0;
930
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530931 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000932
viresh kumar6e2c89d2014-03-04 11:43:59 +0800933 /* Update governor of new_policy to the governor used before hotplug */
Viresh Kumar42f91fa2015-01-02 12:34:26 +0530934 gov = find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
viresh kumar6e2c89d2014-03-04 11:43:59 +0800935 if (gov)
936 pr_debug("Restoring governor %s for cpu %d\n",
937 policy->governor->name, policy->cpu);
938 else
939 gov = CPUFREQ_DEFAULT_GOVERNOR;
940
941 new_policy.governor = gov;
942
Jason Barona27a9ab2013-12-19 22:50:50 +0000943 /* Use the default policy if its valid. */
944 if (cpufreq_driver->setpolicy)
viresh kumar6e2c89d2014-03-04 11:43:59 +0800945 cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
Dave Jonesecf7e462009-07-08 18:48:47 -0400946
947 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530948 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400949 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200950 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200951 if (cpufreq_driver->exit)
952 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400953 }
Dave Jones909a6942009-07-08 18:05:42 -0400954}
955
Viresh Kumard8d3b472013-08-04 01:20:07 +0200956static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530957 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000958{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530959 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000960 unsigned long flags;
961
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530962 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530963 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
964 if (ret) {
965 pr_err("%s: Failed to stop governor\n", __func__);
966 return ret;
967 }
968 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000969
viresh kumarad7722d2013-10-18 19:10:15 +0530970 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530971
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000972 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530973
Viresh Kumarfcf80582013-01-29 14:39:08 +0000974 cpumask_set_cpu(cpu, policy->cpus);
975 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000976 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000977
viresh kumarad7722d2013-10-18 19:10:15 +0530978 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530979
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530980 if (has_target()) {
Stratos Karafotise5c87b72014-03-19 23:29:17 +0200981 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
982 if (!ret)
983 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
984
985 if (ret) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530986 pr_err("%s: Failed to start governor\n", __func__);
987 return ret;
988 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200989 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000990
Viresh Kumar42f921a2013-12-20 21:26:02 +0530991 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000992}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530994static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
995{
996 struct cpufreq_policy *policy;
997 unsigned long flags;
998
Lan Tianyu44871c92013-09-11 15:05:05 +0800999 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301000
1001 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
1002
Lan Tianyu44871c92013-09-11 15:05:05 +08001003 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301004
Geert Uytterhoeven09712f52014-11-04 17:05:25 +01001005 if (policy)
1006 policy->governor = NULL;
viresh kumar6e2c89d2014-03-04 11:43:59 +08001007
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301008 return policy;
1009}
1010
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301011static struct cpufreq_policy *cpufreq_policy_alloc(void)
1012{
1013 struct cpufreq_policy *policy;
1014
1015 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1016 if (!policy)
1017 return NULL;
1018
1019 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1020 goto err_free_policy;
1021
1022 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1023 goto err_free_cpumask;
1024
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301025 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301026 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301027 spin_lock_init(&policy->transition_lock);
1028 init_waitqueue_head(&policy->transition_wait);
Viresh Kumar818c5712015-01-02 12:34:38 +05301029 init_completion(&policy->kobj_unregister);
1030 INIT_WORK(&policy->update, handle_update);
viresh kumarad7722d2013-10-18 19:10:15 +05301031
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301032 return policy;
1033
1034err_free_cpumask:
1035 free_cpumask_var(policy->cpus);
1036err_free_policy:
1037 kfree(policy);
1038
1039 return NULL;
1040}
1041
Viresh Kumar42f921a2013-12-20 21:26:02 +05301042static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1043{
1044 struct kobject *kobj;
1045 struct completion *cmp;
1046
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301047 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1048 CPUFREQ_REMOVE_POLICY, policy);
1049
Viresh Kumar42f921a2013-12-20 21:26:02 +05301050 down_read(&policy->rwsem);
1051 kobj = &policy->kobj;
1052 cmp = &policy->kobj_unregister;
1053 up_read(&policy->rwsem);
1054 kobject_put(kobj);
1055
1056 /*
1057 * We need to make sure that the underlying kobj is
1058 * actually not referenced anymore by anybody before we
1059 * proceed with unloading.
1060 */
1061 pr_debug("waiting for dropping of refcount\n");
1062 wait_for_completion(cmp);
1063 pr_debug("wait complete\n");
1064}
1065
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301066static void cpufreq_policy_free(struct cpufreq_policy *policy)
1067{
1068 free_cpumask_var(policy->related_cpus);
1069 free_cpumask_var(policy->cpus);
1070 kfree(policy);
1071}
1072
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301073static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu,
1074 struct device *cpu_dev)
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301075{
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301076 int ret;
1077
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301078 if (WARN_ON(cpu == policy->cpu))
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301079 return 0;
1080
1081 /* Move kobject to the new policy->cpu */
1082 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1083 if (ret) {
1084 pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
1085 return ret;
1086 }
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301087
viresh kumarad7722d2013-10-18 19:10:15 +05301088 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301089
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301090 policy->last_cpu = policy->cpu;
1091 policy->cpu = cpu;
1092
viresh kumarad7722d2013-10-18 19:10:15 +05301093 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301094
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301095 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1096 CPUFREQ_UPDATE_POLICY_CPU, policy);
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301097
1098 return 0;
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301099}
1100
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301101static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001103 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301104 int ret = -ENOMEM;
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301105 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 unsigned long flags;
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301107 bool recover_policy = cpufreq_suspended;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Ashok Rajc32b6b82005-10-30 14:59:54 -08001109 if (cpu_is_offline(cpu))
1110 return 0;
1111
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001112 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /* check whether a different CPU already registered this
1115 * CPU because it is in the same boat. */
Viresh Kumard7a97712015-01-02 12:34:33 +05301116 policy = cpufreq_cpu_get_raw(cpu);
1117 if (unlikely(policy))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001119
Viresh Kumar6eed9402013-08-06 22:53:11 +05301120 if (!down_read_trylock(&cpufreq_rwsem))
1121 return 0;
1122
Viresh Kumarfcf80582013-01-29 14:39:08 +00001123 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001124 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301125 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
1126 if (cpumask_test_cpu(cpu, policy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001127 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar7f0c0202015-01-02 12:34:32 +05301128 ret = cpufreq_add_policy_cpu(policy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301129 up_read(&cpufreq_rwsem);
1130 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301131 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001132 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001133 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001135 /*
1136 * Restore the saved policy when doing light-weight init and fall back
1137 * to the full init if that fails.
1138 */
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301139 policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001140 if (!policy) {
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301141 recover_policy = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301142 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001143 if (!policy)
1144 goto nomem_out;
1145 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301146
1147 /*
1148 * In the resume path, since we restore a saved policy, the assignment
1149 * to policy->cpu is like an update of the existing policy, rather than
1150 * the creation of a brand new one. So we need to perform this update
1151 * by invoking update_policy_cpu().
1152 */
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301153 if (recover_policy && cpu != policy->cpu)
1154 WARN_ON(update_policy_cpu(policy, cpu, dev));
1155 else
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301156 policy->cpu = cpu;
1157
Rusty Russell835481d2009-01-04 05:18:06 -08001158 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* call driver. From then on the cpufreq must be able
1161 * to accept all calls to ->verify and ->setpolicy for this CPU
1162 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001163 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001165 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301166 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001168
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001169 down_write(&policy->rwsem);
1170
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001171 /* related cpus should atleast have policy->cpus */
1172 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1173
1174 /*
1175 * affected cpus must always be the one, which are online. We aren't
1176 * managing offline cpus here.
1177 */
1178 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1179
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301180 if (!recover_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001181 policy->user_policy.min = policy->min;
1182 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001183
1184 /* prepare interface data */
1185 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1186 &dev->kobj, "cpufreq");
1187 if (ret) {
1188 pr_err("%s: failed to init policy->kobj: %d\n",
1189 __func__, ret);
1190 goto err_init_policy_kobj;
1191 }
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001192 }
1193
Viresh Kumar652ed952014-01-09 20:38:43 +05301194 write_lock_irqsave(&cpufreq_driver_lock, flags);
1195 for_each_cpu(j, policy->cpus)
1196 per_cpu(cpufreq_cpu_data, j) = policy;
1197 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1198
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001199 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301200 policy->cur = cpufreq_driver->get(policy->cpu);
1201 if (!policy->cur) {
1202 pr_err("%s: ->get() failed\n", __func__);
1203 goto err_get_freq;
1204 }
1205 }
1206
Viresh Kumard3916692013-12-03 11:20:46 +05301207 /*
1208 * Sometimes boot loaders set CPU frequency to a value outside of
1209 * frequency table present with cpufreq core. In such cases CPU might be
1210 * unstable if it has to run on that frequency for long duration of time
1211 * and so its better to set it to a frequency which is specified in
1212 * freq-table. This also makes cpufreq stats inconsistent as
1213 * cpufreq-stats would fail to register because current frequency of CPU
1214 * isn't found in freq-table.
1215 *
1216 * Because we don't want this change to effect boot process badly, we go
1217 * for the next freq which is >= policy->cur ('cur' must be set by now,
1218 * otherwise we will end up setting freq to lowest of the table as 'cur'
1219 * is initialized to zero).
1220 *
1221 * We are passing target-freq as "policy->cur - 1" otherwise
1222 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1223 * equal to target-freq.
1224 */
1225 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1226 && has_target()) {
1227 /* Are we running at unknown frequency ? */
1228 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1229 if (ret == -EINVAL) {
1230 /* Warn user and fix it */
1231 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1232 __func__, policy->cpu, policy->cur);
1233 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1234 CPUFREQ_RELATION_L);
1235
1236 /*
1237 * Reaching here after boot in a few seconds may not
1238 * mean that system will remain stable at "unknown"
1239 * frequency for longer duration. Hence, a BUG_ON().
1240 */
1241 BUG_ON(ret);
1242 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1243 __func__, policy->cpu, policy->cur);
1244 }
1245 }
1246
Thomas Renningera1531ac2008-07-29 22:32:58 -07001247 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1248 CPUFREQ_START, policy);
1249
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301250 if (!recover_policy) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001251 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301252 if (ret)
1253 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301254 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1255 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301256 }
Dave Jones8ff69732006-03-05 03:37:23 -05001257
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301258 write_lock_irqsave(&cpufreq_driver_lock, flags);
1259 list_add(&policy->policy_list, &cpufreq_policy_list);
1260 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1261
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301262 cpufreq_init_policy(policy);
1263
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301264 if (!recover_policy) {
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301265 policy->user_policy.policy = policy->policy;
1266 policy->user_policy.governor = policy->governor;
1267 }
Viresh Kumar4e97b632014-03-04 11:44:01 +08001268 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301269
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001270 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301271
Viresh Kumar6eed9402013-08-06 22:53:11 +05301272 up_read(&cpufreq_rwsem);
1273
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301274 /* Callback for handling stuff after policy is ready */
1275 if (cpufreq_driver->ready)
1276 cpufreq_driver->ready(policy);
1277
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001278 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return 0;
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301283err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001284 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301285 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001286 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001287 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001289 if (!recover_policy) {
1290 kobject_put(&policy->kobj);
1291 wait_for_completion(&policy->kobj_unregister);
1292 }
1293err_init_policy_kobj:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001294 up_write(&policy->rwsem);
1295
Viresh Kumarda60ce92013-10-03 20:28:30 +05301296 if (cpufreq_driver->exit)
1297 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301298err_set_policy_cpu:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301299 if (recover_policy) {
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001300 /* Do not leave stale fallback data behind. */
1301 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301302 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001303 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301304 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301307 up_read(&cpufreq_rwsem);
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return ret;
1310}
1311
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301312/**
1313 * cpufreq_add_dev - add a CPU device
1314 *
1315 * Adds the cpufreq interface for a CPU device.
1316 *
1317 * The Oracle says: try running cpufreq registration/unregistration concurrently
1318 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1319 * mess up, but more thorough testing is needed. - Mathieu
1320 */
1321static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1322{
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301323 return __cpufreq_add_dev(dev, sif);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301324}
1325
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301326static int __cpufreq_remove_dev_prepare(struct device *dev,
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301327 struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301329 unsigned int cpu = dev->id, cpus;
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301330 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301332 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001334 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001336 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301338 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301340 /* Save the policy somewhere when doing a light-weight tear-down */
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301341 if (cpufreq_suspended)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301342 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301343
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001344 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301346 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001347 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301351 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301352 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1353 if (ret) {
1354 pr_err("%s: Failed to stop governor\n", __func__);
1355 return ret;
1356 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001357
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001358 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301359 policy->governor->name, CPUFREQ_NAME_LEN);
Viresh Kumardb5f2992015-01-02 12:34:25 +05301360 }
Jacob Shin27ecddc2011-04-27 13:32:11 -05001361
viresh kumarad7722d2013-10-18 19:10:15 +05301362 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301363 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301364 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301366 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301367 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001368 } else if (cpus > 1) {
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301369 /* Nominate new CPU */
1370 int new_cpu = cpumask_any_but(policy->cpus, cpu);
1371 struct device *cpu_dev = get_cpu_device(new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301372
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301373 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1374 ret = update_policy_cpu(policy, new_cpu, cpu_dev);
1375 if (ret) {
1376 if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1377 "cpufreq"))
1378 pr_err("%s: Failed to restore kobj link to cpu:%d\n",
1379 __func__, cpu_dev->id);
1380 return ret;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001381 }
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301382
1383 if (!cpufreq_suspended)
1384 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1385 __func__, new_cpu, cpu);
Preeti U Murthy789ca242014-09-29 15:47:12 +02001386 } else if (cpufreq_driver->stop_cpu) {
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001387 cpufreq_driver->stop_cpu(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001388 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001389
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301390 return 0;
1391}
1392
1393static int __cpufreq_remove_dev_finish(struct device *dev,
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301394 struct subsys_interface *sif)
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301395{
1396 unsigned int cpu = dev->id, cpus;
1397 int ret;
1398 unsigned long flags;
1399 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301400
1401 read_lock_irqsave(&cpufreq_driver_lock, flags);
1402 policy = per_cpu(cpufreq_cpu_data, cpu);
1403 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1404
1405 if (!policy) {
1406 pr_debug("%s: No cpu_data found\n", __func__);
1407 return -EINVAL;
1408 }
1409
viresh kumarad7722d2013-10-18 19:10:15 +05301410 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301411 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301412
1413 if (cpus > 1)
1414 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301415 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301416
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001417 /* If cpu is last user of policy, free policy */
1418 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301419 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301420 ret = __cpufreq_governor(policy,
1421 CPUFREQ_GOV_POLICY_EXIT);
1422 if (ret) {
1423 pr_err("%s: Failed to exit governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001424 __func__);
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301425 return ret;
1426 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301427 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001428
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301429 if (!cpufreq_suspended)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301430 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301431
1432 /*
1433 * Perform the ->exit() even during light-weight tear-down,
1434 * since this is a core component, and is essential for the
1435 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001436 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001437 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301438 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001439
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301440 /* Remove policy from list of active policies */
1441 write_lock_irqsave(&cpufreq_driver_lock, flags);
1442 list_del(&policy->policy_list);
1443 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1444
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301445 if (!cpufreq_suspended)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301446 cpufreq_policy_free(policy);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02001447 } else if (has_target()) {
1448 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1449 if (!ret)
1450 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1451
1452 if (ret) {
1453 pr_err("%s: Failed to start governor\n", __func__);
1454 return ret;
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001455 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Viresh Kumar474deff2013-08-20 12:08:25 +05301458 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return 0;
1460}
1461
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301462/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301463 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301464 *
1465 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301466 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001467static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001468{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001469 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301470 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001471
1472 if (cpu_is_offline(cpu))
1473 return 0;
1474
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301475 ret = __cpufreq_remove_dev_prepare(dev, sif);
Viresh Kumar27a862e2013-10-02 14:13:14 +05301476
1477 if (!ret)
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301478 ret = __cpufreq_remove_dev_finish(dev, sif);
Viresh Kumar27a862e2013-10-02 14:13:14 +05301479
1480 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001481}
1482
David Howells65f27f32006-11-22 14:55:48 +00001483static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
David Howells65f27f32006-11-22 14:55:48 +00001485 struct cpufreq_policy *policy =
1486 container_of(work, struct cpufreq_policy, update);
1487 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001488 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 cpufreq_update_policy(cpu);
1490}
1491
1492/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301493 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1494 * in deep trouble.
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301495 * @policy: policy managing CPUs
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 * @new_freq: CPU frequency the CPU actually runs at
1497 *
Dave Jones29464f22009-01-18 01:37:11 -05001498 * We adjust to current frequency first, and need to clean up later.
1499 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 */
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301501static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301502 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301505
Joe Perchese837f9b2014-03-11 10:03:00 -07001506 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 +05301507 policy->cur, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301509 freqs.old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301511
Viresh Kumar8fec0512014-03-24 13:35:45 +05301512 cpufreq_freq_transition_begin(policy, &freqs);
1513 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
Dave Jones32ee8c32006-02-28 00:43:23 -05001516/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301517 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001518 * @cpu: CPU number
1519 *
1520 * This is the last known freq, without actually getting it from the driver.
1521 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1522 */
1523unsigned int cpufreq_quick_get(unsigned int cpu)
1524{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001525 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301526 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001527
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001528 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1529 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001530
1531 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001532 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301533 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001534 cpufreq_cpu_put(policy);
1535 }
1536
Dave Jones4d34a672008-02-07 16:33:49 -05001537 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001538}
1539EXPORT_SYMBOL(cpufreq_quick_get);
1540
Jesse Barnes3d737102011-06-28 10:59:12 -07001541/**
1542 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1543 * @cpu: CPU number
1544 *
1545 * Just return the max possible frequency for a given CPU.
1546 */
1547unsigned int cpufreq_quick_get_max(unsigned int cpu)
1548{
1549 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1550 unsigned int ret_freq = 0;
1551
1552 if (policy) {
1553 ret_freq = policy->max;
1554 cpufreq_cpu_put(policy);
1555 }
1556
1557 return ret_freq;
1558}
1559EXPORT_SYMBOL(cpufreq_quick_get_max);
1560
Viresh Kumard92d50a2015-01-02 12:34:29 +05301561static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301563 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001565 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001566 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567
Viresh Kumard92d50a2015-01-02 12:34:29 +05301568 ret_freq = cpufreq_driver->get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301570 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001571 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301572 /* verify no discrepancy between actual and
1573 saved value exists */
1574 if (unlikely(ret_freq != policy->cur)) {
Viresh Kumara1e1dc42015-01-02 12:34:28 +05301575 cpufreq_out_of_sync(policy, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 schedule_work(&policy->update);
1577 }
1578 }
1579
Dave Jones4d34a672008-02-07 16:33:49 -05001580 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001583/**
1584 * cpufreq_get - get the current CPU frequency (in kHz)
1585 * @cpu: CPU number
1586 *
1587 * Get the CPU current (static) CPU frequency
1588 */
1589unsigned int cpufreq_get(unsigned int cpu)
1590{
Aaron Plattner999976e2014-03-04 12:42:15 -08001591 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001592 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001593
Aaron Plattner999976e2014-03-04 12:42:15 -08001594 if (policy) {
1595 down_read(&policy->rwsem);
Viresh Kumard92d50a2015-01-02 12:34:29 +05301596 ret_freq = __cpufreq_get(policy);
Aaron Plattner999976e2014-03-04 12:42:15 -08001597 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301598
Aaron Plattner999976e2014-03-04 12:42:15 -08001599 cpufreq_cpu_put(policy);
1600 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301601
Dave Jones4d34a672008-02-07 16:33:49 -05001602 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604EXPORT_SYMBOL(cpufreq_get);
1605
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001606static struct subsys_interface cpufreq_interface = {
1607 .name = "cpufreq",
1608 .subsys = &cpu_subsys,
1609 .add_dev = cpufreq_add_dev,
1610 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001611};
1612
Viresh Kumare28867e2014-03-04 11:00:27 +08001613/*
1614 * In case platform wants some specific frequency to be configured
1615 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001616 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001617int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001618{
Viresh Kumare28867e2014-03-04 11:00:27 +08001619 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001620
Viresh Kumare28867e2014-03-04 11:00:27 +08001621 if (!policy->suspend_freq) {
1622 pr_err("%s: suspend_freq can't be zero\n", __func__);
1623 return -EINVAL;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001624 }
1625
Viresh Kumare28867e2014-03-04 11:00:27 +08001626 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1627 policy->suspend_freq);
1628
1629 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1630 CPUFREQ_RELATION_H);
1631 if (ret)
1632 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1633 __func__, policy->suspend_freq, ret);
1634
Dave Jonesc9060492008-02-07 16:32:18 -05001635 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001636}
Viresh Kumare28867e2014-03-04 11:00:27 +08001637EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001638
1639/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001640 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001642 * Called during system wide Suspend/Hibernate cycles for suspending governors
1643 * as some platforms can't change frequency after this point in suspend cycle.
1644 * Because some of the devices (like: i2c, regulators, etc) they use for
1645 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001647void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301649 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001651 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001654 if (!has_target())
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301655 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001657 pr_debug("%s: Suspending Governors\n", __func__);
1658
1659 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
1660 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1661 pr_err("%s: Failed to stop governor for policy: %p\n",
1662 __func__, policy);
1663 else if (cpufreq_driver->suspend
1664 && cpufreq_driver->suspend(policy))
1665 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1666 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301668
1669suspend:
1670 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001674 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001676 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1677 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001679void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 struct cpufreq_policy *policy;
1682
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001683 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return;
1685
Lan Tianyu8e304442014-09-18 15:03:07 +08001686 cpufreq_suspended = false;
1687
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001688 if (!has_target())
1689 return;
1690
1691 pr_debug("%s: Resuming Governors\n", __func__);
1692
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001693 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301694 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1695 pr_err("%s: Failed to resume driver: %p\n", __func__,
1696 policy);
1697 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001698 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1699 pr_err("%s: Failed to start governor for policy: %p\n",
1700 __func__, policy);
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001701
1702 /*
1703 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
1704 * policy in list. It will verify that the current freq is in
1705 * sync with what we believe it to be.
1706 */
1707 if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
1708 schedule_work(&policy->update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Borislav Petkov9d950462013-01-20 10:24:28 +00001712/**
1713 * cpufreq_get_current_driver - return current driver's name
1714 *
1715 * Return the name string of the currently loaded cpufreq driver
1716 * or NULL, if none.
1717 */
1718const char *cpufreq_get_current_driver(void)
1719{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001720 if (cpufreq_driver)
1721 return cpufreq_driver->name;
1722
1723 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001724}
1725EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001727/**
1728 * cpufreq_get_driver_data - return current driver data
1729 *
1730 * Return the private data of the currently loaded cpufreq
1731 * driver, or NULL if no cpufreq driver is loaded.
1732 */
1733void *cpufreq_get_driver_data(void)
1734{
1735 if (cpufreq_driver)
1736 return cpufreq_driver->driver_data;
1737
1738 return NULL;
1739}
1740EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742/*********************************************************************
1743 * NOTIFIER LISTS INTERFACE *
1744 *********************************************************************/
1745
1746/**
1747 * cpufreq_register_notifier - register a driver with cpufreq
1748 * @nb: notifier function to register
1749 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1750 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001751 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * are notified about clock rate changes (once before and once after
1753 * the transition), or a list of drivers that are notified about
1754 * changes in cpufreq policy.
1755 *
1756 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001757 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 */
1759int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1760{
1761 int ret;
1762
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001763 if (cpufreq_disabled())
1764 return -EINVAL;
1765
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001766 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 switch (list) {
1769 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001770 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001771 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 break;
1773 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001774 ret = blocking_notifier_chain_register(
1775 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777 default:
1778 ret = -EINVAL;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 return ret;
1782}
1783EXPORT_SYMBOL(cpufreq_register_notifier);
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/**
1786 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1787 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301788 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 *
1790 * Remove a driver from the CPU frequency notifier list.
1791 *
1792 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001793 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 */
1795int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1796{
1797 int ret;
1798
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001799 if (cpufreq_disabled())
1800 return -EINVAL;
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 switch (list) {
1803 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001804 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001805 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 break;
1807 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001808 ret = blocking_notifier_chain_unregister(
1809 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811 default:
1812 ret = -EINVAL;
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 return ret;
1816}
1817EXPORT_SYMBOL(cpufreq_unregister_notifier);
1818
1819
1820/*********************************************************************
1821 * GOVERNORS *
1822 *********************************************************************/
1823
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301824/* Must set freqs->new to intermediate frequency */
1825static int __target_intermediate(struct cpufreq_policy *policy,
1826 struct cpufreq_freqs *freqs, int index)
1827{
1828 int ret;
1829
1830 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1831
1832 /* We don't need to switch to intermediate freq */
1833 if (!freqs->new)
1834 return 0;
1835
1836 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1837 __func__, policy->cpu, freqs->old, freqs->new);
1838
1839 cpufreq_freq_transition_begin(policy, freqs);
1840 ret = cpufreq_driver->target_intermediate(policy, index);
1841 cpufreq_freq_transition_end(policy, freqs, ret);
1842
1843 if (ret)
1844 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1845 __func__, ret);
1846
1847 return ret;
1848}
1849
Viresh Kumar8d657752014-05-21 14:29:29 +05301850static int __target_index(struct cpufreq_policy *policy,
1851 struct cpufreq_frequency_table *freq_table, int index)
1852{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301853 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1854 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301855 int retval = -EINVAL;
1856 bool notify;
1857
1858 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301859 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301860 /* Handle switching to intermediate frequency */
1861 if (cpufreq_driver->get_intermediate) {
1862 retval = __target_intermediate(policy, &freqs, index);
1863 if (retval)
1864 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301865
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301866 intermediate_freq = freqs.new;
1867 /* Set old freq to intermediate */
1868 if (intermediate_freq)
1869 freqs.old = freqs.new;
1870 }
1871
1872 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301873 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1874 __func__, policy->cpu, freqs.old, freqs.new);
1875
1876 cpufreq_freq_transition_begin(policy, &freqs);
1877 }
1878
1879 retval = cpufreq_driver->target_index(policy, index);
1880 if (retval)
1881 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1882 retval);
1883
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301884 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301885 cpufreq_freq_transition_end(policy, &freqs, retval);
1886
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301887 /*
1888 * Failed after setting to intermediate freq? Driver should have
1889 * reverted back to initial frequency and so should we. Check
1890 * here for intermediate_freq instead of get_intermediate, in
1891 * case we have't switched to intermediate freq at all.
1892 */
1893 if (unlikely(retval && intermediate_freq)) {
1894 freqs.old = intermediate_freq;
1895 freqs.new = policy->restore_freq;
1896 cpufreq_freq_transition_begin(policy, &freqs);
1897 cpufreq_freq_transition_end(policy, &freqs, 0);
1898 }
1899 }
1900
Viresh Kumar8d657752014-05-21 14:29:29 +05301901 return retval;
1902}
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904int __cpufreq_driver_target(struct cpufreq_policy *policy,
1905 unsigned int target_freq,
1906 unsigned int relation)
1907{
Viresh Kumar72499242012-10-31 01:28:21 +01001908 unsigned int old_target_freq = target_freq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301909 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001910
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001911 if (cpufreq_disabled())
1912 return -ENODEV;
1913
Viresh Kumar72499242012-10-31 01:28:21 +01001914 /* Make sure that target_freq is within supported range */
1915 if (target_freq > policy->max)
1916 target_freq = policy->max;
1917 if (target_freq < policy->min)
1918 target_freq = policy->min;
1919
1920 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001921 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001922
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301923 /*
1924 * This might look like a redundant call as we are checking it again
1925 * after finding index. But it is left intentionally for cases where
1926 * exactly same freq is called again and so we can save on few function
1927 * calls.
1928 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001929 if (target_freq == policy->cur)
1930 return 0;
1931
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301932 /* Save last value to restore later on errors */
1933 policy->restore_freq = policy->cur;
1934
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001935 if (cpufreq_driver->target)
1936 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301937 else if (cpufreq_driver->target_index) {
1938 struct cpufreq_frequency_table *freq_table;
1939 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001940
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301941 freq_table = cpufreq_frequency_get_table(policy->cpu);
1942 if (unlikely(!freq_table)) {
1943 pr_err("%s: Unable to find freq_table\n", __func__);
1944 goto out;
1945 }
1946
1947 retval = cpufreq_frequency_table_target(policy, freq_table,
1948 target_freq, relation, &index);
1949 if (unlikely(retval)) {
1950 pr_err("%s: Unable to find matching freq\n", __func__);
1951 goto out;
1952 }
1953
Viresh Kumard4019f02013-08-14 19:38:24 +05301954 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301955 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301956 goto out;
1957 }
1958
Viresh Kumar8d657752014-05-21 14:29:29 +05301959 retval = __target_index(policy, freq_table, index);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301960 }
1961
1962out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 return retval;
1964}
1965EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967int cpufreq_driver_target(struct cpufreq_policy *policy,
1968 unsigned int target_freq,
1969 unsigned int relation)
1970{
Julia Lawallf1829e42008-07-25 22:44:53 +02001971 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
viresh kumarad7722d2013-10-18 19:10:15 +05301973 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 ret = __cpufreq_driver_target(policy, target_freq, relation);
1976
viresh kumarad7722d2013-10-18 19:10:15 +05301977 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return ret;
1980}
1981EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1982
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301983static int __cpufreq_governor(struct cpufreq_policy *policy,
1984 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985{
Dave Jonescc993ca2005-07-28 09:43:56 -07001986 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001987
1988 /* Only must be defined when default governor is known to have latency
1989 restrictions, like e.g. conservative or ondemand.
1990 That this is the case is already ensured in Kconfig
1991 */
1992#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1993 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1994#else
1995 struct cpufreq_governor *gov = NULL;
1996#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001997
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001998 /* Don't start any governor operations if we are entering suspend */
1999 if (cpufreq_suspended)
2000 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09002001 /*
2002 * Governor might not be initiated here if ACPI _PPC changed
2003 * notification happened, so check it.
2004 */
2005 if (!policy->governor)
2006 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002007
Thomas Renninger1c256242007-10-02 13:28:12 -07002008 if (policy->governor->max_transition_latency &&
2009 policy->cpuinfo.transition_latency >
2010 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07002011 if (!gov)
2012 return -EINVAL;
2013 else {
Joe Perchese837f9b2014-03-11 10:03:00 -07002014 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2015 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002016 policy->governor = gov;
2017 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Viresh Kumarfe492f32013-08-06 22:53:10 +05302020 if (event == CPUFREQ_GOV_POLICY_INIT)
2021 if (!try_module_get(policy->governor->owner))
2022 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002024 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002025 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002026
2027 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05302028 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05302029 || (!policy->governor_enabled
2030 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002031 mutex_unlock(&cpufreq_governor_lock);
2032 return -EBUSY;
2033 }
2034
2035 if (event == CPUFREQ_GOV_STOP)
2036 policy->governor_enabled = false;
2037 else if (event == CPUFREQ_GOV_START)
2038 policy->governor_enabled = true;
2039
2040 mutex_unlock(&cpufreq_governor_lock);
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 ret = policy->governor->governor(policy, event);
2043
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002044 if (!ret) {
2045 if (event == CPUFREQ_GOV_POLICY_INIT)
2046 policy->governor->initialized++;
2047 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2048 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002049 } else {
2050 /* Restore original values */
2051 mutex_lock(&cpufreq_governor_lock);
2052 if (event == CPUFREQ_GOV_STOP)
2053 policy->governor_enabled = true;
2054 else if (event == CPUFREQ_GOV_START)
2055 policy->governor_enabled = false;
2056 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002057 }
Viresh Kumarb3940582013-02-01 05:42:58 +00002058
Viresh Kumarfe492f32013-08-06 22:53:10 +05302059 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2060 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 module_put(policy->governor->owner);
2062
2063 return ret;
2064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066int cpufreq_register_governor(struct cpufreq_governor *governor)
2067{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002068 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 if (!governor)
2071 return -EINVAL;
2072
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002073 if (cpufreq_disabled())
2074 return -ENODEV;
2075
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002076 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002077
Viresh Kumarb3940582013-02-01 05:42:58 +00002078 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002079 err = -EBUSY;
Viresh Kumar42f91fa2015-01-02 12:34:26 +05302080 if (!find_governor(governor->name)) {
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002081 err = 0;
2082 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Dave Jones32ee8c32006-02-28 00:43:23 -05002085 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002086 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2091{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002092 int cpu;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (!governor)
2095 return;
2096
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002097 if (cpufreq_disabled())
2098 return;
2099
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002100 for_each_present_cpu(cpu) {
2101 if (cpu_online(cpu))
2102 continue;
2103 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
2104 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
2105 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002106
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002107 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002109 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return;
2111}
2112EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2113
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115/*********************************************************************
2116 * POLICY INTERFACE *
2117 *********************************************************************/
2118
2119/**
2120 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002121 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2122 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 *
2124 * Reads the current cpufreq policy.
2125 */
2126int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2127{
2128 struct cpufreq_policy *cpu_policy;
2129 if (!policy)
2130 return -EINVAL;
2131
2132 cpu_policy = cpufreq_cpu_get(cpu);
2133 if (!cpu_policy)
2134 return -EINVAL;
2135
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302136 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140}
2141EXPORT_SYMBOL(cpufreq_get_policy);
2142
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002143/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302144 * policy : current policy.
2145 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002146 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302147static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302148 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002150 struct cpufreq_governor *old_gov;
2151 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Joe Perchese837f9b2014-03-11 10:03:00 -07002153 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2154 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302156 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002158 if (new_policy->min > policy->max || new_policy->max < policy->min)
2159 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302162 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002167 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302168 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002171 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302172 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Viresh Kumarbb176f72013-06-19 14:19:33 +05302174 /*
2175 * verify the cpu speed can be set within this limit, which might be
2176 * different to the first one
2177 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302178 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002179 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002180 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002183 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302184 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302186 policy->min = new_policy->min;
2187 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002189 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002190 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002192 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302193 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002194 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002195 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002198 if (new_policy->governor == policy->governor)
2199 goto out;
2200
2201 pr_debug("governor switch\n");
2202
2203 /* save old, working values */
2204 old_gov = policy->governor;
2205 /* end old governor */
2206 if (old_gov) {
2207 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2208 up_write(&policy->rwsem);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02002209 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002210 down_write(&policy->rwsem);
2211 }
2212
2213 /* start new governor */
2214 policy->governor = new_policy->governor;
2215 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2216 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2217 goto out;
2218
2219 up_write(&policy->rwsem);
2220 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2221 down_write(&policy->rwsem);
2222 }
2223
2224 /* new governor failed, so re-start old one */
2225 pr_debug("starting governor %s failed\n", policy->governor->name);
2226 if (old_gov) {
2227 policy->governor = old_gov;
2228 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2229 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2230 }
2231
2232 return -EINVAL;
2233
2234 out:
2235 pr_debug("governor: change or update limits\n");
2236 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2241 * @cpu: CPU which shall be re-evaluated
2242 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002243 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 * at different times.
2245 */
2246int cpufreq_update_policy(unsigned int cpu)
2247{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302248 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2249 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002250 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002252 if (!policy)
2253 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
viresh kumarad7722d2013-10-18 19:10:15 +05302255 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002257 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302258 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302259 new_policy.min = policy->user_policy.min;
2260 new_policy.max = policy->user_policy.max;
2261 new_policy.policy = policy->user_policy.policy;
2262 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Viresh Kumarbb176f72013-06-19 14:19:33 +05302264 /*
2265 * BIOS might change freq behind our back
2266 * -> ask driver for current freq and notify governors about a change
2267 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002268 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302269 new_policy.cur = cpufreq_driver->get(cpu);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302270 if (WARN_ON(!new_policy.cur)) {
2271 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002272 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302273 }
2274
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302275 if (!policy->cur) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002276 pr_debug("Driver did not initialize current freq\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302277 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002278 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302279 if (policy->cur != new_policy.cur && has_target())
Viresh Kumara1e1dc42015-01-02 12:34:28 +05302280 cpufreq_out_of_sync(policy, new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002281 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002282 }
2283
Viresh Kumar037ce832013-10-02 14:13:16 +05302284 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002286unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302287 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002288
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302289 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return ret;
2291}
2292EXPORT_SYMBOL(cpufreq_update_policy);
2293
Paul Gortmaker27609842013-06-19 13:54:04 -04002294static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002295 unsigned long action, void *hcpu)
2296{
2297 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002298 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002299
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002300 dev = get_cpu_device(cpu);
2301 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302302 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002303 case CPU_ONLINE:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302304 __cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002305 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302306
Ashok Rajc32b6b82005-10-30 14:59:54 -08002307 case CPU_DOWN_PREPARE:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302308 __cpufreq_remove_dev_prepare(dev, NULL);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302309 break;
2310
2311 case CPU_POST_DEAD:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302312 __cpufreq_remove_dev_finish(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002313 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302314
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002315 case CPU_DOWN_FAILED:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302316 __cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002317 break;
2318 }
2319 }
2320 return NOTIFY_OK;
2321}
2322
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002323static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302324 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002325};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002328 * BOOST *
2329 *********************************************************************/
2330static int cpufreq_boost_set_sw(int state)
2331{
2332 struct cpufreq_frequency_table *freq_table;
2333 struct cpufreq_policy *policy;
2334 int ret = -EINVAL;
2335
2336 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2337 freq_table = cpufreq_frequency_get_table(policy->cpu);
2338 if (freq_table) {
2339 ret = cpufreq_frequency_table_cpuinfo(policy,
2340 freq_table);
2341 if (ret) {
2342 pr_err("%s: Policy frequency update failed\n",
2343 __func__);
2344 break;
2345 }
2346 policy->user_policy.max = policy->max;
2347 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2348 }
2349 }
2350
2351 return ret;
2352}
2353
2354int cpufreq_boost_trigger_state(int state)
2355{
2356 unsigned long flags;
2357 int ret = 0;
2358
2359 if (cpufreq_driver->boost_enabled == state)
2360 return 0;
2361
2362 write_lock_irqsave(&cpufreq_driver_lock, flags);
2363 cpufreq_driver->boost_enabled = state;
2364 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2365
2366 ret = cpufreq_driver->set_boost(state);
2367 if (ret) {
2368 write_lock_irqsave(&cpufreq_driver_lock, flags);
2369 cpufreq_driver->boost_enabled = !state;
2370 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2371
Joe Perchese837f9b2014-03-11 10:03:00 -07002372 pr_err("%s: Cannot %s BOOST\n",
2373 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002374 }
2375
2376 return ret;
2377}
2378
2379int cpufreq_boost_supported(void)
2380{
2381 if (likely(cpufreq_driver))
2382 return cpufreq_driver->boost_supported;
2383
2384 return 0;
2385}
2386EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2387
2388int cpufreq_boost_enabled(void)
2389{
2390 return cpufreq_driver->boost_enabled;
2391}
2392EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2393
2394/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2396 *********************************************************************/
2397
2398/**
2399 * cpufreq_register_driver - register a CPU Frequency driver
2400 * @driver_data: A struct cpufreq_driver containing the values#
2401 * submitted by the CPU Frequency driver.
2402 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302403 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002405 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 *
2407 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002408int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
2410 unsigned long flags;
2411 int ret;
2412
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002413 if (cpufreq_disabled())
2414 return -ENODEV;
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302417 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002418 driver_data->target) ||
2419 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302420 driver_data->target)) ||
2421 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return -EINVAL;
2423
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002424 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002426 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002427 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002428 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002429 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002431 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002432 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Viresh Kumarbc68b7d2015-01-02 12:34:30 +05302434 if (driver_data->setpolicy)
2435 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2436
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002437 if (cpufreq_boost_supported()) {
2438 /*
2439 * Check if driver provides function to enable boost -
2440 * if not, use cpufreq_boost_set_sw as default
2441 */
2442 if (!cpufreq_driver->set_boost)
2443 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2444
2445 ret = cpufreq_sysfs_create_file(&boost.attr);
2446 if (ret) {
2447 pr_err("%s: cannot register global BOOST sysfs file\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002448 __func__);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002449 goto err_null_driver;
2450 }
2451 }
2452
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002453 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002454 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002455 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302457 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2458 list_empty(&cpufreq_policy_list)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* if all ->init() calls failed, unregister */
Viresh Kumarce1bcfe2015-01-02 12:34:35 +05302460 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2461 driver_data->name);
2462 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002465 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002466 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002468 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002469err_if_unreg:
2470 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002471err_boost_unreg:
2472 if (cpufreq_boost_supported())
2473 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002474err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002475 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002476 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002477 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482/**
2483 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2484 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302485 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * the right to do so, i.e. if you have succeeded in initialising before!
2487 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2488 * currently not initialised.
2489 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002490int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
2492 unsigned long flags;
2493
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002494 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002497 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002499 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002500 if (cpufreq_boost_supported())
2501 cpufreq_sysfs_remove_file(&boost.attr);
2502
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002503 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Viresh Kumar6eed9402013-08-06 22:53:11 +05302505 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002506 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302507
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002508 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302509
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002510 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302511 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512
2513 return 0;
2514}
2515EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002516
Doug Anderson90de2a42014-12-23 22:09:48 -08002517/*
2518 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2519 * or mutexes when secondary CPUs are halted.
2520 */
2521static struct syscore_ops cpufreq_syscore_ops = {
2522 .shutdown = cpufreq_suspend,
2523};
2524
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002525static int __init cpufreq_core_init(void)
2526{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002527 if (cpufreq_disabled())
2528 return -ENODEV;
2529
Viresh Kumar2361be22013-05-17 16:09:09 +05302530 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002531 BUG_ON(!cpufreq_global_kobject);
2532
Doug Anderson90de2a42014-12-23 22:09:48 -08002533 register_syscore_ops(&cpufreq_syscore_ops);
2534
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002535 return 0;
2536}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002537core_initcall(cpufreq_core_init);