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