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