blob: 40877d2190819dd6e0dcf189e7ed25ce7465f2b8 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053032#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
33 "cpufreq-core", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/**
Dave Jonescd878472006-08-11 17:59:28 -040036 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
39 */
Dave Jones7d5e3502006-02-02 17:03:42 -050040static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070041static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070042#ifdef CONFIG_HOTPLUG_CPU
43/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040044static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static DEFINE_SPINLOCK(cpufreq_driver_lock);
47
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080048/*
49 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
50 * all cpufreq/hotplug/workqueue/etc related lock issues.
51 *
52 * The rules for this semaphore:
53 * - Any routine that wants to read from the policy structure will
54 * do a down_read on this semaphore.
55 * - Any routine that will write to the policy structure and/or may take away
56 * the policy altogether (eg. CPU hotplug), will hold this lock in write
57 * mode before doing so.
58 *
59 * Additional rules:
60 * - All holders of the lock should check to make sure that the CPU they
61 * are concerned with are online after they get the lock.
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040064 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080066 */
Tejun Heof1625062009-10-29 22:34:13 +090067static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
69
70#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050071static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080072(int cpu) \
73{ \
Tejun Heof1625062009-10-29 22:34:13 +090074 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080075 BUG_ON(policy_cpu == -1); \
76 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
77 if (unlikely(!cpu_online(cpu))) { \
78 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 return -1; \
80 } \
81 \
82 return 0; \
83}
84
85lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080086
87lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088
Amerigo Wang226528c2010-03-04 03:23:36 -050089static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090{
Tejun Heof1625062009-10-29 22:34:13 +090091 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092 BUG_ON(policy_cpu == -1);
93 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
94}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080095
Amerigo Wang226528c2010-03-04 03:23:36 -050096static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097{
Tejun Heof1625062009-10-29 22:34:13 +090098 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099 BUG_ON(policy_cpu == -1);
100 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
101}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800102
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500105static int __cpufreq_governor(struct cpufreq_policy *policy,
106 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800107static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000108static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500111 * Two notifier lists: the "policy" list is involved in the
112 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * "transition" list for kernel code that needs to handle
114 * changes to devices when the CPU clock speed changes.
115 * The mutex locks both lists.
116 */
Alan Sterne041c682006-03-27 01:16:30 -0800117static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700118static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200120static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700121static int __init init_cpufreq_transition_notifier_list(void)
122{
123 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200124 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700125 return 0;
126}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800127pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500130static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Dave Jones7d5e3502006-02-02 17:03:42 -0500132struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 struct cpufreq_policy *data;
135 unsigned long flags;
136
Mike Travis7a6aedf2008-03-25 15:06:53 -0700137 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 goto err_out;
139
140 /* get the cpufreq driver */
141 spin_lock_irqsave(&cpufreq_driver_lock, flags);
142
143 if (!cpufreq_driver)
144 goto err_out_unlock;
145
146 if (!try_module_get(cpufreq_driver->owner))
147 goto err_out_unlock;
148
149
150 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700151 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 if (!data)
154 goto err_out_put_module;
155
156 if (!kobject_get(&data->kobj))
157 goto err_out_put_module;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return data;
161
Dave Jones7d5e3502006-02-02 17:03:42 -0500162err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500164err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500166err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return NULL;
168}
169EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
170
Dave Jones7d5e3502006-02-02 17:03:42 -0500171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172void cpufreq_cpu_put(struct cpufreq_policy *data)
173{
174 kobject_put(&data->kobj);
175 module_put(cpufreq_driver->owner);
176}
177EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
178
179
180/*********************************************************************
181 * UNIFIED DEBUG HELPERS *
182 *********************************************************************/
183#ifdef CONFIG_CPU_FREQ_DEBUG
184
185/* what part(s) of the CPUfreq subsystem are debugged? */
186static unsigned int debug;
187
188/* is the debug output ratelimit'ed using printk_ratelimit? User can
189 * set or modify this value.
190 */
191static unsigned int debug_ratelimit = 1;
192
193/* is the printk_ratelimit'ing enabled? It's enabled after a successful
194 * loading of a cpufreq driver, temporarily disabled when a new policy
195 * is set, and disabled upon cpufreq driver removal
196 */
197static unsigned int disable_ratelimit = 1;
198static DEFINE_SPINLOCK(disable_ratelimit_lock);
199
Arjan van de Ven858119e2006-01-14 13:20:43 -0800200static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long flags;
203
204 spin_lock_irqsave(&disable_ratelimit_lock, flags);
205 if (disable_ratelimit)
206 disable_ratelimit--;
207 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
208}
209
Arjan van de Ven858119e2006-01-14 13:20:43 -0800210static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 unsigned long flags;
213
214 spin_lock_irqsave(&disable_ratelimit_lock, flags);
215 disable_ratelimit++;
216 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
217}
218
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530219void cpufreq_debug_printk(unsigned int type, const char *prefix,
Dave Jones905d77c2008-03-05 14:28:32 -0500220 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 char s[256];
223 va_list args;
224 unsigned int len;
225 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 WARN_ON(!prefix);
228 if (type & debug) {
229 spin_lock_irqsave(&disable_ratelimit_lock, flags);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530230 if (!disable_ratelimit && debug_ratelimit
231 && !printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
233 return;
234 }
235 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
236
237 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
238
239 va_start(args, fmt);
240 len += vsnprintf(&s[len], (256 - len), fmt, args);
241 va_end(args);
242
243 printk(s);
244
245 WARN_ON(len < 5);
246 }
247}
248EXPORT_SYMBOL(cpufreq_debug_printk);
249
250
251module_param(debug, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530252MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
253 " 2 to debug drivers, and 4 to debug governors.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
255module_param(debug_ratelimit, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530256MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
257 " set to 0 to disable ratelimiting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259#else /* !CONFIG_CPU_FREQ_DEBUG */
260
261static inline void cpufreq_debug_enable_ratelimit(void) { return; }
262static inline void cpufreq_debug_disable_ratelimit(void) { return; }
263
264#endif /* CONFIG_CPU_FREQ_DEBUG */
265
266
267/*********************************************************************
268 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
269 *********************************************************************/
270
271/**
272 * adjust_jiffies - adjust the system "loops_per_jiffy"
273 *
274 * This function alters the system "loops_per_jiffy" for the clock
275 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500276 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * per-CPU loops_per_jiffy value wherever possible.
278 */
279#ifndef CONFIG_SMP
280static unsigned long l_p_j_ref;
281static unsigned int l_p_j_ref_freq;
282
Arjan van de Ven858119e2006-01-14 13:20:43 -0800283static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 if (ci->flags & CPUFREQ_CONST_LOOPS)
286 return;
287
288 if (!l_p_j_ref_freq) {
289 l_p_j_ref = loops_per_jiffy;
290 l_p_j_ref_freq = ci->old;
Joe Perchesa4a9df52007-11-19 17:48:06 -0800291 dprintk("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530292 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 }
294 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
295 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700296 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530297 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
298 ci->new);
Joe Perchesa4a9df52007-11-19 17:48:06 -0800299 dprintk("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530300 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
303#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530304static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
305{
306 return;
307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308#endif
309
310
311/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800312 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
313 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800315 * This function calls the transition notifiers and the "adjust_jiffies"
316 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500317 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
320{
Dave Jonese4472cb2006-01-31 15:53:55 -0800321 struct cpufreq_policy *policy;
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 BUG_ON(irqs_disabled());
324
325 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800326 dprintk("notification %u of frequency transition to %u kHz\n",
327 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Mike Travis7a6aedf2008-03-25 15:06:53 -0700329 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500333 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800334 * which is not equal to what the cpufreq core thinks is
335 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800338 if ((policy) && (policy->cpu == freqs->cpu) &&
339 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200340 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800341 " %u, cpufreq assumed %u kHz.\n",
342 freqs->old, policy->cur);
343 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700346 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800347 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
349 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 case CPUFREQ_POSTCHANGE:
352 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700353 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800354 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800355 if (likely(policy) && likely(policy->cpu == freqs->cpu))
356 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 break;
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
361
362
363
364/*********************************************************************
365 * SYSFS INTERFACE *
366 *********************************************************************/
367
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700368static struct cpufreq_governor *__find_governor(const char *str_governor)
369{
370 struct cpufreq_governor *t;
371
372 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500373 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700374 return t;
375
376 return NULL;
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/**
380 * cpufreq_parse_governor - parse a governor string
381 */
Dave Jones905d77c2008-03-05 14:28:32 -0500382static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 struct cpufreq_governor **governor)
384{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700385 int err = -EINVAL;
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700388 goto out;
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (cpufreq_driver->setpolicy) {
391 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
392 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700393 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530394 } else if (!strnicmp(str_governor, "powersave",
395 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700397 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700399 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700401
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800402 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700403
404 t = __find_governor(str_governor);
405
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700406 if (t == NULL) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530407 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
408 str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700409
410 if (name) {
411 int ret;
412
413 mutex_unlock(&cpufreq_governor_mutex);
Chris Wright326f6a52008-06-06 21:26:02 -0700414 ret = request_module("%s", name);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700415 mutex_lock(&cpufreq_governor_mutex);
416
417 if (ret == 0)
418 t = __find_governor(str_governor);
419 }
420
421 kfree(name);
422 }
423
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700424 if (t != NULL) {
425 *governor = t;
426 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700428
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800429 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
Dave Jones29464f22009-01-18 01:37:11 -0500431out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700432 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530437 * cpufreq_per_cpu_attr_read() / show_##file_name() -
438 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 *
440 * Write out information from cpufreq_driver->policy[cpu]; object must be
441 * "unsigned int".
442 */
443
Dave Jones32ee8c32006-02-28 00:43:23 -0500444#define show_one(file_name, object) \
445static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500446(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500447{ \
Dave Jones29464f22009-01-18 01:37:11 -0500448 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451show_one(cpuinfo_min_freq, cpuinfo.min_freq);
452show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100453show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454show_one(scaling_min_freq, min);
455show_one(scaling_max_freq, max);
456show_one(scaling_cur_freq, cur);
457
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530458static int __cpufreq_set_policy(struct cpufreq_policy *data,
459 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/**
462 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
463 */
464#define store_one(file_name, object) \
465static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500466(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{ \
468 unsigned int ret = -EINVAL; \
469 struct cpufreq_policy new_policy; \
470 \
471 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
472 if (ret) \
473 return -EINVAL; \
474 \
Dave Jones29464f22009-01-18 01:37:11 -0500475 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 if (ret != 1) \
477 return -EINVAL; \
478 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200479 ret = __cpufreq_set_policy(policy, &new_policy); \
480 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 \
482 return ret ? ret : count; \
483}
484
Dave Jones29464f22009-01-18 01:37:11 -0500485store_one(scaling_min_freq, min);
486store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488/**
489 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
490 */
Dave Jones905d77c2008-03-05 14:28:32 -0500491static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
492 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800494 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (!cur_freq)
496 return sprintf(buf, "<unknown>");
497 return sprintf(buf, "%u\n", cur_freq);
498}
499
500
501/**
502 * show_scaling_governor - show the current policy for the specified CPU
503 */
Dave Jones905d77c2008-03-05 14:28:32 -0500504static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Dave Jones29464f22009-01-18 01:37:11 -0500506 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return sprintf(buf, "powersave\n");
508 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
509 return sprintf(buf, "performance\n");
510 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500511 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
512 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -EINVAL;
514}
515
516
517/**
518 * store_scaling_governor - store policy for the specified CPU
519 */
Dave Jones905d77c2008-03-05 14:28:32 -0500520static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
521 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 unsigned int ret = -EINVAL;
524 char str_governor[16];
525 struct cpufreq_policy new_policy;
526
527 ret = cpufreq_get_policy(&new_policy, policy->cpu);
528 if (ret)
529 return ret;
530
Dave Jones29464f22009-01-18 01:37:11 -0500531 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 if (ret != 1)
533 return -EINVAL;
534
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530535 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
536 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EINVAL;
538
Thomas Renninger7970e082006-04-13 15:14:04 +0200539 /* Do not use cpufreq_set_policy here or the user_policy.max
540 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200541 ret = __cpufreq_set_policy(policy, &new_policy);
542
543 policy->user_policy.policy = policy->policy;
544 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200545
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530546 if (ret)
547 return ret;
548 else
549 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552/**
553 * show_scaling_driver - show the cpufreq driver currently loaded
554 */
Dave Jones905d77c2008-03-05 14:28:32 -0500555static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
558}
559
560/**
561 * show_scaling_available_governors - show the available CPUfreq governors
562 */
Dave Jones905d77c2008-03-05 14:28:32 -0500563static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
564 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 ssize_t i = 0;
567 struct cpufreq_governor *t;
568
569 if (!cpufreq_driver->target) {
570 i += sprintf(buf, "performance powersave");
571 goto out;
572 }
573
574 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500575 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
576 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto out;
578 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
579 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500580out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 i += sprintf(&buf[i], "\n");
582 return i;
583}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700584
Rusty Russell835481d2009-01-04 05:18:06 -0800585static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 ssize_t i = 0;
588 unsigned int cpu;
589
Rusty Russell835481d2009-01-04 05:18:06 -0800590 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (i)
592 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
593 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
594 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500595 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597 i += sprintf(&buf[i], "\n");
598 return i;
599}
600
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700601/**
602 * show_related_cpus - show the CPUs affected by each transition even if
603 * hw coordination is in use
604 */
605static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
606{
Rusty Russell835481d2009-01-04 05:18:06 -0800607 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700608 return show_cpus(policy->cpus, buf);
609 return show_cpus(policy->related_cpus, buf);
610}
611
612/**
613 * show_affected_cpus - show the CPUs affected by each transition
614 */
615static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
616{
617 return show_cpus(policy->cpus, buf);
618}
619
Venki Pallipadi9e769882007-10-26 10:18:21 -0700620static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500621 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700622{
623 unsigned int freq = 0;
624 unsigned int ret;
625
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700626 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700627 return -EINVAL;
628
629 ret = sscanf(buf, "%u", &freq);
630 if (ret != 1)
631 return -EINVAL;
632
633 policy->governor->store_setspeed(policy, freq);
634
635 return count;
636}
637
638static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
639{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700640 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700641 return sprintf(buf, "<unsupported>\n");
642
643 return policy->governor->show_setspeed(policy, buf);
644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Thomas Renningere2f74f32009-11-19 12:31:01 +0100646/**
647 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
648 */
649static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
650{
651 unsigned int limit;
652 int ret;
653 if (cpufreq_driver->bios_limit) {
654 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
655 if (!ret)
656 return sprintf(buf, "%u\n", limit);
657 }
658 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
659}
660
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200661cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
662cpufreq_freq_attr_ro(cpuinfo_min_freq);
663cpufreq_freq_attr_ro(cpuinfo_max_freq);
664cpufreq_freq_attr_ro(cpuinfo_transition_latency);
665cpufreq_freq_attr_ro(scaling_available_governors);
666cpufreq_freq_attr_ro(scaling_driver);
667cpufreq_freq_attr_ro(scaling_cur_freq);
668cpufreq_freq_attr_ro(bios_limit);
669cpufreq_freq_attr_ro(related_cpus);
670cpufreq_freq_attr_ro(affected_cpus);
671cpufreq_freq_attr_rw(scaling_min_freq);
672cpufreq_freq_attr_rw(scaling_max_freq);
673cpufreq_freq_attr_rw(scaling_governor);
674cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Dave Jones905d77c2008-03-05 14:28:32 -0500676static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 &cpuinfo_min_freq.attr,
678 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100679 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 &scaling_min_freq.attr,
681 &scaling_max_freq.attr,
682 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700683 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 &scaling_governor.attr,
685 &scaling_driver.attr,
686 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700687 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 NULL
689};
690
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200691struct kobject *cpufreq_global_kobject;
692EXPORT_SYMBOL(cpufreq_global_kobject);
693
Dave Jones29464f22009-01-18 01:37:11 -0500694#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
695#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Dave Jones29464f22009-01-18 01:37:11 -0500697static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Dave Jones905d77c2008-03-05 14:28:32 -0500699 struct cpufreq_policy *policy = to_policy(kobj);
700 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500701 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 policy = cpufreq_cpu_get(policy->cpu);
703 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500704 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800705
706 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500707 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800708
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530709 if (fattr->show)
710 ret = fattr->show(policy, buf);
711 else
712 ret = -EIO;
713
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800714 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500715fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500717no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return ret;
719}
720
Dave Jones905d77c2008-03-05 14:28:32 -0500721static ssize_t store(struct kobject *kobj, struct attribute *attr,
722 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Dave Jones905d77c2008-03-05 14:28:32 -0500724 struct cpufreq_policy *policy = to_policy(kobj);
725 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500726 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 policy = cpufreq_cpu_get(policy->cpu);
728 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500729 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800730
731 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500732 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800733
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530734 if (fattr->store)
735 ret = fattr->store(policy, buf, count);
736 else
737 ret = -EIO;
738
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800739 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500740fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500742no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return ret;
744}
745
Dave Jones905d77c2008-03-05 14:28:32 -0500746static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Dave Jones905d77c2008-03-05 14:28:32 -0500748 struct cpufreq_policy *policy = to_policy(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 dprintk("last reference is dropped\n");
750 complete(&policy->kobj_unregister);
751}
752
Emese Revfy52cf25d2010-01-19 02:58:23 +0100753static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 .show = show,
755 .store = store,
756};
757
758static struct kobj_type ktype_cpufreq = {
759 .sysfs_ops = &sysfs_ops,
760 .default_attrs = default_attrs,
761 .release = cpufreq_sysfs_release,
762};
763
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200764/*
765 * Returns:
766 * Negative: Failure
767 * 0: Success
768 * Positive: When we have a managed CPU and the sysfs got symlinked
769 */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700770static int cpufreq_add_dev_policy(unsigned int cpu,
771 struct cpufreq_policy *policy,
772 struct sys_device *sys_dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400773{
774 int ret = 0;
775#ifdef CONFIG_SMP
776 unsigned long flags;
777 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400778#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400779 struct cpufreq_governor *gov;
780
781 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
782 if (gov) {
783 policy->governor = gov;
Dave Jonesecf7e462009-07-08 18:48:47 -0400784 dprintk("Restoring governor %s for cpu %d\n",
785 policy->governor->name, cpu);
786 }
787#endif
788
789 for_each_cpu(j, policy->cpus) {
790 struct cpufreq_policy *managed_policy;
791
792 if (cpu == j)
793 continue;
794
795 /* Check for existing affected CPUs.
796 * They may not be aware of it due to CPU Hotplug.
797 * cpufreq_cpu_put is called when the device is removed
798 * in __cpufreq_remove_dev()
799 */
800 managed_policy = cpufreq_cpu_get(j);
801 if (unlikely(managed_policy)) {
802
803 /* Set proper policy_cpu */
804 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900805 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400806
807 if (lock_policy_rwsem_write(cpu) < 0) {
808 /* Should not go through policy unlock path */
809 if (cpufreq_driver->exit)
810 cpufreq_driver->exit(policy);
811 cpufreq_cpu_put(managed_policy);
812 return -EBUSY;
813 }
814
815 spin_lock_irqsave(&cpufreq_driver_lock, flags);
816 cpumask_copy(managed_policy->cpus, policy->cpus);
817 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
818 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
819
820 dprintk("CPU already managed, adding link\n");
821 ret = sysfs_create_link(&sys_dev->kobj,
822 &managed_policy->kobj,
823 "cpufreq");
824 if (ret)
825 cpufreq_cpu_put(managed_policy);
826 /*
827 * Success. We only needed to be added to the mask.
828 * Call driver->exit() because only the cpu parent of
829 * the kobj needed to call init().
830 */
831 if (cpufreq_driver->exit)
832 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200833
834 if (!ret)
835 return 1;
836 else
837 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400838 }
839 }
840#endif
841 return ret;
842}
843
844
Dave Jones19d6f7e2009-07-08 17:35:39 -0400845/* symlink affected CPUs */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700846static int cpufreq_add_dev_symlink(unsigned int cpu,
847 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400848{
849 unsigned int j;
850 int ret = 0;
851
852 for_each_cpu(j, policy->cpus) {
853 struct cpufreq_policy *managed_policy;
854 struct sys_device *cpu_sys_dev;
855
856 if (j == cpu)
857 continue;
858 if (!cpu_online(j))
859 continue;
860
861 dprintk("CPU %u already managed, adding link\n", j);
862 managed_policy = cpufreq_cpu_get(cpu);
863 cpu_sys_dev = get_cpu_sysdev(j);
864 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
865 "cpufreq");
866 if (ret) {
867 cpufreq_cpu_put(managed_policy);
868 return ret;
869 }
870 }
871 return ret;
872}
873
Alex Chiangcf3289d2009-11-17 20:27:08 -0700874static int cpufreq_add_dev_interface(unsigned int cpu,
875 struct cpufreq_policy *policy,
876 struct sys_device *sys_dev)
Dave Jones909a6942009-07-08 18:05:42 -0400877{
Dave Jonesecf7e462009-07-08 18:48:47 -0400878 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400879 struct freq_attr **drv_attr;
880 unsigned long flags;
881 int ret = 0;
882 unsigned int j;
883
884 /* prepare interface data */
885 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
886 &sys_dev->kobj, "cpufreq");
887 if (ret)
888 return ret;
889
890 /* set up files for this cpu device */
891 drv_attr = cpufreq_driver->attr;
892 while ((drv_attr) && (*drv_attr)) {
893 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
894 if (ret)
895 goto err_out_kobj_put;
896 drv_attr++;
897 }
898 if (cpufreq_driver->get) {
899 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
900 if (ret)
901 goto err_out_kobj_put;
902 }
903 if (cpufreq_driver->target) {
904 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
905 if (ret)
906 goto err_out_kobj_put;
907 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100908 if (cpufreq_driver->bios_limit) {
909 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
910 if (ret)
911 goto err_out_kobj_put;
912 }
Dave Jones909a6942009-07-08 18:05:42 -0400913
914 spin_lock_irqsave(&cpufreq_driver_lock, flags);
915 for_each_cpu(j, policy->cpus) {
916 if (!cpu_online(j))
917 continue;
918 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900919 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400920 }
921 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
922
923 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400924 if (ret)
925 goto err_out_kobj_put;
926
927 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
928 /* assure that the starting sequence is run in __cpufreq_set_policy */
929 policy->governor = NULL;
930
931 /* set default policy */
932 ret = __cpufreq_set_policy(policy, &new_policy);
933 policy->user_policy.policy = policy->policy;
934 policy->user_policy.governor = policy->governor;
935
936 if (ret) {
937 dprintk("setting policy failed\n");
938 if (cpufreq_driver->exit)
939 cpufreq_driver->exit(policy);
940 }
Dave Jones909a6942009-07-08 18:05:42 -0400941 return ret;
942
943err_out_kobj_put:
944 kobject_put(&policy->kobj);
945 wait_for_completion(&policy->kobj_unregister);
946 return ret;
947}
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950/**
951 * cpufreq_add_dev - add a CPU device
952 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500953 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400954 *
955 * The Oracle says: try running cpufreq registration/unregistration concurrently
956 * with with cpu hotplugging and all hell will break loose. Tried to clean this
957 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 */
Dave Jones905d77c2008-03-05 14:28:32 -0500959static int cpufreq_add_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 unsigned int cpu = sys_dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500962 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 unsigned long flags;
965 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500966#ifdef CONFIG_HOTPLUG_CPU
967 int sibling;
968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Ashok Rajc32b6b82005-10-30 14:59:54 -0800970 if (cpu_is_offline(cpu))
971 return 0;
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 cpufreq_debug_disable_ratelimit();
974 dprintk("adding CPU %u\n", cpu);
975
976#ifdef CONFIG_SMP
977 /* check whether a different CPU already registered this
978 * CPU because it is in the same boat. */
979 policy = cpufreq_cpu_get(cpu);
980 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500981 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 cpufreq_debug_enable_ratelimit();
983 return 0;
984 }
985#endif
986
987 if (!try_module_get(cpufreq_driver->owner)) {
988 ret = -EINVAL;
989 goto module_out;
990 }
991
Dave Jones059019a2009-07-08 16:30:03 -0400992 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700993 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400994 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -0400996
997 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400998 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -0400999
1000 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001001 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -08001004 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001006 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001007 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001008 ret = (lock_policy_rwsem_write(cpu) < 0);
1009 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001012 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Thomas Renninger8122c6c2007-10-02 13:28:09 -07001014 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001015#ifdef CONFIG_HOTPLUG_CPU
1016 for_each_online_cpu(sibling) {
1017 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1018 if (cp && cp->governor &&
1019 (cpumask_test_cpu(cpu, cp->related_cpus))) {
1020 policy->governor = cp->governor;
1021 found = 1;
1022 break;
1023 }
1024 }
1025#endif
1026 if (!found)
1027 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /* call driver. From then on the cpufreq must be able
1029 * to accept all calls to ->verify and ->setpolicy for this CPU
1030 */
1031 ret = cpufreq_driver->init(policy);
1032 if (ret) {
1033 dprintk("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001034 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Mike Chan187d9f42008-12-04 12:19:17 -08001036 policy->user_policy.min = policy->min;
1037 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
Thomas Renningera1531ac2008-07-29 22:32:58 -07001039 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1040 CPUFREQ_START, policy);
1041
Dave Jonesecf7e462009-07-08 18:48:47 -04001042 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001043 if (ret) {
1044 if (ret > 0)
1045 /* This is a managed cpu, symlink created,
1046 exit with 0 */
1047 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001048 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Dave Jones909a6942009-07-08 18:05:42 -04001051 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001052 if (ret)
1053 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001054
Lothar Waßmanndca02612008-05-29 17:54:52 +02001055 unlock_policy_rwsem_write(cpu);
1056
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001057 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 dprintk("initialization complete\n");
1060 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -05001061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return 0;
1063
1064
1065err_out_unregister:
1066 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001067 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001068 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1070
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001071 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 wait_for_completion(&policy->kobj_unregister);
1073
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001074err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001075 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001076 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001077err_free_cpumask:
1078 free_cpumask_var(policy->cpus);
1079err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081nomem_out:
1082 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001083module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 cpufreq_debug_enable_ratelimit();
1085 return ret;
1086}
1087
1088
1089/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001090 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 *
1092 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001093 * Caller should already have policy_rwsem in write mode for this CPU.
1094 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 */
Dave Jones905d77c2008-03-05 14:28:32 -05001096static int __cpufreq_remove_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 unsigned int cpu = sys_dev->id;
1099 unsigned long flags;
1100 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001101 struct kobject *kobj;
1102 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -08001104 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 unsigned int j;
1106#endif
1107
1108 cpufreq_debug_disable_ratelimit();
1109 dprintk("unregistering CPU %u\n", cpu);
1110
1111 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001112 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (!data) {
1115 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001117 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 return -EINVAL;
1119 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001120 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122
1123#ifdef CONFIG_SMP
1124 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001125 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
1127 if (unlikely(cpu != data->cpu)) {
1128 dprintk("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001129 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Amerigo Wang499bca92010-03-04 03:23:46 -05001131 kobj = &sys_dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 cpufreq_cpu_put(data);
1133 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001134 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001135 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return 0;
1137 }
1138#endif
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001141
1142#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001143 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1144 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001145#endif
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* if we have other CPUs still registered, we need to unlink them,
1148 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001149 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1150 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Rusty Russell835481d2009-01-04 05:18:06 -08001152 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1153 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (j == cpu)
1155 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001156 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 }
1158 }
1159
1160 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1161
Rusty Russell835481d2009-01-04 05:18:06 -08001162 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1163 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (j == cpu)
1165 continue;
1166 dprintk("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001167#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001168 strncpy(per_cpu(cpufreq_cpu_governor, j),
1169 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001170#endif
Ashok Rajd434fca2005-10-30 14:59:52 -08001171 cpu_sys_dev = get_cpu_sysdev(j);
Amerigo Wang499bca92010-03-04 03:23:46 -05001172 kobj = &cpu_sys_dev->kobj;
1173 unlock_policy_rwsem_write(cpu);
1174 sysfs_remove_link(kobj, "cpufreq");
1175 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 cpufreq_cpu_put(data);
1177 }
1178 }
1179#else
1180 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1181#endif
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (cpufreq_driver->target)
1184 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001185
Amerigo Wang499bca92010-03-04 03:23:46 -05001186 kobj = &data->kobj;
1187 cmp = &data->kobj_unregister;
1188 unlock_policy_rwsem_write(cpu);
1189 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001192 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 * unloading.
1194 */
1195 dprintk("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001196 wait_for_completion(cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 dprintk("wait complete\n");
1198
Amerigo Wang499bca92010-03-04 03:23:46 -05001199 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (cpufreq_driver->exit)
1201 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001202 unlock_policy_rwsem_write(cpu);
1203
Rusty Russell835481d2009-01-04 05:18:06 -08001204 free_cpumask_var(data->related_cpus);
1205 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 kfree(data);
Rusty Russell835481d2009-01-04 05:18:06 -08001207 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return 0;
1211}
1212
1213
Dave Jones905d77c2008-03-05 14:28:32 -05001214static int cpufreq_remove_dev(struct sys_device *sys_dev)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001215{
1216 unsigned int cpu = sys_dev->id;
1217 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001218
1219 if (cpu_is_offline(cpu))
1220 return 0;
1221
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001222 if (unlikely(lock_policy_rwsem_write(cpu)))
1223 BUG();
1224
1225 retval = __cpufreq_remove_dev(sys_dev);
1226 return retval;
1227}
1228
1229
David Howells65f27f32006-11-22 14:55:48 +00001230static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
David Howells65f27f32006-11-22 14:55:48 +00001232 struct cpufreq_policy *policy =
1233 container_of(work, struct cpufreq_policy, update);
1234 unsigned int cpu = policy->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 dprintk("handle_update for cpu %u called\n", cpu);
1236 cpufreq_update_policy(cpu);
1237}
1238
1239/**
1240 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1241 * @cpu: cpu number
1242 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1243 * @new_freq: CPU frequency the CPU actually runs at
1244 *
Dave Jones29464f22009-01-18 01:37:11 -05001245 * We adjust to current frequency first, and need to clean up later.
1246 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301248static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1249 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct cpufreq_freqs freqs;
1252
Jan Beulichb10eec22006-04-28 13:47:13 +02001253 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1255
1256 freqs.cpu = cpu;
1257 freqs.old = old_freq;
1258 freqs.new = new_freq;
1259 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1260 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1261}
1262
1263
Dave Jones32ee8c32006-02-28 00:43:23 -05001264/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301265 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001266 * @cpu: CPU number
1267 *
1268 * This is the last known freq, without actually getting it from the driver.
1269 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1270 */
1271unsigned int cpufreq_quick_get(unsigned int cpu)
1272{
1273 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301274 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001275
1276 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301277 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001278 cpufreq_cpu_put(policy);
1279 }
1280
Dave Jones4d34a672008-02-07 16:33:49 -05001281 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001282}
1283EXPORT_SYMBOL(cpufreq_quick_get);
1284
1285
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001286static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001288 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301289 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001292 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301294 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301296 if (ret_freq && policy->cur &&
1297 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1298 /* verify no discrepancy between actual and
1299 saved value exists */
1300 if (unlikely(ret_freq != policy->cur)) {
1301 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 schedule_work(&policy->update);
1303 }
1304 }
1305
Dave Jones4d34a672008-02-07 16:33:49 -05001306 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001307}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001309/**
1310 * cpufreq_get - get the current CPU frequency (in kHz)
1311 * @cpu: CPU number
1312 *
1313 * Get the CPU current (static) CPU frequency
1314 */
1315unsigned int cpufreq_get(unsigned int cpu)
1316{
1317 unsigned int ret_freq = 0;
1318 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1319
1320 if (!policy)
1321 goto out;
1322
1323 if (unlikely(lock_policy_rwsem_read(cpu)))
1324 goto out_policy;
1325
1326 ret_freq = __cpufreq_get(cpu);
1327
1328 unlock_policy_rwsem_read(cpu);
1329
1330out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001332out:
Dave Jones4d34a672008-02-07 16:33:49 -05001333 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335EXPORT_SYMBOL(cpufreq_get);
1336
1337
1338/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001339 * cpufreq_suspend - let the low level driver prepare for suspend
1340 */
1341
Dave Jones905d77c2008-03-05 14:28:32 -05001342static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001343{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301344 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001345
Dave Jones4bc5d342009-08-04 14:03:25 -04001346 int cpu = sysdev->id;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001347 struct cpufreq_policy *cpu_policy;
1348
Dave Jones0e37b152006-09-26 23:02:34 -04001349 dprintk("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001350
1351 if (!cpu_online(cpu))
1352 return 0;
1353
1354 /* we may be lax here as interrupts are off. Nonetheless
1355 * we need to grab the correct cpu policy, as to check
1356 * whether we really run on this CPU.
1357 */
1358
1359 cpu_policy = cpufreq_cpu_get(cpu);
1360 if (!cpu_policy)
1361 return -EINVAL;
1362
1363 /* only handle each CPU group once */
Dave Jonesc9060492008-02-07 16:32:18 -05001364 if (unlikely(cpu_policy->cpu != cpu))
1365 goto out;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001366
1367 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -07001368 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001369 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001370 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1371 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001372 }
1373
Dave Jones7d5e3502006-02-02 17:03:42 -05001374out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001375 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001376 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001377}
1378
1379/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 * cpufreq_resume - restore proper CPU frequency handling after resume
1381 *
1382 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001383 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1384 * restored. It will verify that the current freq is in sync with
1385 * what we believe it to be. This is a bit later than when it
1386 * should be, but nonethteless it's better than calling
1387 * cpufreq_driver->get() here which might re-enable interrupts...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
Dave Jones905d77c2008-03-05 14:28:32 -05001389static int cpufreq_resume(struct sys_device *sysdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301391 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001392
Dave Jones4bc5d342009-08-04 14:03:25 -04001393 int cpu = sysdev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct cpufreq_policy *cpu_policy;
1395
1396 dprintk("resuming cpu %u\n", cpu);
1397
1398 if (!cpu_online(cpu))
1399 return 0;
1400
1401 /* we may be lax here as interrupts are off. Nonetheless
1402 * we need to grab the correct cpu policy, as to check
1403 * whether we really run on this CPU.
1404 */
1405
1406 cpu_policy = cpufreq_cpu_get(cpu);
1407 if (!cpu_policy)
1408 return -EINVAL;
1409
1410 /* only handle each CPU group once */
Dave Jonesc9060492008-02-07 16:32:18 -05001411 if (unlikely(cpu_policy->cpu != cpu))
1412 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 if (cpufreq_driver->resume) {
1415 ret = cpufreq_driver->resume(cpu_policy);
1416 if (ret) {
1417 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1418 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001419 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 }
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001424
Dave Jonesc9060492008-02-07 16:32:18 -05001425fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 cpufreq_cpu_put(cpu_policy);
1427 return ret;
1428}
1429
1430static struct sysdev_driver cpufreq_sysdev_driver = {
1431 .add = cpufreq_add_dev,
1432 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001433 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 .resume = cpufreq_resume,
1435};
1436
1437
1438/*********************************************************************
1439 * NOTIFIER LISTS INTERFACE *
1440 *********************************************************************/
1441
1442/**
1443 * cpufreq_register_notifier - register a driver with cpufreq
1444 * @nb: notifier function to register
1445 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1446 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001447 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 * are notified about clock rate changes (once before and once after
1449 * the transition), or a list of drivers that are notified about
1450 * changes in cpufreq policy.
1451 *
1452 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001453 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
1455int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1456{
1457 int ret;
1458
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001459 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 switch (list) {
1462 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001463 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001464 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 break;
1466 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001467 ret = blocking_notifier_chain_register(
1468 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 break;
1470 default:
1471 ret = -EINVAL;
1472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 return ret;
1475}
1476EXPORT_SYMBOL(cpufreq_register_notifier);
1477
1478
1479/**
1480 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1481 * @nb: notifier block to be unregistered
1482 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1483 *
1484 * Remove a driver from the CPU frequency notifier list.
1485 *
1486 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001487 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 */
1489int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1490{
1491 int ret;
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 switch (list) {
1494 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001495 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001496 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 break;
1498 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001499 ret = blocking_notifier_chain_unregister(
1500 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 break;
1502 default:
1503 ret = -EINVAL;
1504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 return ret;
1507}
1508EXPORT_SYMBOL(cpufreq_unregister_notifier);
1509
1510
1511/*********************************************************************
1512 * GOVERNORS *
1513 *********************************************************************/
1514
1515
1516int __cpufreq_driver_target(struct cpufreq_policy *policy,
1517 unsigned int target_freq,
1518 unsigned int relation)
1519{
1520 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001521
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1523 target_freq, relation);
1524 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1525 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return retval;
1528}
1529EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531int cpufreq_driver_target(struct cpufreq_policy *policy,
1532 unsigned int target_freq,
1533 unsigned int relation)
1534{
Julia Lawallf1829e42008-07-25 22:44:53 +02001535 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537 policy = cpufreq_cpu_get(policy->cpu);
1538 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001539 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001541 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001542 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 ret = __cpufreq_driver_target(policy, target_freq, relation);
1545
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001546 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Julia Lawallf1829e42008-07-25 22:44:53 +02001548fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001550no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 return ret;
1552}
1553EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1554
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001555int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001556{
1557 int ret = 0;
1558
1559 policy = cpufreq_cpu_get(policy->cpu);
1560 if (!policy)
1561 return -EINVAL;
1562
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001563 if (cpu_online(cpu) && cpufreq_driver->getavg)
1564 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001565
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001566 cpufreq_cpu_put(policy);
1567 return ret;
1568}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001569EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001570
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001571/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001572 * when "event" is CPUFREQ_GOV_LIMITS
1573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301575static int __cpufreq_governor(struct cpufreq_policy *policy,
1576 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Dave Jonescc993ca2005-07-28 09:43:56 -07001578 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001579
1580 /* Only must be defined when default governor is known to have latency
1581 restrictions, like e.g. conservative or ondemand.
1582 That this is the case is already ensured in Kconfig
1583 */
1584#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1585 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1586#else
1587 struct cpufreq_governor *gov = NULL;
1588#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001589
1590 if (policy->governor->max_transition_latency &&
1591 policy->cpuinfo.transition_latency >
1592 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001593 if (!gov)
1594 return -EINVAL;
1595 else {
1596 printk(KERN_WARNING "%s governor failed, too long"
1597 " transition latency of HW, fallback"
1598 " to %s governor\n",
1599 policy->governor->name,
1600 gov->name);
1601 policy->governor = gov;
1602 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 if (!try_module_get(policy->governor->owner))
1606 return -EINVAL;
1607
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301608 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1609 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 ret = policy->governor->governor(policy, event);
1611
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301612 /* we keep one module reference alive for
1613 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 if ((event != CPUFREQ_GOV_START) || ret)
1615 module_put(policy->governor->owner);
1616 if ((event == CPUFREQ_GOV_STOP) && !ret)
1617 module_put(policy->governor->owner);
1618
1619 return ret;
1620}
1621
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623int cpufreq_register_governor(struct cpufreq_governor *governor)
1624{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001625 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 if (!governor)
1628 return -EINVAL;
1629
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001630 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001631
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001632 err = -EBUSY;
1633 if (__find_governor(governor->name) == NULL) {
1634 err = 0;
1635 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Dave Jones32ee8c32006-02-28 00:43:23 -05001638 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1642
1643
1644void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1645{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001646#ifdef CONFIG_HOTPLUG_CPU
1647 int cpu;
1648#endif
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (!governor)
1651 return;
1652
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001653#ifdef CONFIG_HOTPLUG_CPU
1654 for_each_present_cpu(cpu) {
1655 if (cpu_online(cpu))
1656 continue;
1657 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1658 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1659 }
1660#endif
1661
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001662 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001664 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 return;
1666}
1667EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1668
1669
1670
1671/*********************************************************************
1672 * POLICY INTERFACE *
1673 *********************************************************************/
1674
1675/**
1676 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001677 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1678 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 *
1680 * Reads the current cpufreq policy.
1681 */
1682int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1683{
1684 struct cpufreq_policy *cpu_policy;
1685 if (!policy)
1686 return -EINVAL;
1687
1688 cpu_policy = cpufreq_cpu_get(cpu);
1689 if (!cpu_policy)
1690 return -EINVAL;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696}
1697EXPORT_SYMBOL(cpufreq_get_policy);
1698
1699
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001700/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301701 * data : current policy.
1702 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001703 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301704static int __cpufreq_set_policy(struct cpufreq_policy *data,
1705 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 int ret = 0;
1708
1709 cpufreq_debug_disable_ratelimit();
1710 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1711 policy->min, policy->max);
1712
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301713 memcpy(&policy->cpuinfo, &data->cpuinfo,
1714 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Yi Yang53391fa2008-01-30 13:33:34 +01001716 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001717 ret = -EINVAL;
1718 goto error_out;
1719 }
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* verify the cpu speed can be set within this limit */
1722 ret = cpufreq_driver->verify(policy);
1723 if (ret)
1724 goto error_out;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001727 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1728 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001731 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1732 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /* verify the cpu speed can be set within this limit,
1735 which might be different to the first one */
1736 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001737 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001741 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1742 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Dave Jones7d5e3502006-02-02 17:03:42 -05001744 data->min = policy->min;
1745 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301747 dprintk("new min and max freqs are %u - %u kHz\n",
1748 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (cpufreq_driver->setpolicy) {
1751 data->policy = policy->policy;
1752 dprintk("setting range\n");
1753 ret = cpufreq_driver->setpolicy(policy);
1754 } else {
1755 if (policy->governor != data->governor) {
1756 /* save old, working values */
1757 struct cpufreq_governor *old_gov = data->governor;
1758
1759 dprintk("governor switch\n");
1760
1761 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001762 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1764
1765 /* start new governor */
1766 data->governor = policy->governor;
1767 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1768 /* new governor failed, so re-start old one */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301769 dprintk("starting governor %s failed\n",
1770 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (old_gov) {
1772 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301773 __cpufreq_governor(data,
1774 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776 ret = -EINVAL;
1777 goto error_out;
1778 }
1779 /* might be a policy change, too, so fall through */
1780 }
1781 dprintk("governor: change or update limits\n");
1782 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1783 }
1784
Dave Jones7d5e3502006-02-02 17:03:42 -05001785error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 cpufreq_debug_enable_ratelimit();
1787 return ret;
1788}
1789
1790/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1792 * @cpu: CPU which shall be re-evaluated
1793 *
1794 * Usefull for policy notifiers which have different necessities
1795 * at different times.
1796 */
1797int cpufreq_update_policy(unsigned int cpu)
1798{
1799 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1800 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001801 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Julia Lawallf1829e42008-07-25 22:44:53 +02001803 if (!data) {
1804 ret = -ENODEV;
1805 goto no_policy;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Julia Lawallf1829e42008-07-25 22:44:53 +02001808 if (unlikely(lock_policy_rwsem_write(cpu))) {
1809 ret = -EINVAL;
1810 goto fail;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001814 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 policy.min = data->user_policy.min;
1816 policy.max = data->user_policy.max;
1817 policy.policy = data->user_policy.policy;
1818 policy.governor = data->user_policy.governor;
1819
Thomas Renninger0961dd02006-01-26 18:46:33 +01001820 /* BIOS might change freq behind our back
1821 -> ask driver for current freq and notify governors about a change */
1822 if (cpufreq_driver->get) {
1823 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001824 if (!data->cur) {
1825 dprintk("Driver did not initialize current freq");
1826 data->cur = policy.cur;
1827 } else {
1828 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301829 cpufreq_out_of_sync(cpu, data->cur,
1830 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001831 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001832 }
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 ret = __cpufreq_set_policy(data, &policy);
1835
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001836 unlock_policy_rwsem_write(cpu);
1837
Julia Lawallf1829e42008-07-25 22:44:53 +02001838fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001840no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return ret;
1842}
1843EXPORT_SYMBOL(cpufreq_update_policy);
1844
Satyam Sharmadd184a02007-10-02 13:28:14 -07001845static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001846 unsigned long action, void *hcpu)
1847{
1848 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001849 struct sys_device *sys_dev;
1850
1851 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001852 if (sys_dev) {
1853 switch (action) {
1854 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001855 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001856 cpufreq_add_dev(sys_dev);
1857 break;
1858 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001859 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001860 if (unlikely(lock_policy_rwsem_write(cpu)))
1861 BUG();
1862
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001863 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001864 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001865 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001866 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001867 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001868 break;
1869 }
1870 }
1871 return NOTIFY_OK;
1872}
1873
Sam Ravnborgf6ebef32008-02-17 13:22:52 +01001874static struct notifier_block __refdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001875{
1876 .notifier_call = cpufreq_cpu_callback,
1877};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879/*********************************************************************
1880 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1881 *********************************************************************/
1882
1883/**
1884 * cpufreq_register_driver - register a CPU Frequency driver
1885 * @driver_data: A struct cpufreq_driver containing the values#
1886 * submitted by the CPU Frequency driver.
1887 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001888 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001890 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *
1892 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001893int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
1895 unsigned long flags;
1896 int ret;
1897
1898 if (!driver_data || !driver_data->verify || !driver_data->init ||
1899 ((!driver_data->setpolicy) && (!driver_data->target)))
1900 return -EINVAL;
1901
1902 dprintk("trying to register driver %s\n", driver_data->name);
1903
1904 if (driver_data->setpolicy)
1905 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1906
1907 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1908 if (cpufreq_driver) {
1909 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1910 return -EBUSY;
1911 }
1912 cpufreq_driver = driver_data;
1913 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1914
Mike Travis7a6aedf2008-03-25 15:06:53 -07001915 ret = sysdev_driver_register(&cpu_sysdev_class,
1916 &cpufreq_sysdev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1919 int i;
1920 ret = -ENODEV;
1921
1922 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001923 for (i = 0; i < nr_cpu_ids; i++)
1924 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001926 break;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
1929 /* if all ->init() calls failed, unregister */
1930 if (ret) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301931 dprintk("no CPU initialized for driver %s\n",
1932 driver_data->name);
1933 sysdev_driver_unregister(&cpu_sysdev_class,
1934 &cpufreq_sysdev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1937 cpufreq_driver = NULL;
1938 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1939 }
1940 }
1941
1942 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001943 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 dprintk("driver %s up and running\n", driver_data->name);
1945 cpufreq_debug_enable_ratelimit();
1946 }
1947
Dave Jones4d34a672008-02-07 16:33:49 -05001948 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1951
1952
1953/**
1954 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1955 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001956 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * the right to do so, i.e. if you have succeeded in initialising before!
1958 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1959 * currently not initialised.
1960 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001961int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 unsigned long flags;
1964
1965 cpufreq_debug_disable_ratelimit();
1966
1967 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1968 cpufreq_debug_enable_ratelimit();
1969 return -EINVAL;
1970 }
1971
1972 dprintk("unregistering driver %s\n", driver->name);
1973
1974 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001975 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1978 cpufreq_driver = NULL;
1979 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1980
1981 return 0;
1982}
1983EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001984
1985static int __init cpufreq_core_init(void)
1986{
1987 int cpu;
1988
1989 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001990 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001991 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1992 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001993
1994 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1995 &cpu_sysdev_class.kset.kobj);
1996 BUG_ON(!cpufreq_global_kobject);
1997
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001998 return 0;
1999}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002000core_initcall(cpufreq_core_init);