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