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