blob: 985f325adc4f0dac90471e3ac095f3561f725e4f [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
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080050/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080062 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040064 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066 */
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080067static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
68
69#define lock_policy_rwsem(mode, cpu) \
Viresh Kumar1b750e32013-10-02 14:13:09 +053070static void lock_policy_rwsem_##mode(int cpu) \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053072 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
73 BUG_ON(!policy); \
74 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075}
76
77lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080078lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080079
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053080#define unlock_policy_rwsem(mode, cpu) \
81static void unlock_policy_rwsem_##mode(int cpu) \
82{ \
Viresh Kumar474deff2013-08-20 12:08:25 +053083 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
84 BUG_ON(!policy); \
85 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080086}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080087
Viresh Kumarfa1d8af2013-02-07 15:38:42 +053088unlock_policy_rwsem(read, cpu);
89unlock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
Viresh Kumar6eed9402013-08-06 22:53:11 +053091/*
92 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
93 * sections
94 */
95static DECLARE_RWSEM(cpufreq_rwsem);
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050098static int __cpufreq_governor(struct cpufreq_policy *policy,
99 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800100static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000101static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500104 * Two notifier lists: the "policy" list is involved in the
105 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * "transition" list for kernel code that needs to handle
107 * changes to devices when the CPU clock speed changes.
108 * The mutex locks both lists.
109 */
Alan Sterne041c682006-03-27 01:16:30 -0800110static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700111static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200113static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700114static int __init init_cpufreq_transition_notifier_list(void)
115{
116 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200117 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700118 return 0;
119}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800120pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400122static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200123static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400124{
125 return off;
126}
127void disable_cpufreq(void)
128{
129 off = 1;
130}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500132static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000134bool have_governor_per_policy(void)
135{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530136 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000137}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000138EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000139
Viresh Kumar944e9a02013-05-16 05:09:57 +0000140struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
141{
142 if (have_governor_per_policy())
143 return &policy->kobj;
144 else
145 return cpufreq_global_kobject;
146}
147EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
148
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000149static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
150{
151 u64 idle_time;
152 u64 cur_wall_time;
153 u64 busy_time;
154
155 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
156
157 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
158 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
159 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
163
164 idle_time = cur_wall_time - busy_time;
165 if (wall)
166 *wall = cputime_to_usecs(cur_wall_time);
167
168 return cputime_to_usecs(idle_time);
169}
170
171u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
172{
173 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
174
175 if (idle_time == -1ULL)
176 return get_cpu_idle_time_jiffy(cpu, wall);
177 else if (!io_busy)
178 idle_time += get_cpu_iowait_time_us(cpu, wall);
179
180 return idle_time;
181}
182EXPORT_SYMBOL_GPL(get_cpu_idle_time);
183
Viresh Kumar70e9e772013-10-03 20:29:07 +0530184/*
185 * This is a generic cpufreq init() routine which can be used by cpufreq
186 * drivers of SMP systems. It will do following:
187 * - validate & show freq table passed
188 * - set policies transition latency
189 * - policy->cpus with all possible CPUs
190 */
191int cpufreq_generic_init(struct cpufreq_policy *policy,
192 struct cpufreq_frequency_table *table,
193 unsigned int transition_latency)
194{
195 int ret;
196
197 ret = cpufreq_table_validate_and_show(policy, table);
198 if (ret) {
199 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
200 return ret;
201 }
202
203 policy->cpuinfo.transition_latency = transition_latency;
204
205 /*
206 * The driver only supports the SMP configuartion where all processors
207 * share the clock and voltage and clock.
208 */
209 cpumask_setall(policy->cpus);
210
211 return 0;
212}
213EXPORT_SYMBOL_GPL(cpufreq_generic_init);
214
Viresh Kumar6eed9402013-08-06 22:53:11 +0530215struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530217 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 unsigned long flags;
219
Viresh Kumar6eed9402013-08-06 22:53:11 +0530220 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
221 return NULL;
222
223 if (!down_read_trylock(&cpufreq_rwsem))
224 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000227 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Viresh Kumar6eed9402013-08-06 22:53:11 +0530229 if (cpufreq_driver) {
230 /* get the CPU */
231 policy = per_cpu(cpufreq_cpu_data, cpu);
232 if (policy)
233 kobject_get(&policy->kobj);
234 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200235
Viresh Kumar6eed9402013-08-06 22:53:11 +0530236 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530238 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530239 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530241 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
244
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530245void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000247 if (cpufreq_disabled())
248 return;
249
Viresh Kumar6eed9402013-08-06 22:53:11 +0530250 kobject_put(&policy->kobj);
251 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
257 *********************************************************************/
258
259/**
260 * adjust_jiffies - adjust the system "loops_per_jiffy"
261 *
262 * This function alters the system "loops_per_jiffy" for the clock
263 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500264 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 * per-CPU loops_per_jiffy value wherever possible.
266 */
267#ifndef CONFIG_SMP
268static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530269static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Arjan van de Ven858119e2006-01-14 13:20:43 -0800271static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 if (ci->flags & CPUFREQ_CONST_LOOPS)
274 return;
275
276 if (!l_p_j_ref_freq) {
277 l_p_j_ref = loops_per_jiffy;
278 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200279 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530280 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530282 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700283 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530284 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
285 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200286 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530287 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289}
290#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530291static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
292{
293 return;
294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295#endif
296
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530297static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530298 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 BUG_ON(irqs_disabled());
301
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000302 if (cpufreq_disabled())
303 return;
304
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200305 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200306 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800307 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500312 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800313 * which is not equal to what the cpufreq core thinks is
314 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200316 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800317 if ((policy) && (policy->cpu == freqs->cpu) &&
318 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200319 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800320 " %u, cpufreq assumed %u kHz.\n",
321 freqs->old, policy->cur);
322 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700325 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800326 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
328 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 case CPUFREQ_POSTCHANGE:
331 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200332 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200333 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100334 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700335 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800336 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800337 if (likely(policy) && likely(policy->cpu == freqs->cpu))
338 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 break;
340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530342
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530343/**
344 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
345 * on frequency transition.
346 *
347 * This function calls the transition notifiers and the "adjust_jiffies"
348 * function. It is called twice on all CPU frequency changes that have
349 * external effects.
350 */
351void cpufreq_notify_transition(struct cpufreq_policy *policy,
352 struct cpufreq_freqs *freqs, unsigned int state)
353{
354 for_each_cpu(freqs->cpu, policy->cpus)
355 __cpufreq_notify_transition(policy, freqs, state);
356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
358
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*********************************************************************
361 * SYSFS INTERFACE *
362 *********************************************************************/
363
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700364static struct cpufreq_governor *__find_governor(const char *str_governor)
365{
366 struct cpufreq_governor *t;
367
368 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500369 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700370 return t;
371
372 return NULL;
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
376 * cpufreq_parse_governor - parse a governor string
377 */
Dave Jones905d77c2008-03-05 14:28:32 -0500378static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 struct cpufreq_governor **governor)
380{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381 int err = -EINVAL;
382
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200383 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700384 goto out;
385
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200386 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
388 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700389 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530390 } else if (!strnicmp(str_governor, "powersave",
391 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700393 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200395 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700397
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800398 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700399
400 t = __find_governor(str_governor);
401
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700402 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700403 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700404
Kees Cook1a8e1462011-05-04 08:38:56 -0700405 mutex_unlock(&cpufreq_governor_mutex);
406 ret = request_module("cpufreq_%s", str_governor);
407 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700408
Kees Cook1a8e1462011-05-04 08:38:56 -0700409 if (ret == 0)
410 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700411 }
412
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700413 if (t != NULL) {
414 *governor = t;
415 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700417
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800418 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Dave Jones29464f22009-01-18 01:37:11 -0500420out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700421 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530425 * cpufreq_per_cpu_attr_read() / show_##file_name() -
426 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
428 * Write out information from cpufreq_driver->policy[cpu]; object must be
429 * "unsigned int".
430 */
431
Dave Jones32ee8c32006-02-28 00:43:23 -0500432#define show_one(file_name, object) \
433static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500434(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500435{ \
Dave Jones29464f22009-01-18 01:37:11 -0500436 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439show_one(cpuinfo_min_freq, cpuinfo.min_freq);
440show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100441show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442show_one(scaling_min_freq, min);
443show_one(scaling_max_freq, max);
444show_one(scaling_cur_freq, cur);
445
Viresh Kumar037ce832013-10-02 14:13:16 +0530446static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530447 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/**
450 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
451 */
452#define store_one(file_name, object) \
453static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500454(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530456 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct cpufreq_policy new_policy; \
458 \
459 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
460 if (ret) \
461 return -EINVAL; \
462 \
Dave Jones29464f22009-01-18 01:37:11 -0500463 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (ret != 1) \
465 return -EINVAL; \
466 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530467 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200468 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 \
470 return ret ? ret : count; \
471}
472
Dave Jones29464f22009-01-18 01:37:11 -0500473store_one(scaling_min_freq, min);
474store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476/**
477 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
478 */
Dave Jones905d77c2008-03-05 14:28:32 -0500479static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
480 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800482 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!cur_freq)
484 return sprintf(buf, "<unknown>");
485 return sprintf(buf, "%u\n", cur_freq);
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/**
489 * show_scaling_governor - show the current policy for the specified CPU
490 */
Dave Jones905d77c2008-03-05 14:28:32 -0500491static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Dave Jones29464f22009-01-18 01:37:11 -0500493 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return sprintf(buf, "powersave\n");
495 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
496 return sprintf(buf, "performance\n");
497 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200498 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500499 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -EINVAL;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/**
504 * store_scaling_governor - store policy for the specified CPU
505 */
Dave Jones905d77c2008-03-05 14:28:32 -0500506static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
507 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530509 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 char str_governor[16];
511 struct cpufreq_policy new_policy;
512
513 ret = cpufreq_get_policy(&new_policy, policy->cpu);
514 if (ret)
515 return ret;
516
Dave Jones29464f22009-01-18 01:37:11 -0500517 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (ret != 1)
519 return -EINVAL;
520
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530521 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
522 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EINVAL;
524
Viresh Kumar037ce832013-10-02 14:13:16 +0530525 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200526
527 policy->user_policy.policy = policy->policy;
528 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200529
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530530 if (ret)
531 return ret;
532 else
533 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536/**
537 * show_scaling_driver - show the cpufreq driver currently loaded
538 */
Dave Jones905d77c2008-03-05 14:28:32 -0500539static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200541 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/**
545 * show_scaling_available_governors - show the available CPUfreq governors
546 */
Dave Jones905d77c2008-03-05 14:28:32 -0500547static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
548 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 ssize_t i = 0;
551 struct cpufreq_governor *t;
552
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200553 if (!cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 i += sprintf(buf, "performance powersave");
555 goto out;
556 }
557
558 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500559 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
560 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200562 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500564out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 i += sprintf(&buf[i], "\n");
566 return i;
567}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700568
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800569ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 ssize_t i = 0;
572 unsigned int cpu;
573
Rusty Russell835481d2009-01-04 05:18:06 -0800574 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (i)
576 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
577 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
578 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500579 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581 i += sprintf(&buf[i], "\n");
582 return i;
583}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800584EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700586/**
587 * show_related_cpus - show the CPUs affected by each transition even if
588 * hw coordination is in use
589 */
590static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
591{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800592 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700593}
594
595/**
596 * show_affected_cpus - show the CPUs affected by each transition
597 */
598static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
599{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800600 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700601}
602
Venki Pallipadi9e769882007-10-26 10:18:21 -0700603static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500604 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700605{
606 unsigned int freq = 0;
607 unsigned int ret;
608
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700609 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700610 return -EINVAL;
611
612 ret = sscanf(buf, "%u", &freq);
613 if (ret != 1)
614 return -EINVAL;
615
616 policy->governor->store_setspeed(policy, freq);
617
618 return count;
619}
620
621static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
622{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700623 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700624 return sprintf(buf, "<unsupported>\n");
625
626 return policy->governor->show_setspeed(policy, buf);
627}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Thomas Renningere2f74f32009-11-19 12:31:01 +0100629/**
viresh kumar8bf1ac72012-10-23 01:23:33 +0200630 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100631 */
632static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
633{
634 unsigned int limit;
635 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200636 if (cpufreq_driver->bios_limit) {
637 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100638 if (!ret)
639 return sprintf(buf, "%u\n", limit);
640 }
641 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
642}
643
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200644cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
645cpufreq_freq_attr_ro(cpuinfo_min_freq);
646cpufreq_freq_attr_ro(cpuinfo_max_freq);
647cpufreq_freq_attr_ro(cpuinfo_transition_latency);
648cpufreq_freq_attr_ro(scaling_available_governors);
649cpufreq_freq_attr_ro(scaling_driver);
650cpufreq_freq_attr_ro(scaling_cur_freq);
651cpufreq_freq_attr_ro(bios_limit);
652cpufreq_freq_attr_ro(related_cpus);
653cpufreq_freq_attr_ro(affected_cpus);
654cpufreq_freq_attr_rw(scaling_min_freq);
655cpufreq_freq_attr_rw(scaling_max_freq);
656cpufreq_freq_attr_rw(scaling_governor);
657cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Dave Jones905d77c2008-03-05 14:28:32 -0500659static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 &cpuinfo_min_freq.attr,
661 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100662 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 &scaling_min_freq.attr,
664 &scaling_max_freq.attr,
665 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700666 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 &scaling_governor.attr,
668 &scaling_driver.attr,
669 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700670 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 NULL
672};
673
Dave Jones29464f22009-01-18 01:37:11 -0500674#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
675#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Dave Jones29464f22009-01-18 01:37:11 -0500677static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
Dave Jones905d77c2008-03-05 14:28:32 -0500679 struct cpufreq_policy *policy = to_policy(kobj);
680 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530681 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530682
683 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530684 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800685
Viresh Kumar1b750e32013-10-02 14:13:09 +0530686 lock_policy_rwsem_read(policy->cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800687
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530688 if (fattr->show)
689 ret = fattr->show(policy, buf);
690 else
691 ret = -EIO;
692
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800693 unlock_policy_rwsem_read(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530694 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return ret;
697}
698
Dave Jones905d77c2008-03-05 14:28:32 -0500699static ssize_t store(struct kobject *kobj, struct attribute *attr,
700 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Dave Jones905d77c2008-03-05 14:28:32 -0500702 struct cpufreq_policy *policy = to_policy(kobj);
703 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500704 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530705
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530706 get_online_cpus();
707
708 if (!cpu_online(policy->cpu))
709 goto unlock;
710
Viresh Kumar6eed9402013-08-06 22:53:11 +0530711 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530712 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800713
Viresh Kumar1b750e32013-10-02 14:13:09 +0530714 lock_policy_rwsem_write(policy->cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800715
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530716 if (fattr->store)
717 ret = fattr->store(policy, buf, count);
718 else
719 ret = -EIO;
720
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800721 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530722
Viresh Kumar6eed9402013-08-06 22:53:11 +0530723 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530724unlock:
725 put_online_cpus();
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return ret;
728}
729
Dave Jones905d77c2008-03-05 14:28:32 -0500730static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Dave Jones905d77c2008-03-05 14:28:32 -0500732 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200733 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 complete(&policy->kobj_unregister);
735}
736
Emese Revfy52cf25d2010-01-19 02:58:23 +0100737static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 .show = show,
739 .store = store,
740};
741
742static struct kobj_type ktype_cpufreq = {
743 .sysfs_ops = &sysfs_ops,
744 .default_attrs = default_attrs,
745 .release = cpufreq_sysfs_release,
746};
747
Viresh Kumar2361be22013-05-17 16:09:09 +0530748struct kobject *cpufreq_global_kobject;
749EXPORT_SYMBOL(cpufreq_global_kobject);
750
751static int cpufreq_global_kobject_usage;
752
753int cpufreq_get_global_kobject(void)
754{
755 if (!cpufreq_global_kobject_usage++)
756 return kobject_add(cpufreq_global_kobject,
757 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
758
759 return 0;
760}
761EXPORT_SYMBOL(cpufreq_get_global_kobject);
762
763void cpufreq_put_global_kobject(void)
764{
765 if (!--cpufreq_global_kobject_usage)
766 kobject_del(cpufreq_global_kobject);
767}
768EXPORT_SYMBOL(cpufreq_put_global_kobject);
769
770int cpufreq_sysfs_create_file(const struct attribute *attr)
771{
772 int ret = cpufreq_get_global_kobject();
773
774 if (!ret) {
775 ret = sysfs_create_file(cpufreq_global_kobject, attr);
776 if (ret)
777 cpufreq_put_global_kobject();
778 }
779
780 return ret;
781}
782EXPORT_SYMBOL(cpufreq_sysfs_create_file);
783
784void cpufreq_sysfs_remove_file(const struct attribute *attr)
785{
786 sysfs_remove_file(cpufreq_global_kobject, attr);
787 cpufreq_put_global_kobject();
788}
789EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
790
Dave Jones19d6f7e2009-07-08 17:35:39 -0400791/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200792static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400793{
794 unsigned int j;
795 int ret = 0;
796
797 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800798 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400799
Viresh Kumar308b60e2013-07-31 14:35:14 +0200800 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400801 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400802
Viresh Kumare8fdde12013-07-31 14:31:33 +0200803 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800804 cpu_dev = get_cpu_device(j);
805 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400806 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200807 if (ret)
808 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400809 }
810 return ret;
811}
812
Viresh Kumar308b60e2013-07-31 14:35:14 +0200813static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800814 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400815{
816 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400817 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400818
819 /* prepare interface data */
820 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800821 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400822 if (ret)
823 return ret;
824
825 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200826 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400827 while ((drv_attr) && (*drv_attr)) {
828 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
829 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200830 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400831 drv_attr++;
832 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200833 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400834 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
835 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200836 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400837 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200838 if (cpufreq_driver->target) {
Dave Jones909a6942009-07-08 18:05:42 -0400839 ret = sysfs_create_file(&policy->kobj, &scaling_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 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200843 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100844 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
845 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200846 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100847 }
Dave Jones909a6942009-07-08 18:05:42 -0400848
Viresh Kumar308b60e2013-07-31 14:35:14 +0200849 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400850 if (ret)
851 goto err_out_kobj_put;
852
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530853 return ret;
854
855err_out_kobj_put:
856 kobject_put(&policy->kobj);
857 wait_for_completion(&policy->kobj_unregister);
858 return ret;
859}
860
861static void cpufreq_init_policy(struct cpufreq_policy *policy)
862{
863 struct cpufreq_policy new_policy;
864 int ret = 0;
865
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530866 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar037ce832013-10-02 14:13:16 +0530867 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400868 policy->governor = NULL;
869
870 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530871 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400872 policy->user_policy.policy = policy->policy;
873 policy->user_policy.governor = policy->governor;
874
875 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200876 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200877 if (cpufreq_driver->exit)
878 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400879 }
Dave Jones909a6942009-07-08 18:05:42 -0400880}
881
Viresh Kumarfcf80582013-01-29 14:39:08 +0000882#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200883static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
884 unsigned int cpu, struct device *dev,
885 bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000886{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200887 int ret = 0, has_target = !!cpufreq_driver->target;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000888 unsigned long flags;
889
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530890 if (has_target) {
891 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
892 if (ret) {
893 pr_err("%s: Failed to stop governor\n", __func__);
894 return ret;
895 }
896 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000897
Viresh Kumard8d3b472013-08-04 01:20:07 +0200898 lock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530899
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000900 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530901
Viresh Kumarfcf80582013-01-29 14:39:08 +0000902 cpumask_set_cpu(cpu, policy->cpus);
903 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000904 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000905
Viresh Kumard8d3b472013-08-04 01:20:07 +0200906 unlock_policy_rwsem_write(policy->cpu);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530907
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200908 if (has_target) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530909 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
910 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
911 pr_err("%s: Failed to start governor\n", __func__);
912 return ret;
913 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200914 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000915
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530916 /* Don't touch sysfs links during light-weight init */
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200917 if (!frozen)
918 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000919
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530920 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000921}
922#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530924static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
925{
926 struct cpufreq_policy *policy;
927 unsigned long flags;
928
Lan Tianyu44871c92013-09-11 15:05:05 +0800929 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530930
931 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
932
Lan Tianyu44871c92013-09-11 15:05:05 +0800933 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530934
935 return policy;
936}
937
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530938static struct cpufreq_policy *cpufreq_policy_alloc(void)
939{
940 struct cpufreq_policy *policy;
941
942 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
943 if (!policy)
944 return NULL;
945
946 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
947 goto err_free_policy;
948
949 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
950 goto err_free_cpumask;
951
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530952 INIT_LIST_HEAD(&policy->policy_list);
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530953 return policy;
954
955err_free_cpumask:
956 free_cpumask_var(policy->cpus);
957err_free_policy:
958 kfree(policy);
959
960 return NULL;
961}
962
963static void cpufreq_policy_free(struct cpufreq_policy *policy)
964{
965 free_cpumask_var(policy->related_cpus);
966 free_cpumask_var(policy->cpus);
967 kfree(policy);
968}
969
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530970static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
971{
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530972 if (cpu == policy->cpu)
973 return;
974
Viresh Kumar8efd5762013-09-17 10:22:11 +0530975 /*
976 * Take direct locks as lock_policy_rwsem_write wouldn't work here.
977 * Also lock for last cpu is enough here as contention will happen only
978 * after policy->cpu is changed and after it is changed, other threads
979 * will try to acquire lock for new cpu. And policy is already updated
980 * by then.
981 */
982 down_write(&per_cpu(cpu_policy_rwsem, policy->cpu));
983
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530984 policy->last_cpu = policy->cpu;
985 policy->cpu = cpu;
986
Viresh Kumar8efd5762013-09-17 10:22:11 +0530987 up_write(&per_cpu(cpu_policy_rwsem, policy->last_cpu));
988
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530989#ifdef CONFIG_CPU_FREQ_TABLE
990 cpufreq_frequency_table_update_policy_cpu(policy);
991#endif
992 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
993 CPUFREQ_UPDATE_POLICY_CPU, policy);
994}
995
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530996static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
997 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000999 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +05301000 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001003#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +05301004 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +00001005 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Ashok Rajc32b6b82005-10-30 14:59:54 -08001008 if (cpu_is_offline(cpu))
1009 return 0;
1010
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001011 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013#ifdef CONFIG_SMP
1014 /* check whether a different CPU already registered this
1015 * CPU because it is in the same boat. */
1016 policy = cpufreq_cpu_get(cpu);
1017 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001018 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020 }
Li Zhong5025d622013-08-21 01:31:08 +02001021#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001022
Viresh Kumar6eed9402013-08-06 22:53:11 +05301023 if (!down_read_trylock(&cpufreq_rwsem))
1024 return 0;
1025
Viresh Kumarfcf80582013-01-29 14:39:08 +00001026#ifdef CONFIG_HOTPLUG_CPU
1027 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001028 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301029 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1030 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001031 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301032 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301033 up_read(&cpufreq_rwsem);
1034 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301035 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001036 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001037 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001038#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301040 if (frozen)
1041 /* Restore the saved policy when doing light-weight init */
1042 policy = cpufreq_policy_restore(cpu);
1043 else
1044 policy = cpufreq_policy_alloc();
1045
Dave Jones059019a2009-07-08 16:30:03 -04001046 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001048
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301049
1050 /*
1051 * In the resume path, since we restore a saved policy, the assignment
1052 * to policy->cpu is like an update of the existing policy, rather than
1053 * the creation of a brand new one. So we need to perform this update
1054 * by invoking update_policy_cpu().
1055 */
1056 if (frozen && cpu != policy->cpu)
1057 update_policy_cpu(policy, cpu);
1058 else
1059 policy->cpu = cpu;
1060
Viresh Kumar65922462013-02-07 10:56:03 +05301061 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001062 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001065 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /* call driver. From then on the cpufreq must be able
1068 * to accept all calls to ->verify and ->setpolicy for this CPU
1069 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001070 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001072 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301073 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001075
Viresh Kumarda60ce92013-10-03 20:28:30 +05301076 if (cpufreq_driver->get) {
1077 policy->cur = cpufreq_driver->get(policy->cpu);
1078 if (!policy->cur) {
1079 pr_err("%s: ->get() failed\n", __func__);
1080 goto err_get_freq;
1081 }
1082 }
1083
Viresh Kumarfcf80582013-01-29 14:39:08 +00001084 /* related cpus should atleast have policy->cpus */
1085 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1086
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001087 /*
1088 * affected cpus must always be the one, which are online. We aren't
1089 * managing offline cpus here.
1090 */
1091 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1092
Mike Chan187d9f42008-12-04 12:19:17 -08001093 policy->user_policy.min = policy->min;
1094 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Thomas Renningera1531ac2008-07-29 22:32:58 -07001096 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1097 CPUFREQ_START, policy);
1098
Viresh Kumarfcf80582013-01-29 14:39:08 +00001099#ifdef CONFIG_HOTPLUG_CPU
1100 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1101 if (gov) {
1102 policy->governor = gov;
1103 pr_debug("Restoring governor %s for cpu %d\n",
1104 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001105 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301108 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301109 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301110 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301111 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1112
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301113 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001114 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301115 if (ret)
1116 goto err_out_unregister;
1117 }
Dave Jones8ff69732006-03-05 03:37:23 -05001118
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301119 write_lock_irqsave(&cpufreq_driver_lock, flags);
1120 list_add(&policy->policy_list, &cpufreq_policy_list);
1121 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1122
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301123 cpufreq_init_policy(policy);
1124
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001125 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301126 up_read(&cpufreq_rwsem);
1127
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001128 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 return 0;
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001133 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301134 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001135 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001136 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Viresh Kumarda60ce92013-10-03 20:28:30 +05301138err_get_freq:
1139 if (cpufreq_driver->exit)
1140 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301141err_set_policy_cpu:
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301142 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301144 up_read(&cpufreq_rwsem);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return ret;
1147}
1148
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301149/**
1150 * cpufreq_add_dev - add a CPU device
1151 *
1152 * Adds the cpufreq interface for a CPU device.
1153 *
1154 * The Oracle says: try running cpufreq registration/unregistration concurrently
1155 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1156 * mess up, but more thorough testing is needed. - Mathieu
1157 */
1158static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1159{
1160 return __cpufreq_add_dev(dev, sif, false);
1161}
1162
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301163static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301164 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301165{
1166 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301167 int ret;
1168
1169 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301170 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301171
1172 /* Don't touch sysfs files during light-weight tear-down */
1173 if (frozen)
1174 return cpu_dev->id;
1175
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301176 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301177 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301178 if (ret) {
1179 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1180
Viresh Kumar1b750e32013-10-02 14:13:09 +05301181 lock_policy_rwsem_write(old_cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301182 cpumask_set_cpu(old_cpu, policy->cpus);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301183 unlock_policy_rwsem_write(old_cpu);
1184
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301185 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301186 "cpufreq");
1187
1188 return -EINVAL;
1189 }
1190
1191 return cpu_dev->id;
1192}
1193
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301194static int __cpufreq_remove_dev_prepare(struct device *dev,
1195 struct subsys_interface *sif,
1196 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301198 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301199 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301201 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001203 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001205 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301207 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301209 /* Save the policy somewhere when doing a light-weight tear-down */
1210 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301211 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301212
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001213 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301215 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001216 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301220 if (cpufreq_driver->target) {
1221 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1222 if (ret) {
1223 pr_err("%s: Failed to stop governor\n", __func__);
1224 return ret;
1225 }
1226 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001227
Jacob Shin27ecddc2011-04-27 13:32:11 -05001228#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001229 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001230 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301231 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001232#endif
1233
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301234 lock_policy_rwsem_read(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301235 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301236 unlock_policy_rwsem_read(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301238 if (cpu != policy->cpu) {
1239 if (!frozen)
1240 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001241 } else if (cpus > 1) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301242 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301243 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301244 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301245
1246 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301247 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1248 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301249 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001250 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001251 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001252
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301253 return 0;
1254}
1255
1256static int __cpufreq_remove_dev_finish(struct device *dev,
1257 struct subsys_interface *sif,
1258 bool frozen)
1259{
1260 unsigned int cpu = dev->id, cpus;
1261 int ret;
1262 unsigned long flags;
1263 struct cpufreq_policy *policy;
1264 struct kobject *kobj;
1265 struct completion *cmp;
1266
1267 read_lock_irqsave(&cpufreq_driver_lock, flags);
1268 policy = per_cpu(cpufreq_cpu_data, cpu);
1269 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1270
1271 if (!policy) {
1272 pr_debug("%s: No cpu_data found\n", __func__);
1273 return -EINVAL;
1274 }
1275
Viresh Kumar1b750e32013-10-02 14:13:09 +05301276 lock_policy_rwsem_write(cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301277 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301278
1279 if (cpus > 1)
1280 cpumask_clear_cpu(cpu, policy->cpus);
1281 unlock_policy_rwsem_write(cpu);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301282
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001283 /* If cpu is last user of policy, free policy */
1284 if (cpus == 1) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301285 if (cpufreq_driver->target) {
1286 ret = __cpufreq_governor(policy,
1287 CPUFREQ_GOV_POLICY_EXIT);
1288 if (ret) {
1289 pr_err("%s: Failed to exit governor\n",
1290 __func__);
1291 return ret;
1292 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301293 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001294
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301295 if (!frozen) {
1296 lock_policy_rwsem_read(cpu);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301297 kobj = &policy->kobj;
1298 cmp = &policy->kobj_unregister;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301299 unlock_policy_rwsem_read(cpu);
1300 kobject_put(kobj);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001301
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301302 /*
1303 * We need to make sure that the underlying kobj is
1304 * actually not referenced anymore by anybody before we
1305 * proceed with unloading.
1306 */
1307 pr_debug("waiting for dropping of refcount\n");
1308 wait_for_completion(cmp);
1309 pr_debug("wait complete\n");
1310 }
1311
1312 /*
1313 * Perform the ->exit() even during light-weight tear-down,
1314 * since this is a core component, and is essential for the
1315 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001316 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001317 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301318 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001319
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301320 /* Remove policy from list of active policies */
1321 write_lock_irqsave(&cpufreq_driver_lock, flags);
1322 list_del(&policy->policy_list);
1323 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1324
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301325 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301326 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001327 } else {
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001328 if (cpufreq_driver->target) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301329 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1330 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1331 pr_err("%s: Failed to start governor\n",
1332 __func__);
1333 return ret;
1334 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001335 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Viresh Kumar474deff2013-08-20 12:08:25 +05301338 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
1340}
1341
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301342/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301343 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301344 *
1345 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301346 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001347static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001348{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001349 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301350 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001351
1352 if (cpu_is_offline(cpu))
1353 return 0;
1354
Viresh Kumar27a862e2013-10-02 14:13:14 +05301355 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1356
1357 if (!ret)
1358 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1359
1360 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001361}
1362
David Howells65f27f32006-11-22 14:55:48 +00001363static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
David Howells65f27f32006-11-22 14:55:48 +00001365 struct cpufreq_policy *policy =
1366 container_of(work, struct cpufreq_policy, update);
1367 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001368 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 cpufreq_update_policy(cpu);
1370}
1371
1372/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301373 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1374 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 * @cpu: cpu number
1376 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1377 * @new_freq: CPU frequency the CPU actually runs at
1378 *
Dave Jones29464f22009-01-18 01:37:11 -05001379 * We adjust to current frequency first, and need to clean up later.
1380 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301382static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1383 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301385 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301387 unsigned long flags;
1388
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001389 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 freqs.old = old_freq;
1393 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301394
1395 read_lock_irqsave(&cpufreq_driver_lock, flags);
1396 policy = per_cpu(cpufreq_cpu_data, cpu);
1397 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1398
1399 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1400 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
Dave Jones32ee8c32006-02-28 00:43:23 -05001403/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301404 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001405 * @cpu: CPU number
1406 *
1407 * This is the last known freq, without actually getting it from the driver.
1408 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1409 */
1410unsigned int cpufreq_quick_get(unsigned int cpu)
1411{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001412 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301413 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001414
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001415 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1416 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001417
1418 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001419 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301420 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001421 cpufreq_cpu_put(policy);
1422 }
1423
Dave Jones4d34a672008-02-07 16:33:49 -05001424 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001425}
1426EXPORT_SYMBOL(cpufreq_quick_get);
1427
Jesse Barnes3d737102011-06-28 10:59:12 -07001428/**
1429 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1430 * @cpu: CPU number
1431 *
1432 * Just return the max possible frequency for a given CPU.
1433 */
1434unsigned int cpufreq_quick_get_max(unsigned int cpu)
1435{
1436 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1437 unsigned int ret_freq = 0;
1438
1439 if (policy) {
1440 ret_freq = policy->max;
1441 cpufreq_cpu_put(policy);
1442 }
1443
1444 return ret_freq;
1445}
1446EXPORT_SYMBOL(cpufreq_quick_get_max);
1447
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001448static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001450 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301451 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001453 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001454 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001456 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301458 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001459 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301460 /* verify no discrepancy between actual and
1461 saved value exists */
1462 if (unlikely(ret_freq != policy->cur)) {
1463 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 schedule_work(&policy->update);
1465 }
1466 }
1467
Dave Jones4d34a672008-02-07 16:33:49 -05001468 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001469}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001471/**
1472 * cpufreq_get - get the current CPU frequency (in kHz)
1473 * @cpu: CPU number
1474 *
1475 * Get the CPU current (static) CPU frequency
1476 */
1477unsigned int cpufreq_get(unsigned int cpu)
1478{
1479 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001480
Viresh Kumar26ca8692013-09-20 22:37:31 +05301481 if (cpufreq_disabled() || !cpufreq_driver)
1482 return -ENOENT;
1483
Viresh Kumar6eed9402013-08-06 22:53:11 +05301484 if (!down_read_trylock(&cpufreq_rwsem))
1485 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001486
Viresh Kumar1b750e32013-10-02 14:13:09 +05301487 lock_policy_rwsem_read(cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001488
1489 ret_freq = __cpufreq_get(cpu);
1490
1491 unlock_policy_rwsem_read(cpu);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301492 up_read(&cpufreq_rwsem);
1493
Dave Jones4d34a672008-02-07 16:33:49 -05001494 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496EXPORT_SYMBOL(cpufreq_get);
1497
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001498static struct subsys_interface cpufreq_interface = {
1499 .name = "cpufreq",
1500 .subsys = &cpu_subsys,
1501 .add_dev = cpufreq_add_dev,
1502 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001503};
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001506 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1507 *
1508 * This function is only executed for the boot processor. The other CPUs
1509 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001510 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001511static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001512{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301513 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001514
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001515 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301516 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001517
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001518 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001519
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001520 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301521 policy = cpufreq_cpu_get(cpu);
1522 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001523 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001524
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001525 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301526 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001527 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001528 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301529 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001530 }
1531
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301532 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001533 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001534}
1535
1536/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001537 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 *
1539 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001540 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1541 * restored. It will verify that the current freq is in sync with
1542 * what we believe it to be. This is a bit later than when it
1543 * should be, but nonethteless it's better than calling
1544 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001545 *
1546 * This function is only executed for the boot CPU. The other CPUs have not
1547 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001549static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301551 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001552
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001553 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301554 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001556 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001558 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301559 policy = cpufreq_cpu_get(cpu);
1560 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001563 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301564 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 if (ret) {
1566 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301567 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001568 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570 }
1571
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301572 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001573
Dave Jonesc9060492008-02-07 16:32:18 -05001574fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301575 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
1577
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001578static struct syscore_ops cpufreq_syscore_ops = {
1579 .suspend = cpufreq_bp_suspend,
1580 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581};
1582
Borislav Petkov9d950462013-01-20 10:24:28 +00001583/**
1584 * cpufreq_get_current_driver - return current driver's name
1585 *
1586 * Return the name string of the currently loaded cpufreq driver
1587 * or NULL, if none.
1588 */
1589const char *cpufreq_get_current_driver(void)
1590{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001591 if (cpufreq_driver)
1592 return cpufreq_driver->name;
1593
1594 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001595}
1596EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598/*********************************************************************
1599 * NOTIFIER LISTS INTERFACE *
1600 *********************************************************************/
1601
1602/**
1603 * cpufreq_register_notifier - register a driver with cpufreq
1604 * @nb: notifier function to register
1605 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1606 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001607 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 * are notified about clock rate changes (once before and once after
1609 * the transition), or a list of drivers that are notified about
1610 * changes in cpufreq policy.
1611 *
1612 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001613 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1616{
1617 int ret;
1618
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001619 if (cpufreq_disabled())
1620 return -EINVAL;
1621
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001622 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 switch (list) {
1625 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001626 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001627 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 break;
1629 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001630 ret = blocking_notifier_chain_register(
1631 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633 default:
1634 ret = -EINVAL;
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 return ret;
1638}
1639EXPORT_SYMBOL(cpufreq_register_notifier);
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/**
1642 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1643 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301644 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 *
1646 * Remove a driver from the CPU frequency notifier list.
1647 *
1648 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001649 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1652{
1653 int ret;
1654
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001655 if (cpufreq_disabled())
1656 return -EINVAL;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 switch (list) {
1659 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001660 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001661 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 break;
1663 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001664 ret = blocking_notifier_chain_unregister(
1665 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 break;
1667 default:
1668 ret = -EINVAL;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 return ret;
1672}
1673EXPORT_SYMBOL(cpufreq_unregister_notifier);
1674
1675
1676/*********************************************************************
1677 * GOVERNORS *
1678 *********************************************************************/
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680int __cpufreq_driver_target(struct cpufreq_policy *policy,
1681 unsigned int target_freq,
1682 unsigned int relation)
1683{
1684 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001685 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001686
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001687 if (cpufreq_disabled())
1688 return -ENODEV;
1689
Viresh Kumar72499242012-10-31 01:28:21 +01001690 /* Make sure that target_freq is within supported range */
1691 if (target_freq > policy->max)
1692 target_freq = policy->max;
1693 if (target_freq < policy->min)
1694 target_freq = policy->min;
1695
1696 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1697 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001698
1699 if (target_freq == policy->cur)
1700 return 0;
1701
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001702 if (cpufreq_driver->target)
1703 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return retval;
1706}
1707EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709int cpufreq_driver_target(struct cpufreq_policy *policy,
1710 unsigned int target_freq,
1711 unsigned int relation)
1712{
Julia Lawallf1829e42008-07-25 22:44:53 +02001713 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Viresh Kumar1b750e32013-10-02 14:13:09 +05301715 lock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 ret = __cpufreq_driver_target(policy, target_freq, relation);
1718
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001719 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return ret;
1722}
1723EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1724
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001725/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001726 * when "event" is CPUFREQ_GOV_LIMITS
1727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301729static int __cpufreq_governor(struct cpufreq_policy *policy,
1730 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Dave Jonescc993ca2005-07-28 09:43:56 -07001732 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001733
1734 /* Only must be defined when default governor is known to have latency
1735 restrictions, like e.g. conservative or ondemand.
1736 That this is the case is already ensured in Kconfig
1737 */
1738#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1739 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1740#else
1741 struct cpufreq_governor *gov = NULL;
1742#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001743
1744 if (policy->governor->max_transition_latency &&
1745 policy->cpuinfo.transition_latency >
1746 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001747 if (!gov)
1748 return -EINVAL;
1749 else {
1750 printk(KERN_WARNING "%s governor failed, too long"
1751 " transition latency of HW, fallback"
1752 " to %s governor\n",
1753 policy->governor->name,
1754 gov->name);
1755 policy->governor = gov;
1756 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Viresh Kumarfe492f32013-08-06 22:53:10 +05301759 if (event == CPUFREQ_GOV_POLICY_INIT)
1760 if (!try_module_get(policy->governor->owner))
1761 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001763 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301764 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001765
1766 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301767 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301768 || (!policy->governor_enabled
1769 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001770 mutex_unlock(&cpufreq_governor_lock);
1771 return -EBUSY;
1772 }
1773
1774 if (event == CPUFREQ_GOV_STOP)
1775 policy->governor_enabled = false;
1776 else if (event == CPUFREQ_GOV_START)
1777 policy->governor_enabled = true;
1778
1779 mutex_unlock(&cpufreq_governor_lock);
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 ret = policy->governor->governor(policy, event);
1782
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001783 if (!ret) {
1784 if (event == CPUFREQ_GOV_POLICY_INIT)
1785 policy->governor->initialized++;
1786 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1787 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001788 } else {
1789 /* Restore original values */
1790 mutex_lock(&cpufreq_governor_lock);
1791 if (event == CPUFREQ_GOV_STOP)
1792 policy->governor_enabled = true;
1793 else if (event == CPUFREQ_GOV_START)
1794 policy->governor_enabled = false;
1795 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001796 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001797
Viresh Kumarfe492f32013-08-06 22:53:10 +05301798 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1799 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 module_put(policy->governor->owner);
1801
1802 return ret;
1803}
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805int cpufreq_register_governor(struct cpufreq_governor *governor)
1806{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001807 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 if (!governor)
1810 return -EINVAL;
1811
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001812 if (cpufreq_disabled())
1813 return -ENODEV;
1814
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001815 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001816
Viresh Kumarb3940582013-02-01 05:42:58 +00001817 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001818 err = -EBUSY;
1819 if (__find_governor(governor->name) == NULL) {
1820 err = 0;
1821 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Dave Jones32ee8c32006-02-28 00:43:23 -05001824 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001825 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1830{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001831#ifdef CONFIG_HOTPLUG_CPU
1832 int cpu;
1833#endif
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (!governor)
1836 return;
1837
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001838 if (cpufreq_disabled())
1839 return;
1840
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001841#ifdef CONFIG_HOTPLUG_CPU
1842 for_each_present_cpu(cpu) {
1843 if (cpu_online(cpu))
1844 continue;
1845 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1846 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1847 }
1848#endif
1849
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001850 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001852 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return;
1854}
1855EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1856
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858/*********************************************************************
1859 * POLICY INTERFACE *
1860 *********************************************************************/
1861
1862/**
1863 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001864 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1865 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 *
1867 * Reads the current cpufreq policy.
1868 */
1869int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1870{
1871 struct cpufreq_policy *cpu_policy;
1872 if (!policy)
1873 return -EINVAL;
1874
1875 cpu_policy = cpufreq_cpu_get(cpu);
1876 if (!cpu_policy)
1877 return -EINVAL;
1878
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301879 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return 0;
1883}
1884EXPORT_SYMBOL(cpufreq_get_policy);
1885
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001886/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301887 * policy : current policy.
1888 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001889 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301890static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301891 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001893 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301895 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1896 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301898 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301900 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001901 ret = -EINVAL;
1902 goto error_out;
1903 }
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301906 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if (ret)
1908 goto error_out;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001911 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301912 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001915 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301916 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Viresh Kumarbb176f72013-06-19 14:19:33 +05301918 /*
1919 * verify the cpu speed can be set within this limit, which might be
1920 * different to the first one
1921 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301922 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001923 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001927 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301928 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301930 policy->min = new_policy->min;
1931 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001933 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301934 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001936 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301937 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001938 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301939 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301941 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301943 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001945 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301948 if (policy->governor) {
1949 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1950 unlock_policy_rwsem_write(new_policy->cpu);
1951 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001952 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301953 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301957 policy->governor = new_policy->governor;
1958 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1959 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001960 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00001961 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301962 unlock_policy_rwsem_write(new_policy->cpu);
1963 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001964 CPUFREQ_GOV_POLICY_EXIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301965 lock_policy_rwsem_write(new_policy->cpu);
Viresh Kumar955ef482013-05-16 05:09:58 +00001966 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001967 }
1968
1969 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001971 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301972 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301974 policy->governor = old_gov;
1975 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001976 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301977 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301978 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980 ret = -EINVAL;
1981 goto error_out;
1982 }
1983 /* might be a policy change, too, so fall through */
1984 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001985 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301986 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988
Dave Jones7d5e3502006-02-02 17:03:42 -05001989error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 return ret;
1991}
1992
1993/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1995 * @cpu: CPU which shall be re-evaluated
1996 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001997 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 * at different times.
1999 */
2000int cpufreq_update_policy(unsigned int cpu)
2001{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302002 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2003 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002004 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302006 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002007 ret = -ENODEV;
2008 goto no_policy;
2009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Viresh Kumar1b750e32013-10-02 14:13:09 +05302011 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002013 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302014 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302015 new_policy.min = policy->user_policy.min;
2016 new_policy.max = policy->user_policy.max;
2017 new_policy.policy = policy->user_policy.policy;
2018 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Viresh Kumarbb176f72013-06-19 14:19:33 +05302020 /*
2021 * BIOS might change freq behind our back
2022 * -> ask driver for current freq and notify governors about a change
2023 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002024 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302025 new_policy.cur = cpufreq_driver->get(cpu);
2026 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002027 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302028 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002029 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302030 if (policy->cur != new_policy.cur && cpufreq_driver->target)
2031 cpufreq_out_of_sync(cpu, policy->cur,
2032 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002033 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002034 }
2035
Viresh Kumar037ce832013-10-02 14:13:16 +05302036 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002038 unlock_policy_rwsem_write(cpu);
2039
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302040 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002041no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return ret;
2043}
2044EXPORT_SYMBOL(cpufreq_update_policy);
2045
Paul Gortmaker27609842013-06-19 13:54:04 -04002046static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002047 unsigned long action, void *hcpu)
2048{
2049 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002050 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302051 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002052
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002053 dev = get_cpu_device(cpu);
2054 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302055
2056 if (action & CPU_TASKS_FROZEN)
2057 frozen = true;
2058
2059 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002060 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302061 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302062 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002063 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302064
Ashok Rajc32b6b82005-10-30 14:59:54 -08002065 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302066 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302067 break;
2068
2069 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302070 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002071 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302072
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002073 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302074 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002075 break;
2076 }
2077 }
2078 return NOTIFY_OK;
2079}
2080
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002081static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302082 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002083};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085/*********************************************************************
2086 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2087 *********************************************************************/
2088
2089/**
2090 * cpufreq_register_driver - register a CPU Frequency driver
2091 * @driver_data: A struct cpufreq_driver containing the values#
2092 * submitted by the CPU Frequency driver.
2093 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302094 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002096 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 *
2098 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002099int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100{
2101 unsigned long flags;
2102 int ret;
2103
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002104 if (cpufreq_disabled())
2105 return -ENODEV;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (!driver_data || !driver_data->verify || !driver_data->init ||
2108 ((!driver_data->setpolicy) && (!driver_data->target)))
2109 return -EINVAL;
2110
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002111 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 if (driver_data->setpolicy)
2114 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2115
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002116 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002117 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002118 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea5802013-09-18 21:05:20 -07002119 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002121 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002122 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002124 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002125 if (ret)
2126 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002128 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int i;
2130 ret = -ENODEV;
2131
2132 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002133 for (i = 0; i < nr_cpu_ids; i++)
2134 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002136 break;
2137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 /* if all ->init() calls failed, unregister */
2140 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002141 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302142 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002143 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145 }
2146
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002147 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002148 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002150 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002151err_if_unreg:
2152 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002153err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002154 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002155 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002156 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002157 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158}
2159EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161/**
2162 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2163 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302164 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * the right to do so, i.e. if you have succeeded in initialising before!
2166 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2167 * currently not initialised.
2168 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002169int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 unsigned long flags;
2172
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002173 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002176 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002178 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002179 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Viresh Kumar6eed9402013-08-06 22:53:11 +05302181 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002182 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302183
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002184 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302185
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002186 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302187 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 return 0;
2190}
2191EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002192
2193static int __init cpufreq_core_init(void)
2194{
2195 int cpu;
2196
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002197 if (cpufreq_disabled())
2198 return -ENODEV;
2199
Viresh Kumar474deff2013-08-20 12:08:25 +05302200 for_each_possible_cpu(cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002201 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002202
Viresh Kumar2361be22013-05-17 16:09:09 +05302203 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002204 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002205 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002206
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002207 return 0;
2208}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002209core_initcall(cpufreq_core_init);