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