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