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