blob: de9951766dd8beac8d2785383f857f1f348c9078 [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>
6 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08007 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -05009 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/init.h>
23#include <linux/notifier.h>
24#include <linux/cpufreq.h>
25#include <linux/delay.h>
26#include <linux/interrupt.h>
27#include <linux/spinlock.h>
28#include <linux/device.h>
29#include <linux/slab.h>
30#include <linux/cpu.h>
31#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080032#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010033#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Renninger6f4f2722010-04-20 13:17:36 +020035#include <trace/events/power.h>
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/**
Dave Jonescd878472006-08-11 17:59:28 -040038 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * level driver of CPUFreq support, and its spinlock. This lock
40 * also protects the cpufreq_cpu_data array.
41 */
Dave Jones7d5e3502006-02-02 17:03:42 -050042static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070043static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070044#ifdef CONFIG_HOTPLUG_CPU
45/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040046static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(cpufreq_driver_lock);
49
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:
62 * - All holders of the lock should check to make sure that the CPU they
63 * are concerned with are online after they get the lock.
64 * - Governor routines that can be called in cpufreq hotplug path should not
65 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040066 * - Lock should not be held across
67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068 */
Tejun Heof1625062009-10-29 22:34:13 +090069static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
71
72#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050073static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080074(int cpu) \
75{ \
Tejun Heof1625062009-10-29 22:34:13 +090076 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077 BUG_ON(policy_cpu == -1); \
78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 if (unlikely(!cpu_online(cpu))) { \
80 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
81 return -1; \
82 } \
83 \
84 return 0; \
85}
86
87lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088
89lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
Amerigo Wang226528c2010-03-04 03:23:36 -050091static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092{
Tejun Heof1625062009-10-29 22:34:13 +090093 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094 BUG_ON(policy_cpu == -1);
95 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
96}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097
Amerigo Wang226528c2010-03-04 03:23:36 -050098static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099{
Tejun Heof1625062009-10-29 22:34:13 +0900100 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800101 BUG_ON(policy_cpu == -1);
102 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
103}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800104
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500107static int __cpufreq_governor(struct cpufreq_policy *policy,
108 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800109static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000110static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500113 * Two notifier lists: the "policy" list is involved in the
114 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * "transition" list for kernel code that needs to handle
116 * changes to devices when the CPU clock speed changes.
117 * The mutex locks both lists.
118 */
Alan Sterne041c682006-03-27 01:16:30 -0800119static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700120static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200122static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700123static int __init init_cpufreq_transition_notifier_list(void)
124{
125 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200126 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700127 return 0;
128}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800129pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400131static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +0200132static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -0400133{
134 return off;
135}
136void disable_cpufreq(void)
137{
138 off = 1;
139}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500141static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Stephen Boyda9144432012-07-20 18:14:38 +0000143static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 struct cpufreq_policy *data;
146 unsigned long flags;
147
Mike Travis7a6aedf2008-03-25 15:06:53 -0700148 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 goto err_out;
150
151 /* get the cpufreq driver */
152 spin_lock_irqsave(&cpufreq_driver_lock, flags);
153
154 if (!cpufreq_driver)
155 goto err_out_unlock;
156
157 if (!try_module_get(cpufreq_driver->owner))
158 goto err_out_unlock;
159
160
161 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700162 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (!data)
165 goto err_out_put_module;
166
Stephen Boyda9144432012-07-20 18:14:38 +0000167 if (!sysfs && !kobject_get(&data->kobj))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 goto err_out_put_module;
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return data;
172
Dave Jones7d5e3502006-02-02 17:03:42 -0500173err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500175err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500177err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return NULL;
179}
Stephen Boyda9144432012-07-20 18:14:38 +0000180
181struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
182{
183 return __cpufreq_cpu_get(cpu, false);
184}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
186
Stephen Boyda9144432012-07-20 18:14:38 +0000187static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
188{
189 return __cpufreq_cpu_get(cpu, true);
190}
191
192static void __cpufreq_cpu_put(struct cpufreq_policy *data, bool sysfs)
193{
194 if (!sysfs)
195 kobject_put(&data->kobj);
196 module_put(cpufreq_driver->owner);
197}
Dave Jones7d5e3502006-02-02 17:03:42 -0500198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199void cpufreq_cpu_put(struct cpufreq_policy *data)
200{
Stephen Boyda9144432012-07-20 18:14:38 +0000201 __cpufreq_cpu_put(data, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
204
Stephen Boyda9144432012-07-20 18:14:38 +0000205static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *data)
206{
207 __cpufreq_cpu_put(data, true);
208}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
212 *********************************************************************/
213
214/**
215 * adjust_jiffies - adjust the system "loops_per_jiffy"
216 *
217 * This function alters the system "loops_per_jiffy" for the clock
218 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500219 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * per-CPU loops_per_jiffy value wherever possible.
221 */
222#ifndef CONFIG_SMP
223static unsigned long l_p_j_ref;
224static unsigned int l_p_j_ref_freq;
225
Arjan van de Ven858119e2006-01-14 13:20:43 -0800226static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 if (ci->flags & CPUFREQ_CONST_LOOPS)
229 return;
230
231 if (!l_p_j_ref_freq) {
232 l_p_j_ref = loops_per_jiffy;
233 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200234 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530235 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
Afzal Mohammedd08de0c12012-01-04 10:52:46 +0530237 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700238 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530239 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
240 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200241 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530242 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244}
245#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530246static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
247{
248 return;
249}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250#endif
251
252
253/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800254 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
255 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800257 * This function calls the transition notifiers and the "adjust_jiffies"
258 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500259 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 */
261void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
262{
Dave Jonese4472cb2006-01-31 15:53:55 -0800263 struct cpufreq_policy *policy;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 BUG_ON(irqs_disabled());
266
267 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200268 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800269 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Mike Travis7a6aedf2008-03-25 15:06:53 -0700271 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500275 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800276 * which is not equal to what the cpufreq core thinks is
277 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 */
279 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800280 if ((policy) && (policy->cpu == freqs->cpu) &&
281 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200282 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800283 " %u, cpufreq assumed %u kHz.\n",
284 freqs->old, policy->cur);
285 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700288 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800289 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
291 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 case CPUFREQ_POSTCHANGE:
294 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200295 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200296 (unsigned long)freqs->cpu);
297 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100298 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700299 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800300 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800301 if (likely(policy) && likely(policy->cpu == freqs->cpu))
302 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
307
308
309
310/*********************************************************************
311 * SYSFS INTERFACE *
312 *********************************************************************/
313
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700314static struct cpufreq_governor *__find_governor(const char *str_governor)
315{
316 struct cpufreq_governor *t;
317
318 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500319 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700320 return t;
321
322 return NULL;
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/**
326 * cpufreq_parse_governor - parse a governor string
327 */
Dave Jones905d77c2008-03-05 14:28:32 -0500328static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct cpufreq_governor **governor)
330{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700331 int err = -EINVAL;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700334 goto out;
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (cpufreq_driver->setpolicy) {
337 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
338 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700339 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530340 } else if (!strnicmp(str_governor, "powersave",
341 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700343 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700345 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700347
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800348 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700349
350 t = __find_governor(str_governor);
351
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700352 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700353 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700354
Kees Cook1a8e1462011-05-04 08:38:56 -0700355 mutex_unlock(&cpufreq_governor_mutex);
356 ret = request_module("cpufreq_%s", str_governor);
357 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700358
Kees Cook1a8e1462011-05-04 08:38:56 -0700359 if (ret == 0)
360 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700361 }
362
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700363 if (t != NULL) {
364 *governor = t;
365 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700367
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800368 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Dave Jones29464f22009-01-18 01:37:11 -0500370out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700371 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530376 * cpufreq_per_cpu_attr_read() / show_##file_name() -
377 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 *
379 * Write out information from cpufreq_driver->policy[cpu]; object must be
380 * "unsigned int".
381 */
382
Dave Jones32ee8c32006-02-28 00:43:23 -0500383#define show_one(file_name, object) \
384static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500385(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500386{ \
Dave Jones29464f22009-01-18 01:37:11 -0500387 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390show_one(cpuinfo_min_freq, cpuinfo.min_freq);
391show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100392show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393show_one(scaling_min_freq, min);
394show_one(scaling_max_freq, max);
395show_one(scaling_cur_freq, cur);
396
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530397static int __cpufreq_set_policy(struct cpufreq_policy *data,
398 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/**
401 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
402 */
403#define store_one(file_name, object) \
404static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500405(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{ \
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000407 unsigned int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct cpufreq_policy new_policy; \
409 \
410 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
411 if (ret) \
412 return -EINVAL; \
413 \
Dave Jones29464f22009-01-18 01:37:11 -0500414 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (ret != 1) \
416 return -EINVAL; \
417 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200418 ret = __cpufreq_set_policy(policy, &new_policy); \
419 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 \
421 return ret ? ret : count; \
422}
423
Dave Jones29464f22009-01-18 01:37:11 -0500424store_one(scaling_min_freq, min);
425store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427/**
428 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
429 */
Dave Jones905d77c2008-03-05 14:28:32 -0500430static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
431 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800433 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 if (!cur_freq)
435 return sprintf(buf, "<unknown>");
436 return sprintf(buf, "%u\n", cur_freq);
437}
438
439
440/**
441 * show_scaling_governor - show the current policy for the specified CPU
442 */
Dave Jones905d77c2008-03-05 14:28:32 -0500443static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Dave Jones29464f22009-01-18 01:37:11 -0500445 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return sprintf(buf, "powersave\n");
447 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
448 return sprintf(buf, "performance\n");
449 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200450 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500451 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return -EINVAL;
453}
454
455
456/**
457 * store_scaling_governor - store policy for the specified CPU
458 */
Dave Jones905d77c2008-03-05 14:28:32 -0500459static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
460 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Jingoo Hanf55c9c22012-10-31 05:49:13 +0000462 unsigned int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 char str_governor[16];
464 struct cpufreq_policy new_policy;
465
466 ret = cpufreq_get_policy(&new_policy, policy->cpu);
467 if (ret)
468 return ret;
469
Dave Jones29464f22009-01-18 01:37:11 -0500470 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (ret != 1)
472 return -EINVAL;
473
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530474 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
475 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return -EINVAL;
477
Thomas Renninger7970e082006-04-13 15:14:04 +0200478 /* Do not use cpufreq_set_policy here or the user_policy.max
479 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200480 ret = __cpufreq_set_policy(policy, &new_policy);
481
482 policy->user_policy.policy = policy->policy;
483 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200484
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530485 if (ret)
486 return ret;
487 else
488 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
491/**
492 * show_scaling_driver - show the cpufreq driver currently loaded
493 */
Dave Jones905d77c2008-03-05 14:28:32 -0500494static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
viresh kumar4b972f02012-10-23 01:23:43 +0200496 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/**
500 * show_scaling_available_governors - show the available CPUfreq governors
501 */
Dave Jones905d77c2008-03-05 14:28:32 -0500502static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
503 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 ssize_t i = 0;
506 struct cpufreq_governor *t;
507
508 if (!cpufreq_driver->target) {
509 i += sprintf(buf, "performance powersave");
510 goto out;
511 }
512
513 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500514 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
515 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200517 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500519out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 i += sprintf(&buf[i], "\n");
521 return i;
522}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700523
Rusty Russell835481d2009-01-04 05:18:06 -0800524static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 ssize_t i = 0;
527 unsigned int cpu;
528
Rusty Russell835481d2009-01-04 05:18:06 -0800529 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (i)
531 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
532 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
533 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500534 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
536 i += sprintf(&buf[i], "\n");
537 return i;
538}
539
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700540/**
541 * show_related_cpus - show the CPUs affected by each transition even if
542 * hw coordination is in use
543 */
544static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
545{
Rusty Russell835481d2009-01-04 05:18:06 -0800546 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700547 return show_cpus(policy->cpus, buf);
548 return show_cpus(policy->related_cpus, buf);
549}
550
551/**
552 * show_affected_cpus - show the CPUs affected by each transition
553 */
554static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
555{
556 return show_cpus(policy->cpus, buf);
557}
558
Venki Pallipadi9e769882007-10-26 10:18:21 -0700559static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500560 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700561{
562 unsigned int freq = 0;
563 unsigned int ret;
564
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700565 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700566 return -EINVAL;
567
568 ret = sscanf(buf, "%u", &freq);
569 if (ret != 1)
570 return -EINVAL;
571
572 policy->governor->store_setspeed(policy, freq);
573
574 return count;
575}
576
577static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
578{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700579 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700580 return sprintf(buf, "<unsupported>\n");
581
582 return policy->governor->show_setspeed(policy, buf);
583}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Thomas Renningere2f74f32009-11-19 12:31:01 +0100585/**
viresh kumar8bf1ac72012-10-23 01:23:33 +0200586 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100587 */
588static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
589{
590 unsigned int limit;
591 int ret;
592 if (cpufreq_driver->bios_limit) {
593 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
594 if (!ret)
595 return sprintf(buf, "%u\n", limit);
596 }
597 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
598}
599
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200600cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
601cpufreq_freq_attr_ro(cpuinfo_min_freq);
602cpufreq_freq_attr_ro(cpuinfo_max_freq);
603cpufreq_freq_attr_ro(cpuinfo_transition_latency);
604cpufreq_freq_attr_ro(scaling_available_governors);
605cpufreq_freq_attr_ro(scaling_driver);
606cpufreq_freq_attr_ro(scaling_cur_freq);
607cpufreq_freq_attr_ro(bios_limit);
608cpufreq_freq_attr_ro(related_cpus);
609cpufreq_freq_attr_ro(affected_cpus);
610cpufreq_freq_attr_rw(scaling_min_freq);
611cpufreq_freq_attr_rw(scaling_max_freq);
612cpufreq_freq_attr_rw(scaling_governor);
613cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dave Jones905d77c2008-03-05 14:28:32 -0500615static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 &cpuinfo_min_freq.attr,
617 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100618 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 &scaling_min_freq.attr,
620 &scaling_max_freq.attr,
621 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700622 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 &scaling_governor.attr,
624 &scaling_driver.attr,
625 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700626 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 NULL
628};
629
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200630struct kobject *cpufreq_global_kobject;
631EXPORT_SYMBOL(cpufreq_global_kobject);
632
Dave Jones29464f22009-01-18 01:37:11 -0500633#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
634#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Dave Jones29464f22009-01-18 01:37:11 -0500636static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Dave Jones905d77c2008-03-05 14:28:32 -0500638 struct cpufreq_policy *policy = to_policy(kobj);
639 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500640 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000641 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500643 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800644
645 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500646 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800647
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530648 if (fattr->show)
649 ret = fattr->show(policy, buf);
650 else
651 ret = -EIO;
652
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800653 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500654fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000655 cpufreq_cpu_put_sysfs(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500656no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return ret;
658}
659
Dave Jones905d77c2008-03-05 14:28:32 -0500660static ssize_t store(struct kobject *kobj, struct attribute *attr,
661 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dave Jones905d77c2008-03-05 14:28:32 -0500663 struct cpufreq_policy *policy = to_policy(kobj);
664 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500665 ssize_t ret = -EINVAL;
Stephen Boyda9144432012-07-20 18:14:38 +0000666 policy = cpufreq_cpu_get_sysfs(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500668 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800669
670 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500671 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800672
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530673 if (fattr->store)
674 ret = fattr->store(policy, buf, count);
675 else
676 ret = -EIO;
677
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800678 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500679fail:
Stephen Boyda9144432012-07-20 18:14:38 +0000680 cpufreq_cpu_put_sysfs(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500681no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return ret;
683}
684
Dave Jones905d77c2008-03-05 14:28:32 -0500685static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686{
Dave Jones905d77c2008-03-05 14:28:32 -0500687 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200688 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 complete(&policy->kobj_unregister);
690}
691
Emese Revfy52cf25d2010-01-19 02:58:23 +0100692static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .show = show,
694 .store = store,
695};
696
697static struct kobj_type ktype_cpufreq = {
698 .sysfs_ops = &sysfs_ops,
699 .default_attrs = default_attrs,
700 .release = cpufreq_sysfs_release,
701};
702
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200703/*
704 * Returns:
705 * Negative: Failure
706 * 0: Success
707 * Positive: When we have a managed CPU and the sysfs got symlinked
708 */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700709static int cpufreq_add_dev_policy(unsigned int cpu,
710 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800711 struct device *dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400712{
713 int ret = 0;
714#ifdef CONFIG_SMP
715 unsigned long flags;
716 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400717#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400718 struct cpufreq_governor *gov;
719
720 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
721 if (gov) {
722 policy->governor = gov;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200723 pr_debug("Restoring governor %s for cpu %d\n",
Dave Jonesecf7e462009-07-08 18:48:47 -0400724 policy->governor->name, cpu);
725 }
726#endif
727
728 for_each_cpu(j, policy->cpus) {
729 struct cpufreq_policy *managed_policy;
730
731 if (cpu == j)
732 continue;
733
734 /* Check for existing affected CPUs.
735 * They may not be aware of it due to CPU Hotplug.
736 * cpufreq_cpu_put is called when the device is removed
737 * in __cpufreq_remove_dev()
738 */
739 managed_policy = cpufreq_cpu_get(j);
740 if (unlikely(managed_policy)) {
741
742 /* Set proper policy_cpu */
743 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900744 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400745
746 if (lock_policy_rwsem_write(cpu) < 0) {
747 /* Should not go through policy unlock path */
748 if (cpufreq_driver->exit)
749 cpufreq_driver->exit(policy);
750 cpufreq_cpu_put(managed_policy);
751 return -EBUSY;
752 }
753
754 spin_lock_irqsave(&cpufreq_driver_lock, flags);
755 cpumask_copy(managed_policy->cpus, policy->cpus);
756 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
757 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
758
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200759 pr_debug("CPU already managed, adding link\n");
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800760 ret = sysfs_create_link(&dev->kobj,
Dave Jonesecf7e462009-07-08 18:48:47 -0400761 &managed_policy->kobj,
762 "cpufreq");
763 if (ret)
764 cpufreq_cpu_put(managed_policy);
765 /*
766 * Success. We only needed to be added to the mask.
767 * Call driver->exit() because only the cpu parent of
768 * the kobj needed to call init().
769 */
770 if (cpufreq_driver->exit)
771 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200772
773 if (!ret)
774 return 1;
775 else
776 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400777 }
778 }
779#endif
780 return ret;
781}
782
783
Dave Jones19d6f7e2009-07-08 17:35:39 -0400784/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700785static int cpufreq_add_dev_symlink(unsigned int cpu,
786 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400787{
788 unsigned int j;
789 int ret = 0;
790
791 for_each_cpu(j, policy->cpus) {
792 struct cpufreq_policy *managed_policy;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800793 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400794
795 if (j == cpu)
796 continue;
797 if (!cpu_online(j))
798 continue;
799
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200800 pr_debug("CPU %u already managed, adding link\n", j);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400801 managed_policy = cpufreq_cpu_get(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800802 cpu_dev = get_cpu_device(j);
803 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400804 "cpufreq");
805 if (ret) {
806 cpufreq_cpu_put(managed_policy);
807 return ret;
808 }
809 }
810 return ret;
811}
812
Alex Chiangcf3289d02009-11-17 20:27:08 -0700813static int cpufreq_add_dev_interface(unsigned int cpu,
814 struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800815 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400816{
Dave Jonesecf7e462009-07-08 18:48:47 -0400817 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400818 struct freq_attr **drv_attr;
819 unsigned long flags;
820 int ret = 0;
821 unsigned int j;
822
823 /* prepare interface data */
824 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800825 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400826 if (ret)
827 return ret;
828
829 /* set up files for this cpu device */
830 drv_attr = cpufreq_driver->attr;
831 while ((drv_attr) && (*drv_attr)) {
832 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
833 if (ret)
834 goto err_out_kobj_put;
835 drv_attr++;
836 }
837 if (cpufreq_driver->get) {
838 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
839 if (ret)
840 goto err_out_kobj_put;
841 }
842 if (cpufreq_driver->target) {
843 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
844 if (ret)
845 goto err_out_kobj_put;
846 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100847 if (cpufreq_driver->bios_limit) {
848 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
849 if (ret)
850 goto err_out_kobj_put;
851 }
Dave Jones909a6942009-07-08 18:05:42 -0400852
853 spin_lock_irqsave(&cpufreq_driver_lock, flags);
854 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200855 if (!cpu_online(j))
856 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400857 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900858 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400859 }
860 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
861
862 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400863 if (ret)
864 goto err_out_kobj_put;
865
866 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
867 /* assure that the starting sequence is run in __cpufreq_set_policy */
868 policy->governor = NULL;
869
870 /* set default policy */
871 ret = __cpufreq_set_policy(policy, &new_policy);
872 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");
Dave Jonesecf7e462009-07-08 18:48:47 -0400877 if (cpufreq_driver->exit)
878 cpufreq_driver->exit(policy);
879 }
Dave Jones909a6942009-07-08 18:05:42 -0400880 return ret;
881
882err_out_kobj_put:
883 kobject_put(&policy->kobj);
884 wait_for_completion(&policy->kobj_unregister);
885 return ret;
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889/**
890 * cpufreq_add_dev - add a CPU device
891 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500892 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400893 *
894 * The Oracle says: try running cpufreq registration/unregistration concurrently
895 * with with cpu hotplugging and all hell will break loose. Tried to clean this
896 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800898static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800900 unsigned int cpu = dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500901 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 unsigned long flags;
904 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500905#ifdef CONFIG_HOTPLUG_CPU
906 int sibling;
907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Ashok Rajc32b6b82005-10-30 14:59:54 -0800909 if (cpu_is_offline(cpu))
910 return 0;
911
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200912 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914#ifdef CONFIG_SMP
915 /* check whether a different CPU already registered this
916 * CPU because it is in the same boat. */
917 policy = cpufreq_cpu_get(cpu);
918 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500919 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return 0;
921 }
922#endif
923
924 if (!try_module_get(cpufreq_driver->owner)) {
925 ret = -EINVAL;
926 goto module_out;
927 }
928
Dave Jones059019a2009-07-08 16:30:03 -0400929 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700930 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400931 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400933
934 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400935 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400936
937 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400938 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -0800941 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800943 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +0900944 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400945 ret = (lock_policy_rwsem_write(cpu) < 0);
946 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000949 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Thomas Renninger8122c6c2007-10-02 13:28:09 -0700951 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500952#ifdef CONFIG_HOTPLUG_CPU
953 for_each_online_cpu(sibling) {
954 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
955 if (cp && cp->governor &&
956 (cpumask_test_cpu(cpu, cp->related_cpus))) {
957 policy->governor = cp->governor;
958 found = 1;
959 break;
960 }
961 }
962#endif
963 if (!found)
964 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* call driver. From then on the cpufreq must be able
966 * to accept all calls to ->verify and ->setpolicy for this CPU
967 */
968 ret = cpufreq_driver->init(policy);
969 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200970 pr_debug("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400971 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +0000973
974 /*
975 * affected cpus must always be the one, which are online. We aren't
976 * managing offline cpus here.
977 */
978 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
979
Mike Chan187d9f42008-12-04 12:19:17 -0800980 policy->user_policy.min = policy->min;
981 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Thomas Renningera1531ac2008-07-29 22:32:58 -0700983 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
984 CPUFREQ_START, policy);
985
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800986 ret = cpufreq_add_dev_policy(cpu, policy, dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200987 if (ret) {
988 if (ret > 0)
989 /* This is a managed cpu, symlink created,
990 exit with 0 */
991 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -0400992 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800995 ret = cpufreq_add_dev_interface(cpu, policy, dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -0400996 if (ret)
997 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -0500998
Lothar Waßmanndca02612008-05-29 17:54:52 +0200999 unlock_policy_rwsem_write(cpu);
1000
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001001 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 module_put(cpufreq_driver->owner);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001003 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006
1007
1008err_out_unregister:
1009 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001010 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001011 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1013
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001014 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 wait_for_completion(&policy->kobj_unregister);
1016
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001017err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001018 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001019 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001020err_free_cpumask:
1021 free_cpumask_var(policy->cpus);
1022err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024nomem_out:
1025 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001026module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return ret;
1028}
1029
1030
1031/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001032 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 *
1034 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001035 * Caller should already have policy_rwsem in write mode for this CPU.
1036 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001038static int __cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001040 unsigned int cpu = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 unsigned long flags;
1042 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001043 struct kobject *kobj;
1044 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045#ifdef CONFIG_SMP
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001046 struct device *cpu_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 unsigned int j;
1048#endif
1049
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001050 pr_debug("unregistering CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001053 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 if (!data) {
1056 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001057 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return -EINVAL;
1059 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001060 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062
1063#ifdef CONFIG_SMP
1064 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001065 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
1067 if (unlikely(cpu != data->cpu)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001068 pr_debug("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001069 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001071 kobj = &dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 cpufreq_cpu_put(data);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001073 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001074 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076 }
1077#endif
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001080
1081#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001082 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1083 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001084#endif
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 /* if we have other CPUs still registered, we need to unlink them,
1087 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001088 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1089 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 */
Rusty Russell835481d2009-01-04 05:18:06 -08001091 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1092 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (j == cpu)
1094 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001095 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097 }
1098
1099 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1100
Rusty Russell835481d2009-01-04 05:18:06 -08001101 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1102 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (j == cpu)
1104 continue;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001105 pr_debug("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001106#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001107 strncpy(per_cpu(cpufreq_cpu_governor, j),
1108 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001109#endif
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001110 cpu_dev = get_cpu_device(j);
1111 kobj = &cpu_dev->kobj;
Amerigo Wang499bca92010-03-04 03:23:46 -05001112 unlock_policy_rwsem_write(cpu);
1113 sysfs_remove_link(kobj, "cpufreq");
1114 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 cpufreq_cpu_put(data);
1116 }
1117 }
1118#else
1119 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1120#endif
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (cpufreq_driver->target)
1123 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001124
Amerigo Wang499bca92010-03-04 03:23:46 -05001125 kobj = &data->kobj;
1126 cmp = &data->kobj_unregister;
1127 unlock_policy_rwsem_write(cpu);
1128 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001131 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * unloading.
1133 */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001134 pr_debug("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001135 wait_for_completion(cmp);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001136 pr_debug("wait complete\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Amerigo Wang499bca92010-03-04 03:23:46 -05001138 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (cpufreq_driver->exit)
1140 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001141 unlock_policy_rwsem_write(cpu);
1142
Jacob Shin27ecddc2011-04-27 13:32:11 -05001143#ifdef CONFIG_HOTPLUG_CPU
1144 /* when the CPU which is the parent of the kobj is hotplugged
1145 * offline, check for siblings, and create cpufreq sysfs interface
1146 * and symlinks
1147 */
1148 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1149 /* first sibling now owns the new sysfs dir */
1150 cpumask_clear_cpu(cpu, data->cpus);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001151 cpufreq_add_dev(get_cpu_device(cpumask_first(data->cpus)), NULL);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001152
1153 /* finally remove our own symlink */
1154 lock_policy_rwsem_write(cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001155 __cpufreq_remove_dev(dev, sif);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001156 }
1157#endif
1158
Rusty Russell835481d2009-01-04 05:18:06 -08001159 free_cpumask_var(data->related_cpus);
1160 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 kfree(data);
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return 0;
1164}
1165
1166
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001167static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001168{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001169 unsigned int cpu = dev->id;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001170 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001171
1172 if (cpu_is_offline(cpu))
1173 return 0;
1174
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001175 if (unlikely(lock_policy_rwsem_write(cpu)))
1176 BUG();
1177
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001178 retval = __cpufreq_remove_dev(dev, sif);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001179 return retval;
1180}
1181
1182
David Howells65f27f32006-11-22 14:55:48 +00001183static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
David Howells65f27f32006-11-22 14:55:48 +00001185 struct cpufreq_policy *policy =
1186 container_of(work, struct cpufreq_policy, update);
1187 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001188 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 cpufreq_update_policy(cpu);
1190}
1191
1192/**
1193 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1194 * @cpu: cpu number
1195 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1196 * @new_freq: CPU frequency the CPU actually runs at
1197 *
Dave Jones29464f22009-01-18 01:37:11 -05001198 * We adjust to current frequency first, and need to clean up later.
1199 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301201static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1202 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct cpufreq_freqs freqs;
1205
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001206 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1208
1209 freqs.cpu = cpu;
1210 freqs.old = old_freq;
1211 freqs.new = new_freq;
1212 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1213 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1214}
1215
1216
Dave Jones32ee8c32006-02-28 00:43:23 -05001217/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301218 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001219 * @cpu: CPU number
1220 *
1221 * This is the last known freq, without actually getting it from the driver.
1222 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1223 */
1224unsigned int cpufreq_quick_get(unsigned int cpu)
1225{
1226 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301227 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001228
1229 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301230 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001231 cpufreq_cpu_put(policy);
1232 }
1233
Dave Jones4d34a672008-02-07 16:33:49 -05001234 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001235}
1236EXPORT_SYMBOL(cpufreq_quick_get);
1237
Jesse Barnes3d737102011-06-28 10:59:12 -07001238/**
1239 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1240 * @cpu: CPU number
1241 *
1242 * Just return the max possible frequency for a given CPU.
1243 */
1244unsigned int cpufreq_quick_get_max(unsigned int cpu)
1245{
1246 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1247 unsigned int ret_freq = 0;
1248
1249 if (policy) {
1250 ret_freq = policy->max;
1251 cpufreq_cpu_put(policy);
1252 }
1253
1254 return ret_freq;
1255}
1256EXPORT_SYMBOL(cpufreq_quick_get_max);
1257
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001258
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001259static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001261 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301262 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001265 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301267 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301269 if (ret_freq && policy->cur &&
1270 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1271 /* verify no discrepancy between actual and
1272 saved value exists */
1273 if (unlikely(ret_freq != policy->cur)) {
1274 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 schedule_work(&policy->update);
1276 }
1277 }
1278
Dave Jones4d34a672008-02-07 16:33:49 -05001279 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001280}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001282/**
1283 * cpufreq_get - get the current CPU frequency (in kHz)
1284 * @cpu: CPU number
1285 *
1286 * Get the CPU current (static) CPU frequency
1287 */
1288unsigned int cpufreq_get(unsigned int cpu)
1289{
1290 unsigned int ret_freq = 0;
1291 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1292
1293 if (!policy)
1294 goto out;
1295
1296 if (unlikely(lock_policy_rwsem_read(cpu)))
1297 goto out_policy;
1298
1299 ret_freq = __cpufreq_get(cpu);
1300
1301 unlock_policy_rwsem_read(cpu);
1302
1303out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001305out:
Dave Jones4d34a672008-02-07 16:33:49 -05001306 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308EXPORT_SYMBOL(cpufreq_get);
1309
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001310static struct subsys_interface cpufreq_interface = {
1311 .name = "cpufreq",
1312 .subsys = &cpu_subsys,
1313 .add_dev = cpufreq_add_dev,
1314 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001315};
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001319 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1320 *
1321 * This function is only executed for the boot processor. The other CPUs
1322 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001323 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001324static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001325{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301326 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001327
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001328 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001329 struct cpufreq_policy *cpu_policy;
1330
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001331 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001332
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001333 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001334 cpu_policy = cpufreq_cpu_get(cpu);
1335 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001336 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001337
1338 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001339 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001340 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001341 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1342 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001343 }
1344
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001345 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001346 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001347}
1348
1349/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001350 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 *
1352 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001353 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1354 * restored. It will verify that the current freq is in sync with
1355 * what we believe it to be. This is a bit later than when it
1356 * should be, but nonethteless it's better than calling
1357 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001358 *
1359 * This function is only executed for the boot CPU. The other CPUs have not
1360 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001362static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301364 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001365
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001366 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct cpufreq_policy *cpu_policy;
1368
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001369 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001371 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 cpu_policy = cpufreq_cpu_get(cpu);
1373 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001374 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (cpufreq_driver->resume) {
1377 ret = cpufreq_driver->resume(cpu_policy);
1378 if (ret) {
1379 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1380 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001381 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
1383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001386
Dave Jonesc9060492008-02-07 16:32:18 -05001387fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001391static struct syscore_ops cpufreq_syscore_ops = {
1392 .suspend = cpufreq_bp_suspend,
1393 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394};
1395
1396
1397/*********************************************************************
1398 * NOTIFIER LISTS INTERFACE *
1399 *********************************************************************/
1400
1401/**
1402 * cpufreq_register_notifier - register a driver with cpufreq
1403 * @nb: notifier function to register
1404 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1405 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001406 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * are notified about clock rate changes (once before and once after
1408 * the transition), or a list of drivers that are notified about
1409 * changes in cpufreq policy.
1410 *
1411 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001412 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
1414int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1415{
1416 int ret;
1417
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001418 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 switch (list) {
1421 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001422 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001423 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 break;
1425 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001426 ret = blocking_notifier_chain_register(
1427 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 break;
1429 default:
1430 ret = -EINVAL;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
1433 return ret;
1434}
1435EXPORT_SYMBOL(cpufreq_register_notifier);
1436
1437
1438/**
1439 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1440 * @nb: notifier block to be unregistered
1441 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1442 *
1443 * Remove a driver from the CPU frequency notifier list.
1444 *
1445 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001446 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
1448int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1449{
1450 int ret;
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 switch (list) {
1453 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001454 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001455 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001458 ret = blocking_notifier_chain_unregister(
1459 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break;
1461 default:
1462 ret = -EINVAL;
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return ret;
1466}
1467EXPORT_SYMBOL(cpufreq_unregister_notifier);
1468
1469
1470/*********************************************************************
1471 * GOVERNORS *
1472 *********************************************************************/
1473
1474
1475int __cpufreq_driver_target(struct cpufreq_policy *policy,
1476 unsigned int target_freq,
1477 unsigned int relation)
1478{
1479 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001480 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001481
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001482 if (cpufreq_disabled())
1483 return -ENODEV;
1484
Viresh Kumar72499242012-10-31 01:28:21 +01001485 /* Make sure that target_freq is within supported range */
1486 if (target_freq > policy->max)
1487 target_freq = policy->max;
1488 if (target_freq < policy->min)
1489 target_freq = policy->min;
1490
1491 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1492 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001493
1494 if (target_freq == policy->cur)
1495 return 0;
1496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1498 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 return retval;
1501}
1502EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504int cpufreq_driver_target(struct cpufreq_policy *policy,
1505 unsigned int target_freq,
1506 unsigned int relation)
1507{
Julia Lawallf1829e42008-07-25 22:44:53 +02001508 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 policy = cpufreq_cpu_get(policy->cpu);
1511 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001512 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001514 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001515 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 ret = __cpufreq_driver_target(policy, target_freq, relation);
1518
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001519 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Julia Lawallf1829e42008-07-25 22:44:53 +02001521fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001523no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 return ret;
1525}
1526EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1527
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001528int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001529{
1530 int ret = 0;
1531
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001532 if (!(cpu_online(cpu) && cpufreq_driver->getavg))
1533 return 0;
1534
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001535 policy = cpufreq_cpu_get(policy->cpu);
1536 if (!policy)
1537 return -EINVAL;
1538
Viresh Kumar0676f7f2012-10-24 23:39:48 +02001539 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001540
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001541 cpufreq_cpu_put(policy);
1542 return ret;
1543}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001544EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001545
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001546/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001547 * when "event" is CPUFREQ_GOV_LIMITS
1548 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301550static int __cpufreq_governor(struct cpufreq_policy *policy,
1551 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Dave Jonescc993ca2005-07-28 09:43:56 -07001553 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001554
1555 /* Only must be defined when default governor is known to have latency
1556 restrictions, like e.g. conservative or ondemand.
1557 That this is the case is already ensured in Kconfig
1558 */
1559#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1560 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1561#else
1562 struct cpufreq_governor *gov = NULL;
1563#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001564
1565 if (policy->governor->max_transition_latency &&
1566 policy->cpuinfo.transition_latency >
1567 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001568 if (!gov)
1569 return -EINVAL;
1570 else {
1571 printk(KERN_WARNING "%s governor failed, too long"
1572 " transition latency of HW, fallback"
1573 " to %s governor\n",
1574 policy->governor->name,
1575 gov->name);
1576 policy->governor = gov;
1577 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 if (!try_module_get(policy->governor->owner))
1581 return -EINVAL;
1582
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001583 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301584 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 ret = policy->governor->governor(policy, event);
1586
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301587 /* we keep one module reference alive for
1588 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 if ((event != CPUFREQ_GOV_START) || ret)
1590 module_put(policy->governor->owner);
1591 if ((event == CPUFREQ_GOV_STOP) && !ret)
1592 module_put(policy->governor->owner);
1593
1594 return ret;
1595}
1596
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598int cpufreq_register_governor(struct cpufreq_governor *governor)
1599{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001600 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
1602 if (!governor)
1603 return -EINVAL;
1604
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001605 if (cpufreq_disabled())
1606 return -ENODEV;
1607
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001608 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001609
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001610 err = -EBUSY;
1611 if (__find_governor(governor->name) == NULL) {
1612 err = 0;
1613 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Dave Jones32ee8c32006-02-28 00:43:23 -05001616 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001617 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1620
1621
1622void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1623{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001624#ifdef CONFIG_HOTPLUG_CPU
1625 int cpu;
1626#endif
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (!governor)
1629 return;
1630
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001631 if (cpufreq_disabled())
1632 return;
1633
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001634#ifdef CONFIG_HOTPLUG_CPU
1635 for_each_present_cpu(cpu) {
1636 if (cpu_online(cpu))
1637 continue;
1638 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1639 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1640 }
1641#endif
1642
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001643 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001645 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return;
1647}
1648EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1649
1650
1651
1652/*********************************************************************
1653 * POLICY INTERFACE *
1654 *********************************************************************/
1655
1656/**
1657 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001658 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1659 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 *
1661 * Reads the current cpufreq policy.
1662 */
1663int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1664{
1665 struct cpufreq_policy *cpu_policy;
1666 if (!policy)
1667 return -EINVAL;
1668
1669 cpu_policy = cpufreq_cpu_get(cpu);
1670 if (!cpu_policy)
1671 return -EINVAL;
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return 0;
1677}
1678EXPORT_SYMBOL(cpufreq_get_policy);
1679
1680
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001681/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301682 * data : current policy.
1683 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001684 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301685static int __cpufreq_set_policy(struct cpufreq_policy *data,
1686 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 int ret = 0;
1689
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001690 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 policy->min, policy->max);
1692
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301693 memcpy(&policy->cpuinfo, &data->cpuinfo,
1694 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Yi Yang53391fa2008-01-30 13:33:34 +01001696 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001697 ret = -EINVAL;
1698 goto error_out;
1699 }
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /* verify the cpu speed can be set within this limit */
1702 ret = cpufreq_driver->verify(policy);
1703 if (ret)
1704 goto error_out;
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001707 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1708 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001711 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1712 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 /* verify the cpu speed can be set within this limit,
1715 which might be different to the first one */
1716 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001717 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001721 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1722 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Dave Jones7d5e3502006-02-02 17:03:42 -05001724 data->min = policy->min;
1725 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001727 pr_debug("new min and max freqs are %u - %u kHz\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301728 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (cpufreq_driver->setpolicy) {
1731 data->policy = policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001732 pr_debug("setting range\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 ret = cpufreq_driver->setpolicy(policy);
1734 } else {
1735 if (policy->governor != data->governor) {
1736 /* save old, working values */
1737 struct cpufreq_governor *old_gov = data->governor;
1738
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001739 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001742 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1744
1745 /* start new governor */
1746 data->governor = policy->governor;
1747 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1748 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001749 pr_debug("starting governor %s failed\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301750 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (old_gov) {
1752 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301753 __cpufreq_governor(data,
1754 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 ret = -EINVAL;
1757 goto error_out;
1758 }
1759 /* might be a policy change, too, so fall through */
1760 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001761 pr_debug("governor: change or update limits\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1763 }
1764
Dave Jones7d5e3502006-02-02 17:03:42 -05001765error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return ret;
1767}
1768
1769/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1771 * @cpu: CPU which shall be re-evaluated
1772 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001773 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * at different times.
1775 */
1776int cpufreq_update_policy(unsigned int cpu)
1777{
1778 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1779 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001780 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Julia Lawallf1829e42008-07-25 22:44:53 +02001782 if (!data) {
1783 ret = -ENODEV;
1784 goto no_policy;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Julia Lawallf1829e42008-07-25 22:44:53 +02001787 if (unlikely(lock_policy_rwsem_write(cpu))) {
1788 ret = -EINVAL;
1789 goto fail;
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001792 pr_debug("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001793 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 policy.min = data->user_policy.min;
1795 policy.max = data->user_policy.max;
1796 policy.policy = data->user_policy.policy;
1797 policy.governor = data->user_policy.governor;
1798
Thomas Renninger0961dd02006-01-26 18:46:33 +01001799 /* BIOS might change freq behind our back
1800 -> ask driver for current freq and notify governors about a change */
1801 if (cpufreq_driver->get) {
1802 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001803 if (!data->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001804 pr_debug("Driver did not initialize current freq");
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001805 data->cur = policy.cur;
1806 } else {
1807 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301808 cpufreq_out_of_sync(cpu, data->cur,
1809 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001810 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001811 }
1812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 ret = __cpufreq_set_policy(data, &policy);
1814
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001815 unlock_policy_rwsem_write(cpu);
1816
Julia Lawallf1829e42008-07-25 22:44:53 +02001817fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001819no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return ret;
1821}
1822EXPORT_SYMBOL(cpufreq_update_policy);
1823
Satyam Sharmadd184a02007-10-02 13:28:14 -07001824static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001825 unsigned long action, void *hcpu)
1826{
1827 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001828 struct device *dev;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001829
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001830 dev = get_cpu_device(cpu);
1831 if (dev) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001832 switch (action) {
1833 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001834 case CPU_ONLINE_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001835 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001836 break;
1837 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001838 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001839 if (unlikely(lock_policy_rwsem_write(cpu)))
1840 BUG();
1841
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001842 __cpufreq_remove_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001843 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001844 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001845 case CPU_DOWN_FAILED_FROZEN:
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001846 cpufreq_add_dev(dev, NULL);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001847 break;
1848 }
1849 }
1850 return NOTIFY_OK;
1851}
1852
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001853static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001854 .notifier_call = cpufreq_cpu_callback,
1855};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857/*********************************************************************
1858 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1859 *********************************************************************/
1860
1861/**
1862 * cpufreq_register_driver - register a CPU Frequency driver
1863 * @driver_data: A struct cpufreq_driver containing the values#
1864 * submitted by the CPU Frequency driver.
1865 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001866 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001868 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
1870 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001871int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 unsigned long flags;
1874 int ret;
1875
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001876 if (cpufreq_disabled())
1877 return -ENODEV;
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!driver_data || !driver_data->verify || !driver_data->init ||
1880 ((!driver_data->setpolicy) && (!driver_data->target)))
1881 return -EINVAL;
1882
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001883 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 if (driver_data->setpolicy)
1886 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1887
1888 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1889 if (cpufreq_driver) {
1890 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1891 return -EBUSY;
1892 }
1893 cpufreq_driver = driver_data;
1894 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1895
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001896 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001897 if (ret)
1898 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001900 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 int i;
1902 ret = -ENODEV;
1903
1904 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001905 for (i = 0; i < nr_cpu_ids; i++)
1906 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001908 break;
1909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* if all ->init() calls failed, unregister */
1912 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001913 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301914 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001915 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 }
1918
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001919 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001920 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001922 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001923err_if_unreg:
1924 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001925err_null_driver:
1926 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1927 cpufreq_driver = NULL;
1928 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001929 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1932
1933
1934/**
1935 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1936 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001937 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 * the right to do so, i.e. if you have succeeded in initialising before!
1939 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1940 * currently not initialised.
1941 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001942int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 unsigned long flags;
1945
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001946 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001949 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001951 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001952 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1955 cpufreq_driver = NULL;
1956 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1957
1958 return 0;
1959}
1960EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001961
1962static int __init cpufreq_core_init(void)
1963{
1964 int cpu;
1965
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001966 if (cpufreq_disabled())
1967 return -ENODEV;
1968
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001969 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001970 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001971 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1972 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001973
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001974 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001975 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001976 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001977
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001978 return 0;
1979}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001980core_initcall(cpufreq_core_init);