cpufreq: Send START policy notification after sending CREATE
[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
194d99c7 1261 if (new_policy) {
d9612a49 1262 ret = cpufreq_add_dev_interface(policy);
a82fab29 1263 if (ret)
8101f997 1264 goto out_exit_policy;
fcd7af91
VK
1265 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1266 CPUFREQ_CREATE_POLICY, policy);
8ff69732 1267
988bed09
VK
1268 write_lock_irqsave(&cpufreq_driver_lock, flags);
1269 list_add(&policy->policy_list, &cpufreq_policy_list);
1270 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1271 }
9515f4d6 1272
388612ba
VK
1273 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1274 CPUFREQ_START, policy);
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
e788892b
RW
2026 if (policy->governor->init) {
2027 ret = policy->governor->init(policy);
2028 if (ret) {
2029 module_put(policy->governor->owner);
2030 return ret;
2031 }
36be3418 2032 }
1da177e4 2033
a92604b4
RW
2034 return 0;
2035}
2036
2037static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2038{
2039 if (cpufreq_suspended || !policy->governor)
2040 return;
2041
2042 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2043
e788892b
RW
2044 if (policy->governor->exit)
2045 policy->governor->exit(policy);
a92604b4 2046
a92604b4 2047 module_put(policy->governor->owner);
1da177e4
LT
2048}
2049
0a300767
RW
2050static int cpufreq_start_governor(struct cpufreq_policy *policy)
2051{
2052 int ret;
2053
a92604b4
RW
2054 if (cpufreq_suspended)
2055 return 0;
2056
2057 if (!policy->governor)
2058 return -EINVAL;
2059
2060 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2061
3bbf8fe3
RW
2062 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2063 cpufreq_update_current_freq(policy);
2064
e788892b
RW
2065 if (policy->governor->start) {
2066 ret = policy->governor->start(policy);
2067 if (ret)
2068 return ret;
2069 }
2070
2071 if (policy->governor->limits)
2072 policy->governor->limits(policy);
d6ff44d6 2073
d6ff44d6 2074 return 0;
0a300767
RW
2075}
2076
a92604b4
RW
2077static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2078{
2079 if (cpufreq_suspended || !policy->governor)
2080 return;
2081
2082 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2083
e788892b
RW
2084 if (policy->governor->stop)
2085 policy->governor->stop(policy);
a92604b4
RW
2086}
2087
2088static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2089{
2090 if (cpufreq_suspended || !policy->governor)
2091 return;
2092
2093 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2094
e788892b
RW
2095 if (policy->governor->limits)
2096 policy->governor->limits(policy);
a92604b4
RW
2097}
2098
1da177e4
LT
2099int cpufreq_register_governor(struct cpufreq_governor *governor)
2100{
3bcb09a3 2101 int err;
1da177e4
LT
2102
2103 if (!governor)
2104 return -EINVAL;
2105
a7b422cd
KRW
2106 if (cpufreq_disabled())
2107 return -ENODEV;
2108
3fc54d37 2109 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 2110
3bcb09a3 2111 err = -EBUSY;
42f91fa1 2112 if (!find_governor(governor->name)) {
3bcb09a3
JF
2113 err = 0;
2114 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 2115 }
1da177e4 2116
32ee8c3e 2117 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 2118 return err;
1da177e4
LT
2119}
2120EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2121
1da177e4
LT
2122void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2123{
4573237b
VK
2124 struct cpufreq_policy *policy;
2125 unsigned long flags;
90e41bac 2126
1da177e4
LT
2127 if (!governor)
2128 return;
2129
a7b422cd
KRW
2130 if (cpufreq_disabled())
2131 return;
2132
4573237b
VK
2133 /* clear last_governor for all inactive policies */
2134 read_lock_irqsave(&cpufreq_driver_lock, flags);
2135 for_each_inactive_policy(policy) {
18bf3a12
VK
2136 if (!strcmp(policy->last_governor, governor->name)) {
2137 policy->governor = NULL;
4573237b 2138 strcpy(policy->last_governor, "\0");
18bf3a12 2139 }
90e41bac 2140 }
4573237b 2141 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
90e41bac 2142
3fc54d37 2143 mutex_lock(&cpufreq_governor_mutex);
1da177e4 2144 list_del(&governor->governor_list);
3fc54d37 2145 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
2146 return;
2147}
2148EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2149
2150
1da177e4
LT
2151/*********************************************************************
2152 * POLICY INTERFACE *
2153 *********************************************************************/
2154
2155/**
2156 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
2157 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2158 * is written
1da177e4
LT
2159 *
2160 * Reads the current cpufreq policy.
2161 */
2162int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2163{
2164 struct cpufreq_policy *cpu_policy;
2165 if (!policy)
2166 return -EINVAL;
2167
2168 cpu_policy = cpufreq_cpu_get(cpu);
2169 if (!cpu_policy)
2170 return -EINVAL;
2171
d5b73cd8 2172 memcpy(policy, cpu_policy, sizeof(*policy));
1da177e4
LT
2173
2174 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
2175 return 0;
2176}
2177EXPORT_SYMBOL(cpufreq_get_policy);
2178
153d7f3f 2179/*
037ce839
VK
2180 * policy : current policy.
2181 * new_policy: policy to be set.
153d7f3f 2182 */
037ce839 2183static int cpufreq_set_policy(struct cpufreq_policy *policy,
3a3e9e06 2184 struct cpufreq_policy *new_policy)
1da177e4 2185{
d9a789c7
RW
2186 struct cpufreq_governor *old_gov;
2187 int ret;
1da177e4 2188
e837f9b5
JP
2189 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2190 new_policy->cpu, new_policy->min, new_policy->max);
1da177e4 2191
d5b73cd8 2192 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1da177e4 2193
fba9573b
PX
2194 /*
2195 * This check works well when we store new min/max freq attributes,
2196 * because new_policy is a copy of policy with one field updated.
2197 */
2198 if (new_policy->min > new_policy->max)
d9a789c7 2199 return -EINVAL;
9c9a43ed 2200
1da177e4 2201 /* verify the cpu speed can be set within this limit */
3a3e9e06 2202 ret = cpufreq_driver->verify(new_policy);
1da177e4 2203 if (ret)
d9a789c7 2204 return ret;
1da177e4 2205
1da177e4 2206 /* adjust if necessary - all reasons */
e041c683 2207 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2208 CPUFREQ_ADJUST, new_policy);
1da177e4 2209
bb176f7d
VK
2210 /*
2211 * verify the cpu speed can be set within this limit, which might be
2212 * different to the first one
2213 */
3a3e9e06 2214 ret = cpufreq_driver->verify(new_policy);
e041c683 2215 if (ret)
d9a789c7 2216 return ret;
1da177e4
LT
2217
2218 /* notification of the new policy */
e041c683 2219 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2220 CPUFREQ_NOTIFY, new_policy);
1da177e4 2221
3a3e9e06
VK
2222 policy->min = new_policy->min;
2223 policy->max = new_policy->max;
1da177e4 2224
2d06d8c4 2225 pr_debug("new min and max freqs are %u - %u kHz\n",
e837f9b5 2226 policy->min, policy->max);
1da177e4 2227
1c3d85dd 2228 if (cpufreq_driver->setpolicy) {
3a3e9e06 2229 policy->policy = new_policy->policy;
2d06d8c4 2230 pr_debug("setting range\n");
d9a789c7
RW
2231 return cpufreq_driver->setpolicy(new_policy);
2232 }
1da177e4 2233
0a300767
RW
2234 if (new_policy->governor == policy->governor) {
2235 pr_debug("cpufreq: governor limits update\n");
a92604b4 2236 cpufreq_governor_limits(policy);
d6ff44d6 2237 return 0;
0a300767 2238 }
7bd353a9 2239
d9a789c7
RW
2240 pr_debug("governor switch\n");
2241
2242 /* save old, working values */
2243 old_gov = policy->governor;
2244 /* end old governor */
2245 if (old_gov) {
45482c70 2246 cpufreq_stop_governor(policy);
36be3418 2247 cpufreq_exit_governor(policy);
1da177e4
LT
2248 }
2249
d9a789c7
RW
2250 /* start new governor */
2251 policy->governor = new_policy->governor;
a92604b4 2252 ret = cpufreq_init_governor(policy);
4bc384ae 2253 if (!ret) {
0a300767
RW
2254 ret = cpufreq_start_governor(policy);
2255 if (!ret) {
2256 pr_debug("cpufreq: governor change\n");
2257 return 0;
2258 }
b7898fda 2259 cpufreq_exit_governor(policy);
d9a789c7
RW
2260 }
2261
2262 /* new governor failed, so re-start old one */
2263 pr_debug("starting governor %s failed\n", policy->governor->name);
2264 if (old_gov) {
2265 policy->governor = old_gov;
a92604b4 2266 if (cpufreq_init_governor(policy))
4bc384ae
VK
2267 policy->governor = NULL;
2268 else
0a300767 2269 cpufreq_start_governor(policy);
d9a789c7
RW
2270 }
2271
4bc384ae 2272 return ret;
1da177e4
LT
2273}
2274
1da177e4
LT
2275/**
2276 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2277 * @cpu: CPU which shall be re-evaluated
2278 *
25985edc 2279 * Useful for policy notifiers which have different necessities
1da177e4
LT
2280 * at different times.
2281 */
2282int cpufreq_update_policy(unsigned int cpu)
2283{
3a3e9e06
VK
2284 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2285 struct cpufreq_policy new_policy;
f1829e4a 2286 int ret;
1da177e4 2287
fefa8ff8
AP
2288 if (!policy)
2289 return -ENODEV;
1da177e4 2290
ad7722da 2291 down_write(&policy->rwsem);
1da177e4 2292
2d06d8c4 2293 pr_debug("updating policy for CPU %u\n", cpu);
d5b73cd8 2294 memcpy(&new_policy, policy, sizeof(*policy));
3a3e9e06
VK
2295 new_policy.min = policy->user_policy.min;
2296 new_policy.max = policy->user_policy.max;
1da177e4 2297
bb176f7d
VK
2298 /*
2299 * BIOS might change freq behind our back
2300 * -> ask driver for current freq and notify governors about a change
2301 */
2ed99e39 2302 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
999f5729 2303 new_policy.cur = cpufreq_update_current_freq(policy);
bd0fa9bb
VK
2304 if (WARN_ON(!new_policy.cur)) {
2305 ret = -EIO;
fefa8ff8 2306 goto unlock;
bd0fa9bb 2307 }
0961dd0d
TR
2308 }
2309
037ce839 2310 ret = cpufreq_set_policy(policy, &new_policy);
1da177e4 2311
fefa8ff8 2312unlock:
ad7722da 2313 up_write(&policy->rwsem);
5a01f2e8 2314
3a3e9e06 2315 cpufreq_cpu_put(policy);
1da177e4
LT
2316 return ret;
2317}
2318EXPORT_SYMBOL(cpufreq_update_policy);
2319
2760984f 2320static int cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
2321 unsigned long action, void *hcpu)
2322{
2323 unsigned int cpu = (unsigned long)hcpu;
c32b6b8e 2324
0b275352
RW
2325 switch (action & ~CPU_TASKS_FROZEN) {
2326 case CPU_ONLINE:
cd73e9b0 2327 case CPU_DOWN_FAILED:
0b275352
RW
2328 cpufreq_online(cpu);
2329 break;
5302c3fb 2330
0b275352 2331 case CPU_DOWN_PREPARE:
69cee714 2332 cpufreq_offline(cpu);
0b275352 2333 break;
c32b6b8e
AR
2334 }
2335 return NOTIFY_OK;
2336}
2337
9c36f746 2338static struct notifier_block __refdata cpufreq_cpu_notifier = {
bb176f7d 2339 .notifier_call = cpufreq_cpu_callback,
c32b6b8e 2340};
1da177e4 2341
6f19efc0
LM
2342/*********************************************************************
2343 * BOOST *
2344 *********************************************************************/
2345static int cpufreq_boost_set_sw(int state)
2346{
2347 struct cpufreq_frequency_table *freq_table;
2348 struct cpufreq_policy *policy;
2349 int ret = -EINVAL;
2350
f963735a 2351 for_each_active_policy(policy) {
6f19efc0
LM
2352 freq_table = cpufreq_frequency_get_table(policy->cpu);
2353 if (freq_table) {
2354 ret = cpufreq_frequency_table_cpuinfo(policy,
2355 freq_table);
2356 if (ret) {
2357 pr_err("%s: Policy frequency update failed\n",
2358 __func__);
2359 break;
2360 }
49f18560
VK
2361
2362 down_write(&policy->rwsem);
6f19efc0 2363 policy->user_policy.max = policy->max;
a92604b4 2364 cpufreq_governor_limits(policy);
49f18560 2365 up_write(&policy->rwsem);
6f19efc0
LM
2366 }
2367 }
2368
2369 return ret;
2370}
2371
2372int cpufreq_boost_trigger_state(int state)
2373{
2374 unsigned long flags;
2375 int ret = 0;
2376
2377 if (cpufreq_driver->boost_enabled == state)
2378 return 0;
2379
2380 write_lock_irqsave(&cpufreq_driver_lock, flags);
2381 cpufreq_driver->boost_enabled = state;
2382 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2383
2384 ret = cpufreq_driver->set_boost(state);
2385 if (ret) {
2386 write_lock_irqsave(&cpufreq_driver_lock, flags);
2387 cpufreq_driver->boost_enabled = !state;
2388 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2389
e837f9b5
JP
2390 pr_err("%s: Cannot %s BOOST\n",
2391 __func__, state ? "enable" : "disable");
6f19efc0
LM
2392 }
2393
2394 return ret;
2395}
2396
41669da0 2397static bool cpufreq_boost_supported(void)
6f19efc0 2398{
7a6c79f2 2399 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
6f19efc0 2400}
6f19efc0 2401
44139ed4
VK
2402static int create_boost_sysfs_file(void)
2403{
2404 int ret;
2405
c82bd444 2406 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
44139ed4
VK
2407 if (ret)
2408 pr_err("%s: cannot register global BOOST sysfs file\n",
2409 __func__);
2410
2411 return ret;
2412}
2413
2414static void remove_boost_sysfs_file(void)
2415{
2416 if (cpufreq_boost_supported())
c82bd444 2417 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
44139ed4
VK
2418}
2419
2420int cpufreq_enable_boost_support(void)
2421{
2422 if (!cpufreq_driver)
2423 return -EINVAL;
2424
2425 if (cpufreq_boost_supported())
2426 return 0;
2427
7a6c79f2 2428 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
44139ed4
VK
2429
2430 /* This will get removed on driver unregister */
2431 return create_boost_sysfs_file();
2432}
2433EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2434
6f19efc0
LM
2435int cpufreq_boost_enabled(void)
2436{
2437 return cpufreq_driver->boost_enabled;
2438}
2439EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2440
1da177e4
LT
2441/*********************************************************************
2442 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2443 *********************************************************************/
2444
2445/**
2446 * cpufreq_register_driver - register a CPU Frequency driver
2447 * @driver_data: A struct cpufreq_driver containing the values#
2448 * submitted by the CPU Frequency driver.
2449 *
bb176f7d 2450 * Registers a CPU Frequency driver to this core code. This code
63af4055 2451 * returns zero on success, -EEXIST when another driver got here first
32ee8c3e 2452 * (and isn't unregistered in the meantime).
1da177e4
LT
2453 *
2454 */
221dee28 2455int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
2456{
2457 unsigned long flags;
2458 int ret;
2459
a7b422cd
KRW
2460 if (cpufreq_disabled())
2461 return -ENODEV;
2462
1da177e4 2463 if (!driver_data || !driver_data->verify || !driver_data->init ||
9c0ebcf7 2464 !(driver_data->setpolicy || driver_data->target_index ||
9832235f
RW
2465 driver_data->target) ||
2466 (driver_data->setpolicy && (driver_data->target_index ||
1c03a2d0
VK
2467 driver_data->target)) ||
2468 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
1da177e4
LT
2469 return -EINVAL;
2470
2d06d8c4 2471 pr_debug("trying to register driver %s\n", driver_data->name);
1da177e4 2472
fdd320da
RW
2473 /* Protect against concurrent CPU online/offline. */
2474 get_online_cpus();
2475
0d1857a1 2476 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2477 if (cpufreq_driver) {
0d1857a1 2478 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
fdd320da
RW
2479 ret = -EEXIST;
2480 goto out;
1da177e4 2481 }
1c3d85dd 2482 cpufreq_driver = driver_data;
0d1857a1 2483 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 2484
bc68b7df
VK
2485 if (driver_data->setpolicy)
2486 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2487
7a6c79f2
RW
2488 if (cpufreq_boost_supported()) {
2489 ret = create_boost_sysfs_file();
2490 if (ret)
2491 goto err_null_driver;
2492 }
6f19efc0 2493
8a25a2fd 2494 ret = subsys_interface_register(&cpufreq_interface);
8f5bc2ab 2495 if (ret)
6f19efc0 2496 goto err_boost_unreg;
1da177e4 2497
ce1bcfe9
VK
2498 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2499 list_empty(&cpufreq_policy_list)) {
1da177e4 2500 /* if all ->init() calls failed, unregister */
ce1bcfe9
VK
2501 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2502 driver_data->name);
2503 goto err_if_unreg;
1da177e4
LT
2504 }
2505
8f5bc2ab 2506 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2d06d8c4 2507 pr_debug("driver %s up and running\n", driver_data->name);
3834abb4 2508 goto out;
fdd320da 2509
8a25a2fd
KS
2510err_if_unreg:
2511 subsys_interface_unregister(&cpufreq_interface);
6f19efc0 2512err_boost_unreg:
44139ed4 2513 remove_boost_sysfs_file();
8f5bc2ab 2514err_null_driver:
0d1857a1 2515 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2516 cpufreq_driver = NULL;
0d1857a1 2517 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
3834abb4
PG
2518out:
2519 put_online_cpus();
2520 return ret;
1da177e4
LT
2521}
2522EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2523
1da177e4
LT
2524/**
2525 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2526 *
bb176f7d 2527 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
2528 * the right to do so, i.e. if you have succeeded in initialising before!
2529 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2530 * currently not initialised.
2531 */
221dee28 2532int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
2533{
2534 unsigned long flags;
2535
1c3d85dd 2536 if (!cpufreq_driver || (driver != cpufreq_driver))
1da177e4 2537 return -EINVAL;
1da177e4 2538
2d06d8c4 2539 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 2540
454d3a25
SAS
2541 /* Protect against concurrent cpu hotplug */
2542 get_online_cpus();
8a25a2fd 2543 subsys_interface_unregister(&cpufreq_interface);
44139ed4 2544 remove_boost_sysfs_file();
65edc68c 2545 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4 2546
0d1857a1 2547 write_lock_irqsave(&cpufreq_driver_lock, flags);
6eed9404 2548
1c3d85dd 2549 cpufreq_driver = NULL;
6eed9404 2550
0d1857a1 2551 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
454d3a25 2552 put_online_cpus();
1da177e4
LT
2553
2554 return 0;
2555}
2556EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8 2557
90de2a4a
DA
2558/*
2559 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2560 * or mutexes when secondary CPUs are halted.
2561 */
2562static struct syscore_ops cpufreq_syscore_ops = {
2563 .shutdown = cpufreq_suspend,
2564};
2565
c82bd444
VK
2566struct kobject *cpufreq_global_kobject;
2567EXPORT_SYMBOL(cpufreq_global_kobject);
2568
5a01f2e8
VP
2569static int __init cpufreq_core_init(void)
2570{
a7b422cd
KRW
2571 if (cpufreq_disabled())
2572 return -ENODEV;
2573
8eec1020 2574 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
8aa84ad8
TR
2575 BUG_ON(!cpufreq_global_kobject);
2576
90de2a4a
DA
2577 register_syscore_ops(&cpufreq_syscore_ops);
2578
5a01f2e8
VP
2579 return 0;
2580}
5a01f2e8 2581core_initcall(cpufreq_core_init);