blob: 199dcb9f0b836da1431a5791b05309d79f9c330c [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
Thomas Renninger6f4f2722010-04-20 13:17:36 +020032#include <trace/events/power.h>
33
Gautham R Shenoye08f5f52006-10-26 16:20:58 +053034#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
35 "cpufreq-core", msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37/**
Dave Jonescd878472006-08-11 17:59:28 -040038 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * level driver of CPUFreq support, and its spinlock. This lock
40 * also protects the cpufreq_cpu_data array.
41 */
Dave Jones7d5e3502006-02-02 17:03:42 -050042static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070043static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Thomas Renninger084f3492007-07-09 11:35:28 -070044#ifdef CONFIG_HOTPLUG_CPU
45/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040046static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static DEFINE_SPINLOCK(cpufreq_driver_lock);
49
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080050/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
62 * - All holders of the lock should check to make sure that the CPU they
63 * are concerned with are online after they get the lock.
64 * - Governor routines that can be called in cpufreq hotplug path should not
65 * take this sem as top level hotplug notifier handler takes this.
Mathieu Desnoyers395913d2009-06-08 13:17:31 -040066 * - Lock should not be held across
67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068 */
Tejun Heof1625062009-10-29 22:34:13 +090069static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080070static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
71
72#define lock_policy_rwsem(mode, cpu) \
Amerigo Wang226528c2010-03-04 03:23:36 -050073static int lock_policy_rwsem_##mode \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080074(int cpu) \
75{ \
Tejun Heof1625062009-10-29 22:34:13 +090076 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080077 BUG_ON(policy_cpu == -1); \
78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 if (unlikely(!cpu_online(cpu))) { \
80 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
81 return -1; \
82 } \
83 \
84 return 0; \
85}
86
87lock_policy_rwsem(read, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080088
89lock_policy_rwsem(write, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080090
Amerigo Wang226528c2010-03-04 03:23:36 -050091static void unlock_policy_rwsem_read(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080092{
Tejun Heof1625062009-10-29 22:34:13 +090093 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080094 BUG_ON(policy_cpu == -1);
95 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
96}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080097
Amerigo Wang226528c2010-03-04 03:23:36 -050098static void unlock_policy_rwsem_write(int cpu)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080099{
Tejun Heof1625062009-10-29 22:34:13 +0900100 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800101 BUG_ON(policy_cpu == -1);
102 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
103}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800104
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -0500107static int __cpufreq_governor(struct cpufreq_policy *policy,
108 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800109static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000110static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500113 * Two notifier lists: the "policy" list is involved in the
114 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * "transition" list for kernel code that needs to handle
116 * changes to devices when the CPU clock speed changes.
117 * The mutex locks both lists.
118 */
Alan Sterne041c682006-03-27 01:16:30 -0800119static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700120static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200122static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700123static int __init init_cpufreq_transition_notifier_list(void)
124{
125 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -0200126 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700127 return 0;
128}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800129pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500132static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Dave Jones7d5e3502006-02-02 17:03:42 -0500134struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 struct cpufreq_policy *data;
137 unsigned long flags;
138
Mike Travis7a6aedf2008-03-25 15:06:53 -0700139 if (cpu >= nr_cpu_ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto err_out;
141
142 /* get the cpufreq driver */
143 spin_lock_irqsave(&cpufreq_driver_lock, flags);
144
145 if (!cpufreq_driver)
146 goto err_out_unlock;
147
148 if (!try_module_get(cpufreq_driver->owner))
149 goto err_out_unlock;
150
151
152 /* get the CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -0700153 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 if (!data)
156 goto err_out_put_module;
157
158 if (!kobject_get(&data->kobj))
159 goto err_out_put_module;
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return data;
163
Dave Jones7d5e3502006-02-02 17:03:42 -0500164err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500166err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500168err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return NULL;
170}
171EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
172
Dave Jones7d5e3502006-02-02 17:03:42 -0500173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174void cpufreq_cpu_put(struct cpufreq_policy *data)
175{
176 kobject_put(&data->kobj);
177 module_put(cpufreq_driver->owner);
178}
179EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
180
181
182/*********************************************************************
183 * UNIFIED DEBUG HELPERS *
184 *********************************************************************/
185#ifdef CONFIG_CPU_FREQ_DEBUG
186
187/* what part(s) of the CPUfreq subsystem are debugged? */
188static unsigned int debug;
189
190/* is the debug output ratelimit'ed using printk_ratelimit? User can
191 * set or modify this value.
192 */
193static unsigned int debug_ratelimit = 1;
194
195/* is the printk_ratelimit'ing enabled? It's enabled after a successful
196 * loading of a cpufreq driver, temporarily disabled when a new policy
197 * is set, and disabled upon cpufreq driver removal
198 */
199static unsigned int disable_ratelimit = 1;
200static DEFINE_SPINLOCK(disable_ratelimit_lock);
201
Arjan van de Ven858119e2006-01-14 13:20:43 -0800202static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 unsigned long flags;
205
206 spin_lock_irqsave(&disable_ratelimit_lock, flags);
207 if (disable_ratelimit)
208 disable_ratelimit--;
209 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
210}
211
Arjan van de Ven858119e2006-01-14 13:20:43 -0800212static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 unsigned long flags;
215
216 spin_lock_irqsave(&disable_ratelimit_lock, flags);
217 disable_ratelimit++;
218 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
219}
220
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530221void cpufreq_debug_printk(unsigned int type, const char *prefix,
Dave Jones905d77c2008-03-05 14:28:32 -0500222 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 char s[256];
225 va_list args;
226 unsigned int len;
227 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 WARN_ON(!prefix);
230 if (type & debug) {
231 spin_lock_irqsave(&disable_ratelimit_lock, flags);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530232 if (!disable_ratelimit && debug_ratelimit
233 && !printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
235 return;
236 }
237 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
238
239 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
240
241 va_start(args, fmt);
242 len += vsnprintf(&s[len], (256 - len), fmt, args);
243 va_end(args);
244
245 printk(s);
246
247 WARN_ON(len < 5);
248 }
249}
250EXPORT_SYMBOL(cpufreq_debug_printk);
251
252
253module_param(debug, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530254MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
255 " 2 to debug drivers, and 4 to debug governors.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257module_param(debug_ratelimit, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530258MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
259 " set to 0 to disable ratelimiting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261#else /* !CONFIG_CPU_FREQ_DEBUG */
262
263static inline void cpufreq_debug_enable_ratelimit(void) { return; }
264static inline void cpufreq_debug_disable_ratelimit(void) { return; }
265
266#endif /* CONFIG_CPU_FREQ_DEBUG */
267
268
269/*********************************************************************
270 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
271 *********************************************************************/
272
273/**
274 * adjust_jiffies - adjust the system "loops_per_jiffy"
275 *
276 * This function alters the system "loops_per_jiffy" for the clock
277 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500278 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * per-CPU loops_per_jiffy value wherever possible.
280 */
281#ifndef CONFIG_SMP
282static unsigned long l_p_j_ref;
283static unsigned int l_p_j_ref_freq;
284
Arjan van de Ven858119e2006-01-14 13:20:43 -0800285static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 if (ci->flags & CPUFREQ_CONST_LOOPS)
288 return;
289
290 if (!l_p_j_ref_freq) {
291 l_p_j_ref = loops_per_jiffy;
292 l_p_j_ref_freq = ci->old;
Joe Perchesa4a9df52007-11-19 17:48:06 -0800293 dprintk("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530294 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
297 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700298 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530299 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
300 ci->new);
Joe Perchesa4a9df52007-11-19 17:48:06 -0800301 dprintk("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530302 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304}
305#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530306static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
307{
308 return;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#endif
311
312
313/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800314 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
315 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800317 * This function calls the transition notifiers and the "adjust_jiffies"
318 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500319 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
322{
Dave Jonese4472cb2006-01-31 15:53:55 -0800323 struct cpufreq_policy *policy;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 BUG_ON(irqs_disabled());
326
327 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800328 dprintk("notification %u of frequency transition to %u kHz\n",
329 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Mike Travis7a6aedf2008-03-25 15:06:53 -0700331 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500335 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800336 * which is not equal to what the cpufreq core thinks is
337 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
339 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800340 if ((policy) && (policy->cpu == freqs->cpu) &&
341 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200342 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800343 " %u, cpufreq assumed %u kHz.\n",
344 freqs->old, policy->cur);
345 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800349 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
351 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 case CPUFREQ_POSTCHANGE:
354 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200355 dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
356 (unsigned long)freqs->cpu);
357 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700358 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800359 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800360 if (likely(policy) && likely(policy->cpu == freqs->cpu))
361 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
366
367
368
369/*********************************************************************
370 * SYSFS INTERFACE *
371 *********************************************************************/
372
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700373static struct cpufreq_governor *__find_governor(const char *str_governor)
374{
375 struct cpufreq_governor *t;
376
377 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500378 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700379 return t;
380
381 return NULL;
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/**
385 * cpufreq_parse_governor - parse a governor string
386 */
Dave Jones905d77c2008-03-05 14:28:32 -0500387static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct cpufreq_governor **governor)
389{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700390 int err = -EINVAL;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700393 goto out;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (cpufreq_driver->setpolicy) {
396 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
397 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700398 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530399 } else if (!strnicmp(str_governor, "powersave",
400 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700402 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700404 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700406
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800407 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700408
409 t = __find_governor(str_governor);
410
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700411 if (t == NULL) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530412 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
413 str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700414
415 if (name) {
416 int ret;
417
418 mutex_unlock(&cpufreq_governor_mutex);
Chris Wright326f6a52008-06-06 21:26:02 -0700419 ret = request_module("%s", name);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700420 mutex_lock(&cpufreq_governor_mutex);
421
422 if (ret == 0)
423 t = __find_governor(str_governor);
424 }
425
426 kfree(name);
427 }
428
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700429 if (t != NULL) {
430 *governor = t;
431 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700433
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800434 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Dave Jones29464f22009-01-18 01:37:11 -0500436out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700437 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530442 * cpufreq_per_cpu_attr_read() / show_##file_name() -
443 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 *
445 * Write out information from cpufreq_driver->policy[cpu]; object must be
446 * "unsigned int".
447 */
448
Dave Jones32ee8c32006-02-28 00:43:23 -0500449#define show_one(file_name, object) \
450static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500451(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500452{ \
Dave Jones29464f22009-01-18 01:37:11 -0500453 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456show_one(cpuinfo_min_freq, cpuinfo.min_freq);
457show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100458show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459show_one(scaling_min_freq, min);
460show_one(scaling_max_freq, max);
461show_one(scaling_cur_freq, cur);
462
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530463static int __cpufreq_set_policy(struct cpufreq_policy *data,
464 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/**
467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
468 */
469#define store_one(file_name, object) \
470static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500471(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{ \
473 unsigned int ret = -EINVAL; \
474 struct cpufreq_policy new_policy; \
475 \
476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
477 if (ret) \
478 return -EINVAL; \
479 \
Dave Jones29464f22009-01-18 01:37:11 -0500480 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (ret != 1) \
482 return -EINVAL; \
483 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200484 ret = __cpufreq_set_policy(policy, &new_policy); \
485 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 \
487 return ret ? ret : count; \
488}
489
Dave Jones29464f22009-01-18 01:37:11 -0500490store_one(scaling_min_freq, min);
491store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493/**
494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
495 */
Dave Jones905d77c2008-03-05 14:28:32 -0500496static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
497 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800499 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!cur_freq)
501 return sprintf(buf, "<unknown>");
502 return sprintf(buf, "%u\n", cur_freq);
503}
504
505
506/**
507 * show_scaling_governor - show the current policy for the specified CPU
508 */
Dave Jones905d77c2008-03-05 14:28:32 -0500509static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Dave Jones29464f22009-01-18 01:37:11 -0500511 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return sprintf(buf, "powersave\n");
513 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
514 return sprintf(buf, "performance\n");
515 else if (policy->governor)
Dave Jones29464f22009-01-18 01:37:11 -0500516 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
517 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EINVAL;
519}
520
521
522/**
523 * store_scaling_governor - store policy for the specified CPU
524 */
Dave Jones905d77c2008-03-05 14:28:32 -0500525static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
526 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 unsigned int ret = -EINVAL;
529 char str_governor[16];
530 struct cpufreq_policy new_policy;
531
532 ret = cpufreq_get_policy(&new_policy, policy->cpu);
533 if (ret)
534 return ret;
535
Dave Jones29464f22009-01-18 01:37:11 -0500536 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (ret != 1)
538 return -EINVAL;
539
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530540 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
541 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -EINVAL;
543
Thomas Renninger7970e082006-04-13 15:14:04 +0200544 /* Do not use cpufreq_set_policy here or the user_policy.max
545 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200546 ret = __cpufreq_set_policy(policy, &new_policy);
547
548 policy->user_policy.policy = policy->policy;
549 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200550
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530551 if (ret)
552 return ret;
553 else
554 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557/**
558 * show_scaling_driver - show the cpufreq driver currently loaded
559 */
Dave Jones905d77c2008-03-05 14:28:32 -0500560static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
563}
564
565/**
566 * show_scaling_available_governors - show the available CPUfreq governors
567 */
Dave Jones905d77c2008-03-05 14:28:32 -0500568static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
569 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
571 ssize_t i = 0;
572 struct cpufreq_governor *t;
573
574 if (!cpufreq_driver->target) {
575 i += sprintf(buf, "performance powersave");
576 goto out;
577 }
578
579 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500580 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
581 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 goto out;
583 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
584 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500585out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 i += sprintf(&buf[i], "\n");
587 return i;
588}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700589
Rusty Russell835481d2009-01-04 05:18:06 -0800590static ssize_t show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592 ssize_t i = 0;
593 unsigned int cpu;
594
Rusty Russell835481d2009-01-04 05:18:06 -0800595 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 if (i)
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
598 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
599 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602 i += sprintf(&buf[i], "\n");
603 return i;
604}
605
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700606/**
607 * show_related_cpus - show the CPUs affected by each transition even if
608 * hw coordination is in use
609 */
610static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
611{
Rusty Russell835481d2009-01-04 05:18:06 -0800612 if (cpumask_empty(policy->related_cpus))
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700613 return show_cpus(policy->cpus, buf);
614 return show_cpus(policy->related_cpus, buf);
615}
616
617/**
618 * show_affected_cpus - show the CPUs affected by each transition
619 */
620static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
621{
622 return show_cpus(policy->cpus, buf);
623}
624
Venki Pallipadi9e769882007-10-26 10:18:21 -0700625static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500626 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700627{
628 unsigned int freq = 0;
629 unsigned int ret;
630
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700631 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700632 return -EINVAL;
633
634 ret = sscanf(buf, "%u", &freq);
635 if (ret != 1)
636 return -EINVAL;
637
638 policy->governor->store_setspeed(policy, freq);
639
640 return count;
641}
642
643static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
644{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700645 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700646 return sprintf(buf, "<unsupported>\n");
647
648 return policy->governor->show_setspeed(policy, buf);
649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Thomas Renningere2f74f32009-11-19 12:31:01 +0100651/**
652 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
653 */
654static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
655{
656 unsigned int limit;
657 int ret;
658 if (cpufreq_driver->bios_limit) {
659 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
660 if (!ret)
661 return sprintf(buf, "%u\n", limit);
662 }
663 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
664}
665
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200666cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
667cpufreq_freq_attr_ro(cpuinfo_min_freq);
668cpufreq_freq_attr_ro(cpuinfo_max_freq);
669cpufreq_freq_attr_ro(cpuinfo_transition_latency);
670cpufreq_freq_attr_ro(scaling_available_governors);
671cpufreq_freq_attr_ro(scaling_driver);
672cpufreq_freq_attr_ro(scaling_cur_freq);
673cpufreq_freq_attr_ro(bios_limit);
674cpufreq_freq_attr_ro(related_cpus);
675cpufreq_freq_attr_ro(affected_cpus);
676cpufreq_freq_attr_rw(scaling_min_freq);
677cpufreq_freq_attr_rw(scaling_max_freq);
678cpufreq_freq_attr_rw(scaling_governor);
679cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dave Jones905d77c2008-03-05 14:28:32 -0500681static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 &cpuinfo_min_freq.attr,
683 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100684 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 &scaling_min_freq.attr,
686 &scaling_max_freq.attr,
687 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700688 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 &scaling_governor.attr,
690 &scaling_driver.attr,
691 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700692 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 NULL
694};
695
Thomas Renninger8aa84ad2009-07-24 15:25:05 +0200696struct kobject *cpufreq_global_kobject;
697EXPORT_SYMBOL(cpufreq_global_kobject);
698
Dave Jones29464f22009-01-18 01:37:11 -0500699#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
700#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Dave Jones29464f22009-01-18 01:37:11 -0500702static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Dave Jones905d77c2008-03-05 14:28:32 -0500704 struct cpufreq_policy *policy = to_policy(kobj);
705 struct freq_attr *fattr = to_attr(attr);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500706 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 policy = cpufreq_cpu_get(policy->cpu);
708 if (!policy)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500709 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800710
711 if (lock_policy_rwsem_read(policy->cpu) < 0)
Dave Jones0db4a8a2008-03-05 14:20:57 -0500712 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800713
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530714 if (fattr->show)
715 ret = fattr->show(policy, buf);
716 else
717 ret = -EIO;
718
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800719 unlock_policy_rwsem_read(policy->cpu);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500720fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 cpufreq_cpu_put(policy);
Dave Jones0db4a8a2008-03-05 14:20:57 -0500722no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return ret;
724}
725
Dave Jones905d77c2008-03-05 14:28:32 -0500726static ssize_t store(struct kobject *kobj, struct attribute *attr,
727 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Dave Jones905d77c2008-03-05 14:28:32 -0500729 struct cpufreq_policy *policy = to_policy(kobj);
730 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500731 ssize_t ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 policy = cpufreq_cpu_get(policy->cpu);
733 if (!policy)
Dave Jonesa07530b2008-03-05 14:22:25 -0500734 goto no_policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800735
736 if (lock_policy_rwsem_write(policy->cpu) < 0)
Dave Jonesa07530b2008-03-05 14:22:25 -0500737 goto fail;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800738
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530739 if (fattr->store)
740 ret = fattr->store(policy, buf, count);
741 else
742 ret = -EIO;
743
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800744 unlock_policy_rwsem_write(policy->cpu);
Dave Jonesa07530b2008-03-05 14:22:25 -0500745fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 cpufreq_cpu_put(policy);
Dave Jonesa07530b2008-03-05 14:22:25 -0500747no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return ret;
749}
750
Dave Jones905d77c2008-03-05 14:28:32 -0500751static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Dave Jones905d77c2008-03-05 14:28:32 -0500753 struct cpufreq_policy *policy = to_policy(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dprintk("last reference is dropped\n");
755 complete(&policy->kobj_unregister);
756}
757
Emese Revfy52cf25d2010-01-19 02:58:23 +0100758static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .show = show,
760 .store = store,
761};
762
763static struct kobj_type ktype_cpufreq = {
764 .sysfs_ops = &sysfs_ops,
765 .default_attrs = default_attrs,
766 .release = cpufreq_sysfs_release,
767};
768
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200769/*
770 * Returns:
771 * Negative: Failure
772 * 0: Success
773 * Positive: When we have a managed CPU and the sysfs got symlinked
774 */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700775static int cpufreq_add_dev_policy(unsigned int cpu,
776 struct cpufreq_policy *policy,
777 struct sys_device *sys_dev)
Dave Jonesecf7e462009-07-08 18:48:47 -0400778{
779 int ret = 0;
780#ifdef CONFIG_SMP
781 unsigned long flags;
782 unsigned int j;
Dave Jonesecf7e462009-07-08 18:48:47 -0400783#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +0400784 struct cpufreq_governor *gov;
785
786 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
787 if (gov) {
788 policy->governor = gov;
Dave Jonesecf7e462009-07-08 18:48:47 -0400789 dprintk("Restoring governor %s for cpu %d\n",
790 policy->governor->name, cpu);
791 }
792#endif
793
794 for_each_cpu(j, policy->cpus) {
795 struct cpufreq_policy *managed_policy;
796
797 if (cpu == j)
798 continue;
799
800 /* Check for existing affected CPUs.
801 * They may not be aware of it due to CPU Hotplug.
802 * cpufreq_cpu_put is called when the device is removed
803 * in __cpufreq_remove_dev()
804 */
805 managed_policy = cpufreq_cpu_get(j);
806 if (unlikely(managed_policy)) {
807
808 /* Set proper policy_cpu */
809 unlock_policy_rwsem_write(cpu);
Tejun Heof1625062009-10-29 22:34:13 +0900810 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
Dave Jonesecf7e462009-07-08 18:48:47 -0400811
812 if (lock_policy_rwsem_write(cpu) < 0) {
813 /* Should not go through policy unlock path */
814 if (cpufreq_driver->exit)
815 cpufreq_driver->exit(policy);
816 cpufreq_cpu_put(managed_policy);
817 return -EBUSY;
818 }
819
820 spin_lock_irqsave(&cpufreq_driver_lock, flags);
821 cpumask_copy(managed_policy->cpus, policy->cpus);
822 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
823 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
824
825 dprintk("CPU already managed, adding link\n");
826 ret = sysfs_create_link(&sys_dev->kobj,
827 &managed_policy->kobj,
828 "cpufreq");
829 if (ret)
830 cpufreq_cpu_put(managed_policy);
831 /*
832 * Success. We only needed to be added to the mask.
833 * Call driver->exit() because only the cpu parent of
834 * the kobj needed to call init().
835 */
836 if (cpufreq_driver->exit)
837 cpufreq_driver->exit(policy);
Thomas Renninger4bfa0422009-07-24 15:25:03 +0200838
839 if (!ret)
840 return 1;
841 else
842 return ret;
Dave Jonesecf7e462009-07-08 18:48:47 -0400843 }
844 }
845#endif
846 return ret;
847}
848
849
Dave Jones19d6f7e2009-07-08 17:35:39 -0400850/* symlink affected CPUs */
Alex Chiangcf3289d2009-11-17 20:27:08 -0700851static int cpufreq_add_dev_symlink(unsigned int cpu,
852 struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400853{
854 unsigned int j;
855 int ret = 0;
856
857 for_each_cpu(j, policy->cpus) {
858 struct cpufreq_policy *managed_policy;
859 struct sys_device *cpu_sys_dev;
860
861 if (j == cpu)
862 continue;
863 if (!cpu_online(j))
864 continue;
865
866 dprintk("CPU %u already managed, adding link\n", j);
867 managed_policy = cpufreq_cpu_get(cpu);
868 cpu_sys_dev = get_cpu_sysdev(j);
869 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
870 "cpufreq");
871 if (ret) {
872 cpufreq_cpu_put(managed_policy);
873 return ret;
874 }
875 }
876 return ret;
877}
878
Alex Chiangcf3289d2009-11-17 20:27:08 -0700879static int cpufreq_add_dev_interface(unsigned int cpu,
880 struct cpufreq_policy *policy,
881 struct sys_device *sys_dev)
Dave Jones909a6942009-07-08 18:05:42 -0400882{
Dave Jonesecf7e462009-07-08 18:48:47 -0400883 struct cpufreq_policy new_policy;
Dave Jones909a6942009-07-08 18:05:42 -0400884 struct freq_attr **drv_attr;
885 unsigned long flags;
886 int ret = 0;
887 unsigned int j;
888
889 /* prepare interface data */
890 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
891 &sys_dev->kobj, "cpufreq");
892 if (ret)
893 return ret;
894
895 /* set up files for this cpu device */
896 drv_attr = cpufreq_driver->attr;
897 while ((drv_attr) && (*drv_attr)) {
898 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
899 if (ret)
900 goto err_out_kobj_put;
901 drv_attr++;
902 }
903 if (cpufreq_driver->get) {
904 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
905 if (ret)
906 goto err_out_kobj_put;
907 }
908 if (cpufreq_driver->target) {
909 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
910 if (ret)
911 goto err_out_kobj_put;
912 }
Thomas Renningere2f74f32009-11-19 12:31:01 +0100913 if (cpufreq_driver->bios_limit) {
914 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
915 if (ret)
916 goto err_out_kobj_put;
917 }
Dave Jones909a6942009-07-08 18:05:42 -0400918
919 spin_lock_irqsave(&cpufreq_driver_lock, flags);
920 for_each_cpu(j, policy->cpus) {
921 if (!cpu_online(j))
922 continue;
923 per_cpu(cpufreq_cpu_data, j) = policy;
Tejun Heof1625062009-10-29 22:34:13 +0900924 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
Dave Jones909a6942009-07-08 18:05:42 -0400925 }
926 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
927
928 ret = cpufreq_add_dev_symlink(cpu, policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400929 if (ret)
930 goto err_out_kobj_put;
931
932 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
933 /* assure that the starting sequence is run in __cpufreq_set_policy */
934 policy->governor = NULL;
935
936 /* set default policy */
937 ret = __cpufreq_set_policy(policy, &new_policy);
938 policy->user_policy.policy = policy->policy;
939 policy->user_policy.governor = policy->governor;
940
941 if (ret) {
942 dprintk("setting policy failed\n");
943 if (cpufreq_driver->exit)
944 cpufreq_driver->exit(policy);
945 }
Dave Jones909a6942009-07-08 18:05:42 -0400946 return ret;
947
948err_out_kobj_put:
949 kobject_put(&policy->kobj);
950 wait_for_completion(&policy->kobj_unregister);
951 return ret;
952}
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955/**
956 * cpufreq_add_dev - add a CPU device
957 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500958 * Adds the cpufreq interface for a CPU device.
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -0400959 *
960 * The Oracle says: try running cpufreq registration/unregistration concurrently
961 * with with cpu hotplugging and all hell will break loose. Tried to clean this
962 * mess up, but more thorough testing is needed. - Mathieu
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 */
Dave Jones905d77c2008-03-05 14:28:32 -0500964static int cpufreq_add_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 unsigned int cpu = sys_dev->id;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500967 int ret = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 unsigned long flags;
970 unsigned int j;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500971#ifdef CONFIG_HOTPLUG_CPU
972 int sibling;
973#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Ashok Rajc32b6b82005-10-30 14:59:54 -0800975 if (cpu_is_offline(cpu))
976 return 0;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 cpufreq_debug_disable_ratelimit();
979 dprintk("adding CPU %u\n", cpu);
980
981#ifdef CONFIG_SMP
982 /* check whether a different CPU already registered this
983 * CPU because it is in the same boat. */
984 policy = cpufreq_cpu_get(cpu);
985 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500986 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 cpufreq_debug_enable_ratelimit();
988 return 0;
989 }
990#endif
991
992 if (!try_module_get(cpufreq_driver->owner)) {
993 ret = -EINVAL;
994 goto module_out;
995 }
996
Dave Jones059019a2009-07-08 16:30:03 -0400997 ret = -ENOMEM;
Dave Jonese98df502005-10-20 15:17:43 -0700998 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Dave Jones059019a2009-07-08 16:30:03 -0400999 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001001
1002 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001003 goto err_free_policy;
Dave Jones059019a2009-07-08 16:30:03 -04001004
1005 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001006 goto err_free_cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 policy->cpu = cpu;
Rusty Russell835481d2009-01-04 05:18:06 -08001009 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001011 /* Initially set CPU itself as the policy_cpu */
Tejun Heof1625062009-10-29 22:34:13 +09001012 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001013 ret = (lock_policy_rwsem_write(cpu) < 0);
1014 WARN_ON(ret);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001015
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001017 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Thomas Renninger8122c6c2007-10-02 13:28:09 -07001019 /* Set governor before ->init, so that driver could check it */
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001020#ifdef CONFIG_HOTPLUG_CPU
1021 for_each_online_cpu(sibling) {
1022 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1023 if (cp && cp->governor &&
1024 (cpumask_test_cpu(cpu, cp->related_cpus))) {
1025 policy->governor = cp->governor;
1026 found = 1;
1027 break;
1028 }
1029 }
1030#endif
1031 if (!found)
1032 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /* call driver. From then on the cpufreq must be able
1034 * to accept all calls to ->verify and ->setpolicy for this CPU
1035 */
1036 ret = cpufreq_driver->init(policy);
1037 if (ret) {
1038 dprintk("initialization failed\n");
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001039 goto err_unlock_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
Mike Chan187d9f42008-12-04 12:19:17 -08001041 policy->user_policy.min = policy->min;
1042 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Thomas Renningera1531ac2008-07-29 22:32:58 -07001044 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1045 CPUFREQ_START, policy);
1046
Dave Jonesecf7e462009-07-08 18:48:47 -04001047 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001048 if (ret) {
1049 if (ret > 0)
1050 /* This is a managed cpu, symlink created,
1051 exit with 0 */
1052 ret = 0;
Dave Jonesecf7e462009-07-08 18:48:47 -04001053 goto err_unlock_policy;
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Dave Jones909a6942009-07-08 18:05:42 -04001056 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
Dave Jones19d6f7e2009-07-08 17:35:39 -04001057 if (ret)
1058 goto err_out_unregister;
Dave Jones8ff69732006-03-05 03:37:23 -05001059
Lothar Waßmanndca02612008-05-29 17:54:52 +02001060 unlock_policy_rwsem_write(cpu);
1061
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001062 kobject_uevent(&policy->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dprintk("initialization complete\n");
1065 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -05001066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 return 0;
1068
1069
1070err_out_unregister:
1071 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Rusty Russell835481d2009-01-04 05:18:06 -08001072 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001073 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1075
Greg Kroah-Hartmanc10997f2007-12-20 08:13:05 -08001076 kobject_put(&policy->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 wait_for_completion(&policy->kobj_unregister);
1078
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001079err_unlock_policy:
Dave Jones45709112008-03-05 14:07:34 -05001080 unlock_policy_rwsem_write(cpu);
Xiaotian Fengcad70a62010-07-20 20:11:02 +08001081 free_cpumask_var(policy->related_cpus);
Mathieu Desnoyers3f4a7822009-07-03 11:25:16 -04001082err_free_cpumask:
1083 free_cpumask_var(policy->cpus);
1084err_free_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 kfree(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086nomem_out:
1087 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001088module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 cpufreq_debug_enable_ratelimit();
1090 return ret;
1091}
1092
1093
1094/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001095 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 *
1097 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001098 * Caller should already have policy_rwsem in write mode for this CPU.
1099 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Dave Jones905d77c2008-03-05 14:28:32 -05001101static int __cpufreq_remove_dev(struct sys_device *sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
1103 unsigned int cpu = sys_dev->id;
1104 unsigned long flags;
1105 struct cpufreq_policy *data;
Amerigo Wang499bca92010-03-04 03:23:46 -05001106 struct kobject *kobj;
1107 struct completion *cmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -08001109 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 unsigned int j;
1111#endif
1112
1113 cpufreq_debug_disable_ratelimit();
1114 dprintk("unregistering CPU %u\n", cpu);
1115
1116 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Mike Travis7a6aedf2008-03-25 15:06:53 -07001117 data = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (!data) {
1120 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001122 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -EINVAL;
1124 }
Mike Travis7a6aedf2008-03-25 15:06:53 -07001125 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127
1128#ifdef CONFIG_SMP
1129 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -05001130 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
1132 if (unlikely(cpu != data->cpu)) {
1133 dprintk("removing link\n");
Rusty Russell835481d2009-01-04 05:18:06 -08001134 cpumask_clear_cpu(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Amerigo Wang499bca92010-03-04 03:23:46 -05001136 kobj = &sys_dev->kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 cpufreq_cpu_put(data);
1138 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001139 unlock_policy_rwsem_write(cpu);
Amerigo Wang499bca92010-03-04 03:23:46 -05001140 sysfs_remove_link(kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return 0;
1142 }
1143#endif
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145#ifdef CONFIG_SMP
Thomas Renninger084f3492007-07-09 11:35:28 -07001146
1147#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001148 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1149 CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001150#endif
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 /* if we have other CPUs still registered, we need to unlink them,
1153 * or else wait_for_completion below will lock up. Clean the
Mike Travis7a6aedf2008-03-25 15:06:53 -07001154 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1155 * the sysfs links afterwards.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 */
Rusty Russell835481d2009-01-04 05:18:06 -08001157 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1158 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 if (j == cpu)
1160 continue;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001161 per_cpu(cpufreq_cpu_data, j) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
1163 }
1164
1165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1166
Rusty Russell835481d2009-01-04 05:18:06 -08001167 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1168 for_each_cpu(j, data->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (j == cpu)
1170 continue;
1171 dprintk("removing link for cpu %u\n", j);
Thomas Renninger084f3492007-07-09 11:35:28 -07001172#ifdef CONFIG_HOTPLUG_CPU
Dmitry Monakhove77b89f2009-10-05 00:38:55 +04001173 strncpy(per_cpu(cpufreq_cpu_governor, j),
1174 data->governor->name, CPUFREQ_NAME_LEN);
Thomas Renninger084f3492007-07-09 11:35:28 -07001175#endif
Ashok Rajd434fca2005-10-30 14:59:52 -08001176 cpu_sys_dev = get_cpu_sysdev(j);
Amerigo Wang499bca92010-03-04 03:23:46 -05001177 kobj = &cpu_sys_dev->kobj;
1178 unlock_policy_rwsem_write(cpu);
1179 sysfs_remove_link(kobj, "cpufreq");
1180 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 cpufreq_cpu_put(data);
1182 }
1183 }
1184#else
1185 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1186#endif
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 if (cpufreq_driver->target)
1189 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001190
Amerigo Wang499bca92010-03-04 03:23:46 -05001191 kobj = &data->kobj;
1192 cmp = &data->kobj_unregister;
1193 unlock_policy_rwsem_write(cpu);
1194 kobject_put(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001197 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 * unloading.
1199 */
1200 dprintk("waiting for dropping of refcount\n");
Amerigo Wang499bca92010-03-04 03:23:46 -05001201 wait_for_completion(cmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 dprintk("wait complete\n");
1203
Amerigo Wang499bca92010-03-04 03:23:46 -05001204 lock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (cpufreq_driver->exit)
1206 cpufreq_driver->exit(data);
venkatesh.pallipadi@intel.com7d26e2d2009-07-02 17:08:30 -07001207 unlock_policy_rwsem_write(cpu);
1208
Rusty Russell835481d2009-01-04 05:18:06 -08001209 free_cpumask_var(data->related_cpus);
1210 free_cpumask_var(data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 kfree(data);
Rusty Russell835481d2009-01-04 05:18:06 -08001212 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216}
1217
1218
Dave Jones905d77c2008-03-05 14:28:32 -05001219static int cpufreq_remove_dev(struct sys_device *sys_dev)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001220{
1221 unsigned int cpu = sys_dev->id;
1222 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001223
1224 if (cpu_is_offline(cpu))
1225 return 0;
1226
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001227 if (unlikely(lock_policy_rwsem_write(cpu)))
1228 BUG();
1229
1230 retval = __cpufreq_remove_dev(sys_dev);
1231 return retval;
1232}
1233
1234
David Howells65f27f32006-11-22 14:55:48 +00001235static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
David Howells65f27f32006-11-22 14:55:48 +00001237 struct cpufreq_policy *policy =
1238 container_of(work, struct cpufreq_policy, update);
1239 unsigned int cpu = policy->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 dprintk("handle_update for cpu %u called\n", cpu);
1241 cpufreq_update_policy(cpu);
1242}
1243
1244/**
1245 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1246 * @cpu: cpu number
1247 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1248 * @new_freq: CPU frequency the CPU actually runs at
1249 *
Dave Jones29464f22009-01-18 01:37:11 -05001250 * We adjust to current frequency first, and need to clean up later.
1251 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301253static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1254 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 struct cpufreq_freqs freqs;
1257
Jan Beulichb10eec22006-04-28 13:47:13 +02001258 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1260
1261 freqs.cpu = cpu;
1262 freqs.old = old_freq;
1263 freqs.new = new_freq;
1264 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1265 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1266}
1267
1268
Dave Jones32ee8c32006-02-28 00:43:23 -05001269/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301270 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001271 * @cpu: CPU number
1272 *
1273 * This is the last known freq, without actually getting it from the driver.
1274 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1275 */
1276unsigned int cpufreq_quick_get(unsigned int cpu)
1277{
1278 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301279 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001280
1281 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301282 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001283 cpufreq_cpu_put(policy);
1284 }
1285
Dave Jones4d34a672008-02-07 16:33:49 -05001286 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001287}
1288EXPORT_SYMBOL(cpufreq_quick_get);
1289
1290
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001291static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001293 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301294 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001297 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301299 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301301 if (ret_freq && policy->cur &&
1302 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1303 /* verify no discrepancy between actual and
1304 saved value exists */
1305 if (unlikely(ret_freq != policy->cur)) {
1306 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 schedule_work(&policy->update);
1308 }
1309 }
1310
Dave Jones4d34a672008-02-07 16:33:49 -05001311 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001312}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001314/**
1315 * cpufreq_get - get the current CPU frequency (in kHz)
1316 * @cpu: CPU number
1317 *
1318 * Get the CPU current (static) CPU frequency
1319 */
1320unsigned int cpufreq_get(unsigned int cpu)
1321{
1322 unsigned int ret_freq = 0;
1323 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1324
1325 if (!policy)
1326 goto out;
1327
1328 if (unlikely(lock_policy_rwsem_read(cpu)))
1329 goto out_policy;
1330
1331 ret_freq = __cpufreq_get(cpu);
1332
1333 unlock_policy_rwsem_read(cpu);
1334
1335out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001337out:
Dave Jones4d34a672008-02-07 16:33:49 -05001338 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340EXPORT_SYMBOL(cpufreq_get);
1341
1342
1343/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001344 * cpufreq_suspend - let the low level driver prepare for suspend
1345 */
1346
Dave Jones905d77c2008-03-05 14:28:32 -05001347static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001348{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301349 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001350
Dave Jones4bc5d342009-08-04 14:03:25 -04001351 int cpu = sysdev->id;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001352 struct cpufreq_policy *cpu_policy;
1353
Dave Jones0e37b152006-09-26 23:02:34 -04001354 dprintk("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001355
1356 if (!cpu_online(cpu))
1357 return 0;
1358
1359 /* we may be lax here as interrupts are off. Nonetheless
1360 * we need to grab the correct cpu policy, as to check
1361 * whether we really run on this CPU.
1362 */
1363
1364 cpu_policy = cpufreq_cpu_get(cpu);
1365 if (!cpu_policy)
1366 return -EINVAL;
1367
1368 /* only handle each CPU group once */
Dave Jonesc9060492008-02-07 16:32:18 -05001369 if (unlikely(cpu_policy->cpu != cpu))
1370 goto out;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001371
1372 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -07001373 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001374 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001375 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1376 "step on CPU %u\n", cpu_policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001377 }
1378
Dave Jones7d5e3502006-02-02 17:03:42 -05001379out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001380 cpufreq_cpu_put(cpu_policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001381 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001382}
1383
1384/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 * cpufreq_resume - restore proper CPU frequency handling after resume
1386 *
1387 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001388 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1389 * restored. It will verify that the current freq is in sync with
1390 * what we believe it to be. This is a bit later than when it
1391 * should be, but nonethteless it's better than calling
1392 * cpufreq_driver->get() here which might re-enable interrupts...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
Dave Jones905d77c2008-03-05 14:28:32 -05001394static int cpufreq_resume(struct sys_device *sysdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301396 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001397
Dave Jones4bc5d342009-08-04 14:03:25 -04001398 int cpu = sysdev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 struct cpufreq_policy *cpu_policy;
1400
1401 dprintk("resuming cpu %u\n", cpu);
1402
1403 if (!cpu_online(cpu))
1404 return 0;
1405
1406 /* we may be lax here as interrupts are off. Nonetheless
1407 * we need to grab the correct cpu policy, as to check
1408 * whether we really run on this CPU.
1409 */
1410
1411 cpu_policy = cpufreq_cpu_get(cpu);
1412 if (!cpu_policy)
1413 return -EINVAL;
1414
1415 /* only handle each CPU group once */
Dave Jonesc9060492008-02-07 16:32:18 -05001416 if (unlikely(cpu_policy->cpu != cpu))
1417 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
1419 if (cpufreq_driver->resume) {
1420 ret = cpufreq_driver->resume(cpu_policy);
1421 if (ret) {
1422 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1423 "step on CPU %u\n", cpu_policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001424 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426 }
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 schedule_work(&cpu_policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001429
Dave Jonesc9060492008-02-07 16:32:18 -05001430fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 cpufreq_cpu_put(cpu_policy);
1432 return ret;
1433}
1434
1435static struct sysdev_driver cpufreq_sysdev_driver = {
1436 .add = cpufreq_add_dev,
1437 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001438 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 .resume = cpufreq_resume,
1440};
1441
1442
1443/*********************************************************************
1444 * NOTIFIER LISTS INTERFACE *
1445 *********************************************************************/
1446
1447/**
1448 * cpufreq_register_notifier - register a driver with cpufreq
1449 * @nb: notifier function to register
1450 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1451 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001452 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 * are notified about clock rate changes (once before and once after
1454 * the transition), or a list of drivers that are notified about
1455 * changes in cpufreq policy.
1456 *
1457 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001458 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
1460int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1461{
1462 int ret;
1463
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001464 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 switch (list) {
1467 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001468 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001469 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001472 ret = blocking_notifier_chain_register(
1473 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 default:
1476 ret = -EINVAL;
1477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 return ret;
1480}
1481EXPORT_SYMBOL(cpufreq_register_notifier);
1482
1483
1484/**
1485 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1486 * @nb: notifier block to be unregistered
1487 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1488 *
1489 * Remove a driver from the CPU frequency notifier list.
1490 *
1491 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001492 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 */
1494int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1495{
1496 int ret;
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 switch (list) {
1499 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001500 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001501 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 break;
1503 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001504 ret = blocking_notifier_chain_unregister(
1505 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
1507 default:
1508 ret = -EINVAL;
1509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 return ret;
1512}
1513EXPORT_SYMBOL(cpufreq_unregister_notifier);
1514
1515
1516/*********************************************************************
1517 * GOVERNORS *
1518 *********************************************************************/
1519
1520
1521int __cpufreq_driver_target(struct cpufreq_policy *policy,
1522 unsigned int target_freq,
1523 unsigned int relation)
1524{
1525 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1528 target_freq, relation);
1529 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1530 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return retval;
1533}
1534EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536int cpufreq_driver_target(struct cpufreq_policy *policy,
1537 unsigned int target_freq,
1538 unsigned int relation)
1539{
Julia Lawallf1829e42008-07-25 22:44:53 +02001540 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 policy = cpufreq_cpu_get(policy->cpu);
1543 if (!policy)
Julia Lawallf1829e42008-07-25 22:44:53 +02001544 goto no_policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001546 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
Julia Lawallf1829e42008-07-25 22:44:53 +02001547 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 ret = __cpufreq_driver_target(policy, target_freq, relation);
1550
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001551 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Julia Lawallf1829e42008-07-25 22:44:53 +02001553fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02001555no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return ret;
1557}
1558EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1559
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001560int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001561{
1562 int ret = 0;
1563
1564 policy = cpufreq_cpu_get(policy->cpu);
1565 if (!policy)
1566 return -EINVAL;
1567
venkatesh.pallipadi@intel.combf0b90e2008-08-04 11:59:07 -07001568 if (cpu_online(cpu) && cpufreq_driver->getavg)
1569 ret = cpufreq_driver->getavg(policy, cpu);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001570
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001571 cpufreq_cpu_put(policy);
1572 return ret;
1573}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001574EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001575
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001576/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001577 * when "event" is CPUFREQ_GOV_LIMITS
1578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301580static int __cpufreq_governor(struct cpufreq_policy *policy,
1581 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Dave Jonescc993ca2005-07-28 09:43:56 -07001583 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001584
1585 /* Only must be defined when default governor is known to have latency
1586 restrictions, like e.g. conservative or ondemand.
1587 That this is the case is already ensured in Kconfig
1588 */
1589#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1590 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1591#else
1592 struct cpufreq_governor *gov = NULL;
1593#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001594
1595 if (policy->governor->max_transition_latency &&
1596 policy->cpuinfo.transition_latency >
1597 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001598 if (!gov)
1599 return -EINVAL;
1600 else {
1601 printk(KERN_WARNING "%s governor failed, too long"
1602 " transition latency of HW, fallback"
1603 " to %s governor\n",
1604 policy->governor->name,
1605 gov->name);
1606 policy->governor = gov;
1607 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
1610 if (!try_module_get(policy->governor->owner))
1611 return -EINVAL;
1612
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301613 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1614 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 ret = policy->governor->governor(policy, event);
1616
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301617 /* we keep one module reference alive for
1618 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if ((event != CPUFREQ_GOV_START) || ret)
1620 module_put(policy->governor->owner);
1621 if ((event == CPUFREQ_GOV_STOP) && !ret)
1622 module_put(policy->governor->owner);
1623
1624 return ret;
1625}
1626
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628int cpufreq_register_governor(struct cpufreq_governor *governor)
1629{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631
1632 if (!governor)
1633 return -EINVAL;
1634
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001635 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001636
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001637 err = -EBUSY;
1638 if (__find_governor(governor->name) == NULL) {
1639 err = 0;
1640 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Dave Jones32ee8c32006-02-28 00:43:23 -05001643 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001644 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1647
1648
1649void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1650{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001651#ifdef CONFIG_HOTPLUG_CPU
1652 int cpu;
1653#endif
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (!governor)
1656 return;
1657
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001658#ifdef CONFIG_HOTPLUG_CPU
1659 for_each_present_cpu(cpu) {
1660 if (cpu_online(cpu))
1661 continue;
1662 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1663 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1664 }
1665#endif
1666
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001667 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001669 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return;
1671}
1672EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1673
1674
1675
1676/*********************************************************************
1677 * POLICY INTERFACE *
1678 *********************************************************************/
1679
1680/**
1681 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001682 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1683 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 *
1685 * Reads the current cpufreq policy.
1686 */
1687int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1688{
1689 struct cpufreq_policy *cpu_policy;
1690 if (!policy)
1691 return -EINVAL;
1692
1693 cpu_policy = cpufreq_cpu_get(cpu);
1694 if (!cpu_policy)
1695 return -EINVAL;
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return 0;
1701}
1702EXPORT_SYMBOL(cpufreq_get_policy);
1703
1704
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001705/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301706 * data : current policy.
1707 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001708 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301709static int __cpufreq_set_policy(struct cpufreq_policy *data,
1710 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 int ret = 0;
1713
1714 cpufreq_debug_disable_ratelimit();
1715 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1716 policy->min, policy->max);
1717
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301718 memcpy(&policy->cpuinfo, &data->cpuinfo,
1719 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Yi Yang53391fa2008-01-30 13:33:34 +01001721 if (policy->min > data->max || policy->max < data->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001722 ret = -EINVAL;
1723 goto error_out;
1724 }
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /* verify the cpu speed can be set within this limit */
1727 ret = cpufreq_driver->verify(policy);
1728 if (ret)
1729 goto error_out;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001732 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1733 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001736 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1737 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 /* verify the cpu speed can be set within this limit,
1740 which might be different to the first one */
1741 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001742 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001746 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1747 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Dave Jones7d5e3502006-02-02 17:03:42 -05001749 data->min = policy->min;
1750 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301752 dprintk("new min and max freqs are %u - %u kHz\n",
1753 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
1755 if (cpufreq_driver->setpolicy) {
1756 data->policy = policy->policy;
1757 dprintk("setting range\n");
1758 ret = cpufreq_driver->setpolicy(policy);
1759 } else {
1760 if (policy->governor != data->governor) {
1761 /* save old, working values */
1762 struct cpufreq_governor *old_gov = data->governor;
1763
1764 dprintk("governor switch\n");
1765
1766 /* end old governor */
Andrej Gelenbergffe62752010-05-14 15:15:58 -07001767 if (data->governor)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1769
1770 /* start new governor */
1771 data->governor = policy->governor;
1772 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1773 /* new governor failed, so re-start old one */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301774 dprintk("starting governor %s failed\n",
1775 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (old_gov) {
1777 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301778 __cpufreq_governor(data,
1779 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781 ret = -EINVAL;
1782 goto error_out;
1783 }
1784 /* might be a policy change, too, so fall through */
1785 }
1786 dprintk("governor: change or update limits\n");
1787 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1788 }
1789
Dave Jones7d5e3502006-02-02 17:03:42 -05001790error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 cpufreq_debug_enable_ratelimit();
1792 return ret;
1793}
1794
1795/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1797 * @cpu: CPU which shall be re-evaluated
1798 *
1799 * Usefull for policy notifiers which have different necessities
1800 * at different times.
1801 */
1802int cpufreq_update_policy(unsigned int cpu)
1803{
1804 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1805 struct cpufreq_policy policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02001806 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Julia Lawallf1829e42008-07-25 22:44:53 +02001808 if (!data) {
1809 ret = -ENODEV;
1810 goto no_policy;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Julia Lawallf1829e42008-07-25 22:44:53 +02001813 if (unlikely(lock_policy_rwsem_write(cpu))) {
1814 ret = -EINVAL;
1815 goto fail;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001819 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 policy.min = data->user_policy.min;
1821 policy.max = data->user_policy.max;
1822 policy.policy = data->user_policy.policy;
1823 policy.governor = data->user_policy.governor;
1824
Thomas Renninger0961dd02006-01-26 18:46:33 +01001825 /* BIOS might change freq behind our back
1826 -> ask driver for current freq and notify governors about a change */
1827 if (cpufreq_driver->get) {
1828 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001829 if (!data->cur) {
1830 dprintk("Driver did not initialize current freq");
1831 data->cur = policy.cur;
1832 } else {
1833 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301834 cpufreq_out_of_sync(cpu, data->cur,
1835 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001836 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001837 }
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 ret = __cpufreq_set_policy(data, &policy);
1840
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001841 unlock_policy_rwsem_write(cpu);
1842
Julia Lawallf1829e42008-07-25 22:44:53 +02001843fail:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 cpufreq_cpu_put(data);
Julia Lawallf1829e42008-07-25 22:44:53 +02001845no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return ret;
1847}
1848EXPORT_SYMBOL(cpufreq_update_policy);
1849
Satyam Sharmadd184a02007-10-02 13:28:14 -07001850static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001851 unsigned long action, void *hcpu)
1852{
1853 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001854 struct sys_device *sys_dev;
1855
1856 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001857 if (sys_dev) {
1858 switch (action) {
1859 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001860 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001861 cpufreq_add_dev(sys_dev);
1862 break;
1863 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001864 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001865 if (unlikely(lock_policy_rwsem_write(cpu)))
1866 BUG();
1867
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001868 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001869 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001870 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001871 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001872 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001873 break;
1874 }
1875 }
1876 return NOTIFY_OK;
1877}
1878
Neal Buckendahl9c36f742010-06-22 22:02:44 -05001879static struct notifier_block __refdata cpufreq_cpu_notifier = {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001880 .notifier_call = cpufreq_cpu_callback,
1881};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883/*********************************************************************
1884 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1885 *********************************************************************/
1886
1887/**
1888 * cpufreq_register_driver - register a CPU Frequency driver
1889 * @driver_data: A struct cpufreq_driver containing the values#
1890 * submitted by the CPU Frequency driver.
1891 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001892 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001894 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 *
1896 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001897int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 unsigned long flags;
1900 int ret;
1901
1902 if (!driver_data || !driver_data->verify || !driver_data->init ||
1903 ((!driver_data->setpolicy) && (!driver_data->target)))
1904 return -EINVAL;
1905
1906 dprintk("trying to register driver %s\n", driver_data->name);
1907
1908 if (driver_data->setpolicy)
1909 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1910
1911 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1912 if (cpufreq_driver) {
1913 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1914 return -EBUSY;
1915 }
1916 cpufreq_driver = driver_data;
1917 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1918
Mike Travis7a6aedf2008-03-25 15:06:53 -07001919 ret = sysdev_driver_register(&cpu_sysdev_class,
1920 &cpufreq_sysdev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1923 int i;
1924 ret = -ENODEV;
1925
1926 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07001927 for (i = 0; i < nr_cpu_ids; i++)
1928 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07001930 break;
1931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 /* if all ->init() calls failed, unregister */
1934 if (ret) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301935 dprintk("no CPU initialized for driver %s\n",
1936 driver_data->name);
1937 sysdev_driver_unregister(&cpu_sysdev_class,
1938 &cpufreq_sysdev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1941 cpufreq_driver = NULL;
1942 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1943 }
1944 }
1945
1946 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001947 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 dprintk("driver %s up and running\n", driver_data->name);
1949 cpufreq_debug_enable_ratelimit();
1950 }
1951
Dave Jones4d34a672008-02-07 16:33:49 -05001952 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1955
1956
1957/**
1958 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1959 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001960 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * the right to do so, i.e. if you have succeeded in initialising before!
1962 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1963 * currently not initialised.
1964 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001965int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 unsigned long flags;
1968
1969 cpufreq_debug_disable_ratelimit();
1970
1971 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1972 cpufreq_debug_enable_ratelimit();
1973 return -EINVAL;
1974 }
1975
1976 dprintk("unregistering driver %s\n", driver->name);
1977
1978 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001979 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
1981 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1982 cpufreq_driver = NULL;
1983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1984
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001988
1989static int __init cpufreq_core_init(void)
1990{
1991 int cpu;
1992
1993 for_each_possible_cpu(cpu) {
Tejun Heof1625062009-10-29 22:34:13 +09001994 per_cpu(cpufreq_policy_cpu, cpu) = -1;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001995 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1996 }
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02001997
1998 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1999 &cpu_sysdev_class.kset.kobj);
2000 BUG_ON(!cpufreq_global_kobject);
2001
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002002 return 0;
2003}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002004core_initcall(cpufreq_core_init);