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