blob: 87ed83a6c894ebfe2f0f21071ea2706f690f1a6f [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>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010029#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053030#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020031#include <trace/events/power.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
Dave Jonescd878472006-08-11 17:59:28 -040034 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020038static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070039static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053041static DEFINE_RWLOCK(cpufreq_driver_lock);
42static DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053043static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053044
Thomas Renninger084f3492007-07-09 11:35:28 -070045#ifdef CONFIG_HOTPLUG_CPU
46/* 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);
Thomas Renninger084f3492007-07-09 11:35:28 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053050static inline bool has_target(void)
51{
52 return cpufreq_driver->target_index || cpufreq_driver->target;
53}
54
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080055/*
56 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
57 * all cpufreq/hotplug/workqueue/etc related lock issues.
58 *
59 * The rules for this semaphore:
60 * - Any routine that wants to read from the policy structure will
61 * do a down_read on this semaphore.
62 * - Any routine that will write to the policy structure and/or may take away
63 * the policy altogether (eg. CPU hotplug), will hold this lock in write
64 * mode before doing so.
65 *
66 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080067 * - Governor routines that can be called in cpufreq hotplug path should not
68 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040069 * - Lock should not be held across
70 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071 */
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
73
74#define lock_policy_rwsem(mode, cpu) \
Viresh Kumar1b750e32013-10-02 14:13:09 +053075static void lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080076{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053077 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
78 BUG_ON(!policy); \
79 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080080}
81
82lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080083lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080084
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053085#define unlock_policy_rwsem(mode, cpu) \
86static void unlock_policy_rwsem_##mode(int cpu) \
87{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053088 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
89 BUG_ON(!policy); \
90 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080091}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053093unlock_policy_rwsem(read, cpu);
94unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095
Viresh Kumar6eed9402013-08-06 22:53:11 +053096/*
97 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
98 * sections
99 */
100static DECLARE_RWSEM(cpufreq_rwsem);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500103static int __cpufreq_governor(struct cpufreq_policy *policy,
104 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800105static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000106static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500109 * Two notifier lists: the "policy" list is involved in the
110 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * "transition" list for kernel code that needs to handle
112 * changes to devices when the CPU clock speed changes.
113 * The mutex locks both lists.
114 */
Alan Sterne041c682006-03-27 01:16:30 -0800115static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700116static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200118static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700119static int __init init_cpufreq_transition_notifier_list(void)
120{
121 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200122 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700123 return 0;
124}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800125pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400127static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200128static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400129{
130 return off;
131}
132void disable_cpufreq(void)
133{
134 off = 1;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500137static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000139bool have_governor_per_policy(void)
140{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530141 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000142}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000143EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000144
Viresh Kumar944e9a02013-05-16 05:09:57 +0000145struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
146{
147 if (have_governor_per_policy())
148 return &policy->kobj;
149 else
150 return cpufreq_global_kobject;
151}
152EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
153
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000154static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
155{
156 u64 idle_time;
157 u64 cur_wall_time;
158 u64 busy_time;
159
160 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
161
162 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
165 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
166 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
167 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
168
169 idle_time = cur_wall_time - busy_time;
170 if (wall)
171 *wall = cputime_to_usecs(cur_wall_time);
172
173 return cputime_to_usecs(idle_time);
174}
175
176u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
177{
178 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
179
180 if (idle_time == -1ULL)
181 return get_cpu_idle_time_jiffy(cpu, wall);
182 else if (!io_busy)
183 idle_time += get_cpu_iowait_time_us(cpu, wall);
184
185 return idle_time;
186}
187EXPORT_SYMBOL_GPL(get_cpu_idle_time);
188
Viresh Kumar70e9e772013-10-03 20:29:07 +0530189/*
190 * This is a generic cpufreq init() routine which can be used by cpufreq
191 * drivers of SMP systems. It will do following:
192 * - validate & show freq table passed
193 * - set policies transition latency
194 * - policy->cpus with all possible CPUs
195 */
196int cpufreq_generic_init(struct cpufreq_policy *policy,
197 struct cpufreq_frequency_table *table,
198 unsigned int transition_latency)
199{
200 int ret;
201
202 ret = cpufreq_table_validate_and_show(policy, table);
203 if (ret) {
204 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
205 return ret;
206 }
207
208 policy->cpuinfo.transition_latency = transition_latency;
209
210 /*
211 * The driver only supports the SMP configuartion where all processors
212 * share the clock and voltage and clock.
213 */
214 cpumask_setall(policy->cpus);
215
216 return 0;
217}
218EXPORT_SYMBOL_GPL(cpufreq_generic_init);
219
Viresh Kumar6eed9402013-08-06 22:53:11 +0530220struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530222 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 unsigned long flags;
224
Viresh Kumar6eed9402013-08-06 22:53:11 +0530225 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
226 return NULL;
227
228 if (!down_read_trylock(&cpufreq_rwsem))
229 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000232 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Viresh Kumar6eed9402013-08-06 22:53:11 +0530234 if (cpufreq_driver) {
235 /* get the CPU */
236 policy = per_cpu(cpufreq_cpu_data, cpu);
237 if (policy)
238 kobject_get(&policy->kobj);
239 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200240
Viresh Kumar6eed9402013-08-06 22:53:11 +0530241 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530243 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530244 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530246 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000247}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
249
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530250void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000252 if (cpufreq_disabled())
253 return;
254
Viresh Kumar6eed9402013-08-06 22:53:11 +0530255 kobject_put(&policy->kobj);
256 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
262 *********************************************************************/
263
264/**
265 * adjust_jiffies - adjust the system "loops_per_jiffy"
266 *
267 * This function alters the system "loops_per_jiffy" for the clock
268 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500269 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 * per-CPU loops_per_jiffy value wherever possible.
271 */
272#ifndef CONFIG_SMP
273static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530274static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Arjan van de Ven858119e2006-01-14 13:20:43 -0800276static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277{
278 if (ci->flags & CPUFREQ_CONST_LOOPS)
279 return;
280
281 if (!l_p_j_ref_freq) {
282 l_p_j_ref = loops_per_jiffy;
283 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200284 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530285 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530287 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700288 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530289 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
290 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200291 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530292 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294}
295#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530296static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
297{
298 return;
299}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#endif
301
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530302static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530303 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 BUG_ON(irqs_disabled());
306
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000307 if (cpufreq_disabled())
308 return;
309
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200310 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200311 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800312 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500317 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800318 * which is not equal to what the cpufreq core thinks is
319 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200321 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800322 if ((policy) && (policy->cpu == freqs->cpu) &&
323 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200324 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800325 " %u, cpufreq assumed %u kHz.\n",
326 freqs->old, policy->cur);
327 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 }
329 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700330 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800331 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
333 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case CPUFREQ_POSTCHANGE:
336 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200337 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200338 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100339 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700340 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800341 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800342 if (likely(policy) && likely(policy->cpu == freqs->cpu))
343 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 break;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530347
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530348/**
349 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
350 * on frequency transition.
351 *
352 * This function calls the transition notifiers and the "adjust_jiffies"
353 * function. It is called twice on all CPU frequency changes that have
354 * external effects.
355 */
356void cpufreq_notify_transition(struct cpufreq_policy *policy,
357 struct cpufreq_freqs *freqs, unsigned int state)
358{
359 for_each_cpu(freqs->cpu, policy->cpus)
360 __cpufreq_notify_transition(policy, freqs, state);
361}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
363
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365/*********************************************************************
366 * SYSFS INTERFACE *
367 *********************************************************************/
368
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700369static struct cpufreq_governor *__find_governor(const char *str_governor)
370{
371 struct cpufreq_governor *t;
372
373 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500374 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700375 return t;
376
377 return NULL;
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/**
381 * cpufreq_parse_governor - parse a governor string
382 */
Dave Jones905d77c2008-03-05 14:28:32 -0500383static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct cpufreq_governor **governor)
385{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700386 int err = -EINVAL;
387
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200388 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700389 goto out;
390
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200391 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
393 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700394 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530395 } else if (!strnicmp(str_governor, "powersave",
396 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700398 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530400 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700402
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800403 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700404
405 t = __find_governor(str_governor);
406
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700407 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700408 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700409
Kees Cook1a8e1462011-05-04 08:38:56 -0700410 mutex_unlock(&cpufreq_governor_mutex);
411 ret = request_module("cpufreq_%s", str_governor);
412 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700413
Kees Cook1a8e1462011-05-04 08:38:56 -0700414 if (ret == 0)
415 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700416 }
417
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700418 if (t != NULL) {
419 *governor = t;
420 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700422
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800423 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
Dave Jones29464f22009-01-18 01:37:11 -0500425out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530430 * cpufreq_per_cpu_attr_read() / show_##file_name() -
431 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 *
433 * Write out information from cpufreq_driver->policy[cpu]; object must be
434 * "unsigned int".
435 */
436
Dave Jones32ee8c32006-02-28 00:43:23 -0500437#define show_one(file_name, object) \
438static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500439(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500440{ \
Dave Jones29464f22009-01-18 01:37:11 -0500441 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444show_one(cpuinfo_min_freq, cpuinfo.min_freq);
445show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100446show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447show_one(scaling_min_freq, min);
448show_one(scaling_max_freq, max);
449show_one(scaling_cur_freq, cur);
450
Viresh Kumar037ce832013-10-02 14:13:16 +0530451static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530452 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454/**
455 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
456 */
457#define store_one(file_name, object) \
458static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500459(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530461 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct cpufreq_policy new_policy; \
463 \
464 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
465 if (ret) \
466 return -EINVAL; \
467 \
Dave Jones29464f22009-01-18 01:37:11 -0500468 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (ret != 1) \
470 return -EINVAL; \
471 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530472 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200473 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 \
475 return ret ? ret : count; \
476}
477
Dave Jones29464f22009-01-18 01:37:11 -0500478store_one(scaling_min_freq, min);
479store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481/**
482 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
483 */
Dave Jones905d77c2008-03-05 14:28:32 -0500484static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
485 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800487 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (!cur_freq)
489 return sprintf(buf, "<unknown>");
490 return sprintf(buf, "%u\n", cur_freq);
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/**
494 * show_scaling_governor - show the current policy for the specified CPU
495 */
Dave Jones905d77c2008-03-05 14:28:32 -0500496static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Dave Jones29464f22009-01-18 01:37:11 -0500498 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return sprintf(buf, "powersave\n");
500 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
501 return sprintf(buf, "performance\n");
502 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200503 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500504 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -EINVAL;
506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/**
509 * store_scaling_governor - store policy for the specified CPU
510 */
Dave Jones905d77c2008-03-05 14:28:32 -0500511static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
512 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530514 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 char str_governor[16];
516 struct cpufreq_policy new_policy;
517
518 ret = cpufreq_get_policy(&new_policy, policy->cpu);
519 if (ret)
520 return ret;
521
Dave Jones29464f22009-01-18 01:37:11 -0500522 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (ret != 1)
524 return -EINVAL;
525
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530526 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
527 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EINVAL;
529
Viresh Kumar037ce832013-10-02 14:13:16 +0530530 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200531
532 policy->user_policy.policy = policy->policy;
533 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200534
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530535 if (ret)
536 return ret;
537 else
538 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541/**
542 * show_scaling_driver - show the cpufreq driver currently loaded
543 */
Dave Jones905d77c2008-03-05 14:28:32 -0500544static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200546 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/**
550 * show_scaling_available_governors - show the available CPUfreq governors
551 */
Dave Jones905d77c2008-03-05 14:28:32 -0500552static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
553 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 ssize_t i = 0;
556 struct cpufreq_governor *t;
557
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530558 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 i += sprintf(buf, "performance powersave");
560 goto out;
561 }
562
563 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500564 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
565 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200567 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 i += sprintf(&buf[i], "\n");
571 return i;
572}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700573
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800574ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
576 ssize_t i = 0;
577 unsigned int cpu;
578
Rusty Russell835481d2009-01-04 05:18:06 -0800579 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (i)
581 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
582 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
583 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500584 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 i += sprintf(&buf[i], "\n");
587 return i;
588}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800589EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700591/**
592 * show_related_cpus - show the CPUs affected by each transition even if
593 * hw coordination is in use
594 */
595static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
596{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800597 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700598}
599
600/**
601 * show_affected_cpus - show the CPUs affected by each transition
602 */
603static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
604{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800605 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700606}
607
Venki Pallipadi9e769882007-10-26 10:18:21 -0700608static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500609 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700610{
611 unsigned int freq = 0;
612 unsigned int ret;
613
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700614 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700615 return -EINVAL;
616
617 ret = sscanf(buf, "%u", &freq);
618 if (ret != 1)
619 return -EINVAL;
620
621 policy->governor->store_setspeed(policy, freq);
622
623 return count;
624}
625
626static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
627{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700628 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700629 return sprintf(buf, "<unsupported>\n");
630
631 return policy->governor->show_setspeed(policy, buf);
632}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Thomas Renningere2f74f32009-11-19 12:31:01 +0100634/**
viresh kumar8bf1ac72012-10-23 01:23:33 +0200635 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100636 */
637static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
638{
639 unsigned int limit;
640 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200641 if (cpufreq_driver->bios_limit) {
642 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100643 if (!ret)
644 return sprintf(buf, "%u\n", limit);
645 }
646 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
647}
648
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200649cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
650cpufreq_freq_attr_ro(cpuinfo_min_freq);
651cpufreq_freq_attr_ro(cpuinfo_max_freq);
652cpufreq_freq_attr_ro(cpuinfo_transition_latency);
653cpufreq_freq_attr_ro(scaling_available_governors);
654cpufreq_freq_attr_ro(scaling_driver);
655cpufreq_freq_attr_ro(scaling_cur_freq);
656cpufreq_freq_attr_ro(bios_limit);
657cpufreq_freq_attr_ro(related_cpus);
658cpufreq_freq_attr_ro(affected_cpus);
659cpufreq_freq_attr_rw(scaling_min_freq);
660cpufreq_freq_attr_rw(scaling_max_freq);
661cpufreq_freq_attr_rw(scaling_governor);
662cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Dave Jones905d77c2008-03-05 14:28:32 -0500664static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 &cpuinfo_min_freq.attr,
666 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100667 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 &scaling_min_freq.attr,
669 &scaling_max_freq.attr,
670 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700671 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 &scaling_governor.attr,
673 &scaling_driver.attr,
674 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700675 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 NULL
677};
678
Dave Jones29464f22009-01-18 01:37:11 -0500679#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
680#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Dave Jones29464f22009-01-18 01:37:11 -0500682static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Dave Jones905d77c2008-03-05 14:28:32 -0500684 struct cpufreq_policy *policy = to_policy(kobj);
685 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530686 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530687
688 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530689 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800690
Viresh Kumar1b750e32013-10-02 14:13:09 +0530691 lock_policy_rwsem_read(policy->cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800692
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530693 if (fattr->show)
694 ret = fattr->show(policy, buf);
695 else
696 ret = -EIO;
697
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800698 unlock_policy_rwsem_read(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530699 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return ret;
702}
703
Dave Jones905d77c2008-03-05 14:28:32 -0500704static ssize_t store(struct kobject *kobj, struct attribute *attr,
705 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Dave Jones905d77c2008-03-05 14:28:32 -0500707 struct cpufreq_policy *policy = to_policy(kobj);
708 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500709 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530710
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530711 get_online_cpus();
712
713 if (!cpu_online(policy->cpu))
714 goto unlock;
715
Viresh Kumar6eed9402013-08-06 22:53:11 +0530716 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530717 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800718
Viresh Kumar1b750e32013-10-02 14:13:09 +0530719 lock_policy_rwsem_write(policy->cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800720
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530721 if (fattr->store)
722 ret = fattr->store(policy, buf, count);
723 else
724 ret = -EIO;
725
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800726 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530727
Viresh Kumar6eed9402013-08-06 22:53:11 +0530728 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530729unlock:
730 put_online_cpus();
731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return ret;
733}
734
Dave Jones905d77c2008-03-05 14:28:32 -0500735static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Dave Jones905d77c2008-03-05 14:28:32 -0500737 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200738 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 complete(&policy->kobj_unregister);
740}
741
Emese Revfy52cf25d2010-01-19 02:58:23 +0100742static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 .show = show,
744 .store = store,
745};
746
747static struct kobj_type ktype_cpufreq = {
748 .sysfs_ops = &sysfs_ops,
749 .default_attrs = default_attrs,
750 .release = cpufreq_sysfs_release,
751};
752
Viresh Kumar2361be22013-05-17 16:09:09 +0530753struct kobject *cpufreq_global_kobject;
754EXPORT_SYMBOL(cpufreq_global_kobject);
755
756static int cpufreq_global_kobject_usage;
757
758int cpufreq_get_global_kobject(void)
759{
760 if (!cpufreq_global_kobject_usage++)
761 return kobject_add(cpufreq_global_kobject,
762 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
763
764 return 0;
765}
766EXPORT_SYMBOL(cpufreq_get_global_kobject);
767
768void cpufreq_put_global_kobject(void)
769{
770 if (!--cpufreq_global_kobject_usage)
771 kobject_del(cpufreq_global_kobject);
772}
773EXPORT_SYMBOL(cpufreq_put_global_kobject);
774
775int cpufreq_sysfs_create_file(const struct attribute *attr)
776{
777 int ret = cpufreq_get_global_kobject();
778
779 if (!ret) {
780 ret = sysfs_create_file(cpufreq_global_kobject, attr);
781 if (ret)
782 cpufreq_put_global_kobject();
783 }
784
785 return ret;
786}
787EXPORT_SYMBOL(cpufreq_sysfs_create_file);
788
789void cpufreq_sysfs_remove_file(const struct attribute *attr)
790{
791 sysfs_remove_file(cpufreq_global_kobject, attr);
792 cpufreq_put_global_kobject();
793}
794EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
795
Dave Jones19d6f7e2009-07-08 17:35:39 -0400796/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200797static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400798{
799 unsigned int j;
800 int ret = 0;
801
802 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800803 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400804
Viresh Kumar308b60e2013-07-31 14:35:14 +0200805 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400806 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400807
Viresh Kumare8fdde12013-07-31 14:31:33 +0200808 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800809 cpu_dev = get_cpu_device(j);
810 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400811 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200812 if (ret)
813 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400814 }
815 return ret;
816}
817
Viresh Kumar308b60e2013-07-31 14:35:14 +0200818static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800819 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400820{
821 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400822 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400823
824 /* prepare interface data */
825 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800826 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400827 if (ret)
828 return ret;
829
830 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200831 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400832 while ((drv_attr) && (*drv_attr)) {
833 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
834 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200835 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400836 drv_attr++;
837 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200838 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400839 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
840 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200841 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400842 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530843 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400844 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
845 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200846 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400847 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200848 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100849 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
850 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200851 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100852 }
Dave Jones909a6942009-07-08 18:05:42 -0400853
Viresh Kumar308b60e2013-07-31 14:35:14 +0200854 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400855 if (ret)
856 goto err_out_kobj_put;
857
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530858 return ret;
859
860err_out_kobj_put:
861 kobject_put(&policy->kobj);
862 wait_for_completion(&policy->kobj_unregister);
863 return ret;
864}
865
866static void cpufreq_init_policy(struct cpufreq_policy *policy)
867{
868 struct cpufreq_policy new_policy;
869 int ret = 0;
870
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530871 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar037ce832013-10-02 14:13:16 +0530872 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400873 policy->governor = NULL;
874
875 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530876 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400877 policy->user_policy.policy = policy->policy;
878 policy->user_policy.governor = policy->governor;
879
880 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200881 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200882 if (cpufreq_driver->exit)
883 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400884 }
Dave Jones909a6942009-07-08 18:05:42 -0400885}
886
Viresh Kumarfcf80582013-01-29 14:39:08 +0000887#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200888static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
889 unsigned int cpu, struct device *dev,
890 bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000891{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530892 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000893 unsigned long flags;
894
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530895 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530896 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
897 if (ret) {
898 pr_err("%s: Failed to stop governor\n", __func__);
899 return ret;
900 }
901 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000902
Viresh Kumard8d3b472013-08-04 01:20:07 +0200903 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530904
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000905 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530906
Viresh Kumarfcf80582013-01-29 14:39:08 +0000907 cpumask_set_cpu(cpu, policy->cpus);
908 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000909 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000910
Viresh Kumard8d3b472013-08-04 01:20:07 +0200911 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530912
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530913 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530914 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
915 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
916 pr_err("%s: Failed to start governor\n", __func__);
917 return ret;
918 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200919 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000920
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530921 /* Don't touch sysfs links during light-weight init */
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200922 if (!frozen)
923 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000924
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530925 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000926}
927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530929static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
930{
931 struct cpufreq_policy *policy;
932 unsigned long flags;
933
Lan Tianyu44871c92013-09-11 15:05:05 +0800934 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530935
936 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
937
Lan Tianyu44871c92013-09-11 15:05:05 +0800938 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530939
940 return policy;
941}
942
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530943static struct cpufreq_policy *cpufreq_policy_alloc(void)
944{
945 struct cpufreq_policy *policy;
946
947 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
948 if (!policy)
949 return NULL;
950
951 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
952 goto err_free_policy;
953
954 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
955 goto err_free_cpumask;
956
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530957 INIT_LIST_HEAD(&policy->policy_list);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530958 return policy;
959
960err_free_cpumask:
961 free_cpumask_var(policy->cpus);
962err_free_policy:
963 kfree(policy);
964
965 return NULL;
966}
967
968static void cpufreq_policy_free(struct cpufreq_policy *policy)
969{
970 free_cpumask_var(policy->related_cpus);
971 free_cpumask_var(policy->cpus);
972 kfree(policy);
973}
974
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530975static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
976{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +0530977 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530978 return;
979
Viresh Kumar8efd5762013-09-17 10:22:11 +0530980 /*
981 * Take direct locks as lock_policy_rwsem_write wouldn't work here.
982 * Also lock for last cpu is enough here as contention will happen only
983 * after policy->cpu is changed and after it is changed, other threads
984 * will try to acquire lock for new cpu. And policy is already updated
985 * by then.
986 */
987 down_write(&per_cpu(cpu_policy_rwsem, policy->cpu));
988
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530989 policy->last_cpu = policy->cpu;
990 policy->cpu = cpu;
991
Viresh Kumar8efd5762013-09-17 10:22:11 +0530992 up_write(&per_cpu(cpu_policy_rwsem, policy->last_cpu));
993
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530994 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530995 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
996 CPUFREQ_UPDATE_POLICY_CPU, policy);
997}
998
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530999static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1000 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Viresh Kumarfcf80582013-01-29 14:39:08 +00001002 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301003 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001006#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301007 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001008 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001009#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Ashok Rajc32b6b82005-10-30 14:59:54 -08001011 if (cpu_is_offline(cpu))
1012 return 0;
1013
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001014 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016#ifdef CONFIG_SMP
1017 /* check whether a different CPU already registered this
1018 * CPU because it is in the same boat. */
1019 policy = cpufreq_cpu_get(cpu);
1020 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001021 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return 0;
1023 }
Li Zhong5025d622013-08-21 01:31:08 +02001024#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001025
Viresh Kumar6eed9402013-08-06 22:53:11 +05301026 if (!down_read_trylock(&cpufreq_rwsem))
1027 return 0;
1028
Viresh Kumarfcf80582013-01-29 14:39:08 +00001029#ifdef CONFIG_HOTPLUG_CPU
1030 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001031 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301032 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1033 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001034 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301035 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301036 up_read(&cpufreq_rwsem);
1037 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301038 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001039 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001040 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001041#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301043 if (frozen)
1044 /* Restore the saved policy when doing light-weight init */
1045 policy = cpufreq_policy_restore(cpu);
1046 else
1047 policy = cpufreq_policy_alloc();
1048
Dave Jones059019a2009-07-08 16:30:03 -04001049 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001051
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301052
1053 /*
1054 * In the resume path, since we restore a saved policy, the assignment
1055 * to policy->cpu is like an update of the existing policy, rather than
1056 * the creation of a brand new one. So we need to perform this update
1057 * by invoking update_policy_cpu().
1058 */
1059 if (frozen && cpu != policy->cpu)
1060 update_policy_cpu(policy, cpu);
1061 else
1062 policy->cpu = cpu;
1063
Viresh Kumar65922462013-02-07 10:56:03 +05301064 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001065 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001068 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 /* call driver. From then on the cpufreq must be able
1071 * to accept all calls to ->verify and ->setpolicy for this CPU
1072 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001073 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001075 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301076 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001078
Viresh Kumarda60ce92013-10-03 20:28:30 +05301079 if (cpufreq_driver->get) {
1080 policy->cur = cpufreq_driver->get(policy->cpu);
1081 if (!policy->cur) {
1082 pr_err("%s: ->get() failed\n", __func__);
1083 goto err_get_freq;
1084 }
1085 }
1086
Viresh Kumarfcf80582013-01-29 14:39:08 +00001087 /* related cpus should atleast have policy->cpus */
1088 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1089
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001090 /*
1091 * affected cpus must always be the one, which are online. We aren't
1092 * managing offline cpus here.
1093 */
1094 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1095
Mike Chan187d9f42008-12-04 12:19:17 -08001096 policy->user_policy.min = policy->min;
1097 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Thomas Renningera1531ac2008-07-29 22:32:58 -07001099 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1100 CPUFREQ_START, policy);
1101
Viresh Kumarfcf80582013-01-29 14:39:08 +00001102#ifdef CONFIG_HOTPLUG_CPU
1103 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1104 if (gov) {
1105 policy->governor = gov;
1106 pr_debug("Restoring governor %s for cpu %d\n",
1107 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001108 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001109#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301111 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301112 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301113 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301114 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1115
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301116 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001117 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301118 if (ret)
1119 goto err_out_unregister;
1120 }
Dave Jones8ff69732006-03-05 03:37:23 -05001121
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301122 write_lock_irqsave(&cpufreq_driver_lock, flags);
1123 list_add(&policy->policy_list, &cpufreq_policy_list);
1124 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1125
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301126 cpufreq_init_policy(policy);
1127
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001128 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301129 up_read(&cpufreq_rwsem);
1130
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001131 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return 0;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001136 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301137 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001138 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001139 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Viresh Kumarda60ce92013-10-03 20:28:30 +05301141err_get_freq:
1142 if (cpufreq_driver->exit)
1143 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301144err_set_policy_cpu:
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301145 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301147 up_read(&cpufreq_rwsem);
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return ret;
1150}
1151
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301152/**
1153 * cpufreq_add_dev - add a CPU device
1154 *
1155 * Adds the cpufreq interface for a CPU device.
1156 *
1157 * The Oracle says: try running cpufreq registration/unregistration concurrently
1158 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1159 * mess up, but more thorough testing is needed. - Mathieu
1160 */
1161static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1162{
1163 return __cpufreq_add_dev(dev, sif, false);
1164}
1165
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301166static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301167 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301168{
1169 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301170 int ret;
1171
1172 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301173 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301174
1175 /* Don't touch sysfs files during light-weight tear-down */
1176 if (frozen)
1177 return cpu_dev->id;
1178
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301179 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301180 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301181 if (ret) {
1182 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1183
Viresh Kumar1b750e32013-10-02 14:13:09 +05301184 lock_policy_rwsem_write(old_cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301185 cpumask_set_cpu(old_cpu, policy->cpus);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301186 unlock_policy_rwsem_write(old_cpu);
1187
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301188 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301189 "cpufreq");
1190
1191 return -EINVAL;
1192 }
1193
1194 return cpu_dev->id;
1195}
1196
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301197static int __cpufreq_remove_dev_prepare(struct device *dev,
1198 struct subsys_interface *sif,
1199 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301201 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301202 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301204 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001206 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001208 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301210 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301212 /* Save the policy somewhere when doing a light-weight tear-down */
1213 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301214 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301215
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001216 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301218 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001219 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301223 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301224 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1225 if (ret) {
1226 pr_err("%s: Failed to stop governor\n", __func__);
1227 return ret;
1228 }
1229 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001230
Jacob Shin27ecddc2011-04-27 13:32:11 -05001231#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001232 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001233 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301234 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001235#endif
1236
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301237 lock_policy_rwsem_read(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301238 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301239 unlock_policy_rwsem_read(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301241 if (cpu != policy->cpu) {
1242 if (!frozen)
1243 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001244 } else if (cpus > 1) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301245 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301246 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301247 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301248
1249 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301250 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1251 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301252 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001253 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001254 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001255
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301256 return 0;
1257}
1258
1259static int __cpufreq_remove_dev_finish(struct device *dev,
1260 struct subsys_interface *sif,
1261 bool frozen)
1262{
1263 unsigned int cpu = dev->id, cpus;
1264 int ret;
1265 unsigned long flags;
1266 struct cpufreq_policy *policy;
1267 struct kobject *kobj;
1268 struct completion *cmp;
1269
1270 read_lock_irqsave(&cpufreq_driver_lock, flags);
1271 policy = per_cpu(cpufreq_cpu_data, cpu);
1272 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1273
1274 if (!policy) {
1275 pr_debug("%s: No cpu_data found\n", __func__);
1276 return -EINVAL;
1277 }
1278
Viresh Kumar1b750e32013-10-02 14:13:09 +05301279 lock_policy_rwsem_write(cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301280 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301281
1282 if (cpus > 1)
1283 cpumask_clear_cpu(cpu, policy->cpus);
1284 unlock_policy_rwsem_write(cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301285
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001286 /* If cpu is last user of policy, free policy */
1287 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301288 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301289 ret = __cpufreq_governor(policy,
1290 CPUFREQ_GOV_POLICY_EXIT);
1291 if (ret) {
1292 pr_err("%s: Failed to exit governor\n",
1293 __func__);
1294 return ret;
1295 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301296 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001297
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301298 if (!frozen) {
1299 lock_policy_rwsem_read(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301300 kobj = &policy->kobj;
1301 cmp = &policy->kobj_unregister;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301302 unlock_policy_rwsem_read(cpu);
1303 kobject_put(kobj);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001304
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301305 /*
1306 * We need to make sure that the underlying kobj is
1307 * actually not referenced anymore by anybody before we
1308 * proceed with unloading.
1309 */
1310 pr_debug("waiting for dropping of refcount\n");
1311 wait_for_completion(cmp);
1312 pr_debug("wait complete\n");
1313 }
1314
1315 /*
1316 * Perform the ->exit() even during light-weight tear-down,
1317 * since this is a core component, and is essential for the
1318 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001319 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001320 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301321 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001322
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301323 /* Remove policy from list of active policies */
1324 write_lock_irqsave(&cpufreq_driver_lock, flags);
1325 list_del(&policy->policy_list);
1326 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1327
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301328 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301329 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001330 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301331 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301332 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1333 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1334 pr_err("%s: Failed to start governor\n",
1335 __func__);
1336 return ret;
1337 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001338 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Viresh Kumar474deff2013-08-20 12:08:25 +05301341 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return 0;
1343}
1344
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301345/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301346 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301347 *
1348 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301349 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001350static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001351{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001352 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301353 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001354
1355 if (cpu_is_offline(cpu))
1356 return 0;
1357
Viresh Kumar27a862e2013-10-02 14:13:14 +05301358 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1359
1360 if (!ret)
1361 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1362
1363 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001364}
1365
David Howells65f27f32006-11-22 14:55:48 +00001366static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
David Howells65f27f32006-11-22 14:55:48 +00001368 struct cpufreq_policy *policy =
1369 container_of(work, struct cpufreq_policy, update);
1370 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001371 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 cpufreq_update_policy(cpu);
1373}
1374
1375/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301376 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1377 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * @cpu: cpu number
1379 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1380 * @new_freq: CPU frequency the CPU actually runs at
1381 *
Dave Jones29464f22009-01-18 01:37:11 -05001382 * We adjust to current frequency first, and need to clean up later.
1383 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301385static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1386 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301388 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301390 unsigned long flags;
1391
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001392 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 freqs.old = old_freq;
1396 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301397
1398 read_lock_irqsave(&cpufreq_driver_lock, flags);
1399 policy = per_cpu(cpufreq_cpu_data, cpu);
1400 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1401
1402 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1403 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
Dave Jones32ee8c32006-02-28 00:43:23 -05001406/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301407 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001408 * @cpu: CPU number
1409 *
1410 * This is the last known freq, without actually getting it from the driver.
1411 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1412 */
1413unsigned int cpufreq_quick_get(unsigned int cpu)
1414{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001415 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301416 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001417
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001418 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1419 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001420
1421 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001422 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301423 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001424 cpufreq_cpu_put(policy);
1425 }
1426
Dave Jones4d34a672008-02-07 16:33:49 -05001427 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001428}
1429EXPORT_SYMBOL(cpufreq_quick_get);
1430
Jesse Barnes3d737102011-06-28 10:59:12 -07001431/**
1432 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1433 * @cpu: CPU number
1434 *
1435 * Just return the max possible frequency for a given CPU.
1436 */
1437unsigned int cpufreq_quick_get_max(unsigned int cpu)
1438{
1439 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1440 unsigned int ret_freq = 0;
1441
1442 if (policy) {
1443 ret_freq = policy->max;
1444 cpufreq_cpu_put(policy);
1445 }
1446
1447 return ret_freq;
1448}
1449EXPORT_SYMBOL(cpufreq_quick_get_max);
1450
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001451static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001453 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301454 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001456 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001457 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001459 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301461 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001462 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301463 /* verify no discrepancy between actual and
1464 saved value exists */
1465 if (unlikely(ret_freq != policy->cur)) {
1466 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 schedule_work(&policy->update);
1468 }
1469 }
1470
Dave Jones4d34a672008-02-07 16:33:49 -05001471 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001472}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001474/**
1475 * cpufreq_get - get the current CPU frequency (in kHz)
1476 * @cpu: CPU number
1477 *
1478 * Get the CPU current (static) CPU frequency
1479 */
1480unsigned int cpufreq_get(unsigned int cpu)
1481{
1482 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001483
Viresh Kumar26ca8692013-09-20 22:37:31 +05301484 if (cpufreq_disabled() || !cpufreq_driver)
1485 return -ENOENT;
1486
Viresh Kumar6eed9402013-08-06 22:53:11 +05301487 if (!down_read_trylock(&cpufreq_rwsem))
1488 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001489
Viresh Kumar1b750e32013-10-02 14:13:09 +05301490 lock_policy_rwsem_read(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001491
1492 ret_freq = __cpufreq_get(cpu);
1493
1494 unlock_policy_rwsem_read(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301495 up_read(&cpufreq_rwsem);
1496
Dave Jones4d34a672008-02-07 16:33:49 -05001497 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
1499EXPORT_SYMBOL(cpufreq_get);
1500
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001501static struct subsys_interface cpufreq_interface = {
1502 .name = "cpufreq",
1503 .subsys = &cpu_subsys,
1504 .add_dev = cpufreq_add_dev,
1505 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001506};
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001509 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1510 *
1511 * This function is only executed for the boot processor. The other CPUs
1512 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001513 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001514static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001515{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301516 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001517
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001518 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301519 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001520
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001521 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001522
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001523 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301524 policy = cpufreq_cpu_get(cpu);
1525 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001526 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001527
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001528 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301529 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001530 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001531 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301532 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001533 }
1534
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301535 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001536 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001537}
1538
1539/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001540 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 *
1542 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001543 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1544 * restored. It will verify that the current freq is in sync with
1545 * what we believe it to be. This is a bit later than when it
1546 * should be, but nonethteless it's better than calling
1547 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001548 *
1549 * This function is only executed for the boot CPU. The other CPUs have not
1550 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001552static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301554 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001555
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001556 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301557 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001559 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001561 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301562 policy = cpufreq_cpu_get(cpu);
1563 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001564 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001566 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301567 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (ret) {
1569 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301570 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001571 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573 }
1574
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301575 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001576
Dave Jonesc9060492008-02-07 16:32:18 -05001577fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301578 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001581static struct syscore_ops cpufreq_syscore_ops = {
1582 .suspend = cpufreq_bp_suspend,
1583 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584};
1585
Borislav Petkov9d950462013-01-20 10:24:28 +00001586/**
1587 * cpufreq_get_current_driver - return current driver's name
1588 *
1589 * Return the name string of the currently loaded cpufreq driver
1590 * or NULL, if none.
1591 */
1592const char *cpufreq_get_current_driver(void)
1593{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001594 if (cpufreq_driver)
1595 return cpufreq_driver->name;
1596
1597 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001598}
1599EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601/*********************************************************************
1602 * NOTIFIER LISTS INTERFACE *
1603 *********************************************************************/
1604
1605/**
1606 * cpufreq_register_notifier - register a driver with cpufreq
1607 * @nb: notifier function to register
1608 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1609 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001610 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 * are notified about clock rate changes (once before and once after
1612 * the transition), or a list of drivers that are notified about
1613 * changes in cpufreq policy.
1614 *
1615 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001616 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 */
1618int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1619{
1620 int ret;
1621
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001622 if (cpufreq_disabled())
1623 return -EINVAL;
1624
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001625 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 switch (list) {
1628 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001629 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001630 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001633 ret = blocking_notifier_chain_register(
1634 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 break;
1636 default:
1637 ret = -EINVAL;
1638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 return ret;
1641}
1642EXPORT_SYMBOL(cpufreq_register_notifier);
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644/**
1645 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1646 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301647 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 *
1649 * Remove a driver from the CPU frequency notifier list.
1650 *
1651 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001652 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
1654int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1655{
1656 int ret;
1657
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001658 if (cpufreq_disabled())
1659 return -EINVAL;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 switch (list) {
1662 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001663 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001664 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001667 ret = blocking_notifier_chain_unregister(
1668 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 break;
1670 default:
1671 ret = -EINVAL;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 return ret;
1675}
1676EXPORT_SYMBOL(cpufreq_unregister_notifier);
1677
1678
1679/*********************************************************************
1680 * GOVERNORS *
1681 *********************************************************************/
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683int __cpufreq_driver_target(struct cpufreq_policy *policy,
1684 unsigned int target_freq,
1685 unsigned int relation)
1686{
1687 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001688 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001689
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001690 if (cpufreq_disabled())
1691 return -ENODEV;
1692
Viresh Kumar72499242012-10-31 01:28:21 +01001693 /* Make sure that target_freq is within supported range */
1694 if (target_freq > policy->max)
1695 target_freq = policy->max;
1696 if (target_freq < policy->min)
1697 target_freq = policy->min;
1698
1699 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1700 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001701
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301702 /*
1703 * This might look like a redundant call as we are checking it again
1704 * after finding index. But it is left intentionally for cases where
1705 * exactly same freq is called again and so we can save on few function
1706 * calls.
1707 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001708 if (target_freq == policy->cur)
1709 return 0;
1710
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001711 if (cpufreq_driver->target)
1712 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301713 else if (cpufreq_driver->target_index) {
1714 struct cpufreq_frequency_table *freq_table;
1715 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001716
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301717 freq_table = cpufreq_frequency_get_table(policy->cpu);
1718 if (unlikely(!freq_table)) {
1719 pr_err("%s: Unable to find freq_table\n", __func__);
1720 goto out;
1721 }
1722
1723 retval = cpufreq_frequency_table_target(policy, freq_table,
1724 target_freq, relation, &index);
1725 if (unlikely(retval)) {
1726 pr_err("%s: Unable to find matching freq\n", __func__);
1727 goto out;
1728 }
1729
1730 if (freq_table[index].frequency == policy->cur)
1731 retval = 0;
1732 else
1733 retval = cpufreq_driver->target_index(policy, index);
1734 }
1735
1736out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return retval;
1738}
1739EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741int cpufreq_driver_target(struct cpufreq_policy *policy,
1742 unsigned int target_freq,
1743 unsigned int relation)
1744{
Julia Lawallf1829e42008-07-25 22:44:53 +02001745 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Viresh Kumar1b750e32013-10-02 14:13:09 +05301747 lock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 ret = __cpufreq_driver_target(policy, target_freq, relation);
1750
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001751 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return ret;
1754}
1755EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1756
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001757/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001758 * when "event" is CPUFREQ_GOV_LIMITS
1759 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301761static int __cpufreq_governor(struct cpufreq_policy *policy,
1762 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Dave Jonescc993ca2005-07-28 09:43:56 -07001764 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001765
1766 /* Only must be defined when default governor is known to have latency
1767 restrictions, like e.g. conservative or ondemand.
1768 That this is the case is already ensured in Kconfig
1769 */
1770#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1771 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1772#else
1773 struct cpufreq_governor *gov = NULL;
1774#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001775
1776 if (policy->governor->max_transition_latency &&
1777 policy->cpuinfo.transition_latency >
1778 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001779 if (!gov)
1780 return -EINVAL;
1781 else {
1782 printk(KERN_WARNING "%s governor failed, too long"
1783 " transition latency of HW, fallback"
1784 " to %s governor\n",
1785 policy->governor->name,
1786 gov->name);
1787 policy->governor = gov;
1788 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Viresh Kumarfe492f32013-08-06 22:53:10 +05301791 if (event == CPUFREQ_GOV_POLICY_INIT)
1792 if (!try_module_get(policy->governor->owner))
1793 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001795 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301796 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001797
1798 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301799 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301800 || (!policy->governor_enabled
1801 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001802 mutex_unlock(&cpufreq_governor_lock);
1803 return -EBUSY;
1804 }
1805
1806 if (event == CPUFREQ_GOV_STOP)
1807 policy->governor_enabled = false;
1808 else if (event == CPUFREQ_GOV_START)
1809 policy->governor_enabled = true;
1810
1811 mutex_unlock(&cpufreq_governor_lock);
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ret = policy->governor->governor(policy, event);
1814
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001815 if (!ret) {
1816 if (event == CPUFREQ_GOV_POLICY_INIT)
1817 policy->governor->initialized++;
1818 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1819 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001820 } else {
1821 /* Restore original values */
1822 mutex_lock(&cpufreq_governor_lock);
1823 if (event == CPUFREQ_GOV_STOP)
1824 policy->governor_enabled = true;
1825 else if (event == CPUFREQ_GOV_START)
1826 policy->governor_enabled = false;
1827 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001828 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001829
Viresh Kumarfe492f32013-08-06 22:53:10 +05301830 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1831 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 module_put(policy->governor->owner);
1833
1834 return ret;
1835}
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837int cpufreq_register_governor(struct cpufreq_governor *governor)
1838{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001839 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 if (!governor)
1842 return -EINVAL;
1843
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001844 if (cpufreq_disabled())
1845 return -ENODEV;
1846
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001847 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001848
Viresh Kumarb3940582013-02-01 05:42:58 +00001849 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001850 err = -EBUSY;
1851 if (__find_governor(governor->name) == NULL) {
1852 err = 0;
1853 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Dave Jones32ee8c32006-02-28 00:43:23 -05001856 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001857 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1862{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001863#ifdef CONFIG_HOTPLUG_CPU
1864 int cpu;
1865#endif
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 if (!governor)
1868 return;
1869
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001870 if (cpufreq_disabled())
1871 return;
1872
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001873#ifdef CONFIG_HOTPLUG_CPU
1874 for_each_present_cpu(cpu) {
1875 if (cpu_online(cpu))
1876 continue;
1877 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1878 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1879 }
1880#endif
1881
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001882 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001884 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return;
1886}
1887EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1888
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890/*********************************************************************
1891 * POLICY INTERFACE *
1892 *********************************************************************/
1893
1894/**
1895 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001896 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1897 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 *
1899 * Reads the current cpufreq policy.
1900 */
1901int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1902{
1903 struct cpufreq_policy *cpu_policy;
1904 if (!policy)
1905 return -EINVAL;
1906
1907 cpu_policy = cpufreq_cpu_get(cpu);
1908 if (!cpu_policy)
1909 return -EINVAL;
1910
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301911 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
1913 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 return 0;
1915}
1916EXPORT_SYMBOL(cpufreq_get_policy);
1917
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001918/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301919 * policy : current policy.
1920 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001921 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301922static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301923 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001925 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301927 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1928 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301930 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301932 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001933 ret = -EINVAL;
1934 goto error_out;
1935 }
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301938 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (ret)
1940 goto error_out;
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001943 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301944 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001947 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301948 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Viresh Kumarbb176f72013-06-19 14:19:33 +05301950 /*
1951 * verify the cpu speed can be set within this limit, which might be
1952 * different to the first one
1953 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301954 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001955 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001959 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301960 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301962 policy->min = new_policy->min;
1963 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001965 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301966 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001968 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301969 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001970 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301971 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301973 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301975 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001977 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301980 if (policy->governor) {
1981 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1982 unlock_policy_rwsem_write(new_policy->cpu);
1983 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001984 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301985 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
1988 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301989 policy->governor = new_policy->governor;
1990 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1991 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001992 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001993 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301994 unlock_policy_rwsem_write(new_policy->cpu);
1995 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001996 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301997 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar955ef482013-05-16 05:09:58 +00001998 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001999 }
2000
2001 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002003 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302004 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302006 policy->governor = old_gov;
2007 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002008 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302009 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302010 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012 ret = -EINVAL;
2013 goto error_out;
2014 }
2015 /* might be a policy change, too, so fall through */
2016 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002017 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302018 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Dave Jones7d5e3502006-02-02 17:03:42 -05002021error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 return ret;
2023}
2024
2025/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2027 * @cpu: CPU which shall be re-evaluated
2028 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002029 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 * at different times.
2031 */
2032int cpufreq_update_policy(unsigned int cpu)
2033{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302034 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2035 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002036 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302038 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002039 ret = -ENODEV;
2040 goto no_policy;
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Viresh Kumar1b750e32013-10-02 14:13:09 +05302043 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002045 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302046 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302047 new_policy.min = policy->user_policy.min;
2048 new_policy.max = policy->user_policy.max;
2049 new_policy.policy = policy->user_policy.policy;
2050 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Viresh Kumarbb176f72013-06-19 14:19:33 +05302052 /*
2053 * BIOS might change freq behind our back
2054 * -> ask driver for current freq and notify governors about a change
2055 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002056 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302057 new_policy.cur = cpufreq_driver->get(cpu);
2058 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002059 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302060 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002061 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302062 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302063 cpufreq_out_of_sync(cpu, policy->cur,
2064 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002065 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002066 }
2067
Viresh Kumar037ce832013-10-02 14:13:16 +05302068 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002070 unlock_policy_rwsem_write(cpu);
2071
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302072 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002073no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return ret;
2075}
2076EXPORT_SYMBOL(cpufreq_update_policy);
2077
Paul Gortmaker27609842013-06-19 13:54:04 -04002078static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002079 unsigned long action, void *hcpu)
2080{
2081 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002082 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302083 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002084
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002085 dev = get_cpu_device(cpu);
2086 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302087
2088 if (action & CPU_TASKS_FROZEN)
2089 frozen = true;
2090
2091 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002092 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302093 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302094 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002095 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302096
Ashok Rajc32b6b82005-10-30 14:59:54 -08002097 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302098 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302099 break;
2100
2101 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302102 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002103 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302104
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002105 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302106 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002107 break;
2108 }
2109 }
2110 return NOTIFY_OK;
2111}
2112
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002113static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302114 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002115};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117/*********************************************************************
2118 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2119 *********************************************************************/
2120
2121/**
2122 * cpufreq_register_driver - register a CPU Frequency driver
2123 * @driver_data: A struct cpufreq_driver containing the values#
2124 * submitted by the CPU Frequency driver.
2125 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302126 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002128 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 *
2130 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002131int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
2133 unsigned long flags;
2134 int ret;
2135
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002136 if (cpufreq_disabled())
2137 return -ENODEV;
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302140 !(driver_data->setpolicy || driver_data->target_index ||
2141 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return -EINVAL;
2143
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002144 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 if (driver_data->setpolicy)
2147 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2148
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002149 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002150 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002151 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea5802013-09-18 21:05:20 -07002152 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002154 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002155 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002157 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002158 if (ret)
2159 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002161 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int i;
2163 ret = -ENODEV;
2164
2165 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002166 for (i = 0; i < nr_cpu_ids; i++)
2167 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002169 break;
2170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /* if all ->init() calls failed, unregister */
2173 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002174 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302175 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002176 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
2178 }
2179
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002180 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002181 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002183 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002184err_if_unreg:
2185 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002186err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002187 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002188 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002189 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002190 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191}
2192EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194/**
2195 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2196 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302197 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 * the right to do so, i.e. if you have succeeded in initialising before!
2199 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2200 * currently not initialised.
2201 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002202int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
2204 unsigned long flags;
2205
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002206 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002209 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002211 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002212 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Viresh Kumar6eed9402013-08-06 22:53:11 +05302214 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002215 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302216
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002217 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302218
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002219 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302220 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 return 0;
2223}
2224EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002225
2226static int __init cpufreq_core_init(void)
2227{
2228 int cpu;
2229
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002230 if (cpufreq_disabled())
2231 return -ENODEV;
2232
Viresh Kumar474deff2013-08-20 12:08:25 +05302233 for_each_possible_cpu(cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002234 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002235
Viresh Kumar2361be22013-05-17 16:09:09 +05302236 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002237 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002238 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002239
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002240 return 0;
2241}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002242core_initcall(cpufreq_core_init);