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