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