blob: 7c10f96c5ae981361875e50ea36b028f59b31d32 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/notifier.h>
22#include <linux/cpufreq.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#include <linux/device.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080030#include <linux/mutex.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010031#include <linux/syscore_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Thomas Renninger6f4f2722010-04-20 13:17:36 +020033#include <trace/events/power.h>
34
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053035#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
36 "cpufreq-core", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/**
Dave Jonescd878472006-08-11 17:59:28 -040039 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * level driver of CPUFreq support, and its spinlock. This lock
41 * also protects the cpufreq_cpu_data array.
42 */
Dave Jones7d5e3502006-02-02 17:03:42 -050043static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070044static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070045#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040047static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static DEFINE_SPINLOCK(cpufreq_driver_lock);
50
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080051/*
52 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
53 * all cpufreq/hotplug/workqueue/etc related lock issues.
54 *
55 * The rules for this semaphore:
56 * - Any routine that wants to read from the policy structure will
57 * do a down_read on this semaphore.
58 * - Any routine that will write to the policy structure and/or may take away
59 * the policy altogether (eg. CPU hotplug), will hold this lock in write
60 * mode before doing so.
61 *
62 * Additional rules:
63 * - All holders of the lock should check to make sure that the CPU they
64 * are concerned with are online after they get the lock.
65 * - Governor routines that can be called in cpufreq hotplug path should not
66 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040067 * - Lock should not be held across
68 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080069 */
Tejun Heof1625062009-10-29 22:34:13 +090070static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080071static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
72
73#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050074static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075(int cpu) \
76{ \
Tejun Heof1625062009-10-29 22:34:13 +090077 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080078 BUG_ON(policy_cpu == -1); \
79 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
80 if (unlikely(!cpu_online(cpu))) { \
81 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
82 return -1; \
83 } \
84 \
85 return 0; \
86}
87
88lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080089
90lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080091
Amerigo Wang226528c2010-03-04 03:23:36 -050092static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080093{
Tejun Heof1625062009-10-29 22:34:13 +090094 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095 BUG_ON(policy_cpu == -1);
96 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
97}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080098
Amerigo Wang226528c2010-03-04 03:23:36 -050099static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800100{
Tejun Heof1625062009-10-29 22:34:13 +0900101 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800102 BUG_ON(policy_cpu == -1);
103 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
104}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800105
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500108static int __cpufreq_governor(struct cpufreq_policy *policy,
109 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800110static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000111static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500114 * Two notifier lists: the "policy" list is involved in the
115 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * "transition" list for kernel code that needs to handle
117 * changes to devices when the CPU clock speed changes.
118 * The mutex locks both lists.
119 */
Alan Sterne041c682006-03-27 01:16:30 -0800120static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700121static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200123static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700124static int __init init_cpufreq_transition_notifier_list(void)
125{
126 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200127 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700128 return 0;
129}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800130pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500133static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Dave Jones7d5e3502006-02-02 17:03:42 -0500135struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct cpufreq_policy *data;
138 unsigned long flags;
139
Mike Travis7a6aedf2008-03-25 15:06:53 -0700140 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 goto err_out;
142
143 /* get the cpufreq driver */
144 spin_lock_irqsave(&cpufreq_driver_lock, flags);
145
146 if (!cpufreq_driver)
147 goto err_out_unlock;
148
149 if (!try_module_get(cpufreq_driver->owner))
150 goto err_out_unlock;
151
152
153 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700154 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!data)
157 goto err_out_put_module;
158
159 if (!kobject_get(&data->kobj))
160 goto err_out_put_module;
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return data;
164
Dave Jones7d5e3502006-02-02 17:03:42 -0500165err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500167err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500169err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return NULL;
171}
172EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
173
Dave Jones7d5e3502006-02-02 17:03:42 -0500174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175void cpufreq_cpu_put(struct cpufreq_policy *data)
176{
177 kobject_put(&data->kobj);
178 module_put(cpufreq_driver->owner);
179}
180EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
181
182
183/*********************************************************************
184 * UNIFIED DEBUG HELPERS *
185 *********************************************************************/
186#ifdef CONFIG_CPU_FREQ_DEBUG
187
188/* what part(s) of the CPUfreq subsystem are debugged? */
189static unsigned int debug;
190
191/* is the debug output ratelimit'ed using printk_ratelimit? User can
192 * set or modify this value.
193 */
194static unsigned int debug_ratelimit = 1;
195
196/* is the printk_ratelimit'ing enabled? It's enabled after a successful
197 * loading of a cpufreq driver, temporarily disabled when a new policy
198 * is set, and disabled upon cpufreq driver removal
199 */
200static unsigned int disable_ratelimit = 1;
201static DEFINE_SPINLOCK(disable_ratelimit_lock);
202
Arjan van de Ven858119e2006-01-14 13:20:43 -0800203static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 unsigned long flags;
206
207 spin_lock_irqsave(&disable_ratelimit_lock, flags);
208 if (disable_ratelimit)
209 disable_ratelimit--;
210 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
211}
212
Arjan van de Ven858119e2006-01-14 13:20:43 -0800213static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 unsigned long flags;
216
217 spin_lock_irqsave(&disable_ratelimit_lock, flags);
218 disable_ratelimit++;
219 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
220}
221
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530222void cpufreq_debug_printk(unsigned int type, const char *prefix,
Dave Jones905d77c2008-03-05 14:28:32 -0500223 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 char s[256];
226 va_list args;
227 unsigned int len;
228 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 WARN_ON(!prefix);
231 if (type & debug) {
232 spin_lock_irqsave(&disable_ratelimit_lock, flags);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530233 if (!disable_ratelimit && debug_ratelimit
234 && !printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
236 return;
237 }
238 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
239
240 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
241
242 va_start(args, fmt);
243 len += vsnprintf(&s[len], (256 - len), fmt, args);
244 va_end(args);
245
246 printk(s);
247
248 WARN_ON(len < 5);
249 }
250}
251EXPORT_SYMBOL(cpufreq_debug_printk);
252
253
254module_param(debug, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530255MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
256 " 2 to debug drivers, and 4 to debug governors.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258module_param(debug_ratelimit, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530259MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
260 " set to 0 to disable ratelimiting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262#else /* !CONFIG_CPU_FREQ_DEBUG */
263
264static inline void cpufreq_debug_enable_ratelimit(void) { return; }
265static inline void cpufreq_debug_disable_ratelimit(void) { return; }
266
267#endif /* CONFIG_CPU_FREQ_DEBUG */
268
269
270/*********************************************************************
271 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
272 *********************************************************************/
273
274/**
275 * adjust_jiffies - adjust the system "loops_per_jiffy"
276 *
277 * This function alters the system "loops_per_jiffy" for the clock
278 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500279 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * per-CPU loops_per_jiffy value wherever possible.
281 */
282#ifndef CONFIG_SMP
283static unsigned long l_p_j_ref;
284static unsigned int l_p_j_ref_freq;
285
Arjan van de Ven858119e2006-01-14 13:20:43 -0800286static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 if (ci->flags & CPUFREQ_CONST_LOOPS)
289 return;
290
291 if (!l_p_j_ref_freq) {
292 l_p_j_ref = loops_per_jiffy;
293 l_p_j_ref_freq = ci->old;
Joe Perchesa4a9df52007-11-19 17:48:06 -0800294 dprintk("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530295 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
298 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700299 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530300 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
301 ci->new);
Joe Perchesa4a9df52007-11-19 17:48:06 -0800302 dprintk("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530303 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305}
306#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530307static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
308{
309 return;
310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311#endif
312
313
314/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800315 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
316 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800318 * This function calls the transition notifiers and the "adjust_jiffies"
319 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500320 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 */
322void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
323{
Dave Jonese4472cb2006-01-31 15:53:55 -0800324 struct cpufreq_policy *policy;
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 BUG_ON(irqs_disabled());
327
328 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800329 dprintk("notification %u of frequency transition to %u kHz\n",
330 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Mike Travis7a6aedf2008-03-25 15:06:53 -0700332 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500336 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800337 * which is not equal to what the cpufreq core thinks is
338 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
340 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800341 if ((policy) && (policy->cpu == freqs->cpu) &&
342 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200343 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800344 " %u, cpufreq assumed %u kHz.\n",
345 freqs->old, policy->cur);
346 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700349 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800350 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
352 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 case CPUFREQ_POSTCHANGE:
355 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200356 dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
357 (unsigned long)freqs->cpu);
358 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100359 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700360 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800361 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800362 if (likely(policy) && likely(policy->cpu == freqs->cpu))
363 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 break;
365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
368
369
370
371/*********************************************************************
372 * SYSFS INTERFACE *
373 *********************************************************************/
374
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700375static struct cpufreq_governor *__find_governor(const char *str_governor)
376{
377 struct cpufreq_governor *t;
378
379 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500380 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381 return t;
382
383 return NULL;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/**
387 * cpufreq_parse_governor - parse a governor string
388 */
Dave Jones905d77c2008-03-05 14:28:32 -0500389static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 struct cpufreq_governor **governor)
391{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700392 int err = -EINVAL;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700395 goto out;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (cpufreq_driver->setpolicy) {
398 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
399 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700400 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530401 } else if (!strnicmp(str_governor, "powersave",
402 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700404 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700406 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700408
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800409 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700410
411 t = __find_governor(str_governor);
412
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700413 if (t == NULL) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530414 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
415 str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700416
417 if (name) {
418 int ret;
419
420 mutex_unlock(&cpufreq_governor_mutex);
Chris Wright326f6a52008-06-06 21:26:02 -0700421 ret = request_module("%s", name);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700422 mutex_lock(&cpufreq_governor_mutex);
423
424 if (ret == 0)
425 t = __find_governor(str_governor);
426 }
427
428 kfree(name);
429 }
430
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700431 if (t != NULL) {
432 *governor = t;
433 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700435
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800436 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 }
Dave Jones29464f22009-01-18 01:37:11 -0500438out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530444 * cpufreq_per_cpu_attr_read() / show_##file_name() -
445 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 *
447 * Write out information from cpufreq_driver->policy[cpu]; object must be
448 * "unsigned int".
449 */
450
Dave Jones32ee8c32006-02-28 00:43:23 -0500451#define show_one(file_name, object) \
452static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500453(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500454{ \
Dave Jones29464f22009-01-18 01:37:11 -0500455 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458show_one(cpuinfo_min_freq, cpuinfo.min_freq);
459show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100460show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461show_one(scaling_min_freq, min);
462show_one(scaling_max_freq, max);
463show_one(scaling_cur_freq, cur);
464
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530465static int __cpufreq_set_policy(struct cpufreq_policy *data,
466 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468/**
469 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
470 */
471#define store_one(file_name, object) \
472static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500473(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{ \
475 unsigned int ret = -EINVAL; \
476 struct cpufreq_policy new_policy; \
477 \
478 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
479 if (ret) \
480 return -EINVAL; \
481 \
Dave Jones29464f22009-01-18 01:37:11 -0500482 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (ret != 1) \
484 return -EINVAL; \
485 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200486 ret = __cpufreq_set_policy(policy, &new_policy); \
487 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 \
489 return ret ? ret : count; \
490}
491
Dave Jones29464f22009-01-18 01:37:11 -0500492store_one(scaling_min_freq, min);
493store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495/**
496 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
497 */
Dave Jones905d77c2008-03-05 14:28:32 -0500498static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
499 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800501 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (!cur_freq)
503 return sprintf(buf, "<unknown>");
504 return sprintf(buf, "%u\n", cur_freq);
505}
506
507
508/**
509 * show_scaling_governor - show the current policy for the specified CPU
510 */
Dave Jones905d77c2008-03-05 14:28:32 -0500511static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Dave Jones29464f22009-01-18 01:37:11 -0500513 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return sprintf(buf, "powersave\n");
515 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
516 return sprintf(buf, "performance\n");
517 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500518 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
519 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -EINVAL;
521}
522
523
524/**
525 * store_scaling_governor - store policy for the specified CPU
526 */
Dave Jones905d77c2008-03-05 14:28:32 -0500527static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
528 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 unsigned int ret = -EINVAL;
531 char str_governor[16];
532 struct cpufreq_policy new_policy;
533
534 ret = cpufreq_get_policy(&new_policy, policy->cpu);
535 if (ret)
536 return ret;
537
Dave Jones29464f22009-01-18 01:37:11 -0500538 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ret != 1)
540 return -EINVAL;
541
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530542 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
543 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EINVAL;
545
Thomas Renninger7970e082006-04-13 15:14:04 +0200546 /* Do not use cpufreq_set_policy here or the user_policy.max
547 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200548 ret = __cpufreq_set_policy(policy, &new_policy);
549
550 policy->user_policy.policy = policy->policy;
551 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200552
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530553 if (ret)
554 return ret;
555 else
556 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559/**
560 * show_scaling_driver - show the cpufreq driver currently loaded
561 */
Dave Jones905d77c2008-03-05 14:28:32 -0500562static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
565}
566
567/**
568 * show_scaling_available_governors - show the available CPUfreq governors
569 */
Dave Jones905d77c2008-03-05 14:28:32 -0500570static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
571 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
573 ssize_t i = 0;
574 struct cpufreq_governor *t;
575
576 if (!cpufreq_driver->target) {
577 i += sprintf(buf, "performance powersave");
578 goto out;
579 }
580
581 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500582 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
583 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto out;
585 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
586 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500587out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 i += sprintf(&buf[i], "\n");
589 return i;
590}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700591
Rusty Russell835481d2009-01-04 05:18:06 -0800592static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
594 ssize_t i = 0;
595 unsigned int cpu;
596
Rusty Russell835481d2009-01-04 05:18:06 -0800597 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (i)
599 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
600 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
601 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 i += sprintf(&buf[i], "\n");
605 return i;
606}
607
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700608/**
609 * show_related_cpus - show the CPUs affected by each transition even if
610 * hw coordination is in use
611 */
612static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
613{
Rusty Russell835481d2009-01-04 05:18:06 -0800614 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700615 return show_cpus(policy->cpus, buf);
616 return show_cpus(policy->related_cpus, buf);
617}
618
619/**
620 * show_affected_cpus - show the CPUs affected by each transition
621 */
622static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
623{
624 return show_cpus(policy->cpus, buf);
625}
626
Venki Pallipadi9e769882007-10-26 10:18:21 -0700627static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500628 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700629{
630 unsigned int freq = 0;
631 unsigned int ret;
632
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700633 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700634 return -EINVAL;
635
636 ret = sscanf(buf, "%u", &freq);
637 if (ret != 1)
638 return -EINVAL;
639
640 policy->governor->store_setspeed(policy, freq);
641
642 return count;
643}
644
645static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
646{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700647 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700648 return sprintf(buf, "<unsupported>\n");
649
650 return policy->governor->show_setspeed(policy, buf);
651}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Thomas Renningere2f74f32009-11-19 12:31:01 +0100653/**
654 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
655 */
656static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
657{
658 unsigned int limit;
659 int ret;
660 if (cpufreq_driver->bios_limit) {
661 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
662 if (!ret)
663 return sprintf(buf, "%u\n", limit);
664 }
665 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
666}
667
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200668cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
669cpufreq_freq_attr_ro(cpuinfo_min_freq);
670cpufreq_freq_attr_ro(cpuinfo_max_freq);
671cpufreq_freq_attr_ro(cpuinfo_transition_latency);
672cpufreq_freq_attr_ro(scaling_available_governors);
673cpufreq_freq_attr_ro(scaling_driver);
674cpufreq_freq_attr_ro(scaling_cur_freq);
675cpufreq_freq_attr_ro(bios_limit);
676cpufreq_freq_attr_ro(related_cpus);
677cpufreq_freq_attr_ro(affected_cpus);
678cpufreq_freq_attr_rw(scaling_min_freq);
679cpufreq_freq_attr_rw(scaling_max_freq);
680cpufreq_freq_attr_rw(scaling_governor);
681cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Jones905d77c2008-03-05 14:28:32 -0500683static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 &cpuinfo_min_freq.attr,
685 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100686 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 &scaling_min_freq.attr,
688 &scaling_max_freq.attr,
689 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700690 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 &scaling_governor.attr,
692 &scaling_driver.attr,
693 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700694 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 NULL
696};
697
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200698struct kobject *cpufreq_global_kobject;
699EXPORT_SYMBOL(cpufreq_global_kobject);
700
Dave Jones29464f22009-01-18 01:37:11 -0500701#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
702#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Dave Jones29464f22009-01-18 01:37:11 -0500704static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Dave Jones905d77c2008-03-05 14:28:32 -0500706 struct cpufreq_policy *policy = to_policy(kobj);
707 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500708 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 policy = cpufreq_cpu_get(policy->cpu);
710 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500711 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800712
713 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500714 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800715
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530716 if (fattr->show)
717 ret = fattr->show(policy, buf);
718 else
719 ret = -EIO;
720
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800721 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500722fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500724no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return ret;
726}
727
Dave Jones905d77c2008-03-05 14:28:32 -0500728static ssize_t store(struct kobject *kobj, struct attribute *attr,
729 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Dave Jones905d77c2008-03-05 14:28:32 -0500731 struct cpufreq_policy *policy = to_policy(kobj);
732 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500733 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 policy = cpufreq_cpu_get(policy->cpu);
735 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500736 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800737
738 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500739 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800740
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530741 if (fattr->store)
742 ret = fattr->store(policy, buf, count);
743 else
744 ret = -EIO;
745
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800746 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500747fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500749no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return ret;
751}
752
Dave Jones905d77c2008-03-05 14:28:32 -0500753static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Dave Jones905d77c2008-03-05 14:28:32 -0500755 struct cpufreq_policy *policy = to_policy(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 dprintk("last reference is dropped\n");
757 complete(&policy->kobj_unregister);
758}
759
Emese Revfy52cf25d2010-01-19 02:58:23 +0100760static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 .show = show,
762 .store = store,
763};
764
765static struct kobj_type ktype_cpufreq = {
766 .sysfs_ops = &sysfs_ops,
767 .default_attrs = default_attrs,
768 .release = cpufreq_sysfs_release,
769};
770
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200771/*
772 * Returns:
773 * Negative: Failure
774 * 0: Success
775 * Positive: When we have a managed CPU and the sysfs got symlinked
776 */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700777static int cpufreq_add_dev_policy(unsigned int cpu,
778 struct cpufreq_policy *policy,
779 struct sys_device *sys_dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400780{
781 int ret = 0;
782#ifdef CONFIG_SMP
783 unsigned long flags;
784 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400785#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400786 struct cpufreq_governor *gov;
787
788 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
789 if (gov) {
790 policy->governor = gov;
Dave Jonesecf7e462009-07-08 18:48:47 -0400791 dprintk("Restoring governor %s for cpu %d\n",
792 policy->governor->name, cpu);
793 }
794#endif
795
796 for_each_cpu(j, policy->cpus) {
797 struct cpufreq_policy *managed_policy;
798
799 if (cpu == j)
800 continue;
801
802 /* Check for existing affected CPUs.
803 * They may not be aware of it due to CPU Hotplug.
804 * cpufreq_cpu_put is called when the device is removed
805 * in __cpufreq_remove_dev()
806 */
807 managed_policy = cpufreq_cpu_get(j);
808 if (unlikely(managed_policy)) {
809
810 /* Set proper policy_cpu */
811 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900812 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400813
814 if (lock_policy_rwsem_write(cpu) < 0) {
815 /* Should not go through policy unlock path */
816 if (cpufreq_driver->exit)
817 cpufreq_driver->exit(policy);
818 cpufreq_cpu_put(managed_policy);
819 return -EBUSY;
820 }
821
822 spin_lock_irqsave(&cpufreq_driver_lock, flags);
823 cpumask_copy(managed_policy->cpus, policy->cpus);
824 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
825 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
826
827 dprintk("CPU already managed, adding link\n");
828 ret = sysfs_create_link(&sys_dev->kobj,
829 &managed_policy->kobj,
830 "cpufreq");
831 if (ret)
832 cpufreq_cpu_put(managed_policy);
833 /*
834 * Success. We only needed to be added to the mask.
835 * Call driver->exit() because only the cpu parent of
836 * the kobj needed to call init().
837 */
838 if (cpufreq_driver->exit)
839 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200840
841 if (!ret)
842 return 1;
843 else
844 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400845 }
846 }
847#endif
848 return ret;
849}
850
851
Dave Jones19d6f7e2009-07-08 17:35:39 -0400852/* symlink affected CPUs */
Alex Chiangcf3289d02009-11-17 20:27:08 -0700853static int cpufreq_add_dev_symlink(unsigned int cpu,
854 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400855{
856 unsigned int j;
857 int ret = 0;
858
859 for_each_cpu(j, policy->cpus) {
860 struct cpufreq_policy *managed_policy;
861 struct sys_device *cpu_sys_dev;
862
863 if (j == cpu)
864 continue;
865 if (!cpu_online(j))
866 continue;
867
868 dprintk("CPU %u already managed, adding link\n", j);
869 managed_policy = cpufreq_cpu_get(cpu);
870 cpu_sys_dev = get_cpu_sysdev(j);
871 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
872 "cpufreq");
873 if (ret) {
874 cpufreq_cpu_put(managed_policy);
875 return ret;
876 }
877 }
878 return ret;
879}
880
Alex Chiangcf3289d02009-11-17 20:27:08 -0700881static int cpufreq_add_dev_interface(unsigned int cpu,
882 struct cpufreq_policy *policy,
883 struct sys_device *sys_dev)
Dave Jones909a6942009-07-08 18:05:42 -0400884{
Dave Jonesecf7e462009-07-08 18:48:47 -0400885 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400886 struct freq_attr **drv_attr;
887 unsigned long flags;
888 int ret = 0;
889 unsigned int j;
890
891 /* prepare interface data */
892 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
893 &sys_dev->kobj, "cpufreq");
894 if (ret)
895 return ret;
896
897 /* set up files for this cpu device */
898 drv_attr = cpufreq_driver->attr;
899 while ((drv_attr) && (*drv_attr)) {
900 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
901 if (ret)
902 goto err_out_kobj_put;
903 drv_attr++;
904 }
905 if (cpufreq_driver->get) {
906 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
907 if (ret)
908 goto err_out_kobj_put;
909 }
910 if (cpufreq_driver->target) {
911 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
912 if (ret)
913 goto err_out_kobj_put;
914 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100915 if (cpufreq_driver->bios_limit) {
916 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
917 if (ret)
918 goto err_out_kobj_put;
919 }
Dave Jones909a6942009-07-08 18:05:42 -0400920
921 spin_lock_irqsave(&cpufreq_driver_lock, flags);
922 for_each_cpu(j, policy->cpus) {
Julia Lawallbec037a2010-08-05 22:23:00 +0200923 if (!cpu_online(j))
924 continue;
Dave Jones909a6942009-07-08 18:05:42 -0400925 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900926 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400927 }
928 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
929
930 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400931 if (ret)
932 goto err_out_kobj_put;
933
934 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
935 /* assure that the starting sequence is run in __cpufreq_set_policy */
936 policy->governor = NULL;
937
938 /* set default policy */
939 ret = __cpufreq_set_policy(policy, &new_policy);
940 policy->user_policy.policy = policy->policy;
941 policy->user_policy.governor = policy->governor;
942
943 if (ret) {
944 dprintk("setting policy failed\n");
945 if (cpufreq_driver->exit)
946 cpufreq_driver->exit(policy);
947 }
Dave Jones909a6942009-07-08 18:05:42 -0400948 return ret;
949
950err_out_kobj_put:
951 kobject_put(&policy->kobj);
952 wait_for_completion(&policy->kobj_unregister);
953 return ret;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957/**
958 * cpufreq_add_dev - add a CPU device
959 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500960 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400961 *
962 * The Oracle says: try running cpufreq registration/unregistration concurrently
963 * with with cpu hotplugging and all hell will break loose. Tried to clean this
964 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 */
Dave Jones905d77c2008-03-05 14:28:32 -0500966static int cpufreq_add_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 unsigned int cpu = sys_dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500969 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 unsigned long flags;
972 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500973#ifdef CONFIG_HOTPLUG_CPU
974 int sibling;
975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Ashok Rajc32b6b82005-10-30 14:59:54 -0800977 if (cpu_is_offline(cpu))
978 return 0;
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 cpufreq_debug_disable_ratelimit();
981 dprintk("adding CPU %u\n", cpu);
982
983#ifdef CONFIG_SMP
984 /* check whether a different CPU already registered this
985 * CPU because it is in the same boat. */
986 policy = cpufreq_cpu_get(cpu);
987 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500988 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 cpufreq_debug_enable_ratelimit();
990 return 0;
991 }
992#endif
993
994 if (!try_module_get(cpufreq_driver->owner)) {
995 ret = -EINVAL;
996 goto module_out;
997 }
998
Dave Jones059019a2009-07-08 16:30:03 -0400999 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -07001000 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -04001001 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001003
1004 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001005 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -04001006
1007 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001008 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
1010 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -08001011 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001013 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001014 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001015 ret = (lock_policy_rwsem_write(cpu) < 0);
1016 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001019 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Thomas Renninger8122c6c2007-10-02 13:28:09 -07001021 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001022#ifdef CONFIG_HOTPLUG_CPU
1023 for_each_online_cpu(sibling) {
1024 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1025 if (cp && cp->governor &&
1026 (cpumask_test_cpu(cpu, cp->related_cpus))) {
1027 policy->governor = cp->governor;
1028 found = 1;
1029 break;
1030 }
1031 }
1032#endif
1033 if (!found)
1034 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 /* call driver. From then on the cpufreq must be able
1036 * to accept all calls to ->verify and ->setpolicy for this CPU
1037 */
1038 ret = cpufreq_driver->init(policy);
1039 if (ret) {
1040 dprintk("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001041 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
Mike Chan187d9f42008-12-04 12:19:17 -08001043 policy->user_policy.min = policy->min;
1044 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Thomas Renningera1531ac2008-07-29 22:32:58 -07001046 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1047 CPUFREQ_START, policy);
1048
Dave Jonesecf7e462009-07-08 18:48:47 -04001049 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001050 if (ret) {
1051 if (ret > 0)
1052 /* This is a managed cpu, symlink created,
1053 exit with 0 */
1054 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001055 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Dave Jones909a6942009-07-08 18:05:42 -04001058 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001059 if (ret)
1060 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001061
Lothar Waßmanndca02612008-05-29 17:54:52 +02001062 unlock_policy_rwsem_write(cpu);
1063
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001064 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 dprintk("initialization complete\n");
1067 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -05001068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 return 0;
1070
1071
1072err_out_unregister:
1073 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001074 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001075 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1077
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001078 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 wait_for_completion(&policy->kobj_unregister);
1080
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001081err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001082 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001083 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001084err_free_cpumask:
1085 free_cpumask_var(policy->cpus);
1086err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088nomem_out:
1089 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001090module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 cpufreq_debug_enable_ratelimit();
1092 return ret;
1093}
1094
1095
1096/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001097 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 *
1099 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001100 * Caller should already have policy_rwsem in write mode for this CPU.
1101 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 */
Dave Jones905d77c2008-03-05 14:28:32 -05001103static int __cpufreq_remove_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 unsigned int cpu = sys_dev->id;
1106 unsigned long flags;
1107 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001108 struct kobject *kobj;
1109 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -08001111 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 unsigned int j;
1113#endif
1114
1115 cpufreq_debug_disable_ratelimit();
1116 dprintk("unregistering CPU %u\n", cpu);
1117
1118 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001119 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 if (!data) {
1122 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001124 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return -EINVAL;
1126 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001127 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129
1130#ifdef CONFIG_SMP
1131 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001132 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 */
1134 if (unlikely(cpu != data->cpu)) {
1135 dprintk("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001136 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Amerigo Wang499bca92010-03-04 03:23:46 -05001138 kobj = &sys_dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 cpufreq_cpu_put(data);
1140 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001141 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001142 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return 0;
1144 }
1145#endif
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001148
1149#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001150 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1151 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001152#endif
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 /* if we have other CPUs still registered, we need to unlink them,
1155 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001156 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1157 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Rusty Russell835481d2009-01-04 05:18:06 -08001159 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1160 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (j == cpu)
1162 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001163 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 }
1166
1167 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1168
Rusty Russell835481d2009-01-04 05:18:06 -08001169 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1170 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (j == cpu)
1172 continue;
1173 dprintk("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001174#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001175 strncpy(per_cpu(cpufreq_cpu_governor, j),
1176 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001177#endif
Ashok Rajd434fca2005-10-30 14:59:52 -08001178 cpu_sys_dev = get_cpu_sysdev(j);
Amerigo Wang499bca92010-03-04 03:23:46 -05001179 kobj = &cpu_sys_dev->kobj;
1180 unlock_policy_rwsem_write(cpu);
1181 sysfs_remove_link(kobj, "cpufreq");
1182 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 cpufreq_cpu_put(data);
1184 }
1185 }
1186#else
1187 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1188#endif
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 if (cpufreq_driver->target)
1191 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001192
Amerigo Wang499bca92010-03-04 03:23:46 -05001193 kobj = &data->kobj;
1194 cmp = &data->kobj_unregister;
1195 unlock_policy_rwsem_write(cpu);
1196 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001199 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 * unloading.
1201 */
1202 dprintk("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001203 wait_for_completion(cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 dprintk("wait complete\n");
1205
Amerigo Wang499bca92010-03-04 03:23:46 -05001206 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (cpufreq_driver->exit)
1208 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001209 unlock_policy_rwsem_write(cpu);
1210
Jacob Shin27ecddc2011-04-27 13:32:11 -05001211 cpufreq_debug_enable_ratelimit();
1212
1213#ifdef CONFIG_HOTPLUG_CPU
1214 /* when the CPU which is the parent of the kobj is hotplugged
1215 * offline, check for siblings, and create cpufreq sysfs interface
1216 * and symlinks
1217 */
1218 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1219 /* first sibling now owns the new sysfs dir */
1220 cpumask_clear_cpu(cpu, data->cpus);
1221 cpufreq_add_dev(get_cpu_sysdev(cpumask_first(data->cpus)));
1222
1223 /* finally remove our own symlink */
1224 lock_policy_rwsem_write(cpu);
1225 __cpufreq_remove_dev(sys_dev);
1226 }
1227#endif
1228
Rusty Russell835481d2009-01-04 05:18:06 -08001229 free_cpumask_var(data->related_cpus);
1230 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 kfree(data);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return 0;
1234}
1235
1236
Dave Jones905d77c2008-03-05 14:28:32 -05001237static int cpufreq_remove_dev(struct sys_device *sys_dev)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001238{
1239 unsigned int cpu = sys_dev->id;
1240 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001241
1242 if (cpu_is_offline(cpu))
1243 return 0;
1244
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001245 if (unlikely(lock_policy_rwsem_write(cpu)))
1246 BUG();
1247
1248 retval = __cpufreq_remove_dev(sys_dev);
1249 return retval;
1250}
1251
1252
David Howells65f27f32006-11-22 14:55:48 +00001253static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
David Howells65f27f32006-11-22 14:55:48 +00001255 struct cpufreq_policy *policy =
1256 container_of(work, struct cpufreq_policy, update);
1257 unsigned int cpu = policy->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 dprintk("handle_update for cpu %u called\n", cpu);
1259 cpufreq_update_policy(cpu);
1260}
1261
1262/**
1263 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1264 * @cpu: cpu number
1265 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1266 * @new_freq: CPU frequency the CPU actually runs at
1267 *
Dave Jones29464f22009-01-18 01:37:11 -05001268 * We adjust to current frequency first, and need to clean up later.
1269 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301271static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1272 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
1274 struct cpufreq_freqs freqs;
1275
Jan Beulichb10eec22006-04-28 13:47:13 +02001276 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1278
1279 freqs.cpu = cpu;
1280 freqs.old = old_freq;
1281 freqs.new = new_freq;
1282 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1283 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1284}
1285
1286
Dave Jones32ee8c32006-02-28 00:43:23 -05001287/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301288 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001289 * @cpu: CPU number
1290 *
1291 * This is the last known freq, without actually getting it from the driver.
1292 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1293 */
1294unsigned int cpufreq_quick_get(unsigned int cpu)
1295{
1296 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301297 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001298
1299 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301300 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001301 cpufreq_cpu_put(policy);
1302 }
1303
Dave Jones4d34a672008-02-07 16:33:49 -05001304 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001305}
1306EXPORT_SYMBOL(cpufreq_quick_get);
1307
1308
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001309static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001311 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301312 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001315 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301317 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301319 if (ret_freq && policy->cur &&
1320 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1321 /* verify no discrepancy between actual and
1322 saved value exists */
1323 if (unlikely(ret_freq != policy->cur)) {
1324 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 schedule_work(&policy->update);
1326 }
1327 }
1328
Dave Jones4d34a672008-02-07 16:33:49 -05001329 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001332/**
1333 * cpufreq_get - get the current CPU frequency (in kHz)
1334 * @cpu: CPU number
1335 *
1336 * Get the CPU current (static) CPU frequency
1337 */
1338unsigned int cpufreq_get(unsigned int cpu)
1339{
1340 unsigned int ret_freq = 0;
1341 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1342
1343 if (!policy)
1344 goto out;
1345
1346 if (unlikely(lock_policy_rwsem_read(cpu)))
1347 goto out_policy;
1348
1349 ret_freq = __cpufreq_get(cpu);
1350
1351 unlock_policy_rwsem_read(cpu);
1352
1353out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001355out:
Dave Jones4d34a672008-02-07 16:33:49 -05001356 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357}
1358EXPORT_SYMBOL(cpufreq_get);
1359
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001360static struct sysdev_driver cpufreq_sysdev_driver = {
1361 .add = cpufreq_add_dev,
1362 .remove = cpufreq_remove_dev,
1363};
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001367 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1368 *
1369 * This function is only executed for the boot processor. The other CPUs
1370 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001371 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001372static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001373{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301374 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001375
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001376 int cpu = smp_processor_id();
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001377 struct cpufreq_policy *cpu_policy;
1378
Dave Jones0e37b152006-09-26 23:02:34 -04001379 dprintk("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001380
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001381 /* If there's no policy for the boot CPU, we have nothing to do. */
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001382 cpu_policy = cpufreq_cpu_get(cpu);
1383 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001384 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001385
1386 if (cpufreq_driver->suspend) {
Rafael J. Wysocki7ca64e22011-03-10 21:13:05 +01001387 ret = cpufreq_driver->suspend(cpu_policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001388 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001389 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1390 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001391 }
1392
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001393 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001394 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001395}
1396
1397/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001398 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 *
1400 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001401 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1402 * restored. It will verify that the current freq is in sync with
1403 * what we believe it to be. This is a bit later than when it
1404 * should be, but nonethteless it's better than calling
1405 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001406 *
1407 * This function is only executed for the boot CPU. The other CPUs have not
1408 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001410static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301412 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001413
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001414 int cpu = smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 struct cpufreq_policy *cpu_policy;
1416
1417 dprintk("resuming cpu %u\n", cpu);
1418
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001419 /* If there's no policy for the boot CPU, we have nothing to do. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 cpu_policy = cpufreq_cpu_get(cpu);
1421 if (!cpu_policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001422 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 if (cpufreq_driver->resume) {
1425 ret = cpufreq_driver->resume(cpu_policy);
1426 if (ret) {
1427 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1428 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001429 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431 }
1432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001434
Dave Jonesc9060492008-02-07 16:32:18 -05001435fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001439static struct syscore_ops cpufreq_syscore_ops = {
1440 .suspend = cpufreq_bp_suspend,
1441 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442};
1443
1444
1445/*********************************************************************
1446 * NOTIFIER LISTS INTERFACE *
1447 *********************************************************************/
1448
1449/**
1450 * cpufreq_register_notifier - register a driver with cpufreq
1451 * @nb: notifier function to register
1452 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1453 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001454 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 * are notified about clock rate changes (once before and once after
1456 * the transition), or a list of drivers that are notified about
1457 * changes in cpufreq policy.
1458 *
1459 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001460 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1463{
1464 int ret;
1465
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001466 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 switch (list) {
1469 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001470 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001471 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001474 ret = blocking_notifier_chain_register(
1475 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 break;
1477 default:
1478 ret = -EINVAL;
1479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481 return ret;
1482}
1483EXPORT_SYMBOL(cpufreq_register_notifier);
1484
1485
1486/**
1487 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1488 * @nb: notifier block to be unregistered
1489 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1490 *
1491 * Remove a driver from the CPU frequency notifier list.
1492 *
1493 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001494 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
1496int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1497{
1498 int ret;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 switch (list) {
1501 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001502 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001503 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 break;
1505 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001506 ret = blocking_notifier_chain_unregister(
1507 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 break;
1509 default:
1510 ret = -EINVAL;
1511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 return ret;
1514}
1515EXPORT_SYMBOL(cpufreq_unregister_notifier);
1516
1517
1518/*********************************************************************
1519 * GOVERNORS *
1520 *********************************************************************/
1521
1522
1523int __cpufreq_driver_target(struct cpufreq_policy *policy,
1524 unsigned int target_freq,
1525 unsigned int relation)
1526{
1527 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1530 target_freq, relation);
1531 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1532 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return retval;
1535}
1536EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538int cpufreq_driver_target(struct cpufreq_policy *policy,
1539 unsigned int target_freq,
1540 unsigned int relation)
1541{
Julia Lawallf1829e42008-07-25 22:44:53 +02001542 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 policy = cpufreq_cpu_get(policy->cpu);
1545 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001546 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001548 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001549 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 ret = __cpufreq_driver_target(policy, target_freq, relation);
1552
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001553 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Julia Lawallf1829e42008-07-25 22:44:53 +02001555fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001557no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return ret;
1559}
1560EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1561
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001562int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001563{
1564 int ret = 0;
1565
1566 policy = cpufreq_cpu_get(policy->cpu);
1567 if (!policy)
1568 return -EINVAL;
1569
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001570 if (cpu_online(cpu) && cpufreq_driver->getavg)
1571 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001572
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001573 cpufreq_cpu_put(policy);
1574 return ret;
1575}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001576EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001577
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001578/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001579 * when "event" is CPUFREQ_GOV_LIMITS
1580 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301582static int __cpufreq_governor(struct cpufreq_policy *policy,
1583 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Dave Jonescc993ca2005-07-28 09:43:56 -07001585 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001586
1587 /* Only must be defined when default governor is known to have latency
1588 restrictions, like e.g. conservative or ondemand.
1589 That this is the case is already ensured in Kconfig
1590 */
1591#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1592 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1593#else
1594 struct cpufreq_governor *gov = NULL;
1595#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001596
1597 if (policy->governor->max_transition_latency &&
1598 policy->cpuinfo.transition_latency >
1599 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001600 if (!gov)
1601 return -EINVAL;
1602 else {
1603 printk(KERN_WARNING "%s governor failed, too long"
1604 " transition latency of HW, fallback"
1605 " to %s governor\n",
1606 policy->governor->name,
1607 gov->name);
1608 policy->governor = gov;
1609 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 if (!try_module_get(policy->governor->owner))
1613 return -EINVAL;
1614
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301615 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1616 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 ret = policy->governor->governor(policy, event);
1618
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301619 /* we keep one module reference alive for
1620 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if ((event != CPUFREQ_GOV_START) || ret)
1622 module_put(policy->governor->owner);
1623 if ((event == CPUFREQ_GOV_STOP) && !ret)
1624 module_put(policy->governor->owner);
1625
1626 return ret;
1627}
1628
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630int cpufreq_register_governor(struct cpufreq_governor *governor)
1631{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001632 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 if (!governor)
1635 return -EINVAL;
1636
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001637 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001638
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001639 err = -EBUSY;
1640 if (__find_governor(governor->name) == NULL) {
1641 err = 0;
1642 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Dave Jones32ee8c32006-02-28 00:43:23 -05001645 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001646 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1649
1650
1651void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1652{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001653#ifdef CONFIG_HOTPLUG_CPU
1654 int cpu;
1655#endif
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!governor)
1658 return;
1659
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001660#ifdef CONFIG_HOTPLUG_CPU
1661 for_each_present_cpu(cpu) {
1662 if (cpu_online(cpu))
1663 continue;
1664 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1665 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1666 }
1667#endif
1668
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001669 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001671 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 return;
1673}
1674EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1675
1676
1677
1678/*********************************************************************
1679 * POLICY INTERFACE *
1680 *********************************************************************/
1681
1682/**
1683 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001684 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1685 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 *
1687 * Reads the current cpufreq policy.
1688 */
1689int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1690{
1691 struct cpufreq_policy *cpu_policy;
1692 if (!policy)
1693 return -EINVAL;
1694
1695 cpu_policy = cpufreq_cpu_get(cpu);
1696 if (!cpu_policy)
1697 return -EINVAL;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return 0;
1703}
1704EXPORT_SYMBOL(cpufreq_get_policy);
1705
1706
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001707/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301708 * data : current policy.
1709 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001710 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301711static int __cpufreq_set_policy(struct cpufreq_policy *data,
1712 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int ret = 0;
1715
1716 cpufreq_debug_disable_ratelimit();
1717 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1718 policy->min, policy->max);
1719
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301720 memcpy(&policy->cpuinfo, &data->cpuinfo,
1721 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Yi Yang53391fa2008-01-30 13:33:34 +01001723 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001724 ret = -EINVAL;
1725 goto error_out;
1726 }
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 /* verify the cpu speed can be set within this limit */
1729 ret = cpufreq_driver->verify(policy);
1730 if (ret)
1731 goto error_out;
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001734 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1735 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001738 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1739 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /* verify the cpu speed can be set within this limit,
1742 which might be different to the first one */
1743 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001744 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001748 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1749 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Dave Jones7d5e3502006-02-02 17:03:42 -05001751 data->min = policy->min;
1752 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301754 dprintk("new min and max freqs are %u - %u kHz\n",
1755 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 if (cpufreq_driver->setpolicy) {
1758 data->policy = policy->policy;
1759 dprintk("setting range\n");
1760 ret = cpufreq_driver->setpolicy(policy);
1761 } else {
1762 if (policy->governor != data->governor) {
1763 /* save old, working values */
1764 struct cpufreq_governor *old_gov = data->governor;
1765
1766 dprintk("governor switch\n");
1767
1768 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001769 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1771
1772 /* start new governor */
1773 data->governor = policy->governor;
1774 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1775 /* new governor failed, so re-start old one */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301776 dprintk("starting governor %s failed\n",
1777 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (old_gov) {
1779 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301780 __cpufreq_governor(data,
1781 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783 ret = -EINVAL;
1784 goto error_out;
1785 }
1786 /* might be a policy change, too, so fall through */
1787 }
1788 dprintk("governor: change or update limits\n");
1789 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1790 }
1791
Dave Jones7d5e3502006-02-02 17:03:42 -05001792error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 cpufreq_debug_enable_ratelimit();
1794 return ret;
1795}
1796
1797/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1799 * @cpu: CPU which shall be re-evaluated
1800 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001801 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 * at different times.
1803 */
1804int cpufreq_update_policy(unsigned int cpu)
1805{
1806 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1807 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001808 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Julia Lawallf1829e42008-07-25 22:44:53 +02001810 if (!data) {
1811 ret = -ENODEV;
1812 goto no_policy;
1813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Julia Lawallf1829e42008-07-25 22:44:53 +02001815 if (unlikely(lock_policy_rwsem_write(cpu))) {
1816 ret = -EINVAL;
1817 goto fail;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001821 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 policy.min = data->user_policy.min;
1823 policy.max = data->user_policy.max;
1824 policy.policy = data->user_policy.policy;
1825 policy.governor = data->user_policy.governor;
1826
Thomas Renninger0961dd02006-01-26 18:46:33 +01001827 /* BIOS might change freq behind our back
1828 -> ask driver for current freq and notify governors about a change */
1829 if (cpufreq_driver->get) {
1830 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001831 if (!data->cur) {
1832 dprintk("Driver did not initialize current freq");
1833 data->cur = policy.cur;
1834 } else {
1835 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301836 cpufreq_out_of_sync(cpu, data->cur,
1837 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001838 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 ret = __cpufreq_set_policy(data, &policy);
1842
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001843 unlock_policy_rwsem_write(cpu);
1844
Julia Lawallf1829e42008-07-25 22:44:53 +02001845fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001847no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return ret;
1849}
1850EXPORT_SYMBOL(cpufreq_update_policy);
1851
Satyam Sharmadd184a02007-10-02 13:28:14 -07001852static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001853 unsigned long action, void *hcpu)
1854{
1855 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001856 struct sys_device *sys_dev;
1857
1858 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001859 if (sys_dev) {
1860 switch (action) {
1861 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001862 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001863 cpufreq_add_dev(sys_dev);
1864 break;
1865 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001866 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001867 if (unlikely(lock_policy_rwsem_write(cpu)))
1868 BUG();
1869
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001870 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001871 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001872 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001873 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001874 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001875 break;
1876 }
1877 }
1878 return NOTIFY_OK;
1879}
1880
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001881static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001882 .notifier_call = cpufreq_cpu_callback,
1883};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885/*********************************************************************
1886 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1887 *********************************************************************/
1888
1889/**
1890 * cpufreq_register_driver - register a CPU Frequency driver
1891 * @driver_data: A struct cpufreq_driver containing the values#
1892 * submitted by the CPU Frequency driver.
1893 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001894 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001896 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 *
1898 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001899int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
1901 unsigned long flags;
1902 int ret;
1903
1904 if (!driver_data || !driver_data->verify || !driver_data->init ||
1905 ((!driver_data->setpolicy) && (!driver_data->target)))
1906 return -EINVAL;
1907
1908 dprintk("trying to register driver %s\n", driver_data->name);
1909
1910 if (driver_data->setpolicy)
1911 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1912
1913 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1914 if (cpufreq_driver) {
1915 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1916 return -EBUSY;
1917 }
1918 cpufreq_driver = driver_data;
1919 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1920
Mike Travis7a6aedf2008-03-25 15:06:53 -07001921 ret = sysdev_driver_register(&cpu_sysdev_class,
1922 &cpufreq_sysdev_driver);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001923 if (ret)
1924 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001926 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 int i;
1928 ret = -ENODEV;
1929
1930 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001931 for (i = 0; i < nr_cpu_ids; i++)
1932 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001934 break;
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* if all ->init() calls failed, unregister */
1938 if (ret) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301939 dprintk("no CPU initialized for driver %s\n",
1940 driver_data->name);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001941 goto err_sysdev_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 }
1944
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001945 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1946 dprintk("driver %s up and running\n", driver_data->name);
1947 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01001949 return 0;
1950err_sysdev_unreg:
1951 sysdev_driver_unregister(&cpu_sysdev_class,
1952 &cpufreq_sysdev_driver);
1953err_null_driver:
1954 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1955 cpufreq_driver = NULL;
1956 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05001957 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1960
1961
1962/**
1963 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1964 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001965 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * the right to do so, i.e. if you have succeeded in initialising before!
1967 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1968 * currently not initialised.
1969 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001970int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 unsigned long flags;
1973
1974 cpufreq_debug_disable_ratelimit();
1975
1976 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1977 cpufreq_debug_enable_ratelimit();
1978 return -EINVAL;
1979 }
1980
1981 dprintk("unregistering driver %s\n", driver->name);
1982
1983 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001984 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1987 cpufreq_driver = NULL;
1988 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1989
1990 return 0;
1991}
1992EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001993
1994static int __init cpufreq_core_init(void)
1995{
1996 int cpu;
1997
1998 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001999 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002000 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
2001 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002002
2003 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
2004 &cpu_sysdev_class.kset.kobj);
2005 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002006 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002007
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002008 return 0;
2009}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002010core_initcall(cpufreq_core_init);