]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/cpufreq/cpufreq.c
[PATCH] cfq-iosched: don't use a hard jiffies value, translate from msecs
[net-next-2.6.git] / drivers / cpufreq / cpufreq.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
c32b6b8e 7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
32ee8c3e 8 * Added handling for CPU hotplug
8ff69732
DJ
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
c32b6b8e 11 *
1da177e4
LT
12 * 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
1da177e4
LT
18#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>
3fc54d37 30#include <linux/mutex.h>
1da177e4
LT
31
32#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
33
34/**
35 * The "cpufreq driver" - the arch- or hardware-dependend low
36 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
38 */
7d5e350f
DJ
39static struct cpufreq_driver *cpufreq_driver;
40static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
1da177e4
LT
41static DEFINE_SPINLOCK(cpufreq_driver_lock);
42
1da177e4
LT
43/* internal prototypes */
44static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
45static void handle_update(void *data);
1da177e4
LT
46
47/**
32ee8c3e
DJ
48 * Two notifier lists: the "policy" list is involved in the
49 * validation process for a new CPU frequency policy; the
1da177e4
LT
50 * "transition" list for kernel code that needs to handle
51 * changes to devices when the CPU clock speed changes.
52 * The mutex locks both lists.
53 */
e041c683
AS
54static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
55static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
1da177e4
LT
56
57
58static LIST_HEAD(cpufreq_governor_list);
7d5e350f 59static DEFINE_MUTEX (cpufreq_governor_mutex);
1da177e4 60
7d5e350f 61struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1da177e4
LT
62{
63 struct cpufreq_policy *data;
64 unsigned long flags;
65
66 if (cpu >= NR_CPUS)
67 goto err_out;
68
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
71
72 if (!cpufreq_driver)
73 goto err_out_unlock;
74
75 if (!try_module_get(cpufreq_driver->owner))
76 goto err_out_unlock;
77
78
79 /* get the CPU */
80 data = cpufreq_cpu_data[cpu];
81
82 if (!data)
83 goto err_out_put_module;
84
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
87
1da177e4 88 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
89 return data;
90
7d5e350f 91err_out_put_module:
1da177e4 92 module_put(cpufreq_driver->owner);
7d5e350f 93err_out_unlock:
1da177e4 94 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
7d5e350f 95err_out:
1da177e4
LT
96 return NULL;
97}
98EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
99
7d5e350f 100
1da177e4
LT
101void cpufreq_cpu_put(struct cpufreq_policy *data)
102{
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
105}
106EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
107
108
109/*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112#ifdef CONFIG_CPU_FREQ_DEBUG
113
114/* what part(s) of the CPUfreq subsystem are debugged? */
115static unsigned int debug;
116
117/* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
119 */
120static unsigned int debug_ratelimit = 1;
121
122/* is the printk_ratelimit'ing enabled? It's enabled after a successful
123 * loading of a cpufreq driver, temporarily disabled when a new policy
124 * is set, and disabled upon cpufreq driver removal
125 */
126static unsigned int disable_ratelimit = 1;
127static DEFINE_SPINLOCK(disable_ratelimit_lock);
128
858119e1 129static void cpufreq_debug_enable_ratelimit(void)
1da177e4
LT
130{
131 unsigned long flags;
132
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
135 disable_ratelimit--;
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
137}
138
858119e1 139static void cpufreq_debug_disable_ratelimit(void)
1da177e4
LT
140{
141 unsigned long flags;
142
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
144 disable_ratelimit++;
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
146}
147
148void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
149{
150 char s[256];
151 va_list args;
152 unsigned int len;
153 unsigned long flags;
32ee8c3e 154
1da177e4
LT
155 WARN_ON(!prefix);
156 if (type & debug) {
157 spin_lock_irqsave(&disable_ratelimit_lock, flags);
158 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
159 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
160 return;
161 }
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
163
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
165
166 va_start(args, fmt);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
168 va_end(args);
169
170 printk(s);
171
172 WARN_ON(len < 5);
173 }
174}
175EXPORT_SYMBOL(cpufreq_debug_printk);
176
177
178module_param(debug, uint, 0644);
179MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
180
181module_param(debug_ratelimit, uint, 0644);
182MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
183
184#else /* !CONFIG_CPU_FREQ_DEBUG */
185
186static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187static inline void cpufreq_debug_disable_ratelimit(void) { return; }
188
189#endif /* CONFIG_CPU_FREQ_DEBUG */
190
191
192/*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
195
196/**
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
198 *
199 * This function alters the system "loops_per_jiffy" for the clock
200 * speed change. Note that loops_per_jiffy cannot be updated on SMP
32ee8c3e 201 * systems as each CPU might be scaled differently. So, use the arch
1da177e4
LT
202 * per-CPU loops_per_jiffy value wherever possible.
203 */
204#ifndef CONFIG_SMP
205static unsigned long l_p_j_ref;
206static unsigned int l_p_j_ref_freq;
207
858119e1 208static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1da177e4
LT
209{
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
211 return;
212
213 if (!l_p_j_ref_freq) {
214 l_p_j_ref = loops_per_jiffy;
215 l_p_j_ref_freq = ci->old;
216 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
217 }
218 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
219 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
42d4dc3f 220 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
1da177e4
LT
221 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
222 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
223 }
224}
225#else
226static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
227#endif
228
229
230/**
e4472cb3
DJ
231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
1da177e4 233 *
e4472cb3
DJ
234 * This function calls the transition notifiers and the "adjust_jiffies"
235 * function. It is called twice on all CPU frequency changes that have
32ee8c3e 236 * external effects.
1da177e4
LT
237 */
238void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
239{
e4472cb3
DJ
240 struct cpufreq_policy *policy;
241
1da177e4
LT
242 BUG_ON(irqs_disabled());
243
244 freqs->flags = cpufreq_driver->flags;
e4472cb3
DJ
245 dprintk("notification %u of frequency transition to %u kHz\n",
246 state, freqs->new);
1da177e4 247
e4472cb3 248 policy = cpufreq_cpu_data[freqs->cpu];
1da177e4 249 switch (state) {
e4472cb3 250
1da177e4 251 case CPUFREQ_PRECHANGE:
32ee8c3e 252 /* detect if the driver reported a value as "old frequency"
e4472cb3
DJ
253 * which is not equal to what the cpufreq core thinks is
254 * "old frequency".
1da177e4
LT
255 */
256 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e4472cb3
DJ
257 if ((policy) && (policy->cpu == freqs->cpu) &&
258 (policy->cur) && (policy->cur != freqs->old)) {
b10eec22 259 dprintk("Warning: CPU frequency is"
e4472cb3
DJ
260 " %u, cpufreq assumed %u kHz.\n",
261 freqs->old, policy->cur);
262 freqs->old = policy->cur;
1da177e4
LT
263 }
264 }
e041c683
AS
265 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266 CPUFREQ_PRECHANGE, freqs);
1da177e4
LT
267 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
268 break;
e4472cb3 269
1da177e4
LT
270 case CPUFREQ_POSTCHANGE:
271 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
e041c683
AS
272 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
273 CPUFREQ_POSTCHANGE, freqs);
e4472cb3
DJ
274 if (likely(policy) && likely(policy->cpu == freqs->cpu))
275 policy->cur = freqs->new;
1da177e4
LT
276 break;
277 }
1da177e4
LT
278}
279EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
280
281
282
283/*********************************************************************
284 * SYSFS INTERFACE *
285 *********************************************************************/
286
287/**
288 * cpufreq_parse_governor - parse a governor string
289 */
290static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
291 struct cpufreq_governor **governor)
292{
293 if (!cpufreq_driver)
294 return -EINVAL;
295 if (cpufreq_driver->setpolicy) {
296 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
297 *policy = CPUFREQ_POLICY_PERFORMANCE;
298 return 0;
299 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_POWERSAVE;
301 return 0;
302 }
303 return -EINVAL;
304 } else {
305 struct cpufreq_governor *t;
3fc54d37 306 mutex_lock(&cpufreq_governor_mutex);
1da177e4
LT
307 if (!cpufreq_driver || !cpufreq_driver->target)
308 goto out;
309 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
310 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
311 *governor = t;
3fc54d37 312 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
313 return 0;
314 }
315 }
7d5e350f 316out:
3fc54d37 317 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
318 }
319 return -EINVAL;
320}
1da177e4
LT
321
322
323/* drivers/base/cpu.c */
324extern struct sysdev_class cpu_sysdev_class;
325
326
327/**
328 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
329 *
330 * Write out information from cpufreq_driver->policy[cpu]; object must be
331 * "unsigned int".
332 */
333
32ee8c3e
DJ
334#define show_one(file_name, object) \
335static ssize_t show_##file_name \
336(struct cpufreq_policy * policy, char *buf) \
337{ \
338 return sprintf (buf, "%u\n", policy->object); \
1da177e4
LT
339}
340
341show_one(cpuinfo_min_freq, cpuinfo.min_freq);
342show_one(cpuinfo_max_freq, cpuinfo.max_freq);
343show_one(scaling_min_freq, min);
344show_one(scaling_max_freq, max);
345show_one(scaling_cur_freq, cur);
346
7970e08b
TR
347static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
348
1da177e4
LT
349/**
350 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
351 */
352#define store_one(file_name, object) \
353static ssize_t store_##file_name \
354(struct cpufreq_policy * policy, const char *buf, size_t count) \
355{ \
356 unsigned int ret = -EINVAL; \
357 struct cpufreq_policy new_policy; \
358 \
359 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
360 if (ret) \
361 return -EINVAL; \
362 \
363 ret = sscanf (buf, "%u", &new_policy.object); \
364 if (ret != 1) \
365 return -EINVAL; \
366 \
7970e08b
TR
367 mutex_lock(&policy->lock); \
368 ret = __cpufreq_set_policy(policy, &new_policy); \
369 policy->user_policy.object = policy->object; \
370 mutex_unlock(&policy->lock); \
1da177e4
LT
371 \
372 return ret ? ret : count; \
373}
374
375store_one(scaling_min_freq,min);
376store_one(scaling_max_freq,max);
377
378/**
379 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
380 */
381static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
382{
383 unsigned int cur_freq = cpufreq_get(policy->cpu);
384 if (!cur_freq)
385 return sprintf(buf, "<unknown>");
386 return sprintf(buf, "%u\n", cur_freq);
387}
388
389
390/**
391 * show_scaling_governor - show the current policy for the specified CPU
392 */
393static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
394{
395 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
396 return sprintf(buf, "powersave\n");
397 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
398 return sprintf(buf, "performance\n");
399 else if (policy->governor)
400 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
401 return -EINVAL;
402}
403
404
405/**
406 * store_scaling_governor - store policy for the specified CPU
407 */
32ee8c3e
DJ
408static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
409 const char *buf, size_t count)
1da177e4
LT
410{
411 unsigned int ret = -EINVAL;
412 char str_governor[16];
413 struct cpufreq_policy new_policy;
414
415 ret = cpufreq_get_policy(&new_policy, policy->cpu);
416 if (ret)
417 return ret;
418
419 ret = sscanf (buf, "%15s", str_governor);
420 if (ret != 1)
421 return -EINVAL;
422
423 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
424 return -EINVAL;
425
a496e25d
DJ
426 lock_cpu_hotplug();
427
7970e08b
TR
428 /* Do not use cpufreq_set_policy here or the user_policy.max
429 will be wrongly overridden */
430 mutex_lock(&policy->lock);
431 ret = __cpufreq_set_policy(policy, &new_policy);
432
433 policy->user_policy.policy = policy->policy;
434 policy->user_policy.governor = policy->governor;
435 mutex_unlock(&policy->lock);
436
a496e25d
DJ
437 unlock_cpu_hotplug();
438
1da177e4
LT
439 return ret ? ret : count;
440}
441
442/**
443 * show_scaling_driver - show the cpufreq driver currently loaded
444 */
445static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
446{
447 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
448}
449
450/**
451 * show_scaling_available_governors - show the available CPUfreq governors
452 */
453static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
454 char *buf)
455{
456 ssize_t i = 0;
457 struct cpufreq_governor *t;
458
459 if (!cpufreq_driver->target) {
460 i += sprintf(buf, "performance powersave");
461 goto out;
462 }
463
464 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
465 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
466 goto out;
467 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
468 }
7d5e350f 469out:
1da177e4
LT
470 i += sprintf(&buf[i], "\n");
471 return i;
472}
473/**
474 * show_affected_cpus - show the CPUs affected by each transition
475 */
476static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
477{
478 ssize_t i = 0;
479 unsigned int cpu;
480
481 for_each_cpu_mask(cpu, policy->cpus) {
482 if (i)
483 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
484 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
485 if (i >= (PAGE_SIZE - 5))
486 break;
487 }
488 i += sprintf(&buf[i], "\n");
489 return i;
490}
491
492
493#define define_one_ro(_name) \
494static struct freq_attr _name = \
495__ATTR(_name, 0444, show_##_name, NULL)
496
497#define define_one_ro0400(_name) \
498static struct freq_attr _name = \
499__ATTR(_name, 0400, show_##_name, NULL)
500
501#define define_one_rw(_name) \
502static struct freq_attr _name = \
503__ATTR(_name, 0644, show_##_name, store_##_name)
504
505define_one_ro0400(cpuinfo_cur_freq);
506define_one_ro(cpuinfo_min_freq);
507define_one_ro(cpuinfo_max_freq);
508define_one_ro(scaling_available_governors);
509define_one_ro(scaling_driver);
510define_one_ro(scaling_cur_freq);
511define_one_ro(affected_cpus);
512define_one_rw(scaling_min_freq);
513define_one_rw(scaling_max_freq);
514define_one_rw(scaling_governor);
515
516static struct attribute * default_attrs[] = {
517 &cpuinfo_min_freq.attr,
518 &cpuinfo_max_freq.attr,
519 &scaling_min_freq.attr,
520 &scaling_max_freq.attr,
521 &affected_cpus.attr,
522 &scaling_governor.attr,
523 &scaling_driver.attr,
524 &scaling_available_governors.attr,
525 NULL
526};
527
528#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
529#define to_attr(a) container_of(a,struct freq_attr,attr)
530
531static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
532{
533 struct cpufreq_policy * policy = to_policy(kobj);
534 struct freq_attr * fattr = to_attr(attr);
535 ssize_t ret;
536 policy = cpufreq_cpu_get(policy->cpu);
537 if (!policy)
538 return -EINVAL;
70f2817a 539 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
1da177e4
LT
540 cpufreq_cpu_put(policy);
541 return ret;
542}
543
32ee8c3e 544static ssize_t store(struct kobject * kobj, struct attribute * attr,
1da177e4
LT
545 const char * buf, size_t count)
546{
547 struct cpufreq_policy * policy = to_policy(kobj);
548 struct freq_attr * fattr = to_attr(attr);
549 ssize_t ret;
550 policy = cpufreq_cpu_get(policy->cpu);
551 if (!policy)
552 return -EINVAL;
70f2817a 553 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
1da177e4
LT
554 cpufreq_cpu_put(policy);
555 return ret;
556}
557
558static void cpufreq_sysfs_release(struct kobject * kobj)
559{
560 struct cpufreq_policy * policy = to_policy(kobj);
561 dprintk("last reference is dropped\n");
562 complete(&policy->kobj_unregister);
563}
564
565static struct sysfs_ops sysfs_ops = {
566 .show = show,
567 .store = store,
568};
569
570static struct kobj_type ktype_cpufreq = {
571 .sysfs_ops = &sysfs_ops,
572 .default_attrs = default_attrs,
573 .release = cpufreq_sysfs_release,
574};
575
576
577/**
578 * cpufreq_add_dev - add a CPU device
579 *
32ee8c3e 580 * Adds the cpufreq interface for a CPU device.
1da177e4
LT
581 */
582static int cpufreq_add_dev (struct sys_device * sys_dev)
583{
584 unsigned int cpu = sys_dev->id;
585 int ret = 0;
586 struct cpufreq_policy new_policy;
587 struct cpufreq_policy *policy;
588 struct freq_attr **drv_attr;
8ff69732 589 struct sys_device *cpu_sys_dev;
1da177e4
LT
590 unsigned long flags;
591 unsigned int j;
8ff69732
DJ
592#ifdef CONFIG_SMP
593 struct cpufreq_policy *managed_policy;
594#endif
1da177e4 595
c32b6b8e
AR
596 if (cpu_is_offline(cpu))
597 return 0;
598
1da177e4
LT
599 cpufreq_debug_disable_ratelimit();
600 dprintk("adding CPU %u\n", cpu);
601
602#ifdef CONFIG_SMP
603 /* check whether a different CPU already registered this
604 * CPU because it is in the same boat. */
605 policy = cpufreq_cpu_get(cpu);
606 if (unlikely(policy)) {
8ff69732 607 cpufreq_cpu_put(policy);
1da177e4
LT
608 cpufreq_debug_enable_ratelimit();
609 return 0;
610 }
611#endif
612
613 if (!try_module_get(cpufreq_driver->owner)) {
614 ret = -EINVAL;
615 goto module_out;
616 }
617
e98df50c 618 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
1da177e4
LT
619 if (!policy) {
620 ret = -ENOMEM;
621 goto nomem_out;
622 }
1da177e4
LT
623
624 policy->cpu = cpu;
625 policy->cpus = cpumask_of_cpu(cpu);
626
83933af4
AV
627 mutex_init(&policy->lock);
628 mutex_lock(&policy->lock);
1da177e4
LT
629 init_completion(&policy->kobj_unregister);
630 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
631
632 /* call driver. From then on the cpufreq must be able
633 * to accept all calls to ->verify and ->setpolicy for this CPU
634 */
635 ret = cpufreq_driver->init(policy);
636 if (ret) {
637 dprintk("initialization failed\n");
f3876c1b 638 mutex_unlock(&policy->lock);
1da177e4
LT
639 goto err_out;
640 }
641
8ff69732
DJ
642#ifdef CONFIG_SMP
643 for_each_cpu_mask(j, policy->cpus) {
644 if (cpu == j)
645 continue;
646
647 /* check for existing affected CPUs. They may not be aware
648 * of it due to CPU Hotplug.
649 */
650 managed_policy = cpufreq_cpu_get(j);
651 if (unlikely(managed_policy)) {
652 spin_lock_irqsave(&cpufreq_driver_lock, flags);
653 managed_policy->cpus = policy->cpus;
654 cpufreq_cpu_data[cpu] = managed_policy;
655 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
656
657 dprintk("CPU already managed, adding link\n");
658 sysfs_create_link(&sys_dev->kobj,
659 &managed_policy->kobj, "cpufreq");
660
661 cpufreq_debug_enable_ratelimit();
662 mutex_unlock(&policy->lock);
663 ret = 0;
664 goto err_out_driver_exit; /* call driver->exit() */
665 }
666 }
667#endif
1da177e4
LT
668 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
669
670 /* prepare interface data */
671 policy->kobj.parent = &sys_dev->kobj;
672 policy->kobj.ktype = &ktype_cpufreq;
673 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
674
675 ret = kobject_register(&policy->kobj);
f3876c1b
AM
676 if (ret) {
677 mutex_unlock(&policy->lock);
8085e1f1 678 goto err_out_driver_exit;
f3876c1b 679 }
1da177e4
LT
680 /* set up files for this cpu device */
681 drv_attr = cpufreq_driver->attr;
682 while ((drv_attr) && (*drv_attr)) {
683 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
684 drv_attr++;
685 }
686 if (cpufreq_driver->get)
687 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
688 if (cpufreq_driver->target)
689 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
690
691 spin_lock_irqsave(&cpufreq_driver_lock, flags);
692 for_each_cpu_mask(j, policy->cpus)
693 cpufreq_cpu_data[j] = policy;
694 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
8ff69732
DJ
695
696 /* symlink affected CPUs */
697 for_each_cpu_mask(j, policy->cpus) {
698 if (j == cpu)
699 continue;
700 if (!cpu_online(j))
701 continue;
702
1f8b2c9d 703 dprintk("CPU %u already managed, adding link\n", j);
8ff69732
DJ
704 cpufreq_cpu_get(cpu);
705 cpu_sys_dev = get_cpu_sysdev(j);
706 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
707 "cpufreq");
708 }
709
1da177e4
LT
710 policy->governor = NULL; /* to assure that the starting sequence is
711 * run in cpufreq_set_policy */
83933af4 712 mutex_unlock(&policy->lock);
87c32271 713
1da177e4 714 /* set default policy */
1da177e4
LT
715 ret = cpufreq_set_policy(&new_policy);
716 if (ret) {
717 dprintk("setting policy failed\n");
718 goto err_out_unregister;
719 }
720
721 module_put(cpufreq_driver->owner);
1da177e4
LT
722 dprintk("initialization complete\n");
723 cpufreq_debug_enable_ratelimit();
87c32271 724
1da177e4
LT
725 return 0;
726
727
728err_out_unregister:
729 spin_lock_irqsave(&cpufreq_driver_lock, flags);
730 for_each_cpu_mask(j, policy->cpus)
731 cpufreq_cpu_data[j] = NULL;
732 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
733
734 kobject_unregister(&policy->kobj);
735 wait_for_completion(&policy->kobj_unregister);
736
8085e1f1
VP
737err_out_driver_exit:
738 if (cpufreq_driver->exit)
739 cpufreq_driver->exit(policy);
740
1da177e4
LT
741err_out:
742 kfree(policy);
743
744nomem_out:
745 module_put(cpufreq_driver->owner);
c32b6b8e 746module_out:
1da177e4
LT
747 cpufreq_debug_enable_ratelimit();
748 return ret;
749}
750
751
752/**
753 * cpufreq_remove_dev - remove a CPU device
754 *
755 * Removes the cpufreq interface for a CPU device.
756 */
757static int cpufreq_remove_dev (struct sys_device * sys_dev)
758{
759 unsigned int cpu = sys_dev->id;
760 unsigned long flags;
761 struct cpufreq_policy *data;
762#ifdef CONFIG_SMP
e738cf6d 763 struct sys_device *cpu_sys_dev;
1da177e4
LT
764 unsigned int j;
765#endif
766
767 cpufreq_debug_disable_ratelimit();
768 dprintk("unregistering CPU %u\n", cpu);
769
770 spin_lock_irqsave(&cpufreq_driver_lock, flags);
771 data = cpufreq_cpu_data[cpu];
772
773 if (!data) {
774 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
775 cpufreq_debug_enable_ratelimit();
776 return -EINVAL;
777 }
778 cpufreq_cpu_data[cpu] = NULL;
779
780
781#ifdef CONFIG_SMP
782 /* if this isn't the CPU which is the parent of the kobj, we
32ee8c3e 783 * only need to unlink, put and exit
1da177e4
LT
784 */
785 if (unlikely(cpu != data->cpu)) {
786 dprintk("removing link\n");
8ff69732 787 cpu_clear(cpu, data->cpus);
1da177e4
LT
788 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
789 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
1da177e4
LT
790 cpufreq_cpu_put(data);
791 cpufreq_debug_enable_ratelimit();
792 return 0;
793 }
794#endif
795
1da177e4
LT
796
797 if (!kobject_get(&data->kobj)) {
798 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
799 cpufreq_debug_enable_ratelimit();
32ee8c3e 800 return -EFAULT;
1da177e4
LT
801 }
802
803#ifdef CONFIG_SMP
804 /* if we have other CPUs still registered, we need to unlink them,
805 * or else wait_for_completion below will lock up. Clean the
806 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
807 * links afterwards.
808 */
809 if (unlikely(cpus_weight(data->cpus) > 1)) {
810 for_each_cpu_mask(j, data->cpus) {
811 if (j == cpu)
812 continue;
813 cpufreq_cpu_data[j] = NULL;
814 }
815 }
816
817 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
818
819 if (unlikely(cpus_weight(data->cpus) > 1)) {
820 for_each_cpu_mask(j, data->cpus) {
821 if (j == cpu)
822 continue;
823 dprintk("removing link for cpu %u\n", j);
d434fca7
AR
824 cpu_sys_dev = get_cpu_sysdev(j);
825 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
1da177e4
LT
826 cpufreq_cpu_put(data);
827 }
828 }
829#else
830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
831#endif
832
83933af4 833 mutex_lock(&data->lock);
1da177e4
LT
834 if (cpufreq_driver->target)
835 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
83933af4 836 mutex_unlock(&data->lock);
1da177e4
LT
837
838 kobject_unregister(&data->kobj);
839
840 kobject_put(&data->kobj);
841
842 /* we need to make sure that the underlying kobj is actually
32ee8c3e 843 * not referenced anymore by anybody before we proceed with
1da177e4
LT
844 * unloading.
845 */
846 dprintk("waiting for dropping of refcount\n");
847 wait_for_completion(&data->kobj_unregister);
848 dprintk("wait complete\n");
849
850 if (cpufreq_driver->exit)
851 cpufreq_driver->exit(data);
852
853 kfree(data);
854
855 cpufreq_debug_enable_ratelimit();
1da177e4
LT
856 return 0;
857}
858
859
860static void handle_update(void *data)
861{
862 unsigned int cpu = (unsigned int)(long)data;
863 dprintk("handle_update for cpu %u called\n", cpu);
864 cpufreq_update_policy(cpu);
865}
866
867/**
868 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
869 * @cpu: cpu number
870 * @old_freq: CPU frequency the kernel thinks the CPU runs at
871 * @new_freq: CPU frequency the CPU actually runs at
872 *
873 * We adjust to current frequency first, and need to clean up later. So either call
874 * to cpufreq_update_policy() or schedule handle_update()).
875 */
876static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
877{
878 struct cpufreq_freqs freqs;
879
b10eec22 880 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
1da177e4
LT
881 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
882
883 freqs.cpu = cpu;
884 freqs.old = old_freq;
885 freqs.new = new_freq;
886 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
887 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
888}
889
890
32ee8c3e 891/**
95235ca2
VP
892 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
893 * @cpu: CPU number
894 *
895 * This is the last known freq, without actually getting it from the driver.
896 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
897 */
898unsigned int cpufreq_quick_get(unsigned int cpu)
899{
900 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
901 unsigned int ret = 0;
902
903 if (policy) {
83933af4 904 mutex_lock(&policy->lock);
95235ca2 905 ret = policy->cur;
83933af4 906 mutex_unlock(&policy->lock);
95235ca2
VP
907 cpufreq_cpu_put(policy);
908 }
909
910 return (ret);
911}
912EXPORT_SYMBOL(cpufreq_quick_get);
913
914
32ee8c3e 915/**
1da177e4
LT
916 * cpufreq_get - get the current CPU frequency (in kHz)
917 * @cpu: CPU number
918 *
919 * Get the CPU current (static) CPU frequency
920 */
921unsigned int cpufreq_get(unsigned int cpu)
922{
923 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
924 unsigned int ret = 0;
925
926 if (!policy)
927 return 0;
928
929 if (!cpufreq_driver->get)
930 goto out;
931
83933af4 932 mutex_lock(&policy->lock);
1da177e4
LT
933
934 ret = cpufreq_driver->get(cpu);
935
7d5e350f 936 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1da177e4
LT
937 /* verify no discrepancy between actual and saved value exists */
938 if (unlikely(ret != policy->cur)) {
939 cpufreq_out_of_sync(cpu, policy->cur, ret);
940 schedule_work(&policy->update);
941 }
942 }
943
83933af4 944 mutex_unlock(&policy->lock);
1da177e4 945
7d5e350f 946out:
1da177e4
LT
947 cpufreq_cpu_put(policy);
948
949 return (ret);
950}
951EXPORT_SYMBOL(cpufreq_get);
952
953
42d4dc3f
BH
954/**
955 * cpufreq_suspend - let the low level driver prepare for suspend
956 */
957
e00d9967 958static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
42d4dc3f
BH
959{
960 int cpu = sysdev->id;
961 unsigned int ret = 0;
962 unsigned int cur_freq = 0;
963 struct cpufreq_policy *cpu_policy;
964
965 dprintk("resuming cpu %u\n", cpu);
966
967 if (!cpu_online(cpu))
968 return 0;
969
970 /* we may be lax here as interrupts are off. Nonetheless
971 * we need to grab the correct cpu policy, as to check
972 * whether we really run on this CPU.
973 */
974
975 cpu_policy = cpufreq_cpu_get(cpu);
976 if (!cpu_policy)
977 return -EINVAL;
978
979 /* only handle each CPU group once */
980 if (unlikely(cpu_policy->cpu != cpu)) {
981 cpufreq_cpu_put(cpu_policy);
982 return 0;
983 }
984
985 if (cpufreq_driver->suspend) {
e00d9967 986 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
42d4dc3f
BH
987 if (ret) {
988 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
989 "step on CPU %u\n", cpu_policy->cpu);
990 cpufreq_cpu_put(cpu_policy);
991 return ret;
992 }
993 }
994
995
996 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
997 goto out;
998
999 if (cpufreq_driver->get)
1000 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1001
1002 if (!cur_freq || !cpu_policy->cur) {
1003 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1004 "frequency is what timing core thinks it is.\n");
1005 goto out;
1006 }
1007
1008 if (unlikely(cur_freq != cpu_policy->cur)) {
1009 struct cpufreq_freqs freqs;
1010
1011 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
b10eec22 1012 dprintk("Warning: CPU frequency is %u, "
42d4dc3f
BH
1013 "cpufreq assumed %u kHz.\n",
1014 cur_freq, cpu_policy->cur);
1015
1016 freqs.cpu = cpu;
1017 freqs.old = cpu_policy->cur;
1018 freqs.new = cur_freq;
1019
e041c683 1020 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
42d4dc3f
BH
1021 CPUFREQ_SUSPENDCHANGE, &freqs);
1022 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1023
1024 cpu_policy->cur = cur_freq;
1025 }
1026
7d5e350f 1027out:
42d4dc3f
BH
1028 cpufreq_cpu_put(cpu_policy);
1029 return 0;
1030}
1031
1da177e4
LT
1032/**
1033 * cpufreq_resume - restore proper CPU frequency handling after resume
1034 *
1035 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1036 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
42d4dc3f
BH
1037 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1038 * restored.
1da177e4
LT
1039 */
1040static int cpufreq_resume(struct sys_device * sysdev)
1041{
1042 int cpu = sysdev->id;
1043 unsigned int ret = 0;
1044 struct cpufreq_policy *cpu_policy;
1045
1046 dprintk("resuming cpu %u\n", cpu);
1047
1048 if (!cpu_online(cpu))
1049 return 0;
1050
1051 /* we may be lax here as interrupts are off. Nonetheless
1052 * we need to grab the correct cpu policy, as to check
1053 * whether we really run on this CPU.
1054 */
1055
1056 cpu_policy = cpufreq_cpu_get(cpu);
1057 if (!cpu_policy)
1058 return -EINVAL;
1059
1060 /* only handle each CPU group once */
1061 if (unlikely(cpu_policy->cpu != cpu)) {
1062 cpufreq_cpu_put(cpu_policy);
1063 return 0;
1064 }
1065
1066 if (cpufreq_driver->resume) {
1067 ret = cpufreq_driver->resume(cpu_policy);
1068 if (ret) {
1069 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1070 "step on CPU %u\n", cpu_policy->cpu);
1071 cpufreq_cpu_put(cpu_policy);
1072 return ret;
1073 }
1074 }
1075
1076 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1077 unsigned int cur_freq = 0;
1078
1079 if (cpufreq_driver->get)
1080 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1081
1082 if (!cur_freq || !cpu_policy->cur) {
42d4dc3f
BH
1083 printk(KERN_ERR "cpufreq: resume failed to assert "
1084 "current frequency is what timing core "
1085 "thinks it is.\n");
1da177e4
LT
1086 goto out;
1087 }
1088
1089 if (unlikely(cur_freq != cpu_policy->cur)) {
1090 struct cpufreq_freqs freqs;
1091
ac09f698 1092 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
b10eec22 1093 dprintk("Warning: CPU frequency"
ac09f698
BH
1094 "is %u, cpufreq assumed %u kHz.\n",
1095 cur_freq, cpu_policy->cur);
1da177e4
LT
1096
1097 freqs.cpu = cpu;
1098 freqs.old = cpu_policy->cur;
1099 freqs.new = cur_freq;
1100
e041c683
AS
1101 blocking_notifier_call_chain(
1102 &cpufreq_transition_notifier_list,
42d4dc3f 1103 CPUFREQ_RESUMECHANGE, &freqs);
1da177e4
LT
1104 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1105
1106 cpu_policy->cur = cur_freq;
1107 }
1108 }
1109
1110out:
1111 schedule_work(&cpu_policy->update);
1112 cpufreq_cpu_put(cpu_policy);
1113 return ret;
1114}
1115
1116static struct sysdev_driver cpufreq_sysdev_driver = {
1117 .add = cpufreq_add_dev,
1118 .remove = cpufreq_remove_dev,
42d4dc3f 1119 .suspend = cpufreq_suspend,
1da177e4
LT
1120 .resume = cpufreq_resume,
1121};
1122
1123
1124/*********************************************************************
1125 * NOTIFIER LISTS INTERFACE *
1126 *********************************************************************/
1127
1128/**
1129 * cpufreq_register_notifier - register a driver with cpufreq
1130 * @nb: notifier function to register
1131 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1132 *
32ee8c3e 1133 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1134 * are notified about clock rate changes (once before and once after
1135 * the transition), or a list of drivers that are notified about
1136 * changes in cpufreq policy.
1137 *
1138 * This function may sleep, and has the same return conditions as
e041c683 1139 * blocking_notifier_chain_register.
1da177e4
LT
1140 */
1141int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1142{
1143 int ret;
1144
1da177e4
LT
1145 switch (list) {
1146 case CPUFREQ_TRANSITION_NOTIFIER:
e041c683
AS
1147 ret = blocking_notifier_chain_register(
1148 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1149 break;
1150 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1151 ret = blocking_notifier_chain_register(
1152 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1153 break;
1154 default:
1155 ret = -EINVAL;
1156 }
1da177e4
LT
1157
1158 return ret;
1159}
1160EXPORT_SYMBOL(cpufreq_register_notifier);
1161
1162
1163/**
1164 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1165 * @nb: notifier block to be unregistered
1166 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1167 *
1168 * Remove a driver from the CPU frequency notifier list.
1169 *
1170 * This function may sleep, and has the same return conditions as
e041c683 1171 * blocking_notifier_chain_unregister.
1da177e4
LT
1172 */
1173int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1174{
1175 int ret;
1176
1da177e4
LT
1177 switch (list) {
1178 case CPUFREQ_TRANSITION_NOTIFIER:
e041c683
AS
1179 ret = blocking_notifier_chain_unregister(
1180 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1181 break;
1182 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1183 ret = blocking_notifier_chain_unregister(
1184 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1185 break;
1186 default:
1187 ret = -EINVAL;
1188 }
1da177e4
LT
1189
1190 return ret;
1191}
1192EXPORT_SYMBOL(cpufreq_unregister_notifier);
1193
1194
1195/*********************************************************************
1196 * GOVERNORS *
1197 *********************************************************************/
1198
1199
1200int __cpufreq_driver_target(struct cpufreq_policy *policy,
1201 unsigned int target_freq,
1202 unsigned int relation)
1203{
1204 int retval = -EINVAL;
c32b6b8e 1205
a9d9baa1 1206 lock_cpu_hotplug();
1da177e4
LT
1207 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1208 target_freq, relation);
1209 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1210 retval = cpufreq_driver->target(policy, target_freq, relation);
90d45d17 1211
a9d9baa1 1212 unlock_cpu_hotplug();
90d45d17 1213
1da177e4
LT
1214 return retval;
1215}
1216EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1217
1da177e4
LT
1218int cpufreq_driver_target(struct cpufreq_policy *policy,
1219 unsigned int target_freq,
1220 unsigned int relation)
1221{
cc993cab 1222 int ret;
1da177e4
LT
1223
1224 policy = cpufreq_cpu_get(policy->cpu);
1225 if (!policy)
1226 return -EINVAL;
1227
83933af4 1228 mutex_lock(&policy->lock);
1da177e4
LT
1229
1230 ret = __cpufreq_driver_target(policy, target_freq, relation);
1231
83933af4 1232 mutex_unlock(&policy->lock);
1da177e4
LT
1233
1234 cpufreq_cpu_put(policy);
1da177e4
LT
1235 return ret;
1236}
1237EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1238
1239
1240static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1241{
cc993cab 1242 int ret;
1da177e4
LT
1243
1244 if (!try_module_get(policy->governor->owner))
1245 return -EINVAL;
1246
1247 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1248 ret = policy->governor->governor(policy, event);
1249
1250 /* we keep one module reference alive for each CPU governed by this CPU */
1251 if ((event != CPUFREQ_GOV_START) || ret)
1252 module_put(policy->governor->owner);
1253 if ((event == CPUFREQ_GOV_STOP) && !ret)
1254 module_put(policy->governor->owner);
1255
1256 return ret;
1257}
1258
1259
1260int cpufreq_governor(unsigned int cpu, unsigned int event)
1261{
1262 int ret = 0;
1263 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1264
1265 if (!policy)
1266 return -EINVAL;
1267
83933af4 1268 mutex_lock(&policy->lock);
1da177e4 1269 ret = __cpufreq_governor(policy, event);
83933af4 1270 mutex_unlock(&policy->lock);
1da177e4
LT
1271
1272 cpufreq_cpu_put(policy);
1da177e4
LT
1273 return ret;
1274}
1275EXPORT_SYMBOL_GPL(cpufreq_governor);
1276
1277
1278int cpufreq_register_governor(struct cpufreq_governor *governor)
1279{
1280 struct cpufreq_governor *t;
1281
1282 if (!governor)
1283 return -EINVAL;
1284
3fc54d37 1285 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 1286
1da177e4
LT
1287 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1288 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
3fc54d37 1289 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1290 return -EBUSY;
1291 }
1292 }
1293 list_add(&governor->governor_list, &cpufreq_governor_list);
1294
32ee8c3e 1295 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1296 return 0;
1297}
1298EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1299
1300
1301void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1302{
1303 if (!governor)
1304 return;
1305
3fc54d37 1306 mutex_lock(&cpufreq_governor_mutex);
1da177e4 1307 list_del(&governor->governor_list);
3fc54d37 1308 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1309 return;
1310}
1311EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1312
1313
1314
1315/*********************************************************************
1316 * POLICY INTERFACE *
1317 *********************************************************************/
1318
1319/**
1320 * cpufreq_get_policy - get the current cpufreq_policy
1321 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1322 *
1323 * Reads the current cpufreq policy.
1324 */
1325int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1326{
1327 struct cpufreq_policy *cpu_policy;
1328 if (!policy)
1329 return -EINVAL;
1330
1331 cpu_policy = cpufreq_cpu_get(cpu);
1332 if (!cpu_policy)
1333 return -EINVAL;
1334
83933af4 1335 mutex_lock(&cpu_policy->lock);
1da177e4 1336 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
83933af4 1337 mutex_unlock(&cpu_policy->lock);
1da177e4
LT
1338
1339 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
1340 return 0;
1341}
1342EXPORT_SYMBOL(cpufreq_get_policy);
1343
1344
1345static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1346{
1347 int ret = 0;
1348
1349 cpufreq_debug_disable_ratelimit();
1350 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1351 policy->min, policy->max);
1352
7d5e350f 1353 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1da177e4
LT
1354
1355 /* verify the cpu speed can be set within this limit */
1356 ret = cpufreq_driver->verify(policy);
1357 if (ret)
1358 goto error_out;
1359
1da177e4 1360 /* adjust if necessary - all reasons */
e041c683
AS
1361 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362 CPUFREQ_ADJUST, policy);
1da177e4
LT
1363
1364 /* adjust if necessary - hardware incompatibility*/
e041c683
AS
1365 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1366 CPUFREQ_INCOMPATIBLE, policy);
1da177e4
LT
1367
1368 /* verify the cpu speed can be set within this limit,
1369 which might be different to the first one */
1370 ret = cpufreq_driver->verify(policy);
e041c683 1371 if (ret)
1da177e4 1372 goto error_out;
1da177e4
LT
1373
1374 /* notification of the new policy */
e041c683
AS
1375 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1376 CPUFREQ_NOTIFY, policy);
1da177e4 1377
7d5e350f
DJ
1378 data->min = policy->min;
1379 data->max = policy->max;
1da177e4
LT
1380
1381 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1382
1383 if (cpufreq_driver->setpolicy) {
1384 data->policy = policy->policy;
1385 dprintk("setting range\n");
1386 ret = cpufreq_driver->setpolicy(policy);
1387 } else {
1388 if (policy->governor != data->governor) {
1389 /* save old, working values */
1390 struct cpufreq_governor *old_gov = data->governor;
1391
1392 dprintk("governor switch\n");
1393
1394 /* end old governor */
1395 if (data->governor)
1396 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1397
1398 /* start new governor */
1399 data->governor = policy->governor;
1400 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1401 /* new governor failed, so re-start old one */
1402 dprintk("starting governor %s failed\n", data->governor->name);
1403 if (old_gov) {
1404 data->governor = old_gov;
1405 __cpufreq_governor(data, CPUFREQ_GOV_START);
1406 }
1407 ret = -EINVAL;
1408 goto error_out;
1409 }
1410 /* might be a policy change, too, so fall through */
1411 }
1412 dprintk("governor: change or update limits\n");
1413 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1414 }
1415
7d5e350f 1416error_out:
1da177e4
LT
1417 cpufreq_debug_enable_ratelimit();
1418 return ret;
1419}
1420
1421/**
1422 * cpufreq_set_policy - set a new CPUFreq policy
1423 * @policy: policy to be set.
1424 *
1425 * Sets a new CPU frequency and voltage scaling policy.
1426 */
1427int cpufreq_set_policy(struct cpufreq_policy *policy)
1428{
1429 int ret = 0;
1430 struct cpufreq_policy *data;
1431
1432 if (!policy)
1433 return -EINVAL;
1434
1435 data = cpufreq_cpu_get(policy->cpu);
1436 if (!data)
1437 return -EINVAL;
1438
1439 /* lock this CPU */
83933af4 1440 mutex_lock(&data->lock);
1da177e4
LT
1441
1442 ret = __cpufreq_set_policy(data, policy);
1443 data->user_policy.min = data->min;
1444 data->user_policy.max = data->max;
1445 data->user_policy.policy = data->policy;
1446 data->user_policy.governor = data->governor;
1447
83933af4 1448 mutex_unlock(&data->lock);
1da177e4
LT
1449 cpufreq_cpu_put(data);
1450
1451 return ret;
1452}
1453EXPORT_SYMBOL(cpufreq_set_policy);
1454
1455
1456/**
1457 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1458 * @cpu: CPU which shall be re-evaluated
1459 *
1460 * Usefull for policy notifiers which have different necessities
1461 * at different times.
1462 */
1463int cpufreq_update_policy(unsigned int cpu)
1464{
1465 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1466 struct cpufreq_policy policy;
1467 int ret = 0;
1468
1469 if (!data)
1470 return -ENODEV;
1471
83933af4 1472 mutex_lock(&data->lock);
1da177e4
LT
1473
1474 dprintk("updating policy for CPU %u\n", cpu);
7d5e350f 1475 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1da177e4
LT
1476 policy.min = data->user_policy.min;
1477 policy.max = data->user_policy.max;
1478 policy.policy = data->user_policy.policy;
1479 policy.governor = data->user_policy.governor;
1480
0961dd0d
TR
1481 /* BIOS might change freq behind our back
1482 -> ask driver for current freq and notify governors about a change */
1483 if (cpufreq_driver->get) {
1484 policy.cur = cpufreq_driver->get(cpu);
a85f7bd3
TR
1485 if (!data->cur) {
1486 dprintk("Driver did not initialize current freq");
1487 data->cur = policy.cur;
1488 } else {
1489 if (data->cur != policy.cur)
1490 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1491 }
0961dd0d
TR
1492 }
1493
1da177e4
LT
1494 ret = __cpufreq_set_policy(data, &policy);
1495
83933af4 1496 mutex_unlock(&data->lock);
1da177e4
LT
1497
1498 cpufreq_cpu_put(data);
1499 return ret;
1500}
1501EXPORT_SYMBOL(cpufreq_update_policy);
1502
65edc68c
CS
1503#ifdef CONFIG_HOTPLUG_CPU
1504static int cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
1505 unsigned long action, void *hcpu)
1506{
1507 unsigned int cpu = (unsigned long)hcpu;
1508 struct cpufreq_policy *policy;
1509 struct sys_device *sys_dev;
1510
1511 sys_dev = get_cpu_sysdev(cpu);
1512
1513 if (sys_dev) {
1514 switch (action) {
1515 case CPU_ONLINE:
1516 cpufreq_add_dev(sys_dev);
1517 break;
1518 case CPU_DOWN_PREPARE:
1519 /*
1520 * We attempt to put this cpu in lowest frequency
1521 * possible before going down. This will permit
1522 * hardware-managed P-State to switch other related
1523 * threads to min or higher speeds if possible.
1524 */
1525 policy = cpufreq_cpu_data[cpu];
1526 if (policy) {
1527 cpufreq_driver_target(policy, policy->min,
1528 CPUFREQ_RELATION_H);
1529 }
1530 break;
1531 case CPU_DEAD:
1532 cpufreq_remove_dev(sys_dev);
1533 break;
1534 }
1535 }
1536 return NOTIFY_OK;
1537}
1538
74b85f37 1539static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
c32b6b8e
AR
1540{
1541 .notifier_call = cpufreq_cpu_callback,
1542};
65edc68c 1543#endif /* CONFIG_HOTPLUG_CPU */
1da177e4
LT
1544
1545/*********************************************************************
1546 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1547 *********************************************************************/
1548
1549/**
1550 * cpufreq_register_driver - register a CPU Frequency driver
1551 * @driver_data: A struct cpufreq_driver containing the values#
1552 * submitted by the CPU Frequency driver.
1553 *
32ee8c3e 1554 * Registers a CPU Frequency driver to this core code. This code
1da177e4 1555 * returns zero on success, -EBUSY when another driver got here first
32ee8c3e 1556 * (and isn't unregistered in the meantime).
1da177e4
LT
1557 *
1558 */
1559int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1560{
1561 unsigned long flags;
1562 int ret;
1563
1564 if (!driver_data || !driver_data->verify || !driver_data->init ||
1565 ((!driver_data->setpolicy) && (!driver_data->target)))
1566 return -EINVAL;
1567
1568 dprintk("trying to register driver %s\n", driver_data->name);
1569
1570 if (driver_data->setpolicy)
1571 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1572
1573 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1574 if (cpufreq_driver) {
1575 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576 return -EBUSY;
1577 }
1578 cpufreq_driver = driver_data;
1579 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1580
1581 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1582
1583 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1584 int i;
1585 ret = -ENODEV;
1586
1587 /* check for at least one working CPU */
1588 for (i=0; i<NR_CPUS; i++)
1589 if (cpufreq_cpu_data[i])
1590 ret = 0;
1591
1592 /* if all ->init() calls failed, unregister */
1593 if (ret) {
1594 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1595 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1596
1597 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1598 cpufreq_driver = NULL;
1599 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1600 }
1601 }
1602
1603 if (!ret) {
65edc68c 1604 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4
LT
1605 dprintk("driver %s up and running\n", driver_data->name);
1606 cpufreq_debug_enable_ratelimit();
1607 }
1608
1609 return (ret);
1610}
1611EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1612
1613
1614/**
1615 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1616 *
32ee8c3e 1617 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
1618 * the right to do so, i.e. if you have succeeded in initialising before!
1619 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1620 * currently not initialised.
1621 */
1622int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1623{
1624 unsigned long flags;
1625
1626 cpufreq_debug_disable_ratelimit();
1627
1628 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1629 cpufreq_debug_enable_ratelimit();
1630 return -EINVAL;
1631 }
1632
1633 dprintk("unregistering driver %s\n", driver->name);
1634
1635 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
65edc68c 1636 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4
LT
1637
1638 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1639 cpufreq_driver = NULL;
1640 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1641
1642 return 0;
1643}
1644EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);