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