]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/cpufreq/cpufreq.c
ipv6: AF_INET6 link address family
[net-next-2.6.git] / drivers / cpufreq / cpufreq.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
c32b6b8e 7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
32ee8c3e 8 * Added handling for CPU hotplug
8ff69732
DJ
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
c32b6b8e 11 *
1da177e4
LT
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
1da177e4
LT
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/notifier.h>
22#include <linux/cpufreq.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#include <linux/device.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/completion.h>
3fc54d37 30#include <linux/mutex.h>
1da177e4 31
6f4f2723
TR
32#include <trace/events/power.h>
33
e08f5f5b
GS
34#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
35 "cpufreq-core", msg)
1da177e4
LT
36
37/**
cd878479 38 * The "cpufreq driver" - the arch- or hardware-dependent low
1da177e4
LT
39 * level driver of CPUFreq support, and its spinlock. This lock
40 * also protects the cpufreq_cpu_data array.
41 */
7d5e350f 42static struct cpufreq_driver *cpufreq_driver;
7a6aedfa 43static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
084f3493
TR
44#ifdef CONFIG_HOTPLUG_CPU
45/* This one keeps track of the previously set governor of a removed CPU */
e77b89f1 46static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
084f3493 47#endif
1da177e4
LT
48static DEFINE_SPINLOCK(cpufreq_driver_lock);
49
5a01f2e8
VP
50/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
62 * - All holders of the lock should check to make sure that the CPU they
63 * are concerned with are online after they get the lock.
64 * - Governor routines that can be called in cpufreq hotplug path should not
65 * take this sem as top level hotplug notifier handler takes this.
395913d0
MD
66 * - Lock should not be held across
67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
5a01f2e8 68 */
f1625066 69static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
5a01f2e8
VP
70static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
71
72#define lock_policy_rwsem(mode, cpu) \
226528c6 73static int lock_policy_rwsem_##mode \
5a01f2e8
VP
74(int cpu) \
75{ \
f1625066 76 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
5a01f2e8
VP
77 BUG_ON(policy_cpu == -1); \
78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
79 if (unlikely(!cpu_online(cpu))) { \
80 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
81 return -1; \
82 } \
83 \
84 return 0; \
85}
86
87lock_policy_rwsem(read, cpu);
5a01f2e8
VP
88
89lock_policy_rwsem(write, cpu);
5a01f2e8 90
226528c6 91static void unlock_policy_rwsem_read(int cpu)
5a01f2e8 92{
f1625066 93 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
5a01f2e8
VP
94 BUG_ON(policy_cpu == -1);
95 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
96}
5a01f2e8 97
226528c6 98static void unlock_policy_rwsem_write(int cpu)
5a01f2e8 99{
f1625066 100 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
5a01f2e8
VP
101 BUG_ON(policy_cpu == -1);
102 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
103}
5a01f2e8
VP
104
105
1da177e4 106/* internal prototypes */
29464f28
DJ
107static int __cpufreq_governor(struct cpufreq_policy *policy,
108 unsigned int event);
5a01f2e8 109static unsigned int __cpufreq_get(unsigned int cpu);
65f27f38 110static void handle_update(struct work_struct *work);
1da177e4
LT
111
112/**
32ee8c3e
DJ
113 * Two notifier lists: the "policy" list is involved in the
114 * validation process for a new CPU frequency policy; the
1da177e4
LT
115 * "transition" list for kernel code that needs to handle
116 * changes to devices when the CPU clock speed changes.
117 * The mutex locks both lists.
118 */
e041c683 119static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
b4dfdbb3 120static struct srcu_notifier_head cpufreq_transition_notifier_list;
1da177e4 121
74212ca4 122static bool init_cpufreq_transition_notifier_list_called;
b4dfdbb3
AS
123static int __init init_cpufreq_transition_notifier_list(void)
124{
125 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
74212ca4 126 init_cpufreq_transition_notifier_list_called = true;
b4dfdbb3
AS
127 return 0;
128}
b3438f82 129pure_initcall(init_cpufreq_transition_notifier_list);
1da177e4
LT
130
131static LIST_HEAD(cpufreq_governor_list);
29464f28 132static DEFINE_MUTEX(cpufreq_governor_mutex);
1da177e4 133
7d5e350f 134struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1da177e4
LT
135{
136 struct cpufreq_policy *data;
137 unsigned long flags;
138
7a6aedfa 139 if (cpu >= nr_cpu_ids)
1da177e4
LT
140 goto err_out;
141
142 /* get the cpufreq driver */
143 spin_lock_irqsave(&cpufreq_driver_lock, flags);
144
145 if (!cpufreq_driver)
146 goto err_out_unlock;
147
148 if (!try_module_get(cpufreq_driver->owner))
149 goto err_out_unlock;
150
151
152 /* get the CPU */
7a6aedfa 153 data = per_cpu(cpufreq_cpu_data, cpu);
1da177e4
LT
154
155 if (!data)
156 goto err_out_put_module;
157
158 if (!kobject_get(&data->kobj))
159 goto err_out_put_module;
160
1da177e4 161 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
162 return data;
163
7d5e350f 164err_out_put_module:
1da177e4 165 module_put(cpufreq_driver->owner);
7d5e350f 166err_out_unlock:
1da177e4 167 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
7d5e350f 168err_out:
1da177e4
LT
169 return NULL;
170}
171EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
172
7d5e350f 173
1da177e4
LT
174void cpufreq_cpu_put(struct cpufreq_policy *data)
175{
176 kobject_put(&data->kobj);
177 module_put(cpufreq_driver->owner);
178}
179EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
180
181
182/*********************************************************************
183 * UNIFIED DEBUG HELPERS *
184 *********************************************************************/
185#ifdef CONFIG_CPU_FREQ_DEBUG
186
187/* what part(s) of the CPUfreq subsystem are debugged? */
188static unsigned int debug;
189
190/* is the debug output ratelimit'ed using printk_ratelimit? User can
191 * set or modify this value.
192 */
193static unsigned int debug_ratelimit = 1;
194
195/* is the printk_ratelimit'ing enabled? It's enabled after a successful
196 * loading of a cpufreq driver, temporarily disabled when a new policy
197 * is set, and disabled upon cpufreq driver removal
198 */
199static unsigned int disable_ratelimit = 1;
200static DEFINE_SPINLOCK(disable_ratelimit_lock);
201
858119e1 202static void cpufreq_debug_enable_ratelimit(void)
1da177e4
LT
203{
204 unsigned long flags;
205
206 spin_lock_irqsave(&disable_ratelimit_lock, flags);
207 if (disable_ratelimit)
208 disable_ratelimit--;
209 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
210}
211
858119e1 212static void cpufreq_debug_disable_ratelimit(void)
1da177e4
LT
213{
214 unsigned long flags;
215
216 spin_lock_irqsave(&disable_ratelimit_lock, flags);
217 disable_ratelimit++;
218 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
219}
220
e08f5f5b 221void cpufreq_debug_printk(unsigned int type, const char *prefix,
905d77cd 222 const char *fmt, ...)
1da177e4
LT
223{
224 char s[256];
225 va_list args;
226 unsigned int len;
227 unsigned long flags;
32ee8c3e 228
1da177e4
LT
229 WARN_ON(!prefix);
230 if (type & debug) {
231 spin_lock_irqsave(&disable_ratelimit_lock, flags);
e08f5f5b
GS
232 if (!disable_ratelimit && debug_ratelimit
233 && !printk_ratelimit()) {
1da177e4
LT
234 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
235 return;
236 }
237 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
238
239 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
240
241 va_start(args, fmt);
242 len += vsnprintf(&s[len], (256 - len), fmt, args);
243 va_end(args);
244
245 printk(s);
246
247 WARN_ON(len < 5);
248 }
249}
250EXPORT_SYMBOL(cpufreq_debug_printk);
251
252
253module_param(debug, uint, 0644);
e08f5f5b
GS
254MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
255 " 2 to debug drivers, and 4 to debug governors.");
1da177e4
LT
256
257module_param(debug_ratelimit, uint, 0644);
e08f5f5b
GS
258MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
259 " set to 0 to disable ratelimiting.");
1da177e4
LT
260
261#else /* !CONFIG_CPU_FREQ_DEBUG */
262
263static inline void cpufreq_debug_enable_ratelimit(void) { return; }
264static inline void cpufreq_debug_disable_ratelimit(void) { return; }
265
266#endif /* CONFIG_CPU_FREQ_DEBUG */
267
268
269/*********************************************************************
270 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
271 *********************************************************************/
272
273/**
274 * adjust_jiffies - adjust the system "loops_per_jiffy"
275 *
276 * This function alters the system "loops_per_jiffy" for the clock
277 * speed change. Note that loops_per_jiffy cannot be updated on SMP
32ee8c3e 278 * systems as each CPU might be scaled differently. So, use the arch
1da177e4
LT
279 * per-CPU loops_per_jiffy value wherever possible.
280 */
281#ifndef CONFIG_SMP
282static unsigned long l_p_j_ref;
283static unsigned int l_p_j_ref_freq;
284
858119e1 285static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1da177e4
LT
286{
287 if (ci->flags & CPUFREQ_CONST_LOOPS)
288 return;
289
290 if (!l_p_j_ref_freq) {
291 l_p_j_ref = loops_per_jiffy;
292 l_p_j_ref_freq = ci->old;
a4a9df58 293 dprintk("saving %lu as reference value for loops_per_jiffy; "
e08f5f5b 294 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
1da177e4
LT
295 }
296 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
297 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
42d4dc3f 298 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
e08f5f5b
GS
299 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
300 ci->new);
a4a9df58 301 dprintk("scaling loops_per_jiffy to %lu "
e08f5f5b 302 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
1da177e4
LT
303 }
304}
305#else
e08f5f5b
GS
306static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
307{
308 return;
309}
1da177e4
LT
310#endif
311
312
313/**
e4472cb3
DJ
314 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
315 * on frequency transition.
1da177e4 316 *
e4472cb3
DJ
317 * This function calls the transition notifiers and the "adjust_jiffies"
318 * function. It is called twice on all CPU frequency changes that have
32ee8c3e 319 * external effects.
1da177e4
LT
320 */
321void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
322{
e4472cb3
DJ
323 struct cpufreq_policy *policy;
324
1da177e4
LT
325 BUG_ON(irqs_disabled());
326
327 freqs->flags = cpufreq_driver->flags;
e4472cb3
DJ
328 dprintk("notification %u of frequency transition to %u kHz\n",
329 state, freqs->new);
1da177e4 330
7a6aedfa 331 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
1da177e4 332 switch (state) {
e4472cb3 333
1da177e4 334 case CPUFREQ_PRECHANGE:
32ee8c3e 335 /* detect if the driver reported a value as "old frequency"
e4472cb3
DJ
336 * which is not equal to what the cpufreq core thinks is
337 * "old frequency".
1da177e4
LT
338 */
339 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e4472cb3
DJ
340 if ((policy) && (policy->cpu == freqs->cpu) &&
341 (policy->cur) && (policy->cur != freqs->old)) {
b10eec22 342 dprintk("Warning: CPU frequency is"
e4472cb3
DJ
343 " %u, cpufreq assumed %u kHz.\n",
344 freqs->old, policy->cur);
345 freqs->old = policy->cur;
1da177e4
LT
346 }
347 }
b4dfdbb3 348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 349 CPUFREQ_PRECHANGE, freqs);
1da177e4
LT
350 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
351 break;
e4472cb3 352
1da177e4
LT
353 case CPUFREQ_POSTCHANGE:
354 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
6f4f2723
TR
355 dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
356 (unsigned long)freqs->cpu);
357 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
b4dfdbb3 358 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 359 CPUFREQ_POSTCHANGE, freqs);
e4472cb3
DJ
360 if (likely(policy) && likely(policy->cpu == freqs->cpu))
361 policy->cur = freqs->new;
1da177e4
LT
362 break;
363 }
1da177e4
LT
364}
365EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
366
367
368
369/*********************************************************************
370 * SYSFS INTERFACE *
371 *********************************************************************/
372
3bcb09a3
JF
373static struct cpufreq_governor *__find_governor(const char *str_governor)
374{
375 struct cpufreq_governor *t;
376
377 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
29464f28 378 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3bcb09a3
JF
379 return t;
380
381 return NULL;
382}
383
1da177e4
LT
384/**
385 * cpufreq_parse_governor - parse a governor string
386 */
905d77cd 387static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
1da177e4
LT
388 struct cpufreq_governor **governor)
389{
3bcb09a3
JF
390 int err = -EINVAL;
391
1da177e4 392 if (!cpufreq_driver)
3bcb09a3
JF
393 goto out;
394
1da177e4
LT
395 if (cpufreq_driver->setpolicy) {
396 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
397 *policy = CPUFREQ_POLICY_PERFORMANCE;
3bcb09a3 398 err = 0;
e08f5f5b
GS
399 } else if (!strnicmp(str_governor, "powersave",
400 CPUFREQ_NAME_LEN)) {
1da177e4 401 *policy = CPUFREQ_POLICY_POWERSAVE;
3bcb09a3 402 err = 0;
1da177e4 403 }
3bcb09a3 404 } else if (cpufreq_driver->target) {
1da177e4 405 struct cpufreq_governor *t;
3bcb09a3 406
3fc54d37 407 mutex_lock(&cpufreq_governor_mutex);
3bcb09a3
JF
408
409 t = __find_governor(str_governor);
410
ea714970 411 if (t == NULL) {
e08f5f5b
GS
412 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
413 str_governor);
ea714970
JF
414
415 if (name) {
416 int ret;
417
418 mutex_unlock(&cpufreq_governor_mutex);
326f6a5c 419 ret = request_module("%s", name);
ea714970
JF
420 mutex_lock(&cpufreq_governor_mutex);
421
422 if (ret == 0)
423 t = __find_governor(str_governor);
424 }
425
426 kfree(name);
427 }
428
3bcb09a3
JF
429 if (t != NULL) {
430 *governor = t;
431 err = 0;
1da177e4 432 }
3bcb09a3 433
3fc54d37 434 mutex_unlock(&cpufreq_governor_mutex);
1da177e4 435 }
29464f28 436out:
3bcb09a3 437 return err;
1da177e4 438}
1da177e4
LT
439
440
1da177e4 441/**
e08f5f5b
GS
442 * cpufreq_per_cpu_attr_read() / show_##file_name() -
443 * print out cpufreq information
1da177e4
LT
444 *
445 * Write out information from cpufreq_driver->policy[cpu]; object must be
446 * "unsigned int".
447 */
448
32ee8c3e
DJ
449#define show_one(file_name, object) \
450static ssize_t show_##file_name \
905d77cd 451(struct cpufreq_policy *policy, char *buf) \
32ee8c3e 452{ \
29464f28 453 return sprintf(buf, "%u\n", policy->object); \
1da177e4
LT
454}
455
456show_one(cpuinfo_min_freq, cpuinfo.min_freq);
457show_one(cpuinfo_max_freq, cpuinfo.max_freq);
ed129784 458show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
1da177e4
LT
459show_one(scaling_min_freq, min);
460show_one(scaling_max_freq, max);
461show_one(scaling_cur_freq, cur);
462
e08f5f5b
GS
463static int __cpufreq_set_policy(struct cpufreq_policy *data,
464 struct cpufreq_policy *policy);
7970e08b 465
1da177e4
LT
466/**
467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
468 */
469#define store_one(file_name, object) \
470static ssize_t store_##file_name \
905d77cd 471(struct cpufreq_policy *policy, const char *buf, size_t count) \
1da177e4
LT
472{ \
473 unsigned int ret = -EINVAL; \
474 struct cpufreq_policy new_policy; \
475 \
476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
477 if (ret) \
478 return -EINVAL; \
479 \
29464f28 480 ret = sscanf(buf, "%u", &new_policy.object); \
1da177e4
LT
481 if (ret != 1) \
482 return -EINVAL; \
483 \
7970e08b
TR
484 ret = __cpufreq_set_policy(policy, &new_policy); \
485 policy->user_policy.object = policy->object; \
1da177e4
LT
486 \
487 return ret ? ret : count; \
488}
489
29464f28
DJ
490store_one(scaling_min_freq, min);
491store_one(scaling_max_freq, max);
1da177e4
LT
492
493/**
494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
495 */
905d77cd
DJ
496static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
497 char *buf)
1da177e4 498{
5a01f2e8 499 unsigned int cur_freq = __cpufreq_get(policy->cpu);
1da177e4
LT
500 if (!cur_freq)
501 return sprintf(buf, "<unknown>");
502 return sprintf(buf, "%u\n", cur_freq);
503}
504
505
506/**
507 * show_scaling_governor - show the current policy for the specified CPU
508 */
905d77cd 509static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
1da177e4 510{
29464f28 511 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
1da177e4
LT
512 return sprintf(buf, "powersave\n");
513 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
514 return sprintf(buf, "performance\n");
515 else if (policy->governor)
29464f28
DJ
516 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
517 policy->governor->name);
1da177e4
LT
518 return -EINVAL;
519}
520
521
522/**
523 * store_scaling_governor - store policy for the specified CPU
524 */
905d77cd
DJ
525static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
526 const char *buf, size_t count)
1da177e4
LT
527{
528 unsigned int ret = -EINVAL;
529 char str_governor[16];
530 struct cpufreq_policy new_policy;
531
532 ret = cpufreq_get_policy(&new_policy, policy->cpu);
533 if (ret)
534 return ret;
535
29464f28 536 ret = sscanf(buf, "%15s", str_governor);
1da177e4
LT
537 if (ret != 1)
538 return -EINVAL;
539
e08f5f5b
GS
540 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
541 &new_policy.governor))
1da177e4
LT
542 return -EINVAL;
543
7970e08b
TR
544 /* Do not use cpufreq_set_policy here or the user_policy.max
545 will be wrongly overridden */
7970e08b
TR
546 ret = __cpufreq_set_policy(policy, &new_policy);
547
548 policy->user_policy.policy = policy->policy;
549 policy->user_policy.governor = policy->governor;
7970e08b 550
e08f5f5b
GS
551 if (ret)
552 return ret;
553 else
554 return count;
1da177e4
LT
555}
556
557/**
558 * show_scaling_driver - show the cpufreq driver currently loaded
559 */
905d77cd 560static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
1da177e4
LT
561{
562 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
563}
564
565/**
566 * show_scaling_available_governors - show the available CPUfreq governors
567 */
905d77cd
DJ
568static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
569 char *buf)
1da177e4
LT
570{
571 ssize_t i = 0;
572 struct cpufreq_governor *t;
573
574 if (!cpufreq_driver->target) {
575 i += sprintf(buf, "performance powersave");
576 goto out;
577 }
578
579 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
29464f28
DJ
580 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
581 - (CPUFREQ_NAME_LEN + 2)))
1da177e4
LT
582 goto out;
583 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
584 }
7d5e350f 585out:
1da177e4
LT
586 i += sprintf(&buf[i], "\n");
587 return i;
588}
e8628dd0 589
835481d9 590static ssize_t show_cpus(const struct cpumask *mask, char *buf)
1da177e4
LT
591{
592 ssize_t i = 0;
593 unsigned int cpu;
594
835481d9 595 for_each_cpu(cpu, mask) {
1da177e4
LT
596 if (i)
597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
598 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
599 if (i >= (PAGE_SIZE - 5))
29464f28 600 break;
1da177e4
LT
601 }
602 i += sprintf(&buf[i], "\n");
603 return i;
604}
605
e8628dd0
DW
606/**
607 * show_related_cpus - show the CPUs affected by each transition even if
608 * hw coordination is in use
609 */
610static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
611{
835481d9 612 if (cpumask_empty(policy->related_cpus))
e8628dd0
DW
613 return show_cpus(policy->cpus, buf);
614 return show_cpus(policy->related_cpus, buf);
615}
616
617/**
618 * show_affected_cpus - show the CPUs affected by each transition
619 */
620static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
621{
622 return show_cpus(policy->cpus, buf);
623}
624
9e76988e 625static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
905d77cd 626 const char *buf, size_t count)
9e76988e
VP
627{
628 unsigned int freq = 0;
629 unsigned int ret;
630
879000f9 631 if (!policy->governor || !policy->governor->store_setspeed)
9e76988e
VP
632 return -EINVAL;
633
634 ret = sscanf(buf, "%u", &freq);
635 if (ret != 1)
636 return -EINVAL;
637
638 policy->governor->store_setspeed(policy, freq);
639
640 return count;
641}
642
643static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
644{
879000f9 645 if (!policy->governor || !policy->governor->show_setspeed)
9e76988e
VP
646 return sprintf(buf, "<unsupported>\n");
647
648 return policy->governor->show_setspeed(policy, buf);
649}
1da177e4 650
e2f74f35
TR
651/**
652 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
653 */
654static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
655{
656 unsigned int limit;
657 int ret;
658 if (cpufreq_driver->bios_limit) {
659 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
660 if (!ret)
661 return sprintf(buf, "%u\n", limit);
662 }
663 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
664}
665
6dad2a29
BP
666cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
667cpufreq_freq_attr_ro(cpuinfo_min_freq);
668cpufreq_freq_attr_ro(cpuinfo_max_freq);
669cpufreq_freq_attr_ro(cpuinfo_transition_latency);
670cpufreq_freq_attr_ro(scaling_available_governors);
671cpufreq_freq_attr_ro(scaling_driver);
672cpufreq_freq_attr_ro(scaling_cur_freq);
673cpufreq_freq_attr_ro(bios_limit);
674cpufreq_freq_attr_ro(related_cpus);
675cpufreq_freq_attr_ro(affected_cpus);
676cpufreq_freq_attr_rw(scaling_min_freq);
677cpufreq_freq_attr_rw(scaling_max_freq);
678cpufreq_freq_attr_rw(scaling_governor);
679cpufreq_freq_attr_rw(scaling_setspeed);
1da177e4 680
905d77cd 681static struct attribute *default_attrs[] = {
1da177e4
LT
682 &cpuinfo_min_freq.attr,
683 &cpuinfo_max_freq.attr,
ed129784 684 &cpuinfo_transition_latency.attr,
1da177e4
LT
685 &scaling_min_freq.attr,
686 &scaling_max_freq.attr,
687 &affected_cpus.attr,
e8628dd0 688 &related_cpus.attr,
1da177e4
LT
689 &scaling_governor.attr,
690 &scaling_driver.attr,
691 &scaling_available_governors.attr,
9e76988e 692 &scaling_setspeed.attr,
1da177e4
LT
693 NULL
694};
695
8aa84ad8
TR
696struct kobject *cpufreq_global_kobject;
697EXPORT_SYMBOL(cpufreq_global_kobject);
698
29464f28
DJ
699#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
700#define to_attr(a) container_of(a, struct freq_attr, attr)
1da177e4 701
29464f28 702static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1da177e4 703{
905d77cd
DJ
704 struct cpufreq_policy *policy = to_policy(kobj);
705 struct freq_attr *fattr = to_attr(attr);
0db4a8a9 706 ssize_t ret = -EINVAL;
1da177e4
LT
707 policy = cpufreq_cpu_get(policy->cpu);
708 if (!policy)
0db4a8a9 709 goto no_policy;
5a01f2e8
VP
710
711 if (lock_policy_rwsem_read(policy->cpu) < 0)
0db4a8a9 712 goto fail;
5a01f2e8 713
e08f5f5b
GS
714 if (fattr->show)
715 ret = fattr->show(policy, buf);
716 else
717 ret = -EIO;
718
5a01f2e8 719 unlock_policy_rwsem_read(policy->cpu);
0db4a8a9 720fail:
1da177e4 721 cpufreq_cpu_put(policy);
0db4a8a9 722no_policy:
1da177e4
LT
723 return ret;
724}
725
905d77cd
DJ
726static ssize_t store(struct kobject *kobj, struct attribute *attr,
727 const char *buf, size_t count)
1da177e4 728{
905d77cd
DJ
729 struct cpufreq_policy *policy = to_policy(kobj);
730 struct freq_attr *fattr = to_attr(attr);
a07530b4 731 ssize_t ret = -EINVAL;
1da177e4
LT
732 policy = cpufreq_cpu_get(policy->cpu);
733 if (!policy)
a07530b4 734 goto no_policy;
5a01f2e8
VP
735
736 if (lock_policy_rwsem_write(policy->cpu) < 0)
a07530b4 737 goto fail;
5a01f2e8 738
e08f5f5b
GS
739 if (fattr->store)
740 ret = fattr->store(policy, buf, count);
741 else
742 ret = -EIO;
743
5a01f2e8 744 unlock_policy_rwsem_write(policy->cpu);
a07530b4 745fail:
1da177e4 746 cpufreq_cpu_put(policy);
a07530b4 747no_policy:
1da177e4
LT
748 return ret;
749}
750
905d77cd 751static void cpufreq_sysfs_release(struct kobject *kobj)
1da177e4 752{
905d77cd 753 struct cpufreq_policy *policy = to_policy(kobj);
1da177e4
LT
754 dprintk("last reference is dropped\n");
755 complete(&policy->kobj_unregister);
756}
757
52cf25d0 758static const struct sysfs_ops sysfs_ops = {
1da177e4
LT
759 .show = show,
760 .store = store,
761};
762
763static struct kobj_type ktype_cpufreq = {
764 .sysfs_ops = &sysfs_ops,
765 .default_attrs = default_attrs,
766 .release = cpufreq_sysfs_release,
767};
768
4bfa042c
TR
769/*
770 * Returns:
771 * Negative: Failure
772 * 0: Success
773 * Positive: When we have a managed CPU and the sysfs got symlinked
774 */
cf3289d0
AC
775static int cpufreq_add_dev_policy(unsigned int cpu,
776 struct cpufreq_policy *policy,
777 struct sys_device *sys_dev)
ecf7e461
DJ
778{
779 int ret = 0;
780#ifdef CONFIG_SMP
781 unsigned long flags;
782 unsigned int j;
ecf7e461 783#ifdef CONFIG_HOTPLUG_CPU
e77b89f1
DM
784 struct cpufreq_governor *gov;
785
786 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
787 if (gov) {
788 policy->governor = gov;
ecf7e461
DJ
789 dprintk("Restoring governor %s for cpu %d\n",
790 policy->governor->name, cpu);
791 }
792#endif
793
794 for_each_cpu(j, policy->cpus) {
795 struct cpufreq_policy *managed_policy;
796
797 if (cpu == j)
798 continue;
799
800 /* Check for existing affected CPUs.
801 * They may not be aware of it due to CPU Hotplug.
802 * cpufreq_cpu_put is called when the device is removed
803 * in __cpufreq_remove_dev()
804 */
805 managed_policy = cpufreq_cpu_get(j);
806 if (unlikely(managed_policy)) {
807
808 /* Set proper policy_cpu */
809 unlock_policy_rwsem_write(cpu);
f1625066 810 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
ecf7e461
DJ
811
812 if (lock_policy_rwsem_write(cpu) < 0) {
813 /* Should not go through policy unlock path */
814 if (cpufreq_driver->exit)
815 cpufreq_driver->exit(policy);
816 cpufreq_cpu_put(managed_policy);
817 return -EBUSY;
818 }
819
820 spin_lock_irqsave(&cpufreq_driver_lock, flags);
821 cpumask_copy(managed_policy->cpus, policy->cpus);
822 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
823 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
824
825 dprintk("CPU already managed, adding link\n");
826 ret = sysfs_create_link(&sys_dev->kobj,
827 &managed_policy->kobj,
828 "cpufreq");
829 if (ret)
830 cpufreq_cpu_put(managed_policy);
831 /*
832 * Success. We only needed to be added to the mask.
833 * Call driver->exit() because only the cpu parent of
834 * the kobj needed to call init().
835 */
836 if (cpufreq_driver->exit)
837 cpufreq_driver->exit(policy);
4bfa042c
TR
838
839 if (!ret)
840 return 1;
841 else
842 return ret;
ecf7e461
DJ
843 }
844 }
845#endif
846 return ret;
847}
848
849
19d6f7ec 850/* symlink affected CPUs */
cf3289d0
AC
851static int cpufreq_add_dev_symlink(unsigned int cpu,
852 struct cpufreq_policy *policy)
19d6f7ec
DJ
853{
854 unsigned int j;
855 int ret = 0;
856
857 for_each_cpu(j, policy->cpus) {
858 struct cpufreq_policy *managed_policy;
859 struct sys_device *cpu_sys_dev;
860
861 if (j == cpu)
862 continue;
863 if (!cpu_online(j))
864 continue;
865
866 dprintk("CPU %u already managed, adding link\n", j);
867 managed_policy = cpufreq_cpu_get(cpu);
868 cpu_sys_dev = get_cpu_sysdev(j);
869 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
870 "cpufreq");
871 if (ret) {
872 cpufreq_cpu_put(managed_policy);
873 return ret;
874 }
875 }
876 return ret;
877}
878
cf3289d0
AC
879static int cpufreq_add_dev_interface(unsigned int cpu,
880 struct cpufreq_policy *policy,
881 struct sys_device *sys_dev)
909a694e 882{
ecf7e461 883 struct cpufreq_policy new_policy;
909a694e
DJ
884 struct freq_attr **drv_attr;
885 unsigned long flags;
886 int ret = 0;
887 unsigned int j;
888
889 /* prepare interface data */
890 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
891 &sys_dev->kobj, "cpufreq");
892 if (ret)
893 return ret;
894
895 /* set up files for this cpu device */
896 drv_attr = cpufreq_driver->attr;
897 while ((drv_attr) && (*drv_attr)) {
898 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
899 if (ret)
900 goto err_out_kobj_put;
901 drv_attr++;
902 }
903 if (cpufreq_driver->get) {
904 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
905 if (ret)
906 goto err_out_kobj_put;
907 }
908 if (cpufreq_driver->target) {
909 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
910 if (ret)
911 goto err_out_kobj_put;
912 }
e2f74f35
TR
913 if (cpufreq_driver->bios_limit) {
914 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
915 if (ret)
916 goto err_out_kobj_put;
917 }
909a694e
DJ
918
919 spin_lock_irqsave(&cpufreq_driver_lock, flags);
920 for_each_cpu(j, policy->cpus) {
bec037aa
JL
921 if (!cpu_online(j))
922 continue;
909a694e 923 per_cpu(cpufreq_cpu_data, j) = policy;
f1625066 924 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
909a694e
DJ
925 }
926 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
927
928 ret = cpufreq_add_dev_symlink(cpu, policy);
ecf7e461
DJ
929 if (ret)
930 goto err_out_kobj_put;
931
932 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
933 /* assure that the starting sequence is run in __cpufreq_set_policy */
934 policy->governor = NULL;
935
936 /* set default policy */
937 ret = __cpufreq_set_policy(policy, &new_policy);
938 policy->user_policy.policy = policy->policy;
939 policy->user_policy.governor = policy->governor;
940
941 if (ret) {
942 dprintk("setting policy failed\n");
943 if (cpufreq_driver->exit)
944 cpufreq_driver->exit(policy);
945 }
909a694e
DJ
946 return ret;
947
948err_out_kobj_put:
949 kobject_put(&policy->kobj);
950 wait_for_completion(&policy->kobj_unregister);
951 return ret;
952}
953
1da177e4
LT
954
955/**
956 * cpufreq_add_dev - add a CPU device
957 *
32ee8c3e 958 * Adds the cpufreq interface for a CPU device.
3f4a782b
MD
959 *
960 * The Oracle says: try running cpufreq registration/unregistration concurrently
961 * with with cpu hotplugging and all hell will break loose. Tried to clean this
962 * mess up, but more thorough testing is needed. - Mathieu
1da177e4 963 */
905d77cd 964static int cpufreq_add_dev(struct sys_device *sys_dev)
1da177e4
LT
965{
966 unsigned int cpu = sys_dev->id;
90e41bac 967 int ret = 0, found = 0;
1da177e4 968 struct cpufreq_policy *policy;
1da177e4
LT
969 unsigned long flags;
970 unsigned int j;
90e41bac
PB
971#ifdef CONFIG_HOTPLUG_CPU
972 int sibling;
973#endif
1da177e4 974
c32b6b8e
AR
975 if (cpu_is_offline(cpu))
976 return 0;
977
1da177e4
LT
978 cpufreq_debug_disable_ratelimit();
979 dprintk("adding CPU %u\n", cpu);
980
981#ifdef CONFIG_SMP
982 /* check whether a different CPU already registered this
983 * CPU because it is in the same boat. */
984 policy = cpufreq_cpu_get(cpu);
985 if (unlikely(policy)) {
8ff69732 986 cpufreq_cpu_put(policy);
1da177e4
LT
987 cpufreq_debug_enable_ratelimit();
988 return 0;
989 }
990#endif
991
992 if (!try_module_get(cpufreq_driver->owner)) {
993 ret = -EINVAL;
994 goto module_out;
995 }
996
059019a3 997 ret = -ENOMEM;
e98df50c 998 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
059019a3 999 if (!policy)
1da177e4 1000 goto nomem_out;
059019a3
DJ
1001
1002 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
3f4a782b 1003 goto err_free_policy;
059019a3
DJ
1004
1005 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
3f4a782b 1006 goto err_free_cpumask;
1da177e4
LT
1007
1008 policy->cpu = cpu;
835481d9 1009 cpumask_copy(policy->cpus, cpumask_of(cpu));
1da177e4 1010
5a01f2e8 1011 /* Initially set CPU itself as the policy_cpu */
f1625066 1012 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
3f4a782b
MD
1013 ret = (lock_policy_rwsem_write(cpu) < 0);
1014 WARN_ON(ret);
5a01f2e8 1015
1da177e4 1016 init_completion(&policy->kobj_unregister);
65f27f38 1017 INIT_WORK(&policy->update, handle_update);
1da177e4 1018
8122c6ce 1019 /* Set governor before ->init, so that driver could check it */
90e41bac
PB
1020#ifdef CONFIG_HOTPLUG_CPU
1021 for_each_online_cpu(sibling) {
1022 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1023 if (cp && cp->governor &&
1024 (cpumask_test_cpu(cpu, cp->related_cpus))) {
1025 policy->governor = cp->governor;
1026 found = 1;
1027 break;
1028 }
1029 }
1030#endif
1031 if (!found)
1032 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1da177e4
LT
1033 /* call driver. From then on the cpufreq must be able
1034 * to accept all calls to ->verify and ->setpolicy for this CPU
1035 */
1036 ret = cpufreq_driver->init(policy);
1037 if (ret) {
1038 dprintk("initialization failed\n");
3f4a782b 1039 goto err_unlock_policy;
1da177e4 1040 }
187d9f4e
MC
1041 policy->user_policy.min = policy->min;
1042 policy->user_policy.max = policy->max;
1da177e4 1043
a1531acd
TR
1044 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1045 CPUFREQ_START, policy);
1046
ecf7e461 1047 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
4bfa042c
TR
1048 if (ret) {
1049 if (ret > 0)
1050 /* This is a managed cpu, symlink created,
1051 exit with 0 */
1052 ret = 0;
ecf7e461 1053 goto err_unlock_policy;
4bfa042c 1054 }
1da177e4 1055
909a694e 1056 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
19d6f7ec
DJ
1057 if (ret)
1058 goto err_out_unregister;
8ff69732 1059
dca02613
LW
1060 unlock_policy_rwsem_write(cpu);
1061
038c5b3e 1062 kobject_uevent(&policy->kobj, KOBJ_ADD);
1da177e4 1063 module_put(cpufreq_driver->owner);
1da177e4
LT
1064 dprintk("initialization complete\n");
1065 cpufreq_debug_enable_ratelimit();
87c32271 1066
1da177e4
LT
1067 return 0;
1068
1069
1070err_out_unregister:
1071 spin_lock_irqsave(&cpufreq_driver_lock, flags);
835481d9 1072 for_each_cpu(j, policy->cpus)
7a6aedfa 1073 per_cpu(cpufreq_cpu_data, j) = NULL;
1da177e4
LT
1074 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1075
c10997f6 1076 kobject_put(&policy->kobj);
1da177e4
LT
1077 wait_for_completion(&policy->kobj_unregister);
1078
3f4a782b 1079err_unlock_policy:
45709118 1080 unlock_policy_rwsem_write(cpu);
cad70a6a 1081 free_cpumask_var(policy->related_cpus);
3f4a782b
MD
1082err_free_cpumask:
1083 free_cpumask_var(policy->cpus);
1084err_free_policy:
1da177e4 1085 kfree(policy);
1da177e4
LT
1086nomem_out:
1087 module_put(cpufreq_driver->owner);
c32b6b8e 1088module_out:
1da177e4
LT
1089 cpufreq_debug_enable_ratelimit();
1090 return ret;
1091}
1092
1093
1094/**
5a01f2e8 1095 * __cpufreq_remove_dev - remove a CPU device
1da177e4
LT
1096 *
1097 * Removes the cpufreq interface for a CPU device.
5a01f2e8
VP
1098 * Caller should already have policy_rwsem in write mode for this CPU.
1099 * This routine frees the rwsem before returning.
1da177e4 1100 */
905d77cd 1101static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1da177e4
LT
1102{
1103 unsigned int cpu = sys_dev->id;
1104 unsigned long flags;
1105 struct cpufreq_policy *data;
499bca9b
AW
1106 struct kobject *kobj;
1107 struct completion *cmp;
1da177e4 1108#ifdef CONFIG_SMP
e738cf6d 1109 struct sys_device *cpu_sys_dev;
1da177e4
LT
1110 unsigned int j;
1111#endif
1112
1113 cpufreq_debug_disable_ratelimit();
1114 dprintk("unregistering CPU %u\n", cpu);
1115
1116 spin_lock_irqsave(&cpufreq_driver_lock, flags);
7a6aedfa 1117 data = per_cpu(cpufreq_cpu_data, cpu);
1da177e4
LT
1118
1119 if (!data) {
1120 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1121 cpufreq_debug_enable_ratelimit();
5a01f2e8 1122 unlock_policy_rwsem_write(cpu);
1da177e4
LT
1123 return -EINVAL;
1124 }
7a6aedfa 1125 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1da177e4
LT
1126
1127
1128#ifdef CONFIG_SMP
1129 /* if this isn't the CPU which is the parent of the kobj, we
32ee8c3e 1130 * only need to unlink, put and exit
1da177e4
LT
1131 */
1132 if (unlikely(cpu != data->cpu)) {
1133 dprintk("removing link\n");
835481d9 1134 cpumask_clear_cpu(cpu, data->cpus);
1da177e4 1135 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
499bca9b 1136 kobj = &sys_dev->kobj;
1da177e4
LT
1137 cpufreq_cpu_put(data);
1138 cpufreq_debug_enable_ratelimit();
5a01f2e8 1139 unlock_policy_rwsem_write(cpu);
499bca9b 1140 sysfs_remove_link(kobj, "cpufreq");
1da177e4
LT
1141 return 0;
1142 }
1143#endif
1144
1da177e4 1145#ifdef CONFIG_SMP
084f3493
TR
1146
1147#ifdef CONFIG_HOTPLUG_CPU
e77b89f1
DM
1148 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1149 CPUFREQ_NAME_LEN);
084f3493
TR
1150#endif
1151
1da177e4
LT
1152 /* if we have other CPUs still registered, we need to unlink them,
1153 * or else wait_for_completion below will lock up. Clean the
7a6aedfa
MT
1154 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1155 * the sysfs links afterwards.
1da177e4 1156 */
835481d9
RR
1157 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1158 for_each_cpu(j, data->cpus) {
1da177e4
LT
1159 if (j == cpu)
1160 continue;
7a6aedfa 1161 per_cpu(cpufreq_cpu_data, j) = NULL;
1da177e4
LT
1162 }
1163 }
1164
1165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1166
835481d9
RR
1167 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1168 for_each_cpu(j, data->cpus) {
1da177e4
LT
1169 if (j == cpu)
1170 continue;
1171 dprintk("removing link for cpu %u\n", j);
084f3493 1172#ifdef CONFIG_HOTPLUG_CPU
e77b89f1
DM
1173 strncpy(per_cpu(cpufreq_cpu_governor, j),
1174 data->governor->name, CPUFREQ_NAME_LEN);
084f3493 1175#endif
d434fca7 1176 cpu_sys_dev = get_cpu_sysdev(j);
499bca9b
AW
1177 kobj = &cpu_sys_dev->kobj;
1178 unlock_policy_rwsem_write(cpu);
1179 sysfs_remove_link(kobj, "cpufreq");
1180 lock_policy_rwsem_write(cpu);
1da177e4
LT
1181 cpufreq_cpu_put(data);
1182 }
1183 }
1184#else
1185 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1186#endif
1187
1da177e4
LT
1188 if (cpufreq_driver->target)
1189 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
5a01f2e8 1190
499bca9b
AW
1191 kobj = &data->kobj;
1192 cmp = &data->kobj_unregister;
1193 unlock_policy_rwsem_write(cpu);
1194 kobject_put(kobj);
1da177e4
LT
1195
1196 /* we need to make sure that the underlying kobj is actually
32ee8c3e 1197 * not referenced anymore by anybody before we proceed with
1da177e4
LT
1198 * unloading.
1199 */
1200 dprintk("waiting for dropping of refcount\n");
499bca9b 1201 wait_for_completion(cmp);
1da177e4
LT
1202 dprintk("wait complete\n");
1203
499bca9b 1204 lock_policy_rwsem_write(cpu);
1da177e4
LT
1205 if (cpufreq_driver->exit)
1206 cpufreq_driver->exit(data);
7d26e2d5 1207 unlock_policy_rwsem_write(cpu);
1208
835481d9
RR
1209 free_cpumask_var(data->related_cpus);
1210 free_cpumask_var(data->cpus);
1da177e4 1211 kfree(data);
835481d9 1212 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1da177e4
LT
1213
1214 cpufreq_debug_enable_ratelimit();
1da177e4
LT
1215 return 0;
1216}
1217
1218
905d77cd 1219static int cpufreq_remove_dev(struct sys_device *sys_dev)
5a01f2e8
VP
1220{
1221 unsigned int cpu = sys_dev->id;
1222 int retval;
ec28297a
VP
1223
1224 if (cpu_is_offline(cpu))
1225 return 0;
1226
5a01f2e8
VP
1227 if (unlikely(lock_policy_rwsem_write(cpu)))
1228 BUG();
1229
1230 retval = __cpufreq_remove_dev(sys_dev);
1231 return retval;
1232}
1233
1234
65f27f38 1235static void handle_update(struct work_struct *work)
1da177e4 1236{
65f27f38
DH
1237 struct cpufreq_policy *policy =
1238 container_of(work, struct cpufreq_policy, update);
1239 unsigned int cpu = policy->cpu;
1da177e4
LT
1240 dprintk("handle_update for cpu %u called\n", cpu);
1241 cpufreq_update_policy(cpu);
1242}
1243
1244/**
1245 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1246 * @cpu: cpu number
1247 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1248 * @new_freq: CPU frequency the CPU actually runs at
1249 *
29464f28
DJ
1250 * We adjust to current frequency first, and need to clean up later.
1251 * So either call to cpufreq_update_policy() or schedule handle_update()).
1da177e4 1252 */
e08f5f5b
GS
1253static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1254 unsigned int new_freq)
1da177e4
LT
1255{
1256 struct cpufreq_freqs freqs;
1257
b10eec22 1258 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
1da177e4
LT
1259 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1260
1261 freqs.cpu = cpu;
1262 freqs.old = old_freq;
1263 freqs.new = new_freq;
1264 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1265 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1266}
1267
1268
32ee8c3e 1269/**
4ab70df4 1270 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
95235ca2
VP
1271 * @cpu: CPU number
1272 *
1273 * This is the last known freq, without actually getting it from the driver.
1274 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1275 */
1276unsigned int cpufreq_quick_get(unsigned int cpu)
1277{
1278 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
e08f5f5b 1279 unsigned int ret_freq = 0;
95235ca2
VP
1280
1281 if (policy) {
e08f5f5b 1282 ret_freq = policy->cur;
95235ca2
VP
1283 cpufreq_cpu_put(policy);
1284 }
1285
4d34a67d 1286 return ret_freq;
95235ca2
VP
1287}
1288EXPORT_SYMBOL(cpufreq_quick_get);
1289
1290
5a01f2e8 1291static unsigned int __cpufreq_get(unsigned int cpu)
1da177e4 1292{
7a6aedfa 1293 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
e08f5f5b 1294 unsigned int ret_freq = 0;
1da177e4 1295
1da177e4 1296 if (!cpufreq_driver->get)
4d34a67d 1297 return ret_freq;
1da177e4 1298
e08f5f5b 1299 ret_freq = cpufreq_driver->get(cpu);
1da177e4 1300
e08f5f5b
GS
1301 if (ret_freq && policy->cur &&
1302 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1303 /* verify no discrepancy between actual and
1304 saved value exists */
1305 if (unlikely(ret_freq != policy->cur)) {
1306 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1da177e4
LT
1307 schedule_work(&policy->update);
1308 }
1309 }
1310
4d34a67d 1311 return ret_freq;
5a01f2e8 1312}
1da177e4 1313
5a01f2e8
VP
1314/**
1315 * cpufreq_get - get the current CPU frequency (in kHz)
1316 * @cpu: CPU number
1317 *
1318 * Get the CPU current (static) CPU frequency
1319 */
1320unsigned int cpufreq_get(unsigned int cpu)
1321{
1322 unsigned int ret_freq = 0;
1323 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1324
1325 if (!policy)
1326 goto out;
1327
1328 if (unlikely(lock_policy_rwsem_read(cpu)))
1329 goto out_policy;
1330
1331 ret_freq = __cpufreq_get(cpu);
1332
1333 unlock_policy_rwsem_read(cpu);
1da177e4 1334
5a01f2e8
VP
1335out_policy:
1336 cpufreq_cpu_put(policy);
1337out:
4d34a67d 1338 return ret_freq;
1da177e4
LT
1339}
1340EXPORT_SYMBOL(cpufreq_get);
1341
1342
42d4dc3f
BH
1343/**
1344 * cpufreq_suspend - let the low level driver prepare for suspend
1345 */
1346
905d77cd 1347static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
42d4dc3f 1348{
e08f5f5b 1349 int ret = 0;
4bc5d341 1350
4bc5d341 1351 int cpu = sysdev->id;
42d4dc3f
BH
1352 struct cpufreq_policy *cpu_policy;
1353
0e37b159 1354 dprintk("suspending cpu %u\n", cpu);
42d4dc3f
BH
1355
1356 if (!cpu_online(cpu))
1357 return 0;
1358
1359 /* we may be lax here as interrupts are off. Nonetheless
1360 * we need to grab the correct cpu policy, as to check
1361 * whether we really run on this CPU.
1362 */
1363
1364 cpu_policy = cpufreq_cpu_get(cpu);
1365 if (!cpu_policy)
1366 return -EINVAL;
1367
1368 /* only handle each CPU group once */
c9060494
DJ
1369 if (unlikely(cpu_policy->cpu != cpu))
1370 goto out;
42d4dc3f
BH
1371
1372 if (cpufreq_driver->suspend) {
e00d9967 1373 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
ce6c3997 1374 if (ret)
42d4dc3f
BH
1375 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1376 "step on CPU %u\n", cpu_policy->cpu);
42d4dc3f
BH
1377 }
1378
7d5e350f 1379out:
42d4dc3f 1380 cpufreq_cpu_put(cpu_policy);
c9060494 1381 return ret;
42d4dc3f
BH
1382}
1383
1da177e4
LT
1384/**
1385 * cpufreq_resume - restore proper CPU frequency handling after resume
1386 *
1387 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
ce6c3997
DB
1388 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1389 * restored. It will verify that the current freq is in sync with
1390 * what we believe it to be. This is a bit later than when it
1391 * should be, but nonethteless it's better than calling
1392 * cpufreq_driver->get() here which might re-enable interrupts...
1da177e4 1393 */
905d77cd 1394static int cpufreq_resume(struct sys_device *sysdev)
1da177e4 1395{
e08f5f5b 1396 int ret = 0;
4bc5d341 1397
4bc5d341 1398 int cpu = sysdev->id;
1da177e4
LT
1399 struct cpufreq_policy *cpu_policy;
1400
1401 dprintk("resuming cpu %u\n", cpu);
1402
1403 if (!cpu_online(cpu))
1404 return 0;
1405
1406 /* we may be lax here as interrupts are off. Nonetheless
1407 * we need to grab the correct cpu policy, as to check
1408 * whether we really run on this CPU.
1409 */
1410
1411 cpu_policy = cpufreq_cpu_get(cpu);
1412 if (!cpu_policy)
1413 return -EINVAL;
1414
1415 /* only handle each CPU group once */
c9060494
DJ
1416 if (unlikely(cpu_policy->cpu != cpu))
1417 goto fail;
1da177e4
LT
1418
1419 if (cpufreq_driver->resume) {
1420 ret = cpufreq_driver->resume(cpu_policy);
1421 if (ret) {
1422 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1423 "step on CPU %u\n", cpu_policy->cpu);
c9060494 1424 goto fail;
1da177e4
LT
1425 }
1426 }
1427
1da177e4 1428 schedule_work(&cpu_policy->update);
ce6c3997 1429
c9060494 1430fail:
1da177e4
LT
1431 cpufreq_cpu_put(cpu_policy);
1432 return ret;
1433}
1434
1435static struct sysdev_driver cpufreq_sysdev_driver = {
1436 .add = cpufreq_add_dev,
1437 .remove = cpufreq_remove_dev,
42d4dc3f 1438 .suspend = cpufreq_suspend,
1da177e4
LT
1439 .resume = cpufreq_resume,
1440};
1441
1442
1443/*********************************************************************
1444 * NOTIFIER LISTS INTERFACE *
1445 *********************************************************************/
1446
1447/**
1448 * cpufreq_register_notifier - register a driver with cpufreq
1449 * @nb: notifier function to register
1450 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1451 *
32ee8c3e 1452 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1453 * are notified about clock rate changes (once before and once after
1454 * the transition), or a list of drivers that are notified about
1455 * changes in cpufreq policy.
1456 *
1457 * This function may sleep, and has the same return conditions as
e041c683 1458 * blocking_notifier_chain_register.
1da177e4
LT
1459 */
1460int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1461{
1462 int ret;
1463
74212ca4
CEB
1464 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1465
1da177e4
LT
1466 switch (list) {
1467 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1468 ret = srcu_notifier_chain_register(
e041c683 1469 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1470 break;
1471 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1472 ret = blocking_notifier_chain_register(
1473 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1474 break;
1475 default:
1476 ret = -EINVAL;
1477 }
1da177e4
LT
1478
1479 return ret;
1480}
1481EXPORT_SYMBOL(cpufreq_register_notifier);
1482
1483
1484/**
1485 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1486 * @nb: notifier block to be unregistered
1487 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1488 *
1489 * Remove a driver from the CPU frequency notifier list.
1490 *
1491 * This function may sleep, and has the same return conditions as
e041c683 1492 * blocking_notifier_chain_unregister.
1da177e4
LT
1493 */
1494int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1495{
1496 int ret;
1497
1da177e4
LT
1498 switch (list) {
1499 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1500 ret = srcu_notifier_chain_unregister(
e041c683 1501 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1502 break;
1503 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1504 ret = blocking_notifier_chain_unregister(
1505 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1506 break;
1507 default:
1508 ret = -EINVAL;
1509 }
1da177e4
LT
1510
1511 return ret;
1512}
1513EXPORT_SYMBOL(cpufreq_unregister_notifier);
1514
1515
1516/*********************************************************************
1517 * GOVERNORS *
1518 *********************************************************************/
1519
1520
1521int __cpufreq_driver_target(struct cpufreq_policy *policy,
1522 unsigned int target_freq,
1523 unsigned int relation)
1524{
1525 int retval = -EINVAL;
c32b6b8e 1526
1da177e4
LT
1527 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1528 target_freq, relation);
1529 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1530 retval = cpufreq_driver->target(policy, target_freq, relation);
90d45d17 1531
1da177e4
LT
1532 return retval;
1533}
1534EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1535
1da177e4
LT
1536int cpufreq_driver_target(struct cpufreq_policy *policy,
1537 unsigned int target_freq,
1538 unsigned int relation)
1539{
f1829e4a 1540 int ret = -EINVAL;
1da177e4
LT
1541
1542 policy = cpufreq_cpu_get(policy->cpu);
1543 if (!policy)
f1829e4a 1544 goto no_policy;
1da177e4 1545
5a01f2e8 1546 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
f1829e4a 1547 goto fail;
1da177e4
LT
1548
1549 ret = __cpufreq_driver_target(policy, target_freq, relation);
1550
5a01f2e8 1551 unlock_policy_rwsem_write(policy->cpu);
1da177e4 1552
f1829e4a 1553fail:
1da177e4 1554 cpufreq_cpu_put(policy);
f1829e4a 1555no_policy:
1da177e4
LT
1556 return ret;
1557}
1558EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1559
bf0b90e3 1560int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
dfde5d62
VP
1561{
1562 int ret = 0;
1563
1564 policy = cpufreq_cpu_get(policy->cpu);
1565 if (!policy)
1566 return -EINVAL;
1567
bf0b90e3 1568 if (cpu_online(cpu) && cpufreq_driver->getavg)
1569 ret = cpufreq_driver->getavg(policy, cpu);
dfde5d62 1570
dfde5d62
VP
1571 cpufreq_cpu_put(policy);
1572 return ret;
1573}
5a01f2e8 1574EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
dfde5d62 1575
153d7f3f 1576/*
153d7f3f
AV
1577 * when "event" is CPUFREQ_GOV_LIMITS
1578 */
1da177e4 1579
e08f5f5b
GS
1580static int __cpufreq_governor(struct cpufreq_policy *policy,
1581 unsigned int event)
1da177e4 1582{
cc993cab 1583 int ret;
6afde10c
TR
1584
1585 /* Only must be defined when default governor is known to have latency
1586 restrictions, like e.g. conservative or ondemand.
1587 That this is the case is already ensured in Kconfig
1588 */
1589#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1590 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1591#else
1592 struct cpufreq_governor *gov = NULL;
1593#endif
1c256245
TR
1594
1595 if (policy->governor->max_transition_latency &&
1596 policy->cpuinfo.transition_latency >
1597 policy->governor->max_transition_latency) {
6afde10c
TR
1598 if (!gov)
1599 return -EINVAL;
1600 else {
1601 printk(KERN_WARNING "%s governor failed, too long"
1602 " transition latency of HW, fallback"
1603 " to %s governor\n",
1604 policy->governor->name,
1605 gov->name);
1606 policy->governor = gov;
1607 }
1c256245 1608 }
1da177e4
LT
1609
1610 if (!try_module_get(policy->governor->owner))
1611 return -EINVAL;
1612
e08f5f5b
GS
1613 dprintk("__cpufreq_governor for CPU %u, event %u\n",
1614 policy->cpu, event);
1da177e4
LT
1615 ret = policy->governor->governor(policy, event);
1616
e08f5f5b
GS
1617 /* we keep one module reference alive for
1618 each CPU governed by this CPU */
1da177e4
LT
1619 if ((event != CPUFREQ_GOV_START) || ret)
1620 module_put(policy->governor->owner);
1621 if ((event == CPUFREQ_GOV_STOP) && !ret)
1622 module_put(policy->governor->owner);
1623
1624 return ret;
1625}
1626
1627
1da177e4
LT
1628int cpufreq_register_governor(struct cpufreq_governor *governor)
1629{
3bcb09a3 1630 int err;
1da177e4
LT
1631
1632 if (!governor)
1633 return -EINVAL;
1634
3fc54d37 1635 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 1636
3bcb09a3
JF
1637 err = -EBUSY;
1638 if (__find_governor(governor->name) == NULL) {
1639 err = 0;
1640 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 1641 }
1da177e4 1642
32ee8c3e 1643 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 1644 return err;
1da177e4
LT
1645}
1646EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1647
1648
1649void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1650{
90e41bac
PB
1651#ifdef CONFIG_HOTPLUG_CPU
1652 int cpu;
1653#endif
1654
1da177e4
LT
1655 if (!governor)
1656 return;
1657
90e41bac
PB
1658#ifdef CONFIG_HOTPLUG_CPU
1659 for_each_present_cpu(cpu) {
1660 if (cpu_online(cpu))
1661 continue;
1662 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1663 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1664 }
1665#endif
1666
3fc54d37 1667 mutex_lock(&cpufreq_governor_mutex);
1da177e4 1668 list_del(&governor->governor_list);
3fc54d37 1669 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1670 return;
1671}
1672EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1673
1674
1675
1676/*********************************************************************
1677 * POLICY INTERFACE *
1678 *********************************************************************/
1679
1680/**
1681 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
1682 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1683 * is written
1da177e4
LT
1684 *
1685 * Reads the current cpufreq policy.
1686 */
1687int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1688{
1689 struct cpufreq_policy *cpu_policy;
1690 if (!policy)
1691 return -EINVAL;
1692
1693 cpu_policy = cpufreq_cpu_get(cpu);
1694 if (!cpu_policy)
1695 return -EINVAL;
1696
1da177e4 1697 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1da177e4
LT
1698
1699 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
1700 return 0;
1701}
1702EXPORT_SYMBOL(cpufreq_get_policy);
1703
1704
153d7f3f 1705/*
e08f5f5b
GS
1706 * data : current policy.
1707 * policy : policy to be set.
153d7f3f 1708 */
e08f5f5b
GS
1709static int __cpufreq_set_policy(struct cpufreq_policy *data,
1710 struct cpufreq_policy *policy)
1da177e4
LT
1711{
1712 int ret = 0;
1713
1714 cpufreq_debug_disable_ratelimit();
1715 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1716 policy->min, policy->max);
1717
e08f5f5b
GS
1718 memcpy(&policy->cpuinfo, &data->cpuinfo,
1719 sizeof(struct cpufreq_cpuinfo));
1da177e4 1720
53391fa2 1721 if (policy->min > data->max || policy->max < data->min) {
9c9a43ed
MD
1722 ret = -EINVAL;
1723 goto error_out;
1724 }
1725
1da177e4
LT
1726 /* verify the cpu speed can be set within this limit */
1727 ret = cpufreq_driver->verify(policy);
1728 if (ret)
1729 goto error_out;
1730
1da177e4 1731 /* adjust if necessary - all reasons */
e041c683
AS
1732 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1733 CPUFREQ_ADJUST, policy);
1da177e4
LT
1734
1735 /* adjust if necessary - hardware incompatibility*/
e041c683
AS
1736 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1737 CPUFREQ_INCOMPATIBLE, policy);
1da177e4
LT
1738
1739 /* verify the cpu speed can be set within this limit,
1740 which might be different to the first one */
1741 ret = cpufreq_driver->verify(policy);
e041c683 1742 if (ret)
1da177e4 1743 goto error_out;
1da177e4
LT
1744
1745 /* notification of the new policy */
e041c683
AS
1746 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1747 CPUFREQ_NOTIFY, policy);
1da177e4 1748
7d5e350f
DJ
1749 data->min = policy->min;
1750 data->max = policy->max;
1da177e4 1751
e08f5f5b
GS
1752 dprintk("new min and max freqs are %u - %u kHz\n",
1753 data->min, data->max);
1da177e4
LT
1754
1755 if (cpufreq_driver->setpolicy) {
1756 data->policy = policy->policy;
1757 dprintk("setting range\n");
1758 ret = cpufreq_driver->setpolicy(policy);
1759 } else {
1760 if (policy->governor != data->governor) {
1761 /* save old, working values */
1762 struct cpufreq_governor *old_gov = data->governor;
1763
1764 dprintk("governor switch\n");
1765
1766 /* end old governor */
ffe6275f 1767 if (data->governor)
1da177e4
LT
1768 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1769
1770 /* start new governor */
1771 data->governor = policy->governor;
1772 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1773 /* new governor failed, so re-start old one */
e08f5f5b
GS
1774 dprintk("starting governor %s failed\n",
1775 data->governor->name);
1da177e4
LT
1776 if (old_gov) {
1777 data->governor = old_gov;
e08f5f5b
GS
1778 __cpufreq_governor(data,
1779 CPUFREQ_GOV_START);
1da177e4
LT
1780 }
1781 ret = -EINVAL;
1782 goto error_out;
1783 }
1784 /* might be a policy change, too, so fall through */
1785 }
1786 dprintk("governor: change or update limits\n");
1787 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1788 }
1789
7d5e350f 1790error_out:
1da177e4
LT
1791 cpufreq_debug_enable_ratelimit();
1792 return ret;
1793}
1794
1da177e4
LT
1795/**
1796 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1797 * @cpu: CPU which shall be re-evaluated
1798 *
1799 * Usefull for policy notifiers which have different necessities
1800 * at different times.
1801 */
1802int cpufreq_update_policy(unsigned int cpu)
1803{
1804 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1805 struct cpufreq_policy policy;
f1829e4a 1806 int ret;
1da177e4 1807
f1829e4a
JL
1808 if (!data) {
1809 ret = -ENODEV;
1810 goto no_policy;
1811 }
1da177e4 1812
f1829e4a
JL
1813 if (unlikely(lock_policy_rwsem_write(cpu))) {
1814 ret = -EINVAL;
1815 goto fail;
1816 }
1da177e4
LT
1817
1818 dprintk("updating policy for CPU %u\n", cpu);
7d5e350f 1819 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1da177e4
LT
1820 policy.min = data->user_policy.min;
1821 policy.max = data->user_policy.max;
1822 policy.policy = data->user_policy.policy;
1823 policy.governor = data->user_policy.governor;
1824
0961dd0d
TR
1825 /* BIOS might change freq behind our back
1826 -> ask driver for current freq and notify governors about a change */
1827 if (cpufreq_driver->get) {
1828 policy.cur = cpufreq_driver->get(cpu);
a85f7bd3
TR
1829 if (!data->cur) {
1830 dprintk("Driver did not initialize current freq");
1831 data->cur = policy.cur;
1832 } else {
1833 if (data->cur != policy.cur)
e08f5f5b
GS
1834 cpufreq_out_of_sync(cpu, data->cur,
1835 policy.cur);
a85f7bd3 1836 }
0961dd0d
TR
1837 }
1838
1da177e4
LT
1839 ret = __cpufreq_set_policy(data, &policy);
1840
5a01f2e8
VP
1841 unlock_policy_rwsem_write(cpu);
1842
f1829e4a 1843fail:
1da177e4 1844 cpufreq_cpu_put(data);
f1829e4a 1845no_policy:
1da177e4
LT
1846 return ret;
1847}
1848EXPORT_SYMBOL(cpufreq_update_policy);
1849
dd184a01 1850static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
1851 unsigned long action, void *hcpu)
1852{
1853 unsigned int cpu = (unsigned long)hcpu;
c32b6b8e
AR
1854 struct sys_device *sys_dev;
1855
1856 sys_dev = get_cpu_sysdev(cpu);
c32b6b8e
AR
1857 if (sys_dev) {
1858 switch (action) {
1859 case CPU_ONLINE:
8bb78442 1860 case CPU_ONLINE_FROZEN:
c32b6b8e
AR
1861 cpufreq_add_dev(sys_dev);
1862 break;
1863 case CPU_DOWN_PREPARE:
8bb78442 1864 case CPU_DOWN_PREPARE_FROZEN:
5a01f2e8
VP
1865 if (unlikely(lock_policy_rwsem_write(cpu)))
1866 BUG();
1867
5a01f2e8 1868 __cpufreq_remove_dev(sys_dev);
c32b6b8e 1869 break;
5a01f2e8 1870 case CPU_DOWN_FAILED:
8bb78442 1871 case CPU_DOWN_FAILED_FROZEN:
5a01f2e8 1872 cpufreq_add_dev(sys_dev);
c32b6b8e
AR
1873 break;
1874 }
1875 }
1876 return NOTIFY_OK;
1877}
1878
9c36f746 1879static struct notifier_block __refdata cpufreq_cpu_notifier = {
c32b6b8e
AR
1880 .notifier_call = cpufreq_cpu_callback,
1881};
1da177e4
LT
1882
1883/*********************************************************************
1884 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1885 *********************************************************************/
1886
1887/**
1888 * cpufreq_register_driver - register a CPU Frequency driver
1889 * @driver_data: A struct cpufreq_driver containing the values#
1890 * submitted by the CPU Frequency driver.
1891 *
32ee8c3e 1892 * Registers a CPU Frequency driver to this core code. This code
1da177e4 1893 * returns zero on success, -EBUSY when another driver got here first
32ee8c3e 1894 * (and isn't unregistered in the meantime).
1da177e4
LT
1895 *
1896 */
221dee28 1897int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
1898{
1899 unsigned long flags;
1900 int ret;
1901
1902 if (!driver_data || !driver_data->verify || !driver_data->init ||
1903 ((!driver_data->setpolicy) && (!driver_data->target)))
1904 return -EINVAL;
1905
1906 dprintk("trying to register driver %s\n", driver_data->name);
1907
1908 if (driver_data->setpolicy)
1909 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1910
1911 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1912 if (cpufreq_driver) {
1913 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1914 return -EBUSY;
1915 }
1916 cpufreq_driver = driver_data;
1917 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1918
7a6aedfa
MT
1919 ret = sysdev_driver_register(&cpu_sysdev_class,
1920 &cpufreq_sysdev_driver);
1da177e4
LT
1921
1922 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1923 int i;
1924 ret = -ENODEV;
1925
1926 /* check for at least one working CPU */
7a6aedfa
MT
1927 for (i = 0; i < nr_cpu_ids; i++)
1928 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1da177e4 1929 ret = 0;
7a6aedfa
MT
1930 break;
1931 }
1da177e4
LT
1932
1933 /* if all ->init() calls failed, unregister */
1934 if (ret) {
e08f5f5b
GS
1935 dprintk("no CPU initialized for driver %s\n",
1936 driver_data->name);
1937 sysdev_driver_unregister(&cpu_sysdev_class,
1938 &cpufreq_sysdev_driver);
1da177e4
LT
1939
1940 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1941 cpufreq_driver = NULL;
1942 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1943 }
1944 }
1945
1946 if (!ret) {
65edc68c 1947 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4
LT
1948 dprintk("driver %s up and running\n", driver_data->name);
1949 cpufreq_debug_enable_ratelimit();
1950 }
1951
4d34a67d 1952 return ret;
1da177e4
LT
1953}
1954EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1955
1956
1957/**
1958 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1959 *
32ee8c3e 1960 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
1961 * the right to do so, i.e. if you have succeeded in initialising before!
1962 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1963 * currently not initialised.
1964 */
221dee28 1965int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
1966{
1967 unsigned long flags;
1968
1969 cpufreq_debug_disable_ratelimit();
1970
1971 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1972 cpufreq_debug_enable_ratelimit();
1973 return -EINVAL;
1974 }
1975
1976 dprintk("unregistering driver %s\n", driver->name);
1977
1978 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
65edc68c 1979 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4
LT
1980
1981 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1982 cpufreq_driver = NULL;
1983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1984
1985 return 0;
1986}
1987EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8
VP
1988
1989static int __init cpufreq_core_init(void)
1990{
1991 int cpu;
1992
1993 for_each_possible_cpu(cpu) {
f1625066 1994 per_cpu(cpufreq_policy_cpu, cpu) = -1;
5a01f2e8
VP
1995 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1996 }
8aa84ad8
TR
1997
1998 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1999 &cpu_sysdev_class.kset.kobj);
2000 BUG_ON(!cpufreq_global_kobject);
2001
5a01f2e8
VP
2002 return 0;
2003}
5a01f2e8 2004core_initcall(cpufreq_core_init);