blob: dad7235971bacff766a17b68c04ae31dd0b93d54 [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);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066static unsigned int __cpufreq_get(unsigned int cpu);
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 */
253#ifndef CONFIG_SMP
254static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530255static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Arjan van de Ven858119e2006-01-14 13:20:43 -0800257static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 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 }
274}
275#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530276static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
277{
278 return;
279}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#endif
281
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530282static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530283 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 BUG_ON(irqs_disabled());
286
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000287 if (cpufreq_disabled())
288 return;
289
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200290 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200291 pr_debug("notification %u of frequency transition to %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -0700292 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500297 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800298 * which is not equal to what the cpufreq core thinks is
299 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200301 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800302 if ((policy) && (policy->cpu == freqs->cpu) &&
303 (policy->cur) && (policy->cur != freqs->old)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700304 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
305 freqs->old, policy->cur);
Dave Jonese4472cb2006-01-31 15:53:55 -0800306 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700309 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800310 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
312 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 case CPUFREQ_POSTCHANGE:
315 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Joe Perchese837f9b2014-03-11 10:03:00 -0700316 pr_debug("FREQ: %lu - CPU: %lu\n",
317 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100318 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700319 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800320 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800321 if (likely(policy) && likely(policy->cpu == freqs->cpu))
322 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530326
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530327/**
328 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
329 * on frequency transition.
330 *
331 * This function calls the transition notifiers and the "adjust_jiffies"
332 * function. It is called twice on all CPU frequency changes that have
333 * external effects.
334 */
Viresh Kumar236a9802014-03-24 13:35:46 +0530335static void cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530336 struct cpufreq_freqs *freqs, unsigned int state)
337{
338 for_each_cpu(freqs->cpu, policy->cpus)
339 __cpufreq_notify_transition(policy, freqs, state);
340}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530342/* Do post notifications when there are chances that transition has failed */
Viresh Kumar236a9802014-03-24 13:35:46 +0530343static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530344 struct cpufreq_freqs *freqs, int transition_failed)
345{
346 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
347 if (!transition_failed)
348 return;
349
350 swap(freqs->old, freqs->new);
351 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
352 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
353}
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530354
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530355void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
356 struct cpufreq_freqs *freqs)
357{
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530358
359 /*
360 * Catch double invocations of _begin() which lead to self-deadlock.
361 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
362 * doesn't invoke _begin() on their behalf, and hence the chances of
363 * double invocations are very low. Moreover, there are scenarios
364 * where these checks can emit false-positive warnings in these
365 * drivers; so we avoid that by skipping them altogether.
366 */
367 WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
368 && current == policy->transition_task);
369
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530370wait:
371 wait_event(policy->transition_wait, !policy->transition_ongoing);
372
373 spin_lock(&policy->transition_lock);
374
375 if (unlikely(policy->transition_ongoing)) {
376 spin_unlock(&policy->transition_lock);
377 goto wait;
378 }
379
380 policy->transition_ongoing = true;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530381 policy->transition_task = current;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530382
383 spin_unlock(&policy->transition_lock);
384
385 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
386}
387EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
388
389void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
390 struct cpufreq_freqs *freqs, int transition_failed)
391{
392 if (unlikely(WARN_ON(!policy->transition_ongoing)))
393 return;
394
395 cpufreq_notify_post_transition(policy, freqs, transition_failed);
396
397 policy->transition_ongoing = false;
Srivatsa S. Bhatca654dc2014-05-05 12:52:39 +0530398 policy->transition_task = NULL;
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +0530399
400 wake_up(&policy->transition_wait);
401}
402EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/*********************************************************************
406 * SYSFS INTERFACE *
407 *********************************************************************/
Rashika Kheria8a5c74a2014-02-26 22:12:42 +0530408static ssize_t show_boost(struct kobject *kobj,
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100409 struct attribute *attr, char *buf)
410{
411 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
412}
413
414static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
415 const char *buf, size_t count)
416{
417 int ret, enable;
418
419 ret = sscanf(buf, "%d", &enable);
420 if (ret != 1 || enable < 0 || enable > 1)
421 return -EINVAL;
422
423 if (cpufreq_boost_trigger_state(enable)) {
Joe Perchese837f9b2014-03-11 10:03:00 -0700424 pr_err("%s: Cannot %s BOOST!\n",
425 __func__, enable ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100426 return -EINVAL;
427 }
428
Joe Perchese837f9b2014-03-11 10:03:00 -0700429 pr_debug("%s: cpufreq BOOST %s\n",
430 __func__, enable ? "enabled" : "disabled");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +0100431
432 return count;
433}
434define_one_global_rw(boost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700436static struct cpufreq_governor *__find_governor(const char *str_governor)
437{
438 struct cpufreq_governor *t;
439
440 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200441 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700442 return t;
443
444 return NULL;
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/**
448 * cpufreq_parse_governor - parse a governor string
449 */
Dave Jones905d77c2008-03-05 14:28:32 -0500450static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 struct cpufreq_governor **governor)
452{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700453 int err = -EINVAL;
454
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200455 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700456 goto out;
457
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200458 if (cpufreq_driver->setpolicy) {
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200459 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700461 err = 0;
Rasmus Villemoes7c4f4532014-09-29 15:50:11 +0200462 } else if (!strncasecmp(str_governor, "powersave",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530463 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700465 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530467 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700469
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800470 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700471
472 t = __find_governor(str_governor);
473
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700474 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700475 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700476
Kees Cook1a8e1462011-05-04 08:38:56 -0700477 mutex_unlock(&cpufreq_governor_mutex);
478 ret = request_module("cpufreq_%s", str_governor);
479 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700480
Kees Cook1a8e1462011-05-04 08:38:56 -0700481 if (ret == 0)
482 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700483 }
484
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700485 if (t != NULL) {
486 *governor = t;
487 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700489
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800490 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Dave Jones29464f22009-01-18 01:37:11 -0500492out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530497 * cpufreq_per_cpu_attr_read() / show_##file_name() -
498 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 *
500 * Write out information from cpufreq_driver->policy[cpu]; object must be
501 * "unsigned int".
502 */
503
Dave Jones32ee8c32006-02-28 00:43:23 -0500504#define show_one(file_name, object) \
505static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500506(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500507{ \
Dave Jones29464f22009-01-18 01:37:11 -0500508 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511show_one(cpuinfo_min_freq, cpuinfo.min_freq);
512show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100513show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514show_one(scaling_min_freq, min);
515show_one(scaling_max_freq, max);
Dirk Brandewiec034b022014-10-13 08:37:40 -0700516
517static ssize_t show_scaling_cur_freq(
518 struct cpufreq_policy *policy, char *buf)
519{
520 ssize_t ret;
521
522 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
523 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
524 else
525 ret = sprintf(buf, "%u\n", policy->cur);
526 return ret;
527}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Viresh Kumar037ce832013-10-02 14:13:16 +0530529static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530530 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/**
533 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
534 */
535#define store_one(file_name, object) \
536static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500537(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{ \
Vince Hsu619c144c2014-11-10 14:14:50 +0800539 int ret, temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct cpufreq_policy new_policy; \
541 \
542 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
543 if (ret) \
544 return -EINVAL; \
545 \
Dave Jones29464f22009-01-18 01:37:11 -0500546 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (ret != 1) \
548 return -EINVAL; \
549 \
Vince Hsu619c144c2014-11-10 14:14:50 +0800550 temp = new_policy.object; \
Viresh Kumar037ce832013-10-02 14:13:16 +0530551 ret = cpufreq_set_policy(policy, &new_policy); \
Vince Hsu619c144c2014-11-10 14:14:50 +0800552 if (!ret) \
553 policy->user_policy.object = temp; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 \
555 return ret ? ret : count; \
556}
557
Dave Jones29464f22009-01-18 01:37:11 -0500558store_one(scaling_min_freq, min);
559store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561/**
562 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
563 */
Dave Jones905d77c2008-03-05 14:28:32 -0500564static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
565 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800567 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (!cur_freq)
569 return sprintf(buf, "<unknown>");
570 return sprintf(buf, "%u\n", cur_freq);
571}
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/**
574 * show_scaling_governor - show the current policy for the specified CPU
575 */
Dave Jones905d77c2008-03-05 14:28:32 -0500576static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Dave Jones29464f22009-01-18 01:37:11 -0500578 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return sprintf(buf, "powersave\n");
580 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
581 return sprintf(buf, "performance\n");
582 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200583 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500584 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 return -EINVAL;
586}
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/**
589 * store_scaling_governor - store policy for the specified CPU
590 */
Dave Jones905d77c2008-03-05 14:28:32 -0500591static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
592 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530594 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 char str_governor[16];
596 struct cpufreq_policy new_policy;
597
598 ret = cpufreq_get_policy(&new_policy, policy->cpu);
599 if (ret)
600 return ret;
601
Dave Jones29464f22009-01-18 01:37:11 -0500602 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (ret != 1)
604 return -EINVAL;
605
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530606 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
607 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return -EINVAL;
609
Viresh Kumar037ce832013-10-02 14:13:16 +0530610 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200611
612 policy->user_policy.policy = policy->policy;
613 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200614
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530615 if (ret)
616 return ret;
617 else
618 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621/**
622 * show_scaling_driver - show the cpufreq driver currently loaded
623 */
Dave Jones905d77c2008-03-05 14:28:32 -0500624static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200626 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/**
630 * show_scaling_available_governors - show the available CPUfreq governors
631 */
Dave Jones905d77c2008-03-05 14:28:32 -0500632static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
633 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 ssize_t i = 0;
636 struct cpufreq_governor *t;
637
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530638 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 i += sprintf(buf, "performance powersave");
640 goto out;
641 }
642
643 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500644 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
645 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200647 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500649out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 i += sprintf(&buf[i], "\n");
651 return i;
652}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700653
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800654ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
656 ssize_t i = 0;
657 unsigned int cpu;
658
Rusty Russell835481d2009-01-04 05:18:06 -0800659 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (i)
661 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
662 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
663 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500664 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 i += sprintf(&buf[i], "\n");
667 return i;
668}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800669EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700671/**
672 * show_related_cpus - show the CPUs affected by each transition even if
673 * hw coordination is in use
674 */
675static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
676{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800677 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700678}
679
680/**
681 * show_affected_cpus - show the CPUs affected by each transition
682 */
683static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
684{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800685 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700686}
687
Venki Pallipadi9e769882007-10-26 10:18:21 -0700688static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500689 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700690{
691 unsigned int freq = 0;
692 unsigned int ret;
693
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700694 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700695 return -EINVAL;
696
697 ret = sscanf(buf, "%u", &freq);
698 if (ret != 1)
699 return -EINVAL;
700
701 policy->governor->store_setspeed(policy, freq);
702
703 return count;
704}
705
706static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
707{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700708 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700709 return sprintf(buf, "<unsupported>\n");
710
711 return policy->governor->show_setspeed(policy, buf);
712}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Thomas Renningere2f74f32009-11-19 12:31:01 +0100714/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200715 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100716 */
717static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
718{
719 unsigned int limit;
720 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200721 if (cpufreq_driver->bios_limit) {
722 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100723 if (!ret)
724 return sprintf(buf, "%u\n", limit);
725 }
726 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
727}
728
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200729cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
730cpufreq_freq_attr_ro(cpuinfo_min_freq);
731cpufreq_freq_attr_ro(cpuinfo_max_freq);
732cpufreq_freq_attr_ro(cpuinfo_transition_latency);
733cpufreq_freq_attr_ro(scaling_available_governors);
734cpufreq_freq_attr_ro(scaling_driver);
735cpufreq_freq_attr_ro(scaling_cur_freq);
736cpufreq_freq_attr_ro(bios_limit);
737cpufreq_freq_attr_ro(related_cpus);
738cpufreq_freq_attr_ro(affected_cpus);
739cpufreq_freq_attr_rw(scaling_min_freq);
740cpufreq_freq_attr_rw(scaling_max_freq);
741cpufreq_freq_attr_rw(scaling_governor);
742cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
Dave Jones905d77c2008-03-05 14:28:32 -0500744static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 &cpuinfo_min_freq.attr,
746 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100747 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 &scaling_min_freq.attr,
749 &scaling_max_freq.attr,
750 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700751 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 &scaling_governor.attr,
753 &scaling_driver.attr,
754 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700755 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 NULL
757};
758
Dave Jones29464f22009-01-18 01:37:11 -0500759#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
760#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Dave Jones29464f22009-01-18 01:37:11 -0500762static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Dave Jones905d77c2008-03-05 14:28:32 -0500764 struct cpufreq_policy *policy = to_policy(kobj);
765 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530766 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530767
768 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530769 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800770
viresh kumarad7722d2013-10-18 19:10:15 +0530771 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800772
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530773 if (fattr->show)
774 ret = fattr->show(policy, buf);
775 else
776 ret = -EIO;
777
viresh kumarad7722d2013-10-18 19:10:15 +0530778 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530779 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return ret;
782}
783
Dave Jones905d77c2008-03-05 14:28:32 -0500784static ssize_t store(struct kobject *kobj, struct attribute *attr,
785 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Dave Jones905d77c2008-03-05 14:28:32 -0500787 struct cpufreq_policy *policy = to_policy(kobj);
788 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500789 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530790
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530791 get_online_cpus();
792
793 if (!cpu_online(policy->cpu))
794 goto unlock;
795
Viresh Kumar6eed9402013-08-06 22:53:11 +0530796 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530797 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800798
viresh kumarad7722d2013-10-18 19:10:15 +0530799 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800800
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530801 if (fattr->store)
802 ret = fattr->store(policy, buf, count);
803 else
804 ret = -EIO;
805
viresh kumarad7722d2013-10-18 19:10:15 +0530806 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530807
Viresh Kumar6eed9402013-08-06 22:53:11 +0530808 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530809unlock:
810 put_online_cpus();
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 return ret;
813}
814
Dave Jones905d77c2008-03-05 14:28:32 -0500815static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Dave Jones905d77c2008-03-05 14:28:32 -0500817 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200818 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 complete(&policy->kobj_unregister);
820}
821
Emese Revfy52cf25d2010-01-19 02:58:23 +0100822static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 .show = show,
824 .store = store,
825};
826
827static struct kobj_type ktype_cpufreq = {
828 .sysfs_ops = &sysfs_ops,
829 .default_attrs = default_attrs,
830 .release = cpufreq_sysfs_release,
831};
832
Viresh Kumar2361be22013-05-17 16:09:09 +0530833struct kobject *cpufreq_global_kobject;
834EXPORT_SYMBOL(cpufreq_global_kobject);
835
836static int cpufreq_global_kobject_usage;
837
838int cpufreq_get_global_kobject(void)
839{
840 if (!cpufreq_global_kobject_usage++)
841 return kobject_add(cpufreq_global_kobject,
842 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
843
844 return 0;
845}
846EXPORT_SYMBOL(cpufreq_get_global_kobject);
847
848void cpufreq_put_global_kobject(void)
849{
850 if (!--cpufreq_global_kobject_usage)
851 kobject_del(cpufreq_global_kobject);
852}
853EXPORT_SYMBOL(cpufreq_put_global_kobject);
854
855int cpufreq_sysfs_create_file(const struct attribute *attr)
856{
857 int ret = cpufreq_get_global_kobject();
858
859 if (!ret) {
860 ret = sysfs_create_file(cpufreq_global_kobject, attr);
861 if (ret)
862 cpufreq_put_global_kobject();
863 }
864
865 return ret;
866}
867EXPORT_SYMBOL(cpufreq_sysfs_create_file);
868
869void cpufreq_sysfs_remove_file(const struct attribute *attr)
870{
871 sysfs_remove_file(cpufreq_global_kobject, attr);
872 cpufreq_put_global_kobject();
873}
874EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
875
Dave Jones19d6f7e2009-07-08 17:35:39 -0400876/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200877static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400878{
879 unsigned int j;
880 int ret = 0;
881
882 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800883 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400884
Viresh Kumar308b60e2013-07-31 14:35:14 +0200885 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400886 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400887
Viresh Kumare8fdde12013-07-31 14:31:33 +0200888 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800889 cpu_dev = get_cpu_device(j);
890 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400891 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200892 if (ret)
893 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400894 }
895 return ret;
896}
897
Viresh Kumar308b60e2013-07-31 14:35:14 +0200898static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800899 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400900{
901 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400902 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400903
Dave Jones909a6942009-07-08 18:05:42 -0400904 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200905 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400906 while ((drv_attr) && (*drv_attr)) {
907 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
908 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100909 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400910 drv_attr++;
911 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200912 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400913 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
914 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100915 return ret;
Dave Jones909a6942009-07-08 18:05:42 -0400916 }
Dirk Brandewiec034b022014-10-13 08:37:40 -0700917
918 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
919 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100920 return ret;
Dirk Brandewiec034b022014-10-13 08:37:40 -0700921
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200922 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100923 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
924 if (ret)
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100925 return ret;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100926 }
Dave Jones909a6942009-07-08 18:05:42 -0400927
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +0100928 return cpufreq_add_dev_symlink(policy);
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530929}
930
931static void cpufreq_init_policy(struct cpufreq_policy *policy)
932{
viresh kumar6e2c89d2014-03-04 11:43:59 +0800933 struct cpufreq_governor *gov = NULL;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530934 struct cpufreq_policy new_policy;
935 int ret = 0;
936
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530937 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000938
viresh kumar6e2c89d2014-03-04 11:43:59 +0800939 /* Update governor of new_policy to the governor used before hotplug */
940 gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
941 if (gov)
942 pr_debug("Restoring governor %s for cpu %d\n",
943 policy->governor->name, policy->cpu);
944 else
945 gov = CPUFREQ_DEFAULT_GOVERNOR;
946
947 new_policy.governor = gov;
948
Jason Barona27a9ab2013-12-19 22:50:50 +0000949 /* Use the default policy if its valid. */
950 if (cpufreq_driver->setpolicy)
viresh kumar6e2c89d2014-03-04 11:43:59 +0800951 cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
Dave Jonesecf7e462009-07-08 18:48:47 -0400952
953 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530954 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400955 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200956 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200957 if (cpufreq_driver->exit)
958 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400959 }
Dave Jones909a6942009-07-08 18:05:42 -0400960}
961
Viresh Kumarfcf80582013-01-29 14:39:08 +0000962#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200963static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530964 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000965{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530966 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000967 unsigned long flags;
968
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530969 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530970 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
971 if (ret) {
972 pr_err("%s: Failed to stop governor\n", __func__);
973 return ret;
974 }
975 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000976
viresh kumarad7722d2013-10-18 19:10:15 +0530977 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530978
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000979 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530980
Viresh Kumarfcf80582013-01-29 14:39:08 +0000981 cpumask_set_cpu(cpu, policy->cpus);
982 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000983 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000984
viresh kumarad7722d2013-10-18 19:10:15 +0530985 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530986
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530987 if (has_target()) {
Stratos Karafotise5c87b72014-03-19 23:29:17 +0200988 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
989 if (!ret)
990 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
991
992 if (ret) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530993 pr_err("%s: Failed to start governor\n", __func__);
994 return ret;
995 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200996 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000997
Viresh Kumar42f921a2013-12-20 21:26:02 +0530998 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000999}
1000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301002static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
1003{
1004 struct cpufreq_policy *policy;
1005 unsigned long flags;
1006
Lan Tianyu44871c92013-09-11 15:05:05 +08001007 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301008
1009 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
1010
Lan Tianyu44871c92013-09-11 15:05:05 +08001011 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301012
Geert Uytterhoeven09712f52014-11-04 17:05:25 +01001013 if (policy)
1014 policy->governor = NULL;
viresh kumar6e2c89d2014-03-04 11:43:59 +08001015
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301016 return policy;
1017}
1018
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301019static struct cpufreq_policy *cpufreq_policy_alloc(void)
1020{
1021 struct cpufreq_policy *policy;
1022
1023 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1024 if (!policy)
1025 return NULL;
1026
1027 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1028 goto err_free_policy;
1029
1030 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1031 goto err_free_cpumask;
1032
Lukasz Majewskic88a1f82013-08-06 22:53:08 +05301033 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +05301034 init_rwsem(&policy->rwsem);
Srivatsa S. Bhat12478cf2014-03-24 13:35:44 +05301035 spin_lock_init(&policy->transition_lock);
1036 init_waitqueue_head(&policy->transition_wait);
viresh kumarad7722d2013-10-18 19:10:15 +05301037
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301038 return policy;
1039
1040err_free_cpumask:
1041 free_cpumask_var(policy->cpus);
1042err_free_policy:
1043 kfree(policy);
1044
1045 return NULL;
1046}
1047
Viresh Kumar42f921a2013-12-20 21:26:02 +05301048static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1049{
1050 struct kobject *kobj;
1051 struct completion *cmp;
1052
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301053 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1054 CPUFREQ_REMOVE_POLICY, policy);
1055
Viresh Kumar42f921a2013-12-20 21:26:02 +05301056 down_read(&policy->rwsem);
1057 kobj = &policy->kobj;
1058 cmp = &policy->kobj_unregister;
1059 up_read(&policy->rwsem);
1060 kobject_put(kobj);
1061
1062 /*
1063 * We need to make sure that the underlying kobj is
1064 * actually not referenced anymore by anybody before we
1065 * proceed with unloading.
1066 */
1067 pr_debug("waiting for dropping of refcount\n");
1068 wait_for_completion(cmp);
1069 pr_debug("wait complete\n");
1070}
1071
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301072static void cpufreq_policy_free(struct cpufreq_policy *policy)
1073{
1074 free_cpumask_var(policy->related_cpus);
1075 free_cpumask_var(policy->cpus);
1076 kfree(policy);
1077}
1078
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301079static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu,
1080 struct device *cpu_dev)
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301081{
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301082 int ret;
1083
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +05301084 if (WARN_ON(cpu == policy->cpu))
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301085 return 0;
1086
1087 /* Move kobject to the new policy->cpu */
1088 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1089 if (ret) {
1090 pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
1091 return ret;
1092 }
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +05301093
viresh kumarad7722d2013-10-18 19:10:15 +05301094 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301095
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301096 policy->last_cpu = policy->cpu;
1097 policy->cpu = cpu;
1098
viresh kumarad7722d2013-10-18 19:10:15 +05301099 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +05301100
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301101 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1102 CPUFREQ_UPDATE_POLICY_CPU, policy);
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301103
1104 return 0;
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301105}
1106
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301107static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001109 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301110 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 unsigned long flags;
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301113 bool recover_policy = cpufreq_suspended;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001114#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301115 struct cpufreq_policy *tpolicy;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Ashok Rajc32b6b82005-10-30 14:59:54 -08001118 if (cpu_is_offline(cpu))
1119 return 0;
1120
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001121 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123#ifdef CONFIG_SMP
1124 /* check whether a different CPU already registered this
1125 * CPU because it is in the same boat. */
1126 policy = cpufreq_cpu_get(cpu);
1127 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001128 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return 0;
1130 }
Li Zhong5025d622013-08-21 01:31:08 +02001131#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001132
Viresh Kumar6eed9402013-08-06 22:53:11 +05301133 if (!down_read_trylock(&cpufreq_rwsem))
1134 return 0;
1135
Viresh Kumarfcf80582013-01-29 14:39:08 +00001136#ifdef CONFIG_HOTPLUG_CPU
1137 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001138 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301139 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1140 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001141 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301142 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301143 up_read(&cpufreq_rwsem);
1144 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301145 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001146 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001147 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001148#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001150 /*
1151 * Restore the saved policy when doing light-weight init and fall back
1152 * to the full init if that fails.
1153 */
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301154 policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001155 if (!policy) {
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301156 recover_policy = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301157 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001158 if (!policy)
1159 goto nomem_out;
1160 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301161
1162 /*
1163 * In the resume path, since we restore a saved policy, the assignment
1164 * to policy->cpu is like an update of the existing policy, rather than
1165 * the creation of a brand new one. So we need to perform this update
1166 * by invoking update_policy_cpu().
1167 */
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301168 if (recover_policy && cpu != policy->cpu)
1169 WARN_ON(update_policy_cpu(policy, cpu, dev));
1170 else
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301171 policy->cpu = cpu;
1172
Rusty Russell835481d2009-01-04 05:18:06 -08001173 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001176 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* call driver. From then on the cpufreq must be able
1179 * to accept all calls to ->verify and ->setpolicy for this CPU
1180 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001181 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001183 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301184 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001186
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001187 down_write(&policy->rwsem);
1188
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001189 /* related cpus should atleast have policy->cpus */
1190 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1191
1192 /*
1193 * affected cpus must always be the one, which are online. We aren't
1194 * managing offline cpus here.
1195 */
1196 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1197
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301198 if (!recover_policy) {
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001199 policy->user_policy.min = policy->min;
1200 policy->user_policy.max = policy->max;
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001201
1202 /* prepare interface data */
1203 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1204 &dev->kobj, "cpufreq");
1205 if (ret) {
1206 pr_err("%s: failed to init policy->kobj: %d\n",
1207 __func__, ret);
1208 goto err_init_policy_kobj;
1209 }
Viresh Kumar5a7e56a2014-03-04 11:44:00 +08001210 }
1211
Viresh Kumar652ed952014-01-09 20:38:43 +05301212 write_lock_irqsave(&cpufreq_driver_lock, flags);
1213 for_each_cpu(j, policy->cpus)
1214 per_cpu(cpufreq_cpu_data, j) = policy;
1215 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1216
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01001217 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumarda60ce92013-10-03 20:28:30 +05301218 policy->cur = cpufreq_driver->get(policy->cpu);
1219 if (!policy->cur) {
1220 pr_err("%s: ->get() failed\n", __func__);
1221 goto err_get_freq;
1222 }
1223 }
1224
Viresh Kumard3916692013-12-03 11:20:46 +05301225 /*
1226 * Sometimes boot loaders set CPU frequency to a value outside of
1227 * frequency table present with cpufreq core. In such cases CPU might be
1228 * unstable if it has to run on that frequency for long duration of time
1229 * and so its better to set it to a frequency which is specified in
1230 * freq-table. This also makes cpufreq stats inconsistent as
1231 * cpufreq-stats would fail to register because current frequency of CPU
1232 * isn't found in freq-table.
1233 *
1234 * Because we don't want this change to effect boot process badly, we go
1235 * for the next freq which is >= policy->cur ('cur' must be set by now,
1236 * otherwise we will end up setting freq to lowest of the table as 'cur'
1237 * is initialized to zero).
1238 *
1239 * We are passing target-freq as "policy->cur - 1" otherwise
1240 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1241 * equal to target-freq.
1242 */
1243 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1244 && has_target()) {
1245 /* Are we running at unknown frequency ? */
1246 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1247 if (ret == -EINVAL) {
1248 /* Warn user and fix it */
1249 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1250 __func__, policy->cpu, policy->cur);
1251 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1252 CPUFREQ_RELATION_L);
1253
1254 /*
1255 * Reaching here after boot in a few seconds may not
1256 * mean that system will remain stable at "unknown"
1257 * frequency for longer duration. Hence, a BUG_ON().
1258 */
1259 BUG_ON(ret);
1260 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1261 __func__, policy->cpu, policy->cur);
1262 }
1263 }
1264
Thomas Renningera1531ac2008-07-29 22:32:58 -07001265 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1266 CPUFREQ_START, policy);
1267
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301268 if (!recover_policy) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001269 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301270 if (ret)
1271 goto err_out_unregister;
Viresh Kumarfcd7af92014-01-07 07:10:10 +05301272 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1273 CPUFREQ_CREATE_POLICY, policy);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301274 }
Dave Jones8ff69732006-03-05 03:37:23 -05001275
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301276 write_lock_irqsave(&cpufreq_driver_lock, flags);
1277 list_add(&policy->policy_list, &cpufreq_policy_list);
1278 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1279
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301280 cpufreq_init_policy(policy);
1281
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301282 if (!recover_policy) {
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301283 policy->user_policy.policy = policy->policy;
1284 policy->user_policy.governor = policy->governor;
1285 }
Viresh Kumar4e97b632014-03-04 11:44:01 +08001286 up_write(&policy->rwsem);
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301287
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001288 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301289
Viresh Kumar6eed9402013-08-06 22:53:11 +05301290 up_read(&cpufreq_rwsem);
1291
Viresh Kumar7c45cf32014-11-27 06:07:51 +05301292 /* Callback for handling stuff after policy is ready */
1293 if (cpufreq_driver->ready)
1294 cpufreq_driver->ready(policy);
1295
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001296 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001297
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return 0;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300err_out_unregister:
Viresh Kumar652ed952014-01-09 20:38:43 +05301301err_get_freq:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001302 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301303 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001304 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001305 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Tomeu Vizoso6d4e81e2014-11-24 10:08:03 +01001307 if (!recover_policy) {
1308 kobject_put(&policy->kobj);
1309 wait_for_completion(&policy->kobj_unregister);
1310 }
1311err_init_policy_kobj:
Prarit Bhargava7106e022014-09-10 10:12:08 -04001312 up_write(&policy->rwsem);
1313
Viresh Kumarda60ce92013-10-03 20:28:30 +05301314 if (cpufreq_driver->exit)
1315 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301316err_set_policy_cpu:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301317 if (recover_policy) {
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001318 /* Do not leave stale fallback data behind. */
1319 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301320 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001321 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301322 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301325 up_read(&cpufreq_rwsem);
1326
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return ret;
1328}
1329
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301330/**
1331 * cpufreq_add_dev - add a CPU device
1332 *
1333 * Adds the cpufreq interface for a CPU device.
1334 *
1335 * The Oracle says: try running cpufreq registration/unregistration concurrently
1336 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1337 * mess up, but more thorough testing is needed. - Mathieu
1338 */
1339static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1340{
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301341 return __cpufreq_add_dev(dev, sif);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301342}
1343
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301344static int __cpufreq_remove_dev_prepare(struct device *dev,
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301345 struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301347 unsigned int cpu = dev->id, cpus;
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301348 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301350 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001352 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001354 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301356 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301358 /* Save the policy somewhere when doing a light-weight tear-down */
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301359 if (cpufreq_suspended)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301360 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301361
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001362 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301364 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001365 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301369 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301370 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1371 if (ret) {
1372 pr_err("%s: Failed to stop governor\n", __func__);
1373 return ret;
1374 }
1375 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001376
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001377 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001378 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301379 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001380
viresh kumarad7722d2013-10-18 19:10:15 +05301381 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301382 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301383 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301385 if (cpu != policy->cpu) {
viresh kumar6964d912014-02-17 14:52:11 +05301386 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001387 } else if (cpus > 1) {
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301388 /* Nominate new CPU */
1389 int new_cpu = cpumask_any_but(policy->cpus, cpu);
1390 struct device *cpu_dev = get_cpu_device(new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301391
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301392 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1393 ret = update_policy_cpu(policy, new_cpu, cpu_dev);
1394 if (ret) {
1395 if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1396 "cpufreq"))
1397 pr_err("%s: Failed to restore kobj link to cpu:%d\n",
1398 __func__, cpu_dev->id);
1399 return ret;
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001400 }
Viresh Kumar1bfb4252014-07-17 10:48:28 +05301401
1402 if (!cpufreq_suspended)
1403 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1404 __func__, new_cpu, cpu);
Preeti U Murthy789ca242014-09-29 15:47:12 +02001405 } else if (cpufreq_driver->stop_cpu) {
Dirk Brandewie367dc4a2014-03-19 08:45:53 -07001406 cpufreq_driver->stop_cpu(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001407 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001408
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301409 return 0;
1410}
1411
1412static int __cpufreq_remove_dev_finish(struct device *dev,
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301413 struct subsys_interface *sif)
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301414{
1415 unsigned int cpu = dev->id, cpus;
1416 int ret;
1417 unsigned long flags;
1418 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301419
1420 read_lock_irqsave(&cpufreq_driver_lock, flags);
1421 policy = per_cpu(cpufreq_cpu_data, cpu);
1422 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1423
1424 if (!policy) {
1425 pr_debug("%s: No cpu_data found\n", __func__);
1426 return -EINVAL;
1427 }
1428
viresh kumarad7722d2013-10-18 19:10:15 +05301429 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301430 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301431
1432 if (cpus > 1)
1433 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301434 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301435
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001436 /* If cpu is last user of policy, free policy */
1437 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301438 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301439 ret = __cpufreq_governor(policy,
1440 CPUFREQ_GOV_POLICY_EXIT);
1441 if (ret) {
1442 pr_err("%s: Failed to exit governor\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001443 __func__);
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301444 return ret;
1445 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301446 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001447
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301448 if (!cpufreq_suspended)
Viresh Kumar42f921a2013-12-20 21:26:02 +05301449 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301450
1451 /*
1452 * Perform the ->exit() even during light-weight tear-down,
1453 * since this is a core component, and is essential for the
1454 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001455 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001456 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301457 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001458
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301459 /* Remove policy from list of active policies */
1460 write_lock_irqsave(&cpufreq_driver_lock, flags);
1461 list_del(&policy->policy_list);
1462 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1463
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301464 if (!cpufreq_suspended)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301465 cpufreq_policy_free(policy);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02001466 } else if (has_target()) {
1467 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1468 if (!ret)
1469 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1470
1471 if (ret) {
1472 pr_err("%s: Failed to start governor\n", __func__);
1473 return ret;
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001474 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Viresh Kumar474deff2013-08-20 12:08:25 +05301477 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 return 0;
1479}
1480
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301481/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301482 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301483 *
1484 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301485 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001486static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001487{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001488 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301489 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001490
1491 if (cpu_is_offline(cpu))
1492 return 0;
1493
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301494 ret = __cpufreq_remove_dev_prepare(dev, sif);
Viresh Kumar27a862e2013-10-02 14:13:14 +05301495
1496 if (!ret)
Viresh Kumar96bbbe42014-03-10 14:53:35 +05301497 ret = __cpufreq_remove_dev_finish(dev, sif);
Viresh Kumar27a862e2013-10-02 14:13:14 +05301498
1499 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001500}
1501
David Howells65f27f32006-11-22 14:55:48 +00001502static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
David Howells65f27f32006-11-22 14:55:48 +00001504 struct cpufreq_policy *policy =
1505 container_of(work, struct cpufreq_policy, update);
1506 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001507 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 cpufreq_update_policy(cpu);
1509}
1510
1511/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301512 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1513 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 * @cpu: cpu number
1515 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1516 * @new_freq: CPU frequency the CPU actually runs at
1517 *
Dave Jones29464f22009-01-18 01:37:11 -05001518 * We adjust to current frequency first, and need to clean up later.
1519 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301521static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1522 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301524 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301526 unsigned long flags;
1527
Joe Perchese837f9b2014-03-11 10:03:00 -07001528 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1529 old_freq, new_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 freqs.old = old_freq;
1532 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301533
1534 read_lock_irqsave(&cpufreq_driver_lock, flags);
1535 policy = per_cpu(cpufreq_cpu_data, cpu);
1536 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1537
Viresh Kumar8fec0512014-03-24 13:35:45 +05301538 cpufreq_freq_transition_begin(policy, &freqs);
1539 cpufreq_freq_transition_end(policy, &freqs, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540}
1541
Dave Jones32ee8c32006-02-28 00:43:23 -05001542/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301543 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001544 * @cpu: CPU number
1545 *
1546 * This is the last known freq, without actually getting it from the driver.
1547 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1548 */
1549unsigned int cpufreq_quick_get(unsigned int cpu)
1550{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001551 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301552 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001553
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001554 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1555 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001556
1557 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001558 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301559 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001560 cpufreq_cpu_put(policy);
1561 }
1562
Dave Jones4d34a672008-02-07 16:33:49 -05001563 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001564}
1565EXPORT_SYMBOL(cpufreq_quick_get);
1566
Jesse Barnes3d737102011-06-28 10:59:12 -07001567/**
1568 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1569 * @cpu: CPU number
1570 *
1571 * Just return the max possible frequency for a given CPU.
1572 */
1573unsigned int cpufreq_quick_get_max(unsigned int cpu)
1574{
1575 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1576 unsigned int ret_freq = 0;
1577
1578 if (policy) {
1579 ret_freq = policy->max;
1580 cpufreq_cpu_put(policy);
1581 }
1582
1583 return ret_freq;
1584}
1585EXPORT_SYMBOL(cpufreq_quick_get_max);
1586
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001587static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001589 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301590 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001592 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001593 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001595 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301597 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001598 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301599 /* verify no discrepancy between actual and
1600 saved value exists */
1601 if (unlikely(ret_freq != policy->cur)) {
1602 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 schedule_work(&policy->update);
1604 }
1605 }
1606
Dave Jones4d34a672008-02-07 16:33:49 -05001607 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001610/**
1611 * cpufreq_get - get the current CPU frequency (in kHz)
1612 * @cpu: CPU number
1613 *
1614 * Get the CPU current (static) CPU frequency
1615 */
1616unsigned int cpufreq_get(unsigned int cpu)
1617{
Aaron Plattner999976e2014-03-04 12:42:15 -08001618 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001619 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001620
Aaron Plattner999976e2014-03-04 12:42:15 -08001621 if (policy) {
1622 down_read(&policy->rwsem);
1623 ret_freq = __cpufreq_get(cpu);
1624 up_read(&policy->rwsem);
Viresh Kumar26ca8692013-09-20 22:37:31 +05301625
Aaron Plattner999976e2014-03-04 12:42:15 -08001626 cpufreq_cpu_put(policy);
1627 }
Viresh Kumar6eed9402013-08-06 22:53:11 +05301628
Dave Jones4d34a672008-02-07 16:33:49 -05001629 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631EXPORT_SYMBOL(cpufreq_get);
1632
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001633static struct subsys_interface cpufreq_interface = {
1634 .name = "cpufreq",
1635 .subsys = &cpu_subsys,
1636 .add_dev = cpufreq_add_dev,
1637 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001638};
1639
Viresh Kumare28867e2014-03-04 11:00:27 +08001640/*
1641 * In case platform wants some specific frequency to be configured
1642 * during suspend..
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001643 */
Viresh Kumare28867e2014-03-04 11:00:27 +08001644int cpufreq_generic_suspend(struct cpufreq_policy *policy)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001645{
Viresh Kumare28867e2014-03-04 11:00:27 +08001646 int ret;
Dave Jones4bc5d342009-08-04 14:03:25 -04001647
Viresh Kumare28867e2014-03-04 11:00:27 +08001648 if (!policy->suspend_freq) {
1649 pr_err("%s: suspend_freq can't be zero\n", __func__);
1650 return -EINVAL;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001651 }
1652
Viresh Kumare28867e2014-03-04 11:00:27 +08001653 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1654 policy->suspend_freq);
1655
1656 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1657 CPUFREQ_RELATION_H);
1658 if (ret)
1659 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1660 __func__, policy->suspend_freq, ret);
1661
Dave Jonesc9060492008-02-07 16:32:18 -05001662 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001663}
Viresh Kumare28867e2014-03-04 11:00:27 +08001664EXPORT_SYMBOL(cpufreq_generic_suspend);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001665
1666/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001667 * cpufreq_suspend() - Suspend CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001669 * Called during system wide Suspend/Hibernate cycles for suspending governors
1670 * as some platforms can't change frequency after this point in suspend cycle.
1671 * Because some of the devices (like: i2c, regulators, etc) they use for
1672 * changing frequency are suspended quickly after this point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001674void cpufreq_suspend(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301676 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001678 if (!cpufreq_driver)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001679 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001681 if (!has_target())
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301682 goto suspend;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001684 pr_debug("%s: Suspending Governors\n", __func__);
1685
1686 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
1687 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1688 pr_err("%s: Failed to stop governor for policy: %p\n",
1689 __func__, policy);
1690 else if (cpufreq_driver->suspend
1691 && cpufreq_driver->suspend(policy))
1692 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1693 policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Viresh Kumarb1b12ba2014-09-30 09:33:17 +05301695
1696suspend:
1697 cpufreq_suspended = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700/**
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001701 * cpufreq_resume() - Resume CPUFreq governors
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 *
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001703 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1704 * are suspended with cpufreq_suspend().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001706void cpufreq_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct cpufreq_policy *policy;
1709
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001710 if (!cpufreq_driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return;
1712
Lan Tianyu8e304442014-09-18 15:03:07 +08001713 cpufreq_suspended = false;
1714
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001715 if (!has_target())
1716 return;
1717
1718 pr_debug("%s: Resuming Governors\n", __func__);
1719
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001720 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
Viresh Kumar0c5aa402014-03-24 12:30:29 +05301721 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1722 pr_err("%s: Failed to resume driver: %p\n", __func__,
1723 policy);
1724 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001725 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1726 pr_err("%s: Failed to start governor for policy: %p\n",
1727 __func__, policy);
Viresh Kumar2f0aea92014-03-04 11:00:26 +08001728
1729 /*
1730 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
1731 * policy in list. It will verify that the current freq is in
1732 * sync with what we believe it to be.
1733 */
1734 if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
1735 schedule_work(&policy->update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Borislav Petkov9d950462013-01-20 10:24:28 +00001739/**
1740 * cpufreq_get_current_driver - return current driver's name
1741 *
1742 * Return the name string of the currently loaded cpufreq driver
1743 * or NULL, if none.
1744 */
1745const char *cpufreq_get_current_driver(void)
1746{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001747 if (cpufreq_driver)
1748 return cpufreq_driver->name;
1749
1750 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001751}
1752EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Thomas Petazzoni51315cd2014-10-19 11:30:27 +02001754/**
1755 * cpufreq_get_driver_data - return current driver data
1756 *
1757 * Return the private data of the currently loaded cpufreq
1758 * driver, or NULL if no cpufreq driver is loaded.
1759 */
1760void *cpufreq_get_driver_data(void)
1761{
1762 if (cpufreq_driver)
1763 return cpufreq_driver->driver_data;
1764
1765 return NULL;
1766}
1767EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769/*********************************************************************
1770 * NOTIFIER LISTS INTERFACE *
1771 *********************************************************************/
1772
1773/**
1774 * cpufreq_register_notifier - register a driver with cpufreq
1775 * @nb: notifier function to register
1776 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1777 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001778 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 * are notified about clock rate changes (once before and once after
1780 * the transition), or a list of drivers that are notified about
1781 * changes in cpufreq policy.
1782 *
1783 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001784 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 */
1786int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1787{
1788 int ret;
1789
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001790 if (cpufreq_disabled())
1791 return -EINVAL;
1792
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001793 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 switch (list) {
1796 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001797 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001798 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 break;
1800 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001801 ret = blocking_notifier_chain_register(
1802 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 break;
1804 default:
1805 ret = -EINVAL;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 return ret;
1809}
1810EXPORT_SYMBOL(cpufreq_register_notifier);
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812/**
1813 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1814 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301815 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 *
1817 * Remove a driver from the CPU frequency notifier list.
1818 *
1819 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001820 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 */
1822int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1823{
1824 int ret;
1825
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001826 if (cpufreq_disabled())
1827 return -EINVAL;
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 switch (list) {
1830 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001831 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001832 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 break;
1834 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001835 ret = blocking_notifier_chain_unregister(
1836 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
1838 default:
1839 ret = -EINVAL;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 return ret;
1843}
1844EXPORT_SYMBOL(cpufreq_unregister_notifier);
1845
1846
1847/*********************************************************************
1848 * GOVERNORS *
1849 *********************************************************************/
1850
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301851/* Must set freqs->new to intermediate frequency */
1852static int __target_intermediate(struct cpufreq_policy *policy,
1853 struct cpufreq_freqs *freqs, int index)
1854{
1855 int ret;
1856
1857 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1858
1859 /* We don't need to switch to intermediate freq */
1860 if (!freqs->new)
1861 return 0;
1862
1863 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1864 __func__, policy->cpu, freqs->old, freqs->new);
1865
1866 cpufreq_freq_transition_begin(policy, freqs);
1867 ret = cpufreq_driver->target_intermediate(policy, index);
1868 cpufreq_freq_transition_end(policy, freqs, ret);
1869
1870 if (ret)
1871 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1872 __func__, ret);
1873
1874 return ret;
1875}
1876
Viresh Kumar8d657752014-05-21 14:29:29 +05301877static int __target_index(struct cpufreq_policy *policy,
1878 struct cpufreq_frequency_table *freq_table, int index)
1879{
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301880 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1881 unsigned int intermediate_freq = 0;
Viresh Kumar8d657752014-05-21 14:29:29 +05301882 int retval = -EINVAL;
1883 bool notify;
1884
1885 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
Viresh Kumar8d657752014-05-21 14:29:29 +05301886 if (notify) {
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301887 /* Handle switching to intermediate frequency */
1888 if (cpufreq_driver->get_intermediate) {
1889 retval = __target_intermediate(policy, &freqs, index);
1890 if (retval)
1891 return retval;
Viresh Kumar8d657752014-05-21 14:29:29 +05301892
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301893 intermediate_freq = freqs.new;
1894 /* Set old freq to intermediate */
1895 if (intermediate_freq)
1896 freqs.old = freqs.new;
1897 }
1898
1899 freqs.new = freq_table[index].frequency;
Viresh Kumar8d657752014-05-21 14:29:29 +05301900 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1901 __func__, policy->cpu, freqs.old, freqs.new);
1902
1903 cpufreq_freq_transition_begin(policy, &freqs);
1904 }
1905
1906 retval = cpufreq_driver->target_index(policy, index);
1907 if (retval)
1908 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1909 retval);
1910
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301911 if (notify) {
Viresh Kumar8d657752014-05-21 14:29:29 +05301912 cpufreq_freq_transition_end(policy, &freqs, retval);
1913
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301914 /*
1915 * Failed after setting to intermediate freq? Driver should have
1916 * reverted back to initial frequency and so should we. Check
1917 * here for intermediate_freq instead of get_intermediate, in
1918 * case we have't switched to intermediate freq at all.
1919 */
1920 if (unlikely(retval && intermediate_freq)) {
1921 freqs.old = intermediate_freq;
1922 freqs.new = policy->restore_freq;
1923 cpufreq_freq_transition_begin(policy, &freqs);
1924 cpufreq_freq_transition_end(policy, &freqs, 0);
1925 }
1926 }
1927
Viresh Kumar8d657752014-05-21 14:29:29 +05301928 return retval;
1929}
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931int __cpufreq_driver_target(struct cpufreq_policy *policy,
1932 unsigned int target_freq,
1933 unsigned int relation)
1934{
Viresh Kumar72499242012-10-31 01:28:21 +01001935 unsigned int old_target_freq = target_freq;
Viresh Kumar8d657752014-05-21 14:29:29 +05301936 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001937
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001938 if (cpufreq_disabled())
1939 return -ENODEV;
1940
Viresh Kumar72499242012-10-31 01:28:21 +01001941 /* Make sure that target_freq is within supported range */
1942 if (target_freq > policy->max)
1943 target_freq = policy->max;
1944 if (target_freq < policy->min)
1945 target_freq = policy->min;
1946
1947 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07001948 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001949
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301950 /*
1951 * This might look like a redundant call as we are checking it again
1952 * after finding index. But it is left intentionally for cases where
1953 * exactly same freq is called again and so we can save on few function
1954 * calls.
1955 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001956 if (target_freq == policy->cur)
1957 return 0;
1958
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05301959 /* Save last value to restore later on errors */
1960 policy->restore_freq = policy->cur;
1961
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001962 if (cpufreq_driver->target)
1963 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301964 else if (cpufreq_driver->target_index) {
1965 struct cpufreq_frequency_table *freq_table;
1966 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001967
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301968 freq_table = cpufreq_frequency_get_table(policy->cpu);
1969 if (unlikely(!freq_table)) {
1970 pr_err("%s: Unable to find freq_table\n", __func__);
1971 goto out;
1972 }
1973
1974 retval = cpufreq_frequency_table_target(policy, freq_table,
1975 target_freq, relation, &index);
1976 if (unlikely(retval)) {
1977 pr_err("%s: Unable to find matching freq\n", __func__);
1978 goto out;
1979 }
1980
Viresh Kumard4019f02013-08-14 19:38:24 +05301981 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301982 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301983 goto out;
1984 }
1985
Viresh Kumar8d657752014-05-21 14:29:29 +05301986 retval = __target_index(policy, freq_table, index);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301987 }
1988
1989out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return retval;
1991}
1992EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994int cpufreq_driver_target(struct cpufreq_policy *policy,
1995 unsigned int target_freq,
1996 unsigned int relation)
1997{
Julia Lawallf1829e42008-07-25 22:44:53 +02001998 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
viresh kumarad7722d2013-10-18 19:10:15 +05302000 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 ret = __cpufreq_driver_target(policy, target_freq, relation);
2003
viresh kumarad7722d2013-10-18 19:10:15 +05302004 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return ret;
2007}
2008EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2009
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002010/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002011 * when "event" is CPUFREQ_GOV_LIMITS
2012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302014static int __cpufreq_governor(struct cpufreq_policy *policy,
2015 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Dave Jonescc993ca2005-07-28 09:43:56 -07002017 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07002018
2019 /* Only must be defined when default governor is known to have latency
2020 restrictions, like e.g. conservative or ondemand.
2021 That this is the case is already ensured in Kconfig
2022 */
2023#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
2024 struct cpufreq_governor *gov = &cpufreq_gov_performance;
2025#else
2026 struct cpufreq_governor *gov = NULL;
2027#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07002028
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002029 /* Don't start any governor operations if we are entering suspend */
2030 if (cpufreq_suspended)
2031 return 0;
Ethan Zhaocb577202014-12-18 15:28:19 +09002032 /*
2033 * Governor might not be initiated here if ACPI _PPC changed
2034 * notification happened, so check it.
2035 */
2036 if (!policy->governor)
2037 return -EINVAL;
Viresh Kumar2f0aea92014-03-04 11:00:26 +08002038
Thomas Renninger1c256242007-10-02 13:28:12 -07002039 if (policy->governor->max_transition_latency &&
2040 policy->cpuinfo.transition_latency >
2041 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07002042 if (!gov)
2043 return -EINVAL;
2044 else {
Joe Perchese837f9b2014-03-11 10:03:00 -07002045 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2046 policy->governor->name, gov->name);
Thomas Renninger6afde102007-10-02 13:28:13 -07002047 policy->governor = gov;
2048 }
Thomas Renninger1c256242007-10-02 13:28:12 -07002049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Viresh Kumarfe492f32013-08-06 22:53:10 +05302051 if (event == CPUFREQ_GOV_POLICY_INIT)
2052 if (!try_module_get(policy->governor->owner))
2053 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002055 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002056 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002057
2058 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05302059 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05302060 || (!policy->governor_enabled
2061 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002062 mutex_unlock(&cpufreq_governor_lock);
2063 return -EBUSY;
2064 }
2065
2066 if (event == CPUFREQ_GOV_STOP)
2067 policy->governor_enabled = false;
2068 else if (event == CPUFREQ_GOV_START)
2069 policy->governor_enabled = true;
2070
2071 mutex_unlock(&cpufreq_governor_lock);
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 ret = policy->governor->governor(policy, event);
2074
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002075 if (!ret) {
2076 if (event == CPUFREQ_GOV_POLICY_INIT)
2077 policy->governor->initialized++;
2078 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2079 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08002080 } else {
2081 /* Restore original values */
2082 mutex_lock(&cpufreq_governor_lock);
2083 if (event == CPUFREQ_GOV_STOP)
2084 policy->governor_enabled = true;
2085 else if (event == CPUFREQ_GOV_START)
2086 policy->governor_enabled = false;
2087 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00002088 }
Viresh Kumarb3940582013-02-01 05:42:58 +00002089
Viresh Kumarfe492f32013-08-06 22:53:10 +05302090 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2091 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 module_put(policy->governor->owner);
2093
2094 return ret;
2095}
2096
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097int cpufreq_register_governor(struct cpufreq_governor *governor)
2098{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 if (!governor)
2102 return -EINVAL;
2103
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002104 if (cpufreq_disabled())
2105 return -ENODEV;
2106
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002107 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05002108
Viresh Kumarb3940582013-02-01 05:42:58 +00002109 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002110 err = -EBUSY;
2111 if (__find_governor(governor->name) == NULL) {
2112 err = 0;
2113 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Dave Jones32ee8c32006-02-28 00:43:23 -05002116 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07002117 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2122{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002123 int cpu;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (!governor)
2126 return;
2127
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002128 if (cpufreq_disabled())
2129 return;
2130
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002131 for_each_present_cpu(cpu) {
2132 if (cpu_online(cpu))
2133 continue;
2134 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
2135 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
2136 }
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05002137
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002138 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08002140 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return;
2142}
2143EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2144
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146/*********************************************************************
2147 * POLICY INTERFACE *
2148 *********************************************************************/
2149
2150/**
2151 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05002152 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2153 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 *
2155 * Reads the current cpufreq policy.
2156 */
2157int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2158{
2159 struct cpufreq_policy *cpu_policy;
2160 if (!policy)
2161 return -EINVAL;
2162
2163 cpu_policy = cpufreq_cpu_get(cpu);
2164 if (!cpu_policy)
2165 return -EINVAL;
2166
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302167 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return 0;
2171}
2172EXPORT_SYMBOL(cpufreq_get_policy);
2173
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002174/*
Viresh Kumar037ce832013-10-02 14:13:16 +05302175 * policy : current policy.
2176 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02002177 */
Viresh Kumar037ce832013-10-02 14:13:16 +05302178static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302179 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002181 struct cpufreq_governor *old_gov;
2182 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Joe Perchese837f9b2014-03-11 10:03:00 -07002184 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2185 new_policy->cpu, new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302187 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002189 if (new_policy->min > policy->max || new_policy->max < policy->min)
2190 return -EINVAL;
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302193 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002195 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08002198 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302199 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
2201 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08002202 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302203 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Viresh Kumarbb176f72013-06-19 14:19:33 +05302205 /*
2206 * verify the cpu speed can be set within this limit, which might be
2207 * different to the first one
2208 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302209 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002210 if (ret)
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002211 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002214 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302215 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302217 policy->min = new_policy->min;
2218 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002220 pr_debug("new min and max freqs are %u - %u kHz\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002221 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002223 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302224 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002225 pr_debug("setting range\n");
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002226 return cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002229 if (new_policy->governor == policy->governor)
2230 goto out;
2231
2232 pr_debug("governor switch\n");
2233
2234 /* save old, working values */
2235 old_gov = policy->governor;
2236 /* end old governor */
2237 if (old_gov) {
2238 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2239 up_write(&policy->rwsem);
Stratos Karafotise5c87b72014-03-19 23:29:17 +02002240 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
Rafael J. Wysockid9a789c2014-02-17 22:56:35 +01002241 down_write(&policy->rwsem);
2242 }
2243
2244 /* start new governor */
2245 policy->governor = new_policy->governor;
2246 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2247 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2248 goto out;
2249
2250 up_write(&policy->rwsem);
2251 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2252 down_write(&policy->rwsem);
2253 }
2254
2255 /* new governor failed, so re-start old one */
2256 pr_debug("starting governor %s failed\n", policy->governor->name);
2257 if (old_gov) {
2258 policy->governor = old_gov;
2259 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2260 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2261 }
2262
2263 return -EINVAL;
2264
2265 out:
2266 pr_debug("governor: change or update limits\n");
2267 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
2270/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2272 * @cpu: CPU which shall be re-evaluated
2273 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002274 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 * at different times.
2276 */
2277int cpufreq_update_policy(unsigned int cpu)
2278{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302279 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2280 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002283 if (!policy)
2284 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
viresh kumarad7722d2013-10-18 19:10:15 +05302286 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002288 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302289 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302290 new_policy.min = policy->user_policy.min;
2291 new_policy.max = policy->user_policy.max;
2292 new_policy.policy = policy->user_policy.policy;
2293 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Viresh Kumarbb176f72013-06-19 14:19:33 +05302295 /*
2296 * BIOS might change freq behind our back
2297 * -> ask driver for current freq and notify governors about a change
2298 */
Rafael J. Wysocki2ed99e32014-03-12 21:49:33 +01002299 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302300 new_policy.cur = cpufreq_driver->get(cpu);
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302301 if (WARN_ON(!new_policy.cur)) {
2302 ret = -EIO;
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002303 goto unlock;
Viresh Kumarbd0fa9b2014-02-25 14:29:44 +05302304 }
2305
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302306 if (!policy->cur) {
Joe Perchese837f9b2014-03-11 10:03:00 -07002307 pr_debug("Driver did not initialize current freq\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302308 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002309 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302310 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302311 cpufreq_out_of_sync(cpu, policy->cur,
2312 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002313 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002314 }
2315
Viresh Kumar037ce832013-10-02 14:13:16 +05302316 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Aaron Plattnerfefa8ff2014-06-18 11:27:32 -07002318unlock:
viresh kumarad7722d2013-10-18 19:10:15 +05302319 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002320
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302321 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return ret;
2323}
2324EXPORT_SYMBOL(cpufreq_update_policy);
2325
Paul Gortmaker27609842013-06-19 13:54:04 -04002326static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002327 unsigned long action, void *hcpu)
2328{
2329 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002330 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002331
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002332 dev = get_cpu_device(cpu);
2333 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302334 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002335 case CPU_ONLINE:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302336 __cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002337 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302338
Ashok Rajc32b6b82005-10-30 14:59:54 -08002339 case CPU_DOWN_PREPARE:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302340 __cpufreq_remove_dev_prepare(dev, NULL);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302341 break;
2342
2343 case CPU_POST_DEAD:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302344 __cpufreq_remove_dev_finish(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002345 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302346
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002347 case CPU_DOWN_FAILED:
Viresh Kumar96bbbe42014-03-10 14:53:35 +05302348 __cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002349 break;
2350 }
2351 }
2352 return NOTIFY_OK;
2353}
2354
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002355static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302356 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002357};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
2359/*********************************************************************
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002360 * BOOST *
2361 *********************************************************************/
2362static int cpufreq_boost_set_sw(int state)
2363{
2364 struct cpufreq_frequency_table *freq_table;
2365 struct cpufreq_policy *policy;
2366 int ret = -EINVAL;
2367
2368 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2369 freq_table = cpufreq_frequency_get_table(policy->cpu);
2370 if (freq_table) {
2371 ret = cpufreq_frequency_table_cpuinfo(policy,
2372 freq_table);
2373 if (ret) {
2374 pr_err("%s: Policy frequency update failed\n",
2375 __func__);
2376 break;
2377 }
2378 policy->user_policy.max = policy->max;
2379 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2380 }
2381 }
2382
2383 return ret;
2384}
2385
2386int cpufreq_boost_trigger_state(int state)
2387{
2388 unsigned long flags;
2389 int ret = 0;
2390
2391 if (cpufreq_driver->boost_enabled == state)
2392 return 0;
2393
2394 write_lock_irqsave(&cpufreq_driver_lock, flags);
2395 cpufreq_driver->boost_enabled = state;
2396 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2397
2398 ret = cpufreq_driver->set_boost(state);
2399 if (ret) {
2400 write_lock_irqsave(&cpufreq_driver_lock, flags);
2401 cpufreq_driver->boost_enabled = !state;
2402 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2403
Joe Perchese837f9b2014-03-11 10:03:00 -07002404 pr_err("%s: Cannot %s BOOST\n",
2405 __func__, state ? "enable" : "disable");
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002406 }
2407
2408 return ret;
2409}
2410
2411int cpufreq_boost_supported(void)
2412{
2413 if (likely(cpufreq_driver))
2414 return cpufreq_driver->boost_supported;
2415
2416 return 0;
2417}
2418EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2419
2420int cpufreq_boost_enabled(void)
2421{
2422 return cpufreq_driver->boost_enabled;
2423}
2424EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2425
2426/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2428 *********************************************************************/
2429
2430/**
2431 * cpufreq_register_driver - register a CPU Frequency driver
2432 * @driver_data: A struct cpufreq_driver containing the values#
2433 * submitted by the CPU Frequency driver.
2434 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302435 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002437 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 *
2439 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002440int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441{
2442 unsigned long flags;
2443 int ret;
2444
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002445 if (cpufreq_disabled())
2446 return -ENODEV;
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302449 !(driver_data->setpolicy || driver_data->target_index ||
Rafael J. Wysocki98322352014-03-19 12:48:30 +01002450 driver_data->target) ||
2451 (driver_data->setpolicy && (driver_data->target_index ||
Viresh Kumar1c03a2d2014-06-02 22:49:28 +05302452 driver_data->target)) ||
2453 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 return -EINVAL;
2455
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002456 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 if (driver_data->setpolicy)
2459 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2460
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002461 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002462 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002463 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002464 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002466 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002467 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002469 if (cpufreq_boost_supported()) {
2470 /*
2471 * Check if driver provides function to enable boost -
2472 * if not, use cpufreq_boost_set_sw as default
2473 */
2474 if (!cpufreq_driver->set_boost)
2475 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2476
2477 ret = cpufreq_sysfs_create_file(&boost.attr);
2478 if (ret) {
2479 pr_err("%s: cannot register global BOOST sysfs file\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002480 __func__);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002481 goto err_null_driver;
2482 }
2483 }
2484
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002485 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002486 if (ret)
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002487 goto err_boost_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002489 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 int i;
2491 ret = -ENODEV;
2492
2493 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002494 for (i = 0; i < nr_cpu_ids; i++)
2495 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002497 break;
2498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
2500 /* if all ->init() calls failed, unregister */
2501 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002502 pr_debug("no CPU initialized for driver %s\n",
Joe Perchese837f9b2014-03-11 10:03:00 -07002503 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002504 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506 }
2507
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002508 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002509 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002511 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002512err_if_unreg:
2513 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002514err_boost_unreg:
2515 if (cpufreq_boost_supported())
2516 cpufreq_sysfs_remove_file(&boost.attr);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002517err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002518 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002519 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002520 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
2523EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525/**
2526 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2527 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302528 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 * the right to do so, i.e. if you have succeeded in initialising before!
2530 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2531 * currently not initialised.
2532 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002533int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
2535 unsigned long flags;
2536
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002537 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002540 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002542 subsys_interface_unregister(&cpufreq_interface);
Lukasz Majewski6f19efc2013-12-20 15:24:49 +01002543 if (cpufreq_boost_supported())
2544 cpufreq_sysfs_remove_file(&boost.attr);
2545
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002546 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Viresh Kumar6eed9402013-08-06 22:53:11 +05302548 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002549 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302550
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002551 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302552
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002553 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302554 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 return 0;
2557}
2558EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002559
Doug Anderson90de2a42014-12-23 22:09:48 -08002560/*
2561 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2562 * or mutexes when secondary CPUs are halted.
2563 */
2564static struct syscore_ops cpufreq_syscore_ops = {
2565 .shutdown = cpufreq_suspend,
2566};
2567
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002568static int __init cpufreq_core_init(void)
2569{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002570 if (cpufreq_disabled())
2571 return -ENODEV;
2572
Viresh Kumar2361be22013-05-17 16:09:09 +05302573 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002574 BUG_ON(!cpufreq_global_kobject);
2575
Doug Anderson90de2a42014-12-23 22:09:48 -08002576 register_syscore_ops(&cpufreq_syscore_ops);
2577
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002578 return 0;
2579}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002580core_initcall(cpufreq_core_init);