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