blob: 0db9e1bda3227f36cb383a641ae0ca670724eb0a [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;
41static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_SPINLOCK(cpufreq_driver_lock);
43
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080044/*
45 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
46 * all cpufreq/hotplug/workqueue/etc related lock issues.
47 *
48 * The rules for this semaphore:
49 * - Any routine that wants to read from the policy structure will
50 * do a down_read on this semaphore.
51 * - Any routine that will write to the policy structure and/or may take away
52 * the policy altogether (eg. CPU hotplug), will hold this lock in write
53 * mode before doing so.
54 *
55 * Additional rules:
56 * - All holders of the lock should check to make sure that the CPU they
57 * are concerned with are online after they get the lock.
58 * - Governor routines that can be called in cpufreq hotplug path should not
59 * take this sem as top level hotplug notifier handler takes this.
60 */
61static DEFINE_PER_CPU(int, policy_cpu);
62static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
63
64#define lock_policy_rwsem(mode, cpu) \
65int lock_policy_rwsem_##mode \
66(int cpu) \
67{ \
68 int policy_cpu = per_cpu(policy_cpu, cpu); \
69 BUG_ON(policy_cpu == -1); \
70 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
71 if (unlikely(!cpu_online(cpu))) { \
72 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
73 return -1; \
74 } \
75 \
76 return 0; \
77}
78
79lock_policy_rwsem(read, cpu);
80EXPORT_SYMBOL_GPL(lock_policy_rwsem_read);
81
82lock_policy_rwsem(write, cpu);
83EXPORT_SYMBOL_GPL(lock_policy_rwsem_write);
84
85void unlock_policy_rwsem_read(int cpu)
86{
87 int policy_cpu = per_cpu(policy_cpu, cpu);
88 BUG_ON(policy_cpu == -1);
89 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
90}
91EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read);
92
93void unlock_policy_rwsem_write(int cpu)
94{
95 int policy_cpu = per_cpu(policy_cpu, cpu);
96 BUG_ON(policy_cpu == -1);
97 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
98}
99EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write);
100
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102/* internal prototypes */
103static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800104static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +0000105static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/**
Dave Jones32ee8c32006-02-28 00:43:23 -0500108 * Two notifier lists: the "policy" list is involved in the
109 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 * "transition" list for kernel code that needs to handle
111 * changes to devices when the CPU clock speed changes.
112 * The mutex locks both lists.
113 */
Alan Sterne041c682006-03-27 01:16:30 -0800114static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700115static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700117static int __init init_cpufreq_transition_notifier_list(void)
118{
119 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
120 return 0;
121}
Linus Torvaldsb3438f82006-11-20 11:47:18 -0800122pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124static LIST_HEAD(cpufreq_governor_list);
Dave Jones7d5e3502006-02-02 17:03:42 -0500125static DEFINE_MUTEX (cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Dave Jones7d5e3502006-02-02 17:03:42 -0500127struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct cpufreq_policy *data;
130 unsigned long flags;
131
132 if (cpu >= NR_CPUS)
133 goto err_out;
134
135 /* get the cpufreq driver */
136 spin_lock_irqsave(&cpufreq_driver_lock, flags);
137
138 if (!cpufreq_driver)
139 goto err_out_unlock;
140
141 if (!try_module_get(cpufreq_driver->owner))
142 goto err_out_unlock;
143
144
145 /* get the CPU */
146 data = cpufreq_cpu_data[cpu];
147
148 if (!data)
149 goto err_out_put_module;
150
151 if (!kobject_get(&data->kobj))
152 goto err_out_put_module;
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return data;
156
Dave Jones7d5e3502006-02-02 17:03:42 -0500157err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -0500159err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500161err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return NULL;
163}
164EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
165
Dave Jones7d5e3502006-02-02 17:03:42 -0500166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167void cpufreq_cpu_put(struct cpufreq_policy *data)
168{
169 kobject_put(&data->kobj);
170 module_put(cpufreq_driver->owner);
171}
172EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
173
174
175/*********************************************************************
176 * UNIFIED DEBUG HELPERS *
177 *********************************************************************/
178#ifdef CONFIG_CPU_FREQ_DEBUG
179
180/* what part(s) of the CPUfreq subsystem are debugged? */
181static unsigned int debug;
182
183/* is the debug output ratelimit'ed using printk_ratelimit? User can
184 * set or modify this value.
185 */
186static unsigned int debug_ratelimit = 1;
187
188/* is the printk_ratelimit'ing enabled? It's enabled after a successful
189 * loading of a cpufreq driver, temporarily disabled when a new policy
190 * is set, and disabled upon cpufreq driver removal
191 */
192static unsigned int disable_ratelimit = 1;
193static DEFINE_SPINLOCK(disable_ratelimit_lock);
194
Arjan van de Ven858119e2006-01-14 13:20:43 -0800195static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 unsigned long flags;
198
199 spin_lock_irqsave(&disable_ratelimit_lock, flags);
200 if (disable_ratelimit)
201 disable_ratelimit--;
202 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
203}
204
Arjan van de Ven858119e2006-01-14 13:20:43 -0800205static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 unsigned long flags;
208
209 spin_lock_irqsave(&disable_ratelimit_lock, flags);
210 disable_ratelimit++;
211 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
212}
213
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530214void cpufreq_debug_printk(unsigned int type, const char *prefix,
215 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 char s[256];
218 va_list args;
219 unsigned int len;
220 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 WARN_ON(!prefix);
223 if (type & debug) {
224 spin_lock_irqsave(&disable_ratelimit_lock, flags);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530225 if (!disable_ratelimit && debug_ratelimit
226 && !printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
228 return;
229 }
230 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
231
232 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
233
234 va_start(args, fmt);
235 len += vsnprintf(&s[len], (256 - len), fmt, args);
236 va_end(args);
237
238 printk(s);
239
240 WARN_ON(len < 5);
241 }
242}
243EXPORT_SYMBOL(cpufreq_debug_printk);
244
245
246module_param(debug, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530247MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
248 " 2 to debug drivers, and 4 to debug governors.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250module_param(debug_ratelimit, uint, 0644);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530251MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
252 " set to 0 to disable ratelimiting.");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254#else /* !CONFIG_CPU_FREQ_DEBUG */
255
256static inline void cpufreq_debug_enable_ratelimit(void) { return; }
257static inline void cpufreq_debug_disable_ratelimit(void) { return; }
258
259#endif /* CONFIG_CPU_FREQ_DEBUG */
260
261
262/*********************************************************************
263 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
264 *********************************************************************/
265
266/**
267 * adjust_jiffies - adjust the system "loops_per_jiffy"
268 *
269 * This function alters the system "loops_per_jiffy" for the clock
270 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500271 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 * per-CPU loops_per_jiffy value wherever possible.
273 */
274#ifndef CONFIG_SMP
275static unsigned long l_p_j_ref;
276static unsigned int l_p_j_ref_freq;
277
Arjan van de Ven858119e2006-01-14 13:20:43 -0800278static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 if (ci->flags & CPUFREQ_CONST_LOOPS)
281 return;
282
283 if (!l_p_j_ref_freq) {
284 l_p_j_ref = loops_per_jiffy;
285 l_p_j_ref_freq = ci->old;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530286 dprintk("saving %lu as reference value for loops_per_jiffy;"
287 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
290 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700291 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530292 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
293 ci->new);
294 dprintk("scaling loops_per_jiffy to %lu"
295 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
297}
298#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530299static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
300{
301 return;
302}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#endif
304
305
306/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800307 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
308 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800310 * This function calls the transition notifiers and the "adjust_jiffies"
311 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500312 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 */
314void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
315{
Dave Jonese4472cb2006-01-31 15:53:55 -0800316 struct cpufreq_policy *policy;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 BUG_ON(irqs_disabled());
319
320 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800321 dprintk("notification %u of frequency transition to %u kHz\n",
322 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dave Jonese4472cb2006-01-31 15:53:55 -0800324 policy = cpufreq_cpu_data[freqs->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500328 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800329 * which is not equal to what the cpufreq core thinks is
330 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
332 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800333 if ((policy) && (policy->cpu == freqs->cpu) &&
334 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200335 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800336 " %u, cpufreq assumed %u kHz.\n",
337 freqs->old, policy->cur);
338 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700341 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800342 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
344 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 case CPUFREQ_POSTCHANGE:
347 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
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_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800350 if (likely(policy) && likely(policy->cpu == freqs->cpu))
351 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 break;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
356
357
358
359/*********************************************************************
360 * SYSFS INTERFACE *
361 *********************************************************************/
362
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700363static struct cpufreq_governor *__find_governor(const char *str_governor)
364{
365 struct cpufreq_governor *t;
366
367 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
368 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
369 return t;
370
371 return NULL;
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/**
375 * cpufreq_parse_governor - parse a governor string
376 */
377static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
378 struct cpufreq_governor **governor)
379{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700380 int err = -EINVAL;
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700383 goto out;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (cpufreq_driver->setpolicy) {
386 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
387 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700388 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530389 } else if (!strnicmp(str_governor, "powersave",
390 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700392 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700394 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700396
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800397 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700398
399 t = __find_governor(str_governor);
400
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700401 if (t == NULL) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530402 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
403 str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700404
405 if (name) {
406 int ret;
407
408 mutex_unlock(&cpufreq_governor_mutex);
409 ret = request_module(name);
410 mutex_lock(&cpufreq_governor_mutex);
411
412 if (ret == 0)
413 t = __find_governor(str_governor);
414 }
415
416 kfree(name);
417 }
418
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700419 if (t != NULL) {
420 *governor = t;
421 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700423
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800424 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700426 out:
427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430
431/* drivers/base/cpu.c */
432extern struct sysdev_class cpu_sysdev_class;
433
434
435/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530436 * cpufreq_per_cpu_attr_read() / show_##file_name() -
437 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
439 * Write out information from cpufreq_driver->policy[cpu]; object must be
440 * "unsigned int".
441 */
442
Dave Jones32ee8c32006-02-28 00:43:23 -0500443#define show_one(file_name, object) \
444static ssize_t show_##file_name \
445(struct cpufreq_policy * policy, char *buf) \
446{ \
447 return sprintf (buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450show_one(cpuinfo_min_freq, cpuinfo.min_freq);
451show_one(cpuinfo_max_freq, cpuinfo.max_freq);
452show_one(scaling_min_freq, min);
453show_one(scaling_max_freq, max);
454show_one(scaling_cur_freq, cur);
455
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530456static int __cpufreq_set_policy(struct cpufreq_policy *data,
457 struct cpufreq_policy *policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459/**
460 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
461 */
462#define store_one(file_name, object) \
463static ssize_t store_##file_name \
464(struct cpufreq_policy * policy, const char *buf, size_t count) \
465{ \
466 unsigned int ret = -EINVAL; \
467 struct cpufreq_policy new_policy; \
468 \
469 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
470 if (ret) \
471 return -EINVAL; \
472 \
473 ret = sscanf (buf, "%u", &new_policy.object); \
474 if (ret != 1) \
475 return -EINVAL; \
476 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200477 ret = __cpufreq_set_policy(policy, &new_policy); \
478 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 \
480 return ret ? ret : count; \
481}
482
483store_one(scaling_min_freq,min);
484store_one(scaling_max_freq,max);
485
486/**
487 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
488 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530489static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy,
490 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800492 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (!cur_freq)
494 return sprintf(buf, "<unknown>");
495 return sprintf(buf, "%u\n", cur_freq);
496}
497
498
499/**
500 * show_scaling_governor - show the current policy for the specified CPU
501 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530502static ssize_t show_scaling_governor (struct cpufreq_policy * policy,
503 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
505 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
506 return sprintf(buf, "powersave\n");
507 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
508 return sprintf(buf, "performance\n");
509 else if (policy->governor)
510 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
511 return -EINVAL;
512}
513
514
515/**
516 * store_scaling_governor - store policy for the specified CPU
517 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500518static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
519 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 unsigned int ret = -EINVAL;
522 char str_governor[16];
523 struct cpufreq_policy new_policy;
524
525 ret = cpufreq_get_policy(&new_policy, policy->cpu);
526 if (ret)
527 return ret;
528
529 ret = sscanf (buf, "%15s", str_governor);
530 if (ret != 1)
531 return -EINVAL;
532
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530533 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
534 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return -EINVAL;
536
Thomas Renninger7970e082006-04-13 15:14:04 +0200537 /* Do not use cpufreq_set_policy here or the user_policy.max
538 will be wrongly overridden */
Thomas Renninger7970e082006-04-13 15:14:04 +0200539 ret = __cpufreq_set_policy(policy, &new_policy);
540
541 policy->user_policy.policy = policy->policy;
542 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200543
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530544 if (ret)
545 return ret;
546 else
547 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/**
551 * show_scaling_driver - show the cpufreq driver currently loaded
552 */
553static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
554{
555 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
556}
557
558/**
559 * show_scaling_available_governors - show the available CPUfreq governors
560 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530561static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 char *buf)
563{
564 ssize_t i = 0;
565 struct cpufreq_governor *t;
566
567 if (!cpufreq_driver->target) {
568 i += sprintf(buf, "performance powersave");
569 goto out;
570 }
571
572 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
573 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
574 goto out;
575 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
576 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500577out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 i += sprintf(&buf[i], "\n");
579 return i;
580}
581/**
582 * show_affected_cpus - show the CPUs affected by each transition
583 */
584static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
585{
586 ssize_t i = 0;
587 unsigned int cpu;
588
589 for_each_cpu_mask(cpu, policy->cpus) {
590 if (i)
591 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
592 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
593 if (i >= (PAGE_SIZE - 5))
594 break;
595 }
596 i += sprintf(&buf[i], "\n");
597 return i;
598}
599
600
601#define define_one_ro(_name) \
602static struct freq_attr _name = \
603__ATTR(_name, 0444, show_##_name, NULL)
604
605#define define_one_ro0400(_name) \
606static struct freq_attr _name = \
607__ATTR(_name, 0400, show_##_name, NULL)
608
609#define define_one_rw(_name) \
610static struct freq_attr _name = \
611__ATTR(_name, 0644, show_##_name, store_##_name)
612
613define_one_ro0400(cpuinfo_cur_freq);
614define_one_ro(cpuinfo_min_freq);
615define_one_ro(cpuinfo_max_freq);
616define_one_ro(scaling_available_governors);
617define_one_ro(scaling_driver);
618define_one_ro(scaling_cur_freq);
619define_one_ro(affected_cpus);
620define_one_rw(scaling_min_freq);
621define_one_rw(scaling_max_freq);
622define_one_rw(scaling_governor);
623
624static struct attribute * default_attrs[] = {
625 &cpuinfo_min_freq.attr,
626 &cpuinfo_max_freq.attr,
627 &scaling_min_freq.attr,
628 &scaling_max_freq.attr,
629 &affected_cpus.attr,
630 &scaling_governor.attr,
631 &scaling_driver.attr,
632 &scaling_available_governors.attr,
633 NULL
634};
635
636#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
637#define to_attr(a) container_of(a,struct freq_attr,attr)
638
639static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
640{
641 struct cpufreq_policy * policy = to_policy(kobj);
642 struct freq_attr * fattr = to_attr(attr);
643 ssize_t ret;
644 policy = cpufreq_cpu_get(policy->cpu);
645 if (!policy)
646 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800647
648 if (lock_policy_rwsem_read(policy->cpu) < 0)
649 return -EINVAL;
650
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530651 if (fattr->show)
652 ret = fattr->show(policy, buf);
653 else
654 ret = -EIO;
655
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800656 unlock_policy_rwsem_read(policy->cpu);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 cpufreq_cpu_put(policy);
659 return ret;
660}
661
Dave Jones32ee8c32006-02-28 00:43:23 -0500662static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 const char * buf, size_t count)
664{
665 struct cpufreq_policy * policy = to_policy(kobj);
666 struct freq_attr * fattr = to_attr(attr);
667 ssize_t ret;
668 policy = cpufreq_cpu_get(policy->cpu);
669 if (!policy)
670 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800671
672 if (lock_policy_rwsem_write(policy->cpu) < 0)
673 return -EINVAL;
674
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530675 if (fattr->store)
676 ret = fattr->store(policy, buf, count);
677 else
678 ret = -EIO;
679
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800680 unlock_policy_rwsem_write(policy->cpu);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 cpufreq_cpu_put(policy);
683 return ret;
684}
685
686static void cpufreq_sysfs_release(struct kobject * kobj)
687{
688 struct cpufreq_policy * policy = to_policy(kobj);
689 dprintk("last reference is dropped\n");
690 complete(&policy->kobj_unregister);
691}
692
693static struct sysfs_ops sysfs_ops = {
694 .show = show,
695 .store = store,
696};
697
698static struct kobj_type ktype_cpufreq = {
699 .sysfs_ops = &sysfs_ops,
700 .default_attrs = default_attrs,
701 .release = cpufreq_sysfs_release,
702};
703
704
705/**
706 * cpufreq_add_dev - add a CPU device
707 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500708 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
710static int cpufreq_add_dev (struct sys_device * sys_dev)
711{
712 unsigned int cpu = sys_dev->id;
713 int ret = 0;
714 struct cpufreq_policy new_policy;
715 struct cpufreq_policy *policy;
716 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500717 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned long flags;
719 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500720#ifdef CONFIG_SMP
721 struct cpufreq_policy *managed_policy;
722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Ashok Rajc32b6b82005-10-30 14:59:54 -0800724 if (cpu_is_offline(cpu))
725 return 0;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 cpufreq_debug_disable_ratelimit();
728 dprintk("adding CPU %u\n", cpu);
729
730#ifdef CONFIG_SMP
731 /* check whether a different CPU already registered this
732 * CPU because it is in the same boat. */
733 policy = cpufreq_cpu_get(cpu);
734 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500735 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 cpufreq_debug_enable_ratelimit();
737 return 0;
738 }
739#endif
740
741 if (!try_module_get(cpufreq_driver->owner)) {
742 ret = -EINVAL;
743 goto module_out;
744 }
745
Dave Jonese98df502005-10-20 15:17:43 -0700746 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!policy) {
748 ret = -ENOMEM;
749 goto nomem_out;
750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 policy->cpu = cpu;
753 policy->cpus = cpumask_of_cpu(cpu);
754
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800755 /* Initially set CPU itself as the policy_cpu */
756 per_cpu(policy_cpu, cpu) = cpu;
757 lock_policy_rwsem_write(cpu);
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +0000760 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 /* call driver. From then on the cpufreq must be able
763 * to accept all calls to ->verify and ->setpolicy for this CPU
764 */
765 ret = cpufreq_driver->init(policy);
766 if (ret) {
767 dprintk("initialization failed\n");
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800768 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto err_out;
770 }
Thomas Renninger22c970f2007-04-19 15:48:34 +0200771 policy->user_policy.min = policy->cpuinfo.min_freq;
772 policy->user_policy.max = policy->cpuinfo.max_freq;
773 policy->user_policy.governor = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Dave Jones8ff69732006-03-05 03:37:23 -0500775#ifdef CONFIG_SMP
776 for_each_cpu_mask(j, policy->cpus) {
777 if (cpu == j)
778 continue;
779
780 /* check for existing affected CPUs. They may not be aware
781 * of it due to CPU Hotplug.
782 */
783 managed_policy = cpufreq_cpu_get(j);
784 if (unlikely(managed_policy)) {
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800785
786 /* Set proper policy_cpu */
787 unlock_policy_rwsem_write(cpu);
788 per_cpu(policy_cpu, cpu) = managed_policy->cpu;
789
790 if (lock_policy_rwsem_write(cpu) < 0)
791 goto err_out_driver_exit;
792
Dave Jones8ff69732006-03-05 03:37:23 -0500793 spin_lock_irqsave(&cpufreq_driver_lock, flags);
794 managed_policy->cpus = policy->cpus;
795 cpufreq_cpu_data[cpu] = managed_policy;
796 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
797
798 dprintk("CPU already managed, adding link\n");
Ahmed S. Darwish0142f9d2007-01-05 05:44:54 +0200799 ret = sysfs_create_link(&sys_dev->kobj,
800 &managed_policy->kobj,
801 "cpufreq");
802 if (ret) {
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800803 unlock_policy_rwsem_write(cpu);
Ahmed S. Darwish0142f9d2007-01-05 05:44:54 +0200804 goto err_out_driver_exit;
805 }
Dave Jones8ff69732006-03-05 03:37:23 -0500806
807 cpufreq_debug_enable_ratelimit();
Dave Jones8ff69732006-03-05 03:37:23 -0500808 ret = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800809 unlock_policy_rwsem_write(cpu);
Dave Jones8ff69732006-03-05 03:37:23 -0500810 goto err_out_driver_exit; /* call driver->exit() */
811 }
812 }
813#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
815
816 /* prepare interface data */
817 policy->kobj.parent = &sys_dev->kobj;
818 policy->kobj.ktype = &ktype_cpufreq;
819 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
820
821 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800822 if (ret) {
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800823 unlock_policy_rwsem_write(cpu);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700824 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* set up files for this cpu device */
827 drv_attr = cpufreq_driver->attr;
828 while ((drv_attr) && (*drv_attr)) {
Tobias Klauser58a72952007-06-14 00:28:15 +0200829 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
830 if (ret)
Thomas Renninger0a4b2cc2007-05-21 07:20:04 -0500831 goto err_out_driver_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 drv_attr++;
833 }
Thomas Renninger0a4b2cc2007-05-21 07:20:04 -0500834 if (cpufreq_driver->get){
Tobias Klauser58a72952007-06-14 00:28:15 +0200835 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
836 if (ret)
Thomas Renninger0a4b2cc2007-05-21 07:20:04 -0500837 goto err_out_driver_exit;
838 }
839 if (cpufreq_driver->target){
Tobias Klauser58a72952007-06-14 00:28:15 +0200840 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
841 if (ret)
Thomas Renninger0a4b2cc2007-05-21 07:20:04 -0500842 goto err_out_driver_exit;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 spin_lock_irqsave(&cpufreq_driver_lock, flags);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800846 for_each_cpu_mask(j, policy->cpus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 cpufreq_cpu_data[j] = policy;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800848 per_cpu(policy_cpu, j) = policy->cpu;
849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500851
852 /* symlink affected CPUs */
853 for_each_cpu_mask(j, policy->cpus) {
854 if (j == cpu)
855 continue;
856 if (!cpu_online(j))
857 continue;
858
Dave Jones1f8b2c92006-03-29 01:40:04 -0500859 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500860 cpufreq_cpu_get(cpu);
861 cpu_sys_dev = get_cpu_sysdev(j);
Ahmed S. Darwish0142f9d2007-01-05 05:44:54 +0200862 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
863 "cpufreq");
864 if (ret) {
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800865 unlock_policy_rwsem_write(cpu);
Ahmed S. Darwish0142f9d2007-01-05 05:44:54 +0200866 goto err_out_unregister;
867 }
Dave Jones8ff69732006-03-05 03:37:23 -0500868 }
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 policy->governor = NULL; /* to assure that the starting sequence is
871 * run in cpufreq_set_policy */
Dave Jones87c32272006-03-29 01:48:37 -0500872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 /* set default policy */
Thomas Renninger22c970f2007-04-19 15:48:34 +0200874 ret = __cpufreq_set_policy(policy, &new_policy);
875 policy->user_policy.policy = policy->policy;
876
877 unlock_policy_rwsem_write(cpu);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (ret) {
880 dprintk("setting policy failed\n");
881 goto err_out_unregister;
882 }
883
884 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 dprintk("initialization complete\n");
886 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889
890
891err_out_unregister:
892 spin_lock_irqsave(&cpufreq_driver_lock, flags);
893 for_each_cpu_mask(j, policy->cpus)
894 cpufreq_cpu_data[j] = NULL;
895 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
896
897 kobject_unregister(&policy->kobj);
898 wait_for_completion(&policy->kobj_unregister);
899
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700900err_out_driver_exit:
901 if (cpufreq_driver->exit)
902 cpufreq_driver->exit(policy);
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904err_out:
905 kfree(policy);
906
907nomem_out:
908 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800909module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 cpufreq_debug_enable_ratelimit();
911 return ret;
912}
913
914
915/**
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800916 * __cpufreq_remove_dev - remove a CPU device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *
918 * Removes the cpufreq interface for a CPU device.
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800919 * Caller should already have policy_rwsem in write mode for this CPU.
920 * This routine frees the rwsem before returning.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 */
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800922static int __cpufreq_remove_dev (struct sys_device * sys_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 unsigned int cpu = sys_dev->id;
925 unsigned long flags;
926 struct cpufreq_policy *data;
927#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800928 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 unsigned int j;
930#endif
931
932 cpufreq_debug_disable_ratelimit();
933 dprintk("unregistering CPU %u\n", cpu);
934
935 spin_lock_irqsave(&cpufreq_driver_lock, flags);
936 data = cpufreq_cpu_data[cpu];
937
938 if (!data) {
939 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800941 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -EINVAL;
943 }
944 cpufreq_cpu_data[cpu] = NULL;
945
946
947#ifdef CONFIG_SMP
948 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500949 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
951 if (unlikely(cpu != data->cpu)) {
952 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500953 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
955 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 cpufreq_cpu_put(data);
957 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800958 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return 0;
960 }
961#endif
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (!kobject_get(&data->kobj)) {
965 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
966 cpufreq_debug_enable_ratelimit();
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800967 unlock_policy_rwsem_write(cpu);
Dave Jones32ee8c32006-02-28 00:43:23 -0500968 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
971#ifdef CONFIG_SMP
972 /* if we have other CPUs still registered, we need to unlink them,
973 * or else wait_for_completion below will lock up. Clean the
974 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
975 * links afterwards.
976 */
977 if (unlikely(cpus_weight(data->cpus) > 1)) {
978 for_each_cpu_mask(j, data->cpus) {
979 if (j == cpu)
980 continue;
981 cpufreq_cpu_data[j] = NULL;
982 }
983 }
984
985 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
986
987 if (unlikely(cpus_weight(data->cpus) > 1)) {
988 for_each_cpu_mask(j, data->cpus) {
989 if (j == cpu)
990 continue;
991 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800992 cpu_sys_dev = get_cpu_sysdev(j);
993 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 cpufreq_cpu_put(data);
995 }
996 }
997#else
998 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
999#endif
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 if (cpufreq_driver->target)
1002 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001003
1004 unlock_policy_rwsem_write(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 kobject_unregister(&data->kobj);
1007
1008 kobject_put(&data->kobj);
1009
1010 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -05001011 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * unloading.
1013 */
1014 dprintk("waiting for dropping of refcount\n");
1015 wait_for_completion(&data->kobj_unregister);
1016 dprintk("wait complete\n");
1017
1018 if (cpufreq_driver->exit)
1019 cpufreq_driver->exit(data);
1020
1021 kfree(data);
1022
1023 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return 0;
1025}
1026
1027
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001028static int cpufreq_remove_dev (struct sys_device * sys_dev)
1029{
1030 unsigned int cpu = sys_dev->id;
1031 int retval;
Venki Pallipadiec282972007-03-26 12:03:19 -07001032
1033 if (cpu_is_offline(cpu))
1034 return 0;
1035
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001036 if (unlikely(lock_policy_rwsem_write(cpu)))
1037 BUG();
1038
1039 retval = __cpufreq_remove_dev(sys_dev);
1040 return retval;
1041}
1042
1043
David Howells65f27f32006-11-22 14:55:48 +00001044static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
David Howells65f27f32006-11-22 14:55:48 +00001046 struct cpufreq_policy *policy =
1047 container_of(work, struct cpufreq_policy, update);
1048 unsigned int cpu = policy->cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 dprintk("handle_update for cpu %u called\n", cpu);
1050 cpufreq_update_policy(cpu);
1051}
1052
1053/**
1054 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1055 * @cpu: cpu number
1056 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1057 * @new_freq: CPU frequency the CPU actually runs at
1058 *
1059 * We adjust to current frequency first, and need to clean up later. So either call
1060 * to cpufreq_update_policy() or schedule handle_update()).
1061 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301062static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1063 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct cpufreq_freqs freqs;
1066
Jan Beulichb10eec22006-04-28 13:47:13 +02001067 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1069
1070 freqs.cpu = cpu;
1071 freqs.old = old_freq;
1072 freqs.new = new_freq;
1073 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1074 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1075}
1076
1077
Dave Jones32ee8c32006-02-28 00:43:23 -05001078/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301079 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001080 * @cpu: CPU number
1081 *
1082 * This is the last known freq, without actually getting it from the driver.
1083 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1084 */
1085unsigned int cpufreq_quick_get(unsigned int cpu)
1086{
1087 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301088 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001089
1090 if (policy) {
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001091 if (unlikely(lock_policy_rwsem_read(cpu)))
1092 return ret_freq;
1093
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301094 ret_freq = policy->cur;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001095
1096 unlock_policy_rwsem_read(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001097 cpufreq_cpu_put(policy);
1098 }
1099
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301100 return (ret_freq);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001101}
1102EXPORT_SYMBOL(cpufreq_quick_get);
1103
1104
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001105static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001107 struct cpufreq_policy *policy = cpufreq_cpu_data[cpu];
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301108 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (!cpufreq_driver->get)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001111 return (ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301113 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301115 if (ret_freq && policy->cur &&
1116 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1117 /* verify no discrepancy between actual and
1118 saved value exists */
1119 if (unlikely(ret_freq != policy->cur)) {
1120 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 schedule_work(&policy->update);
1122 }
1123 }
1124
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001125 return (ret_freq);
1126}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001128/**
1129 * cpufreq_get - get the current CPU frequency (in kHz)
1130 * @cpu: CPU number
1131 *
1132 * Get the CPU current (static) CPU frequency
1133 */
1134unsigned int cpufreq_get(unsigned int cpu)
1135{
1136 unsigned int ret_freq = 0;
1137 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1138
1139 if (!policy)
1140 goto out;
1141
1142 if (unlikely(lock_policy_rwsem_read(cpu)))
1143 goto out_policy;
1144
1145 ret_freq = __cpufreq_get(cpu);
1146
1147 unlock_policy_rwsem_read(cpu);
1148
1149out_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 cpufreq_cpu_put(policy);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001151out:
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301152 return (ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154EXPORT_SYMBOL(cpufreq_get);
1155
1156
1157/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001158 * cpufreq_suspend - let the low level driver prepare for suspend
1159 */
1160
Bernard Blackhame00d9962005-07-07 17:56:42 -07001161static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001162{
1163 int cpu = sysdev->id;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301164 int ret = 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001165 unsigned int cur_freq = 0;
1166 struct cpufreq_policy *cpu_policy;
1167
Dave Jones0e37b152006-09-26 23:02:34 -04001168 dprintk("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001169
1170 if (!cpu_online(cpu))
1171 return 0;
1172
1173 /* we may be lax here as interrupts are off. Nonetheless
1174 * we need to grab the correct cpu policy, as to check
1175 * whether we really run on this CPU.
1176 */
1177
1178 cpu_policy = cpufreq_cpu_get(cpu);
1179 if (!cpu_policy)
1180 return -EINVAL;
1181
1182 /* only handle each CPU group once */
1183 if (unlikely(cpu_policy->cpu != cpu)) {
1184 cpufreq_cpu_put(cpu_policy);
1185 return 0;
1186 }
1187
1188 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -07001189 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001190 if (ret) {
1191 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1192 "step on CPU %u\n", cpu_policy->cpu);
1193 cpufreq_cpu_put(cpu_policy);
1194 return ret;
1195 }
1196 }
1197
1198
1199 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1200 goto out;
1201
1202 if (cpufreq_driver->get)
1203 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1204
1205 if (!cur_freq || !cpu_policy->cur) {
1206 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1207 "frequency is what timing core thinks it is.\n");
1208 goto out;
1209 }
1210
1211 if (unlikely(cur_freq != cpu_policy->cur)) {
1212 struct cpufreq_freqs freqs;
1213
1214 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001215 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001216 "cpufreq assumed %u kHz.\n",
1217 cur_freq, cpu_policy->cur);
1218
1219 freqs.cpu = cpu;
1220 freqs.old = cpu_policy->cur;
1221 freqs.new = cur_freq;
1222
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001223 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001224 CPUFREQ_SUSPENDCHANGE, &freqs);
1225 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1226
1227 cpu_policy->cur = cur_freq;
1228 }
1229
Dave Jones7d5e3502006-02-02 17:03:42 -05001230out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001231 cpufreq_cpu_put(cpu_policy);
1232 return 0;
1233}
1234
1235/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 * cpufreq_resume - restore proper CPU frequency handling after resume
1237 *
1238 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1239 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001240 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1241 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 */
1243static int cpufreq_resume(struct sys_device * sysdev)
1244{
1245 int cpu = sysdev->id;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301246 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct cpufreq_policy *cpu_policy;
1248
1249 dprintk("resuming cpu %u\n", cpu);
1250
1251 if (!cpu_online(cpu))
1252 return 0;
1253
1254 /* we may be lax here as interrupts are off. Nonetheless
1255 * we need to grab the correct cpu policy, as to check
1256 * whether we really run on this CPU.
1257 */
1258
1259 cpu_policy = cpufreq_cpu_get(cpu);
1260 if (!cpu_policy)
1261 return -EINVAL;
1262
1263 /* only handle each CPU group once */
1264 if (unlikely(cpu_policy->cpu != cpu)) {
1265 cpufreq_cpu_put(cpu_policy);
1266 return 0;
1267 }
1268
1269 if (cpufreq_driver->resume) {
1270 ret = cpufreq_driver->resume(cpu_policy);
1271 if (ret) {
1272 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1273 "step on CPU %u\n", cpu_policy->cpu);
1274 cpufreq_cpu_put(cpu_policy);
1275 return ret;
1276 }
1277 }
1278
1279 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1280 unsigned int cur_freq = 0;
1281
1282 if (cpufreq_driver->get)
1283 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1284
1285 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001286 printk(KERN_ERR "cpufreq: resume failed to assert "
1287 "current frequency is what timing core "
1288 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 goto out;
1290 }
1291
1292 if (unlikely(cur_freq != cpu_policy->cur)) {
1293 struct cpufreq_freqs freqs;
1294
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001295 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001296 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001297 "is %u, cpufreq assumed %u kHz.\n",
1298 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 freqs.cpu = cpu;
1301 freqs.old = cpu_policy->cur;
1302 freqs.new = cur_freq;
1303
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001304 srcu_notifier_call_chain(
Alan Sterne041c682006-03-27 01:16:30 -08001305 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001306 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1308
1309 cpu_policy->cur = cur_freq;
1310 }
1311 }
1312
1313out:
1314 schedule_work(&cpu_policy->update);
1315 cpufreq_cpu_put(cpu_policy);
1316 return ret;
1317}
1318
1319static struct sysdev_driver cpufreq_sysdev_driver = {
1320 .add = cpufreq_add_dev,
1321 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001322 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 .resume = cpufreq_resume,
1324};
1325
1326
1327/*********************************************************************
1328 * NOTIFIER LISTS INTERFACE *
1329 *********************************************************************/
1330
1331/**
1332 * cpufreq_register_notifier - register a driver with cpufreq
1333 * @nb: notifier function to register
1334 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1335 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001336 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * are notified about clock rate changes (once before and once after
1338 * the transition), or a list of drivers that are notified about
1339 * changes in cpufreq policy.
1340 *
1341 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001342 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
1344int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1345{
1346 int ret;
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 switch (list) {
1349 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001350 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001351 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 break;
1353 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001354 ret = blocking_notifier_chain_register(
1355 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 break;
1357 default:
1358 ret = -EINVAL;
1359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 return ret;
1362}
1363EXPORT_SYMBOL(cpufreq_register_notifier);
1364
1365
1366/**
1367 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1368 * @nb: notifier block to be unregistered
1369 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1370 *
1371 * Remove a driver from the CPU frequency notifier list.
1372 *
1373 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001374 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
1376int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1377{
1378 int ret;
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 switch (list) {
1381 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001382 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001383 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 break;
1385 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001386 ret = blocking_notifier_chain_unregister(
1387 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 break;
1389 default:
1390 ret = -EINVAL;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
1393 return ret;
1394}
1395EXPORT_SYMBOL(cpufreq_unregister_notifier);
1396
1397
1398/*********************************************************************
1399 * GOVERNORS *
1400 *********************************************************************/
1401
1402
1403int __cpufreq_driver_target(struct cpufreq_policy *policy,
1404 unsigned int target_freq,
1405 unsigned int relation)
1406{
1407 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1410 target_freq, relation);
1411 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1412 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return retval;
1415}
1416EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418int cpufreq_driver_target(struct cpufreq_policy *policy,
1419 unsigned int target_freq,
1420 unsigned int relation)
1421{
Dave Jonescc993ca2005-07-28 09:43:56 -07001422 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 policy = cpufreq_cpu_get(policy->cpu);
1425 if (!policy)
1426 return -EINVAL;
1427
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001428 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 ret = __cpufreq_driver_target(policy, target_freq, relation);
1432
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001433 unlock_policy_rwsem_write(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return ret;
1437}
1438EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1439
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001440int __cpufreq_driver_getavg(struct cpufreq_policy *policy)
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001441{
1442 int ret = 0;
1443
1444 policy = cpufreq_cpu_get(policy->cpu);
1445 if (!policy)
1446 return -EINVAL;
1447
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001448 if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
1449 ret = cpufreq_driver->getavg(policy->cpu);
1450
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001451 cpufreq_cpu_put(policy);
1452 return ret;
1453}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001454EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
Venkatesh Pallipadidfde5d62006-10-03 12:38:45 -07001455
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001456/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001457 * when "event" is CPUFREQ_GOV_LIMITS
1458 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301460static int __cpufreq_governor(struct cpufreq_policy *policy,
1461 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Dave Jonescc993ca2005-07-28 09:43:56 -07001463 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 if (!try_module_get(policy->governor->owner))
1466 return -EINVAL;
1467
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301468 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1469 policy->cpu, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 ret = policy->governor->governor(policy, event);
1471
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301472 /* we keep one module reference alive for
1473 each CPU governed by this CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if ((event != CPUFREQ_GOV_START) || ret)
1475 module_put(policy->governor->owner);
1476 if ((event == CPUFREQ_GOV_STOP) && !ret)
1477 module_put(policy->governor->owner);
1478
1479 return ret;
1480}
1481
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483int cpufreq_register_governor(struct cpufreq_governor *governor)
1484{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001485 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (!governor)
1488 return -EINVAL;
1489
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001490 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001491
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001492 err = -EBUSY;
1493 if (__find_governor(governor->name) == NULL) {
1494 err = 0;
1495 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Dave Jones32ee8c32006-02-28 00:43:23 -05001498 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001499 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1502
1503
1504void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1505{
1506 if (!governor)
1507 return;
1508
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001509 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001511 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 return;
1513}
1514EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1515
1516
1517
1518/*********************************************************************
1519 * POLICY INTERFACE *
1520 *********************************************************************/
1521
1522/**
1523 * cpufreq_get_policy - get the current cpufreq_policy
1524 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1525 *
1526 * Reads the current cpufreq policy.
1527 */
1528int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1529{
1530 struct cpufreq_policy *cpu_policy;
1531 if (!policy)
1532 return -EINVAL;
1533
1534 cpu_policy = cpufreq_cpu_get(cpu);
1535 if (!cpu_policy)
1536 return -EINVAL;
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 return 0;
1542}
1543EXPORT_SYMBOL(cpufreq_get_policy);
1544
1545
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001546/*
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301547 * data : current policy.
1548 * policy : policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001549 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301550static int __cpufreq_set_policy(struct cpufreq_policy *data,
1551 struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
1553 int ret = 0;
1554
1555 cpufreq_debug_disable_ratelimit();
1556 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1557 policy->min, policy->max);
1558
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301559 memcpy(&policy->cpuinfo, &data->cpuinfo,
1560 sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001562 if (policy->min > data->min && policy->min > policy->max) {
1563 ret = -EINVAL;
1564 goto error_out;
1565 }
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 /* verify the cpu speed can be set within this limit */
1568 ret = cpufreq_driver->verify(policy);
1569 if (ret)
1570 goto error_out;
1571
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001573 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1574 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001577 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1578 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 /* verify the cpu speed can be set within this limit,
1581 which might be different to the first one */
1582 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001583 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001587 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1588 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Dave Jones7d5e3502006-02-02 17:03:42 -05001590 data->min = policy->min;
1591 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301593 dprintk("new min and max freqs are %u - %u kHz\n",
1594 data->min, data->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 if (cpufreq_driver->setpolicy) {
1597 data->policy = policy->policy;
1598 dprintk("setting range\n");
1599 ret = cpufreq_driver->setpolicy(policy);
1600 } else {
1601 if (policy->governor != data->governor) {
1602 /* save old, working values */
1603 struct cpufreq_governor *old_gov = data->governor;
1604
1605 dprintk("governor switch\n");
1606
1607 /* end old governor */
1608 if (data->governor)
1609 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1610
1611 /* start new governor */
1612 data->governor = policy->governor;
1613 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1614 /* new governor failed, so re-start old one */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301615 dprintk("starting governor %s failed\n",
1616 data->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (old_gov) {
1618 data->governor = old_gov;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301619 __cpufreq_governor(data,
1620 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 ret = -EINVAL;
1623 goto error_out;
1624 }
1625 /* might be a policy change, too, so fall through */
1626 }
1627 dprintk("governor: change or update limits\n");
1628 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1629 }
1630
Dave Jones7d5e3502006-02-02 17:03:42 -05001631error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 cpufreq_debug_enable_ratelimit();
1633 return ret;
1634}
1635
1636/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1638 * @cpu: CPU which shall be re-evaluated
1639 *
1640 * Usefull for policy notifiers which have different necessities
1641 * at different times.
1642 */
1643int cpufreq_update_policy(unsigned int cpu)
1644{
1645 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1646 struct cpufreq_policy policy;
1647 int ret = 0;
1648
1649 if (!data)
1650 return -ENODEV;
1651
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001652 if (unlikely(lock_policy_rwsem_write(cpu)))
1653 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001656 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 policy.min = data->user_policy.min;
1658 policy.max = data->user_policy.max;
1659 policy.policy = data->user_policy.policy;
1660 policy.governor = data->user_policy.governor;
1661
Thomas Renninger0961dd02006-01-26 18:46:33 +01001662 /* BIOS might change freq behind our back
1663 -> ask driver for current freq and notify governors about a change */
1664 if (cpufreq_driver->get) {
1665 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001666 if (!data->cur) {
1667 dprintk("Driver did not initialize current freq");
1668 data->cur = policy.cur;
1669 } else {
1670 if (data->cur != policy.cur)
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301671 cpufreq_out_of_sync(cpu, data->cur,
1672 policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001673 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001674 }
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 ret = __cpufreq_set_policy(data, &policy);
1677
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001678 unlock_policy_rwsem_write(cpu);
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 cpufreq_cpu_put(data);
1681 return ret;
1682}
1683EXPORT_SYMBOL(cpufreq_update_policy);
1684
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001685static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001686 unsigned long action, void *hcpu)
1687{
1688 unsigned int cpu = (unsigned long)hcpu;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001689 struct sys_device *sys_dev;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001690 struct cpufreq_policy *policy;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001691
1692 sys_dev = get_cpu_sysdev(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001693 if (sys_dev) {
1694 switch (action) {
1695 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001696 case CPU_ONLINE_FROZEN:
Ashok Rajc32b6b82005-10-30 14:59:54 -08001697 cpufreq_add_dev(sys_dev);
1698 break;
1699 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001700 case CPU_DOWN_PREPARE_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001701 if (unlikely(lock_policy_rwsem_write(cpu)))
1702 BUG();
1703
Ashok Rajc32b6b82005-10-30 14:59:54 -08001704 policy = cpufreq_cpu_data[cpu];
1705 if (policy) {
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001706 __cpufreq_driver_target(policy, policy->min,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001707 CPUFREQ_RELATION_H);
1708 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001709 __cpufreq_remove_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001710 break;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001711 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001712 case CPU_DOWN_FAILED_FROZEN:
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001713 cpufreq_add_dev(sys_dev);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001714 break;
1715 }
1716 }
1717 return NOTIFY_OK;
1718}
1719
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001720static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001721{
1722 .notifier_call = cpufreq_cpu_callback,
1723};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
1725/*********************************************************************
1726 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1727 *********************************************************************/
1728
1729/**
1730 * cpufreq_register_driver - register a CPU Frequency driver
1731 * @driver_data: A struct cpufreq_driver containing the values#
1732 * submitted by the CPU Frequency driver.
1733 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001734 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001736 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 *
1738 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001739int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 unsigned long flags;
1742 int ret;
1743
1744 if (!driver_data || !driver_data->verify || !driver_data->init ||
1745 ((!driver_data->setpolicy) && (!driver_data->target)))
1746 return -EINVAL;
1747
1748 dprintk("trying to register driver %s\n", driver_data->name);
1749
1750 if (driver_data->setpolicy)
1751 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1752
1753 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1754 if (cpufreq_driver) {
1755 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1756 return -EBUSY;
1757 }
1758 cpufreq_driver = driver_data;
1759 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1760
1761 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1762
1763 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1764 int i;
1765 ret = -ENODEV;
1766
1767 /* check for at least one working CPU */
1768 for (i=0; i<NR_CPUS; i++)
1769 if (cpufreq_cpu_data[i])
1770 ret = 0;
1771
1772 /* if all ->init() calls failed, unregister */
1773 if (ret) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301774 dprintk("no CPU initialized for driver %s\n",
1775 driver_data->name);
1776 sysdev_driver_unregister(&cpu_sysdev_class,
1777 &cpufreq_sysdev_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1780 cpufreq_driver = NULL;
1781 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1782 }
1783 }
1784
1785 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001786 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 dprintk("driver %s up and running\n", driver_data->name);
1788 cpufreq_debug_enable_ratelimit();
1789 }
1790
1791 return (ret);
1792}
1793EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1794
1795
1796/**
1797 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1798 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001799 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 * the right to do so, i.e. if you have succeeded in initialising before!
1801 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1802 * currently not initialised.
1803 */
Linus Torvalds221dee22007-02-26 14:55:48 -08001804int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 unsigned long flags;
1807
1808 cpufreq_debug_disable_ratelimit();
1809
1810 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1811 cpufreq_debug_enable_ratelimit();
1812 return -EINVAL;
1813 }
1814
1815 dprintk("unregistering driver %s\n", driver->name);
1816
1817 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001818 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1821 cpufreq_driver = NULL;
1822 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1823
1824 return 0;
1825}
1826EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001827
1828static int __init cpufreq_core_init(void)
1829{
1830 int cpu;
1831
1832 for_each_possible_cpu(cpu) {
1833 per_cpu(policy_cpu, cpu) = -1;
1834 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1835 }
1836 return 0;
1837}
1838
1839core_initcall(cpufreq_core_init);