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