Merge back cpufreq material for v5.2.
[linux-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;
1c3d85dd
RW
897 if (cpufreq_driver->bios_limit) {
898 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
e2f74f35
TR
899 if (!ret)
900 return sprintf(buf, "%u\n", limit);
901 }
902 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
903}
904
6dad2a29
BP
905cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
906cpufreq_freq_attr_ro(cpuinfo_min_freq);
907cpufreq_freq_attr_ro(cpuinfo_max_freq);
908cpufreq_freq_attr_ro(cpuinfo_transition_latency);
909cpufreq_freq_attr_ro(scaling_available_governors);
910cpufreq_freq_attr_ro(scaling_driver);
911cpufreq_freq_attr_ro(scaling_cur_freq);
912cpufreq_freq_attr_ro(bios_limit);
913cpufreq_freq_attr_ro(related_cpus);
914cpufreq_freq_attr_ro(affected_cpus);
915cpufreq_freq_attr_rw(scaling_min_freq);
916cpufreq_freq_attr_rw(scaling_max_freq);
917cpufreq_freq_attr_rw(scaling_governor);
918cpufreq_freq_attr_rw(scaling_setspeed);
1da177e4 919
905d77cd 920static struct attribute *default_attrs[] = {
1da177e4
LT
921 &cpuinfo_min_freq.attr,
922 &cpuinfo_max_freq.attr,
ed129784 923 &cpuinfo_transition_latency.attr,
1da177e4
LT
924 &scaling_min_freq.attr,
925 &scaling_max_freq.attr,
926 &affected_cpus.attr,
e8628dd0 927 &related_cpus.attr,
1da177e4
LT
928 &scaling_governor.attr,
929 &scaling_driver.attr,
930 &scaling_available_governors.attr,
9e76988e 931 &scaling_setspeed.attr,
1da177e4
LT
932 NULL
933};
934
29464f28
DJ
935#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
936#define to_attr(a) container_of(a, struct freq_attr, attr)
1da177e4 937
29464f28 938static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1da177e4 939{
905d77cd
DJ
940 struct cpufreq_policy *policy = to_policy(kobj);
941 struct freq_attr *fattr = to_attr(attr);
1b750e3b 942 ssize_t ret;
6eed9404 943
ad7722da 944 down_read(&policy->rwsem);
6541aef0 945 ret = fattr->show(policy, buf);
ad7722da 946 up_read(&policy->rwsem);
1b750e3b 947
1da177e4
LT
948 return ret;
949}
950
905d77cd
DJ
951static ssize_t store(struct kobject *kobj, struct attribute *attr,
952 const char *buf, size_t count)
1da177e4 953{
905d77cd
DJ
954 struct cpufreq_policy *policy = to_policy(kobj);
955 struct freq_attr *fattr = to_attr(attr);
a07530b4 956 ssize_t ret = -EINVAL;
6eed9404 957
9b3d9bb3
WL
958 /*
959 * cpus_read_trylock() is used here to work around a circular lock
960 * dependency problem with respect to the cpufreq_register_driver().
961 */
962 if (!cpus_read_trylock())
963 return -EBUSY;
4f750c93 964
6541aef0
RW
965 if (cpu_online(policy->cpu)) {
966 down_write(&policy->rwsem);
e08f5f5b 967 ret = fattr->store(policy, buf, count);
6541aef0
RW
968 up_write(&policy->rwsem);
969 }
e08f5f5b 970
a92551e4 971 cpus_read_unlock();
4f750c93 972
1da177e4
LT
973 return ret;
974}
975
905d77cd 976static void cpufreq_sysfs_release(struct kobject *kobj)
1da177e4 977{
905d77cd 978 struct cpufreq_policy *policy = to_policy(kobj);
2d06d8c4 979 pr_debug("last reference is dropped\n");
1da177e4
LT
980 complete(&policy->kobj_unregister);
981}
982
52cf25d0 983static const struct sysfs_ops sysfs_ops = {
1da177e4
LT
984 .show = show,
985 .store = store,
986};
987
988static struct kobj_type ktype_cpufreq = {
989 .sysfs_ops = &sysfs_ops,
990 .default_attrs = default_attrs,
991 .release = cpufreq_sysfs_release,
992};
993
2f0ba790 994static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
87549141 995{
2f0ba790
RW
996 struct device *dev = get_cpu_device(cpu);
997
998 if (!dev)
999 return;
1000
1001 if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1002 return;
1003
26619804 1004 dev_dbg(dev, "%s: Adding symlink\n", __func__);
2f0ba790
RW
1005 if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
1006 dev_err(dev, "cpufreq symlink creation failed\n");
87549141
VK
1007}
1008
26619804
VK
1009static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
1010 struct device *dev)
87549141 1011{
26619804
VK
1012 dev_dbg(dev, "%s: Removing symlink\n", __func__);
1013 sysfs_remove_link(&dev->kobj, "cpufreq");
87549141
VK
1014}
1015
d9612a49 1016static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
909a694e
DJ
1017{
1018 struct freq_attr **drv_attr;
909a694e 1019 int ret = 0;
909a694e 1020
909a694e 1021 /* set up files for this cpu device */
1c3d85dd 1022 drv_attr = cpufreq_driver->attr;
f13f1184 1023 while (drv_attr && *drv_attr) {
909a694e
DJ
1024 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1025 if (ret)
6d4e81ed 1026 return ret;
909a694e
DJ
1027 drv_attr++;
1028 }
1c3d85dd 1029 if (cpufreq_driver->get) {
909a694e
DJ
1030 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1031 if (ret)
6d4e81ed 1032 return ret;
909a694e 1033 }
c034b02e
DB
1034
1035 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1036 if (ret)
6d4e81ed 1037 return ret;
c034b02e 1038
1c3d85dd 1039 if (cpufreq_driver->bios_limit) {
e2f74f35
TR
1040 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1041 if (ret)
6d4e81ed 1042 return ret;
e2f74f35 1043 }
909a694e 1044
26619804 1045 return 0;
e18f1682
SB
1046}
1047
de1df26b
RW
1048__weak struct cpufreq_governor *cpufreq_default_governor(void)
1049{
1050 return NULL;
1051}
1052
7f0fa40f 1053static int cpufreq_init_policy(struct cpufreq_policy *policy)
e18f1682 1054{
6e2c89d1 1055 struct cpufreq_governor *gov = NULL;
e18f1682 1056 struct cpufreq_policy new_policy;
e18f1682 1057
d5b73cd8 1058 memcpy(&new_policy, policy, sizeof(*policy));
a27a9ab7 1059
6e2c89d1 1060 /* Update governor of new_policy to the governor used before hotplug */
4573237b 1061 gov = find_governor(policy->last_governor);
de1df26b 1062 if (gov) {
6e2c89d1 1063 pr_debug("Restoring governor %s for cpu %d\n",
1064 policy->governor->name, policy->cpu);
de1df26b
RW
1065 } else {
1066 gov = cpufreq_default_governor();
1067 if (!gov)
1068 return -ENODATA;
1069 }
6e2c89d1 1070
1071 new_policy.governor = gov;
1072
69030dd1
SP
1073 /* Use the default policy if there is no last_policy. */
1074 if (cpufreq_driver->setpolicy) {
1075 if (policy->last_policy)
1076 new_policy.policy = policy->last_policy;
1077 else
ae0ff89f 1078 cpufreq_parse_governor(gov->name, &new_policy);
69030dd1 1079 }
ecf7e461 1080 /* set default policy */
7f0fa40f 1081 return cpufreq_set_policy(policy, &new_policy);
909a694e
DJ
1082}
1083
d9612a49 1084static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
fcf80582 1085{
9c0ebcf7 1086 int ret = 0;
fcf80582 1087
bb29ae15
VK
1088 /* Has this CPU been taken care of already? */
1089 if (cpumask_test_cpu(cpu, policy->cpus))
1090 return 0;
1091
49f18560 1092 down_write(&policy->rwsem);
45482c70
RW
1093 if (has_target())
1094 cpufreq_stop_governor(policy);
fcf80582 1095
fcf80582 1096 cpumask_set_cpu(cpu, policy->cpus);
2eaa3e2d 1097
9c0ebcf7 1098 if (has_target()) {
0a300767 1099 ret = cpufreq_start_governor(policy);
49f18560 1100 if (ret)
3de9bdeb 1101 pr_err("%s: Failed to start governor\n", __func__);
820c6ca2 1102 }
49f18560
VK
1103 up_write(&policy->rwsem);
1104 return ret;
fcf80582 1105}
1da177e4 1106
11eb69b9
VK
1107static void handle_update(struct work_struct *work)
1108{
1109 struct cpufreq_policy *policy =
1110 container_of(work, struct cpufreq_policy, update);
1111 unsigned int cpu = policy->cpu;
1112 pr_debug("handle_update for cpu %u called\n", cpu);
1113 cpufreq_update_policy(cpu);
fcf80582 1114}
1da177e4 1115
a34e63b1 1116static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
e9698cc5
SB
1117{
1118 struct cpufreq_policy *policy;
edd4a893 1119 int ret;
e9698cc5
SB
1120
1121 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1122 if (!policy)
1123 return NULL;
1124
1125 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1126 goto err_free_policy;
1127
1128 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1129 goto err_free_cpumask;
1130
559ed407
RW
1131 if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1132 goto err_free_rcpumask;
1133
edd4a893
VK
1134 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1135 cpufreq_global_kobject, "policy%u", cpu);
1136 if (ret) {
1137 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1138 goto err_free_real_cpus;
1139 }
1140
c88a1f8b 1141 INIT_LIST_HEAD(&policy->policy_list);
ad7722da 1142 init_rwsem(&policy->rwsem);
12478cf0
SB
1143 spin_lock_init(&policy->transition_lock);
1144 init_waitqueue_head(&policy->transition_wait);
818c5712
VK
1145 init_completion(&policy->kobj_unregister);
1146 INIT_WORK(&policy->update, handle_update);
ad7722da 1147
a34e63b1 1148 policy->cpu = cpu;
e9698cc5
SB
1149 return policy;
1150
edd4a893
VK
1151err_free_real_cpus:
1152 free_cpumask_var(policy->real_cpus);
2fc3384d
VK
1153err_free_rcpumask:
1154 free_cpumask_var(policy->related_cpus);
e9698cc5
SB
1155err_free_cpumask:
1156 free_cpumask_var(policy->cpus);
1157err_free_policy:
1158 kfree(policy);
1159
1160 return NULL;
1161}
1162
f9f41e3e 1163static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
42f921a6
VK
1164{
1165 struct kobject *kobj;
1166 struct completion *cmp;
1167
87549141 1168 down_write(&policy->rwsem);
1aefc75b 1169 cpufreq_stats_free_table(policy);
42f921a6
VK
1170 kobj = &policy->kobj;
1171 cmp = &policy->kobj_unregister;
87549141 1172 up_write(&policy->rwsem);
42f921a6
VK
1173 kobject_put(kobj);
1174
1175 /*
1176 * We need to make sure that the underlying kobj is
1177 * actually not referenced anymore by anybody before we
1178 * proceed with unloading.
1179 */
1180 pr_debug("waiting for dropping of refcount\n");
1181 wait_for_completion(cmp);
1182 pr_debug("wait complete\n");
1183}
1184
f9f41e3e 1185static void cpufreq_policy_free(struct cpufreq_policy *policy)
e9698cc5 1186{
988bed09
VK
1187 unsigned long flags;
1188 int cpu;
1189
1190 /* Remove policy from list */
1191 write_lock_irqsave(&cpufreq_driver_lock, flags);
1192 list_del(&policy->policy_list);
1193
1194 for_each_cpu(cpu, policy->related_cpus)
1195 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1196 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1197
f9f41e3e 1198 cpufreq_policy_put_kobj(policy);
559ed407 1199 free_cpumask_var(policy->real_cpus);
e9698cc5
SB
1200 free_cpumask_var(policy->related_cpus);
1201 free_cpumask_var(policy->cpus);
1202 kfree(policy);
1203}
1204
0b275352 1205static int cpufreq_online(unsigned int cpu)
1da177e4 1206{
7f0c020a 1207 struct cpufreq_policy *policy;
194d99c7 1208 bool new_policy;
1da177e4 1209 unsigned long flags;
0b275352
RW
1210 unsigned int j;
1211 int ret;
87549141 1212
0b275352 1213 pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
6eed9404 1214
bb29ae15 1215 /* Check if this CPU already has a policy to manage it */
9104bb26 1216 policy = per_cpu(cpufreq_cpu_data, cpu);
11ce707e 1217 if (policy) {
9104bb26 1218 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
11ce707e 1219 if (!policy_is_inactive(policy))
d9612a49 1220 return cpufreq_add_policy_cpu(policy, cpu);
1da177e4 1221
11ce707e 1222 /* This is the only online CPU for the policy. Start over. */
194d99c7 1223 new_policy = false;
11ce707e
RW
1224 down_write(&policy->rwsem);
1225 policy->cpu = cpu;
1226 policy->governor = NULL;
1227 up_write(&policy->rwsem);
1228 } else {
194d99c7 1229 new_policy = true;
a34e63b1 1230 policy = cpufreq_policy_alloc(cpu);
72368d12 1231 if (!policy)
d4d854d6 1232 return -ENOMEM;
72368d12 1233 }
0d66b91e 1234
91a12e91
VK
1235 if (!new_policy && cpufreq_driver->online) {
1236 ret = cpufreq_driver->online(policy);
1237 if (ret) {
1238 pr_debug("%s: %d: initialization failed\n", __func__,
1239 __LINE__);
1240 goto out_exit_policy;
1241 }
1da177e4 1242
91a12e91
VK
1243 /* Recover policy->cpus using related_cpus */
1244 cpumask_copy(policy->cpus, policy->related_cpus);
1245 } else {
1246 cpumask_copy(policy->cpus, cpumask_of(cpu));
643ae6e8 1247
91a12e91
VK
1248 /*
1249 * Call driver. From then on the cpufreq must be able
1250 * to accept all calls to ->verify and ->setpolicy for this CPU.
1251 */
1252 ret = cpufreq_driver->init(policy);
1253 if (ret) {
1254 pr_debug("%s: %d: initialization failed\n", __func__,
1255 __LINE__);
1256 goto out_free_policy;
1257 }
d417e069 1258
91a12e91
VK
1259 ret = cpufreq_table_validate_and_sort(policy);
1260 if (ret)
1261 goto out_exit_policy;
6d4e81ed 1262
4d1f3a5b 1263 /* related_cpus should at least include policy->cpus. */
0998a03a 1264 cpumask_copy(policy->related_cpus, policy->cpus);
4d1f3a5b 1265 }
559ed407 1266
91a12e91 1267 down_write(&policy->rwsem);
5a7e56a5
VK
1268 /*
1269 * affected cpus must always be the one, which are online. We aren't
1270 * managing offline cpus here.
1271 */
1272 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1273
194d99c7 1274 if (new_policy) {
5a7e56a5
VK
1275 policy->user_policy.min = policy->min;
1276 policy->user_policy.max = policy->max;
6d4e81ed 1277
2f0ba790 1278 for_each_cpu(j, policy->related_cpus) {
988bed09 1279 per_cpu(cpufreq_cpu_data, j) = policy;
2f0ba790
RW
1280 add_cpu_dev_symlink(policy, j);
1281 }
ff010472
VK
1282 } else {
1283 policy->min = policy->user_policy.min;
1284 policy->max = policy->user_policy.max;
988bed09 1285 }
652ed95d 1286
2ed99e39 1287 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
da60ce9f
VK
1288 policy->cur = cpufreq_driver->get(policy->cpu);
1289 if (!policy->cur) {
1290 pr_err("%s: ->get() failed\n", __func__);
d417e069 1291 goto out_destroy_policy;
da60ce9f
VK
1292 }
1293 }
1294
d3916691
VK
1295 /*
1296 * Sometimes boot loaders set CPU frequency to a value outside of
1297 * frequency table present with cpufreq core. In such cases CPU might be
1298 * unstable if it has to run on that frequency for long duration of time
1299 * and so its better to set it to a frequency which is specified in
1300 * freq-table. This also makes cpufreq stats inconsistent as
1301 * cpufreq-stats would fail to register because current frequency of CPU
1302 * isn't found in freq-table.
1303 *
1304 * Because we don't want this change to effect boot process badly, we go
1305 * for the next freq which is >= policy->cur ('cur' must be set by now,
1306 * otherwise we will end up setting freq to lowest of the table as 'cur'
1307 * is initialized to zero).
1308 *
1309 * We are passing target-freq as "policy->cur - 1" otherwise
1310 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1311 * equal to target-freq.
1312 */
1313 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1314 && has_target()) {
1315 /* Are we running at unknown frequency ? */
1316 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1317 if (ret == -EINVAL) {
1318 /* Warn user and fix it */
1319 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1320 __func__, policy->cpu, policy->cur);
1321 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1322 CPUFREQ_RELATION_L);
1323
1324 /*
1325 * Reaching here after boot in a few seconds may not
1326 * mean that system will remain stable at "unknown"
1327 * frequency for longer duration. Hence, a BUG_ON().
1328 */
1329 BUG_ON(ret);
1330 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1331 __func__, policy->cpu, policy->cur);
1332 }
1333 }
1334
194d99c7 1335 if (new_policy) {
d9612a49 1336 ret = cpufreq_add_dev_interface(policy);
a82fab29 1337 if (ret)
d417e069 1338 goto out_destroy_policy;
1aefc75b
RW
1339
1340 cpufreq_stats_create_table(policy);
8ff69732 1341
988bed09
VK
1342 write_lock_irqsave(&cpufreq_driver_lock, flags);
1343 list_add(&policy->policy_list, &cpufreq_policy_list);
1344 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1345 }
9515f4d6 1346
7f0fa40f
VK
1347 ret = cpufreq_init_policy(policy);
1348 if (ret) {
1349 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1350 __func__, cpu, ret);
d417e069 1351 goto out_destroy_policy;
08fd8c1c 1352 }
e18f1682 1353
4e97b631 1354 up_write(&policy->rwsem);
08fd8c1c 1355
038c5b3e 1356 kobject_uevent(&policy->kobj, KOBJ_ADD);
7c45cf31 1357
7c45cf31
VK
1358 /* Callback for handling stuff after policy is ready */
1359 if (cpufreq_driver->ready)
1360 cpufreq_driver->ready(policy);
1361
5c238a8b
AK
1362 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1363 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV)
1364 policy->cdev = of_cpufreq_cooling_register(policy);
1365
2d06d8c4 1366 pr_debug("initialization complete\n");
87c32271 1367
1da177e4
LT
1368 return 0;
1369
d417e069 1370out_destroy_policy:
b24b6478
VK
1371 for_each_cpu(j, policy->real_cpus)
1372 remove_cpu_dev_symlink(policy, get_cpu_device(j));
1373
7106e02b
PB
1374 up_write(&policy->rwsem);
1375
d417e069 1376out_exit_policy:
da60ce9f
VK
1377 if (cpufreq_driver->exit)
1378 cpufreq_driver->exit(policy);
2f0ba790 1379
8101f997 1380out_free_policy:
f9f41e3e 1381 cpufreq_policy_free(policy);
1da177e4
LT
1382 return ret;
1383}
1384
0b275352
RW
1385/**
1386 * cpufreq_add_dev - the cpufreq interface for a CPU device.
1387 * @dev: CPU device.
1388 * @sif: Subsystem interface structure pointer (not used)
1389 */
1390static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1391{
a794d613 1392 struct cpufreq_policy *policy;
0b275352 1393 unsigned cpu = dev->id;
26619804 1394 int ret;
0b275352
RW
1395
1396 dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1397
26619804
VK
1398 if (cpu_online(cpu)) {
1399 ret = cpufreq_online(cpu);
1400 if (ret)
1401 return ret;
1402 }
0b275352 1403
26619804 1404 /* Create sysfs link on CPU registration */
a794d613 1405 policy = per_cpu(cpufreq_cpu_data, cpu);
2f0ba790
RW
1406 if (policy)
1407 add_cpu_dev_symlink(policy, cpu);
26619804 1408
2f0ba790 1409 return 0;
1da177e4
LT
1410}
1411
27622b06 1412static int cpufreq_offline(unsigned int cpu)
1da177e4 1413{
3a3e9e06 1414 struct cpufreq_policy *policy;
69cee714 1415 int ret;
1da177e4 1416
b8eed8af 1417 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1da177e4 1418
988bed09 1419 policy = cpufreq_cpu_get_raw(cpu);
3a3e9e06 1420 if (!policy) {
b8eed8af 1421 pr_debug("%s: No cpu_data found\n", __func__);
27622b06 1422 return 0;
1da177e4 1423 }
1da177e4 1424
49f18560 1425 down_write(&policy->rwsem);
45482c70
RW
1426 if (has_target())
1427 cpufreq_stop_governor(policy);
1da177e4 1428
9591becb 1429 cpumask_clear_cpu(cpu, policy->cpus);
4573237b 1430
9591becb
VK
1431 if (policy_is_inactive(policy)) {
1432 if (has_target())
1433 strncpy(policy->last_governor, policy->governor->name,
1434 CPUFREQ_NAME_LEN);
69030dd1
SP
1435 else
1436 policy->last_policy = policy->policy;
9591becb
VK
1437 } else if (cpu == policy->cpu) {
1438 /* Nominate new CPU */
1439 policy->cpu = cpumask_any(policy->cpus);
1440 }
084f3493 1441
9591becb
VK
1442 /* Start governor again for active policy */
1443 if (!policy_is_inactive(policy)) {
1444 if (has_target()) {
0a300767 1445 ret = cpufreq_start_governor(policy);
9591becb
VK
1446 if (ret)
1447 pr_err("%s: Failed to start governor\n", __func__);
1448 }
cedb70af 1449
49f18560 1450 goto unlock;
cedb70af
SB
1451 }
1452
5c238a8b
AK
1453 if (IS_ENABLED(CONFIG_CPU_THERMAL) &&
1454 cpufreq_driver->flags & CPUFREQ_IS_COOLING_DEV) {
1455 cpufreq_cooling_unregister(policy->cdev);
1456 policy->cdev = NULL;
1457 }
1458
69cee714
VK
1459 if (cpufreq_driver->stop_cpu)
1460 cpufreq_driver->stop_cpu(policy);
87549141 1461
36be3418
RW
1462 if (has_target())
1463 cpufreq_exit_governor(policy);
1da177e4 1464
87549141 1465 /*
91a12e91
VK
1466 * Perform the ->offline() during light-weight tear-down, as
1467 * that allows fast recovery when the CPU comes back.
87549141 1468 */
91a12e91
VK
1469 if (cpufreq_driver->offline) {
1470 cpufreq_driver->offline(policy);
1471 } else if (cpufreq_driver->exit) {
87549141 1472 cpufreq_driver->exit(policy);
55582bcc
SP
1473 policy->freq_table = NULL;
1474 }
49f18560
VK
1475
1476unlock:
1477 up_write(&policy->rwsem);
27622b06 1478 return 0;
1da177e4
LT
1479}
1480
cedb70af 1481/**
27a862e9 1482 * cpufreq_remove_dev - remove a CPU device
cedb70af
SB
1483 *
1484 * Removes the cpufreq interface for a CPU device.
cedb70af 1485 */
71db87ba 1486static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
5a01f2e8 1487{
8a25a2fd 1488 unsigned int cpu = dev->id;
559ed407 1489 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
87549141 1490
559ed407 1491 if (!policy)
1af115d6 1492 return;
87549141 1493
69cee714
VK
1494 if (cpu_online(cpu))
1495 cpufreq_offline(cpu);
87549141 1496
559ed407 1497 cpumask_clear_cpu(cpu, policy->real_cpus);
26619804 1498 remove_cpu_dev_symlink(policy, dev);
87549141 1499
91a12e91
VK
1500 if (cpumask_empty(policy->real_cpus)) {
1501 /* We did light-weight exit earlier, do full tear down now */
1502 if (cpufreq_driver->offline)
1503 cpufreq_driver->exit(policy);
1504
f9f41e3e 1505 cpufreq_policy_free(policy);
91a12e91 1506 }
5a01f2e8
VP
1507}
1508
1da177e4 1509/**
bb176f7d
VK
1510 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1511 * in deep trouble.
a1e1dc41 1512 * @policy: policy managing CPUs
1da177e4
LT
1513 * @new_freq: CPU frequency the CPU actually runs at
1514 *
29464f28
DJ
1515 * We adjust to current frequency first, and need to clean up later.
1516 * So either call to cpufreq_update_policy() or schedule handle_update()).
1da177e4 1517 */
a1e1dc41 1518static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
e08f5f5b 1519 unsigned int new_freq)
1da177e4
LT
1520{
1521 struct cpufreq_freqs freqs;
b43a7ffb 1522
e837f9b5 1523 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
a1e1dc41 1524 policy->cur, new_freq);
1da177e4 1525
a1e1dc41 1526 freqs.old = policy->cur;
1da177e4 1527 freqs.new = new_freq;
b43a7ffb 1528
8fec051e
VK
1529 cpufreq_freq_transition_begin(policy, &freqs);
1530 cpufreq_freq_transition_end(policy, &freqs, 0);
1da177e4
LT
1531}
1532
32ee8c3e 1533/**
4ab70df4 1534 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
95235ca2
VP
1535 * @cpu: CPU number
1536 *
1537 * This is the last known freq, without actually getting it from the driver.
1538 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1539 */
1540unsigned int cpufreq_quick_get(unsigned int cpu)
1541{
9e21ba8b 1542 struct cpufreq_policy *policy;
e08f5f5b 1543 unsigned int ret_freq = 0;
c75361c0 1544 unsigned long flags;
95235ca2 1545
c75361c0
RC
1546 read_lock_irqsave(&cpufreq_driver_lock, flags);
1547
1548 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1549 ret_freq = cpufreq_driver->get(cpu);
1550 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1551 return ret_freq;
1552 }
1553
1554 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
9e21ba8b
DB
1555
1556 policy = cpufreq_cpu_get(cpu);
95235ca2 1557 if (policy) {
e08f5f5b 1558 ret_freq = policy->cur;
95235ca2
VP
1559 cpufreq_cpu_put(policy);
1560 }
1561
4d34a67d 1562 return ret_freq;
95235ca2
VP
1563}
1564EXPORT_SYMBOL(cpufreq_quick_get);
1565
3d737108
JB
1566/**
1567 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1568 * @cpu: CPU number
1569 *
1570 * Just return the max possible frequency for a given CPU.
1571 */
1572unsigned int cpufreq_quick_get_max(unsigned int cpu)
1573{
1574 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1575 unsigned int ret_freq = 0;
1576
1577 if (policy) {
1578 ret_freq = policy->max;
1579 cpufreq_cpu_put(policy);
1580 }
1581
1582 return ret_freq;
1583}
1584EXPORT_SYMBOL(cpufreq_quick_get_max);
1585
d92d50a4 1586static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1da177e4 1587{
e08f5f5b 1588 unsigned int ret_freq = 0;
5800043b 1589
2f661962 1590 if (unlikely(policy_is_inactive(policy)) || !cpufreq_driver->get)
4d34a67d 1591 return ret_freq;
1da177e4 1592
d92d50a4 1593 ret_freq = cpufreq_driver->get(policy->cpu);
1da177e4 1594
b7898fda 1595 /*
2f661962 1596 * If fast frequency switching is used with the given policy, the check
b7898fda
RW
1597 * against policy->cur is pointless, so skip it in that case too.
1598 */
2f661962 1599 if (policy->fast_switch_enabled)
11e584cf
VK
1600 return ret_freq;
1601
e08f5f5b 1602 if (ret_freq && policy->cur &&
1c3d85dd 1603 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e08f5f5b
GS
1604 /* verify no discrepancy between actual and
1605 saved value exists */
1606 if (unlikely(ret_freq != policy->cur)) {
a1e1dc41 1607 cpufreq_out_of_sync(policy, ret_freq);
1da177e4
LT
1608 schedule_work(&policy->update);
1609 }
1610 }
1611
4d34a67d 1612 return ret_freq;
5a01f2e8 1613}
1da177e4 1614
5a01f2e8
VP
1615/**
1616 * cpufreq_get - get the current CPU frequency (in kHz)
1617 * @cpu: CPU number
1618 *
1619 * Get the CPU current (static) CPU frequency
1620 */
1621unsigned int cpufreq_get(unsigned int cpu)
1622{
999976e0 1623 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
5a01f2e8 1624 unsigned int ret_freq = 0;
5a01f2e8 1625
999976e0
AP
1626 if (policy) {
1627 down_read(&policy->rwsem);
2f661962 1628 ret_freq = __cpufreq_get(policy);
999976e0 1629 up_read(&policy->rwsem);
5a01f2e8 1630
999976e0
AP
1631 cpufreq_cpu_put(policy);
1632 }
6eed9404 1633
4d34a67d 1634 return ret_freq;
1da177e4
LT
1635}
1636EXPORT_SYMBOL(cpufreq_get);
1637
999f5729
RW
1638static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1639{
1640 unsigned int new_freq;
1641
1642 new_freq = cpufreq_driver->get(policy->cpu);
1643 if (!new_freq)
1644 return 0;
1645
1646 if (!policy->cur) {
1647 pr_debug("cpufreq: Driver did not initialize current freq\n");
1648 policy->cur = new_freq;
1649 } else if (policy->cur != new_freq && has_target()) {
1650 cpufreq_out_of_sync(policy, new_freq);
1651 }
1652
1653 return new_freq;
1654}
1655
8a25a2fd
KS
1656static struct subsys_interface cpufreq_interface = {
1657 .name = "cpufreq",
1658 .subsys = &cpu_subsys,
1659 .add_dev = cpufreq_add_dev,
1660 .remove_dev = cpufreq_remove_dev,
e00e56df
RW
1661};
1662
e28867ea
VK
1663/*
1664 * In case platform wants some specific frequency to be configured
1665 * during suspend..
1666 */
1667int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1668{
1669 int ret;
1670
1671 if (!policy->suspend_freq) {
201f3716
BZ
1672 pr_debug("%s: suspend_freq not defined\n", __func__);
1673 return 0;
e28867ea
VK
1674 }
1675
1676 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1677 policy->suspend_freq);
1678
1679 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1680 CPUFREQ_RELATION_H);
1681 if (ret)
1682 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1683 __func__, policy->suspend_freq, ret);
1684
1685 return ret;
1686}
1687EXPORT_SYMBOL(cpufreq_generic_suspend);
1688
42d4dc3f 1689/**
2f0aea93 1690 * cpufreq_suspend() - Suspend CPUFreq governors
e00e56df 1691 *
2f0aea93
VK
1692 * Called during system wide Suspend/Hibernate cycles for suspending governors
1693 * as some platforms can't change frequency after this point in suspend cycle.
1694 * Because some of the devices (like: i2c, regulators, etc) they use for
1695 * changing frequency are suspended quickly after this point.
42d4dc3f 1696 */
2f0aea93 1697void cpufreq_suspend(void)
42d4dc3f 1698{
3a3e9e06 1699 struct cpufreq_policy *policy;
42d4dc3f 1700
2f0aea93
VK
1701 if (!cpufreq_driver)
1702 return;
42d4dc3f 1703
ba41e1bc 1704 if (!has_target() && !cpufreq_driver->suspend)
b1b12bab 1705 goto suspend;
42d4dc3f 1706
2f0aea93
VK
1707 pr_debug("%s: Suspending Governors\n", __func__);
1708
f963735a 1709 for_each_active_policy(policy) {
ba41e1bc
RW
1710 if (has_target()) {
1711 down_write(&policy->rwsem);
45482c70 1712 cpufreq_stop_governor(policy);
ba41e1bc 1713 up_write(&policy->rwsem);
ba41e1bc
RW
1714 }
1715
1716 if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
2f0aea93
VK
1717 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1718 policy);
42d4dc3f 1719 }
b1b12bab
VK
1720
1721suspend:
1722 cpufreq_suspended = true;
42d4dc3f
BH
1723}
1724
1da177e4 1725/**
2f0aea93 1726 * cpufreq_resume() - Resume CPUFreq governors
1da177e4 1727 *
2f0aea93
VK
1728 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1729 * are suspended with cpufreq_suspend().
1da177e4 1730 */
2f0aea93 1731void cpufreq_resume(void)
1da177e4 1732{
3a3e9e06 1733 struct cpufreq_policy *policy;
49f18560 1734 int ret;
1da177e4 1735
2f0aea93 1736 if (!cpufreq_driver)
703cbaa6
BY
1737 return;
1738
1739 if (unlikely(!cpufreq_suspended))
2f0aea93 1740 return;
1da177e4 1741
8e30444e
LT
1742 cpufreq_suspended = false;
1743
ba41e1bc 1744 if (!has_target() && !cpufreq_driver->resume)
e00e56df 1745 return;
1da177e4 1746
2f0aea93 1747 pr_debug("%s: Resuming Governors\n", __func__);
1da177e4 1748
f963735a 1749 for_each_active_policy(policy) {
49f18560 1750 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
0c5aa405
VK
1751 pr_err("%s: Failed to resume driver: %p\n", __func__,
1752 policy);
ba41e1bc 1753 } else if (has_target()) {
49f18560 1754 down_write(&policy->rwsem);
0a300767 1755 ret = cpufreq_start_governor(policy);
49f18560
VK
1756 up_write(&policy->rwsem);
1757
1758 if (ret)
1759 pr_err("%s: Failed to start governor for policy: %p\n",
1760 __func__, policy);
1761 }
2f0aea93
VK
1762 }
1763}
1da177e4 1764
9d95046e
BP
1765/**
1766 * cpufreq_get_current_driver - return current driver's name
1767 *
1768 * Return the name string of the currently loaded cpufreq driver
1769 * or NULL, if none.
1770 */
1771const char *cpufreq_get_current_driver(void)
1772{
1c3d85dd
RW
1773 if (cpufreq_driver)
1774 return cpufreq_driver->name;
1775
1776 return NULL;
9d95046e
BP
1777}
1778EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1da177e4 1779
51315cdf
TP
1780/**
1781 * cpufreq_get_driver_data - return current driver data
1782 *
1783 * Return the private data of the currently loaded cpufreq
1784 * driver, or NULL if no cpufreq driver is loaded.
1785 */
1786void *cpufreq_get_driver_data(void)
1787{
1788 if (cpufreq_driver)
1789 return cpufreq_driver->driver_data;
1790
1791 return NULL;
1792}
1793EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1794
1da177e4
LT
1795/*********************************************************************
1796 * NOTIFIER LISTS INTERFACE *
1797 *********************************************************************/
1798
1799/**
1800 * cpufreq_register_notifier - register a driver with cpufreq
1801 * @nb: notifier function to register
1802 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1803 *
32ee8c3e 1804 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1805 * are notified about clock rate changes (once before and once after
1806 * the transition), or a list of drivers that are notified about
1807 * changes in cpufreq policy.
1808 *
1809 * This function may sleep, and has the same return conditions as
e041c683 1810 * blocking_notifier_chain_register.
1da177e4
LT
1811 */
1812int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1813{
1814 int ret;
1815
d5aaffa9
DB
1816 if (cpufreq_disabled())
1817 return -EINVAL;
1818
1da177e4
LT
1819 switch (list) {
1820 case CPUFREQ_TRANSITION_NOTIFIER:
b7898fda
RW
1821 mutex_lock(&cpufreq_fast_switch_lock);
1822
1823 if (cpufreq_fast_switch_count > 0) {
1824 mutex_unlock(&cpufreq_fast_switch_lock);
1825 return -EBUSY;
1826 }
b4dfdbb3 1827 ret = srcu_notifier_chain_register(
e041c683 1828 &cpufreq_transition_notifier_list, nb);
b7898fda
RW
1829 if (!ret)
1830 cpufreq_fast_switch_count--;
1831
1832 mutex_unlock(&cpufreq_fast_switch_lock);
1da177e4
LT
1833 break;
1834 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1835 ret = blocking_notifier_chain_register(
1836 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1837 break;
1838 default:
1839 ret = -EINVAL;
1840 }
1da177e4
LT
1841
1842 return ret;
1843}
1844EXPORT_SYMBOL(cpufreq_register_notifier);
1845
1da177e4
LT
1846/**
1847 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1848 * @nb: notifier block to be unregistered
bb176f7d 1849 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1da177e4
LT
1850 *
1851 * Remove a driver from the CPU frequency notifier list.
1852 *
1853 * This function may sleep, and has the same return conditions as
e041c683 1854 * blocking_notifier_chain_unregister.
1da177e4
LT
1855 */
1856int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1857{
1858 int ret;
1859
d5aaffa9
DB
1860 if (cpufreq_disabled())
1861 return -EINVAL;
1862
1da177e4
LT
1863 switch (list) {
1864 case CPUFREQ_TRANSITION_NOTIFIER:
b7898fda
RW
1865 mutex_lock(&cpufreq_fast_switch_lock);
1866
b4dfdbb3 1867 ret = srcu_notifier_chain_unregister(
e041c683 1868 &cpufreq_transition_notifier_list, nb);
b7898fda
RW
1869 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1870 cpufreq_fast_switch_count++;
1871
1872 mutex_unlock(&cpufreq_fast_switch_lock);
1da177e4
LT
1873 break;
1874 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1875 ret = blocking_notifier_chain_unregister(
1876 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1877 break;
1878 default:
1879 ret = -EINVAL;
1880 }
1da177e4
LT
1881
1882 return ret;
1883}
1884EXPORT_SYMBOL(cpufreq_unregister_notifier);
1885
1886
1887/*********************************************************************
1888 * GOVERNORS *
1889 *********************************************************************/
1890
b7898fda
RW
1891/**
1892 * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1893 * @policy: cpufreq policy to switch the frequency for.
1894 * @target_freq: New frequency to set (may be approximate).
1895 *
1896 * Carry out a fast frequency switch without sleeping.
1897 *
1898 * The driver's ->fast_switch() callback invoked by this function must be
1899 * suitable for being called from within RCU-sched read-side critical sections
1900 * and it is expected to select the minimum available frequency greater than or
1901 * equal to @target_freq (CPUFREQ_RELATION_L).
1902 *
1903 * This function must not be called if policy->fast_switch_enabled is unset.
1904 *
1905 * Governors calling this function must guarantee that it will never be invoked
1906 * twice in parallel for the same policy and that it will never be called in
1907 * parallel with either ->target() or ->target_index() for the same policy.
1908 *
209887e6
VK
1909 * Returns the actual frequency set for the CPU.
1910 *
1911 * If 0 is returned by the driver's ->fast_switch() callback to indicate an
1912 * error condition, the hardware configuration must be preserved.
b7898fda
RW
1913 */
1914unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1915 unsigned int target_freq)
1916{
b9af6948 1917 target_freq = clamp_val(target_freq, policy->min, policy->max);
b7898fda
RW
1918
1919 return cpufreq_driver->fast_switch(policy, target_freq);
1920}
1921EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1922
1c03a2d0
VK
1923/* Must set freqs->new to intermediate frequency */
1924static int __target_intermediate(struct cpufreq_policy *policy,
1925 struct cpufreq_freqs *freqs, int index)
1926{
1927 int ret;
1928
1929 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1930
1931 /* We don't need to switch to intermediate freq */
1932 if (!freqs->new)
1933 return 0;
1934
1935 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1936 __func__, policy->cpu, freqs->old, freqs->new);
1937
1938 cpufreq_freq_transition_begin(policy, freqs);
1939 ret = cpufreq_driver->target_intermediate(policy, index);
1940 cpufreq_freq_transition_end(policy, freqs, ret);
1941
1942 if (ret)
1943 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1944 __func__, ret);
1945
1946 return ret;
1947}
1948
23727845 1949static int __target_index(struct cpufreq_policy *policy, int index)
8d65775d 1950{
1c03a2d0
VK
1951 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1952 unsigned int intermediate_freq = 0;
23727845 1953 unsigned int newfreq = policy->freq_table[index].frequency;
8d65775d
VK
1954 int retval = -EINVAL;
1955 bool notify;
1956
23727845
VK
1957 if (newfreq == policy->cur)
1958 return 0;
1959
8d65775d 1960 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
8d65775d 1961 if (notify) {
1c03a2d0
VK
1962 /* Handle switching to intermediate frequency */
1963 if (cpufreq_driver->get_intermediate) {
1964 retval = __target_intermediate(policy, &freqs, index);
1965 if (retval)
1966 return retval;
1967
1968 intermediate_freq = freqs.new;
1969 /* Set old freq to intermediate */
1970 if (intermediate_freq)
1971 freqs.old = freqs.new;
1972 }
8d65775d 1973
23727845 1974 freqs.new = newfreq;
8d65775d
VK
1975 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1976 __func__, policy->cpu, freqs.old, freqs.new);
1977
1978 cpufreq_freq_transition_begin(policy, &freqs);
1979 }
1980
1981 retval = cpufreq_driver->target_index(policy, index);
1982 if (retval)
1983 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1984 retval);
1985
1c03a2d0 1986 if (notify) {
8d65775d
VK
1987 cpufreq_freq_transition_end(policy, &freqs, retval);
1988
1c03a2d0
VK
1989 /*
1990 * Failed after setting to intermediate freq? Driver should have
1991 * reverted back to initial frequency and so should we. Check
1992 * here for intermediate_freq instead of get_intermediate, in
58405af6 1993 * case we haven't switched to intermediate freq at all.
1c03a2d0
VK
1994 */
1995 if (unlikely(retval && intermediate_freq)) {
1996 freqs.old = intermediate_freq;
1997 freqs.new = policy->restore_freq;
1998 cpufreq_freq_transition_begin(policy, &freqs);
1999 cpufreq_freq_transition_end(policy, &freqs, 0);
2000 }
2001 }
2002
8d65775d
VK
2003 return retval;
2004}
2005
1da177e4
LT
2006int __cpufreq_driver_target(struct cpufreq_policy *policy,
2007 unsigned int target_freq,
2008 unsigned int relation)
2009{
7249924e 2010 unsigned int old_target_freq = target_freq;
d218ed77 2011 int index;
c32b6b8e 2012
a7b422cd
KRW
2013 if (cpufreq_disabled())
2014 return -ENODEV;
2015
7249924e 2016 /* Make sure that target_freq is within supported range */
910c6e88 2017 target_freq = clamp_val(target_freq, policy->min, policy->max);
7249924e
VK
2018
2019 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
e837f9b5 2020 policy->cpu, target_freq, relation, old_target_freq);
5a1c0228 2021
9c0ebcf7
VK
2022 /*
2023 * This might look like a redundant call as we are checking it again
2024 * after finding index. But it is left intentionally for cases where
2025 * exactly same freq is called again and so we can save on few function
2026 * calls.
2027 */
5a1c0228
VK
2028 if (target_freq == policy->cur)
2029 return 0;
2030
1c03a2d0
VK
2031 /* Save last value to restore later on errors */
2032 policy->restore_freq = policy->cur;
2033
1c3d85dd 2034 if (cpufreq_driver->target)
6019d23a 2035 return cpufreq_driver->target(policy, target_freq, relation);
9c0ebcf7 2036
6019d23a
RW
2037 if (!cpufreq_driver->target_index)
2038 return -EINVAL;
9c0ebcf7 2039
d218ed77 2040 index = cpufreq_frequency_table_target(policy, target_freq, relation);
6019d23a 2041
23727845 2042 return __target_index(policy, index);
1da177e4
LT
2043}
2044EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2045
1da177e4
LT
2046int cpufreq_driver_target(struct cpufreq_policy *policy,
2047 unsigned int target_freq,
2048 unsigned int relation)
2049{
f1829e4a 2050 int ret = -EINVAL;
1da177e4 2051
ad7722da 2052 down_write(&policy->rwsem);
1da177e4
LT
2053
2054 ret = __cpufreq_driver_target(policy, target_freq, relation);
2055
ad7722da 2056 up_write(&policy->rwsem);
1da177e4 2057
1da177e4
LT
2058 return ret;
2059}
2060EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2061
de1df26b
RW
2062__weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2063{
2064 return NULL;
2065}
2066
a92604b4 2067static int cpufreq_init_governor(struct cpufreq_policy *policy)
1da177e4 2068{
cc993cab 2069 int ret;
6afde10c 2070
2f0aea93
VK
2071 /* Don't start any governor operations if we are entering suspend */
2072 if (cpufreq_suspended)
2073 return 0;
cb57720b
EZ
2074 /*
2075 * Governor might not be initiated here if ACPI _PPC changed
2076 * notification happened, so check it.
2077 */
2078 if (!policy->governor)
2079 return -EINVAL;
2f0aea93 2080
ed4676e2
VK
2081 /* Platform doesn't want dynamic frequency switching ? */
2082 if (policy->governor->dynamic_switching &&
fc4c709f 2083 cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
de1df26b
RW
2084 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2085
2086 if (gov) {
fe829ed8 2087 pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
e837f9b5 2088 policy->governor->name, gov->name);
6afde10c 2089 policy->governor = gov;
de1df26b
RW
2090 } else {
2091 return -EINVAL;
6afde10c 2092 }
1c256245 2093 }
1da177e4 2094
a92604b4
RW
2095 if (!try_module_get(policy->governor->owner))
2096 return -EINVAL;
95731ebb 2097
a92604b4 2098 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
1da177e4 2099
e788892b
RW
2100 if (policy->governor->init) {
2101 ret = policy->governor->init(policy);
2102 if (ret) {
36be3418 2103 module_put(policy->governor->owner);
e788892b
RW
2104 return ret;
2105 }
36be3418 2106 }
1da177e4 2107
a92604b4
RW
2108 return 0;
2109}
2110
2111static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2112{
2113 if (cpufreq_suspended || !policy->governor)
2114 return;
2115
2116 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2117
e788892b
RW
2118 if (policy->governor->exit)
2119 policy->governor->exit(policy);
a92604b4 2120
a92604b4 2121 module_put(policy->governor->owner);
1da177e4
LT
2122}
2123
0a300767
RW
2124static int cpufreq_start_governor(struct cpufreq_policy *policy)
2125{
2126 int ret;
2127
a92604b4
RW
2128 if (cpufreq_suspended)
2129 return 0;
2130
2131 if (!policy->governor)
2132 return -EINVAL;
2133
2134 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2135
3bbf8fe3
RW
2136 if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2137 cpufreq_update_current_freq(policy);
2138
e788892b
RW
2139 if (policy->governor->start) {
2140 ret = policy->governor->start(policy);
2141 if (ret)
2142 return ret;
2143 }
2144
2145 if (policy->governor->limits)
2146 policy->governor->limits(policy);
d6ff44d6 2147
d6ff44d6 2148 return 0;
0a300767
RW
2149}
2150
a92604b4
RW
2151static void cpufreq_stop_governor(struct cpufreq_policy *policy)
2152{
2153 if (cpufreq_suspended || !policy->governor)
2154 return;
2155
2156 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2157
e788892b
RW
2158 if (policy->governor->stop)
2159 policy->governor->stop(policy);
a92604b4
RW
2160}
2161
2162static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2163{
2164 if (cpufreq_suspended || !policy->governor)
2165 return;
2166
2167 pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2168
e788892b
RW
2169 if (policy->governor->limits)
2170 policy->governor->limits(policy);
0a300767
RW
2171}
2172
1da177e4
LT
2173int cpufreq_register_governor(struct cpufreq_governor *governor)
2174{
3bcb09a3 2175 int err;
1da177e4
LT
2176
2177 if (!governor)
2178 return -EINVAL;
2179
a7b422cd
KRW
2180 if (cpufreq_disabled())
2181 return -ENODEV;
2182
3fc54d37 2183 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 2184
3bcb09a3 2185 err = -EBUSY;
42f91fa1 2186 if (!find_governor(governor->name)) {
3bcb09a3
JF
2187 err = 0;
2188 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 2189 }
1da177e4 2190
32ee8c3e 2191 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 2192 return err;
1da177e4
LT
2193}
2194EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2195
1da177e4
LT
2196void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2197{
4573237b
VK
2198 struct cpufreq_policy *policy;
2199 unsigned long flags;
90e41bac 2200
1da177e4
LT
2201 if (!governor)
2202 return;
2203
a7b422cd
KRW
2204 if (cpufreq_disabled())
2205 return;
2206
4573237b
VK
2207 /* clear last_governor for all inactive policies */
2208 read_lock_irqsave(&cpufreq_driver_lock, flags);
2209 for_each_inactive_policy(policy) {
18bf3a12
VK
2210 if (!strcmp(policy->last_governor, governor->name)) {
2211 policy->governor = NULL;
4573237b 2212 strcpy(policy->last_governor, "\0");
18bf3a12 2213 }
90e41bac 2214 }
4573237b 2215 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
90e41bac 2216
3fc54d37 2217 mutex_lock(&cpufreq_governor_mutex);
1da177e4 2218 list_del(&governor->governor_list);
3fc54d37 2219 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
2220}
2221EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2222
2223
1da177e4
LT
2224/*********************************************************************
2225 * POLICY INTERFACE *
2226 *********************************************************************/
2227
2228/**
2229 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
2230 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2231 * is written
1da177e4
LT
2232 *
2233 * Reads the current cpufreq policy.
2234 */
2235int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2236{
2237 struct cpufreq_policy *cpu_policy;
2238 if (!policy)
2239 return -EINVAL;
2240
2241 cpu_policy = cpufreq_cpu_get(cpu);
2242 if (!cpu_policy)
2243 return -EINVAL;
2244
d5b73cd8 2245 memcpy(policy, cpu_policy, sizeof(*policy));
1da177e4
LT
2246
2247 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
2248 return 0;
2249}
2250EXPORT_SYMBOL(cpufreq_get_policy);
2251
a0dbb819
RW
2252/**
2253 * cpufreq_set_policy - Modify cpufreq policy parameters.
2254 * @policy: Policy object to modify.
2255 * @new_policy: New policy data.
2256 *
2257 * Pass @new_policy to the cpufreq driver's ->verify() callback, run the
2258 * installed policy notifiers for it with the CPUFREQ_ADJUST value, pass it to
2259 * the driver's ->verify() callback again and run the notifiers for it again
2260 * with the CPUFREQ_NOTIFY value. Next, copy the min and max parameters
2261 * of @new_policy to @policy and either invoke the driver's ->setpolicy()
2262 * callback (if present) or carry out a governor update for @policy. That is,
2263 * run the current governor's ->limits() callback (if the governor field in
2264 * @new_policy points to the same object as the one in @policy) or replace the
2265 * governor for @policy with the new one stored in @new_policy.
2266 *
2267 * The cpuinfo part of @policy is not updated by this function.
153d7f3f 2268 */
9083e498
RW
2269int cpufreq_set_policy(struct cpufreq_policy *policy,
2270 struct cpufreq_policy *new_policy)
1da177e4 2271{
d9a789c7
RW
2272 struct cpufreq_governor *old_gov;
2273 int ret;
1da177e4 2274
e837f9b5
JP
2275 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2276 new_policy->cpu, new_policy->min, new_policy->max);
1da177e4 2277
d5b73cd8 2278 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1da177e4 2279
fba9573b
PX
2280 /*
2281 * This check works well when we store new min/max freq attributes,
2282 * because new_policy is a copy of policy with one field updated.
2283 */
2284 if (new_policy->min > new_policy->max)
d9a789c7 2285 return -EINVAL;
9c9a43ed 2286
1da177e4 2287 /* verify the cpu speed can be set within this limit */
3a3e9e06 2288 ret = cpufreq_driver->verify(new_policy);
1da177e4 2289 if (ret)
d9a789c7 2290 return ret;
1da177e4 2291
1da177e4 2292 /* adjust if necessary - all reasons */
e041c683 2293 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2294 CPUFREQ_ADJUST, new_policy);
1da177e4 2295
bb176f7d
VK
2296 /*
2297 * verify the cpu speed can be set within this limit, which might be
2298 * different to the first one
2299 */
3a3e9e06 2300 ret = cpufreq_driver->verify(new_policy);
e041c683 2301 if (ret)
d9a789c7 2302 return ret;
1da177e4
LT
2303
2304 /* notification of the new policy */
e041c683 2305 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2306 CPUFREQ_NOTIFY, new_policy);
1da177e4 2307
3a3e9e06
VK
2308 policy->min = new_policy->min;
2309 policy->max = new_policy->max;
601b2185 2310 trace_cpu_frequency_limits(policy);
1da177e4 2311
e3c06236
SM
2312 policy->cached_target_freq = UINT_MAX;
2313
2d06d8c4 2314 pr_debug("new min and max freqs are %u - %u kHz\n",
e837f9b5 2315 policy->min, policy->max);
1da177e4 2316
1c3d85dd 2317 if (cpufreq_driver->setpolicy) {
3a3e9e06 2318 policy->policy = new_policy->policy;
2d06d8c4 2319 pr_debug("setting range\n");
167a38dc 2320 return cpufreq_driver->setpolicy(policy);
d9a789c7 2321 }
1da177e4 2322
0a300767 2323 if (new_policy->governor == policy->governor) {
2bb4059e 2324 pr_debug("governor limits update\n");
a92604b4 2325 cpufreq_governor_limits(policy);
d6ff44d6 2326 return 0;
0a300767 2327 }
7bd353a9 2328
d9a789c7
RW
2329 pr_debug("governor switch\n");
2330
2331 /* save old, working values */
2332 old_gov = policy->governor;
2333 /* end old governor */
2334 if (old_gov) {
45482c70 2335 cpufreq_stop_governor(policy);
36be3418 2336 cpufreq_exit_governor(policy);
1da177e4
LT
2337 }
2338
d9a789c7
RW
2339 /* start new governor */
2340 policy->governor = new_policy->governor;
a92604b4 2341 ret = cpufreq_init_governor(policy);
4bc384ae 2342 if (!ret) {
0a300767
RW
2343 ret = cpufreq_start_governor(policy);
2344 if (!ret) {
2bb4059e 2345 pr_debug("governor change\n");
531b5c9f 2346 sched_cpufreq_governor_change(policy, old_gov);
0a300767
RW
2347 return 0;
2348 }
b7898fda 2349 cpufreq_exit_governor(policy);
d9a789c7
RW
2350 }
2351
2352 /* new governor failed, so re-start old one */
2353 pr_debug("starting governor %s failed\n", policy->governor->name);
2354 if (old_gov) {
2355 policy->governor = old_gov;
a92604b4 2356 if (cpufreq_init_governor(policy))
4bc384ae
VK
2357 policy->governor = NULL;
2358 else
0a300767 2359 cpufreq_start_governor(policy);
d9a789c7
RW
2360 }
2361
4bc384ae 2362 return ret;
1da177e4
LT
2363}
2364
1da177e4 2365/**
a0dbb819
RW
2366 * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2367 * @cpu: CPU to re-evaluate the policy for.
1da177e4 2368 *
a0dbb819
RW
2369 * Update the current frequency for the cpufreq policy of @cpu and use
2370 * cpufreq_set_policy() to re-apply the min and max limits saved in the
2371 * user_policy sub-structure of that policy, which triggers the evaluation
2372 * of policy notifiers and the cpufreq driver's ->verify() callback for the
2373 * policy in question, among other things.
1da177e4 2374 */
30248fef 2375void cpufreq_update_policy(unsigned int cpu)
1da177e4 2376{
540a3758 2377 struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
3a3e9e06 2378 struct cpufreq_policy new_policy;
1da177e4 2379
fefa8ff8 2380 if (!policy)
30248fef 2381 return;
1da177e4 2382
bb176f7d
VK
2383 /*
2384 * BIOS might change freq behind our back
2385 * -> ask driver for current freq and notify governors about a change
2386 */
348a2ec5
RW
2387 if (cpufreq_driver->get && !cpufreq_driver->setpolicy &&
2388 (cpufreq_suspended || WARN_ON(!cpufreq_update_current_freq(policy))))
2389 goto unlock;
30248fef 2390
348a2ec5
RW
2391 pr_debug("updating policy for CPU %u\n", cpu);
2392 memcpy(&new_policy, policy, sizeof(*policy));
2393 new_policy.min = policy->user_policy.min;
2394 new_policy.max = policy->user_policy.max;
0961dd0d 2395
30248fef 2396 cpufreq_set_policy(policy, &new_policy);
1da177e4 2397
fefa8ff8 2398unlock:
540a3758 2399 cpufreq_cpu_release(policy);
1da177e4
LT
2400}
2401EXPORT_SYMBOL(cpufreq_update_policy);
2402
5a25e3f7
RW
2403/**
2404 * cpufreq_update_limits - Update policy limits for a given CPU.
2405 * @cpu: CPU to update the policy limits for.
2406 *
2407 * Invoke the driver's ->update_limits callback if present or call
2408 * cpufreq_update_policy() for @cpu.
2409 */
2410void cpufreq_update_limits(unsigned int cpu)
2411{
2412 if (cpufreq_driver->update_limits)
2413 cpufreq_driver->update_limits(cpu);
2414 else
2415 cpufreq_update_policy(cpu);
2416}
2417EXPORT_SYMBOL_GPL(cpufreq_update_limits);
2418
6f19efc0
LM
2419/*********************************************************************
2420 * BOOST *
2421 *********************************************************************/
2422static int cpufreq_boost_set_sw(int state)
2423{
6f19efc0
LM
2424 struct cpufreq_policy *policy;
2425 int ret = -EINVAL;
2426
f963735a 2427 for_each_active_policy(policy) {
f8bfc116
VK
2428 if (!policy->freq_table)
2429 continue;
49f18560 2430
f8bfc116
VK
2431 ret = cpufreq_frequency_table_cpuinfo(policy,
2432 policy->freq_table);
2433 if (ret) {
2434 pr_err("%s: Policy frequency update failed\n",
2435 __func__);
2436 break;
6f19efc0 2437 }
f8bfc116
VK
2438
2439 down_write(&policy->rwsem);
2440 policy->user_policy.max = policy->max;
2441 cpufreq_governor_limits(policy);
2442 up_write(&policy->rwsem);
6f19efc0
LM
2443 }
2444
2445 return ret;
2446}
2447
2448int cpufreq_boost_trigger_state(int state)
2449{
2450 unsigned long flags;
2451 int ret = 0;
2452
2453 if (cpufreq_driver->boost_enabled == state)
2454 return 0;
2455
2456 write_lock_irqsave(&cpufreq_driver_lock, flags);
2457 cpufreq_driver->boost_enabled = state;
2458 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2459
2460 ret = cpufreq_driver->set_boost(state);
2461 if (ret) {
2462 write_lock_irqsave(&cpufreq_driver_lock, flags);
2463 cpufreq_driver->boost_enabled = !state;
2464 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2465
e837f9b5
JP
2466 pr_err("%s: Cannot %s BOOST\n",
2467 __func__, state ? "enable" : "disable");
6f19efc0
LM
2468 }
2469
2470 return ret;
2471}
2472
41669da0 2473static bool cpufreq_boost_supported(void)
6f19efc0 2474{
7a6c79f2 2475 return likely(cpufreq_driver) && cpufreq_driver->set_boost;
6f19efc0 2476}
6f19efc0 2477
44139ed4
VK
2478static int create_boost_sysfs_file(void)
2479{
2480 int ret;
2481
c82bd444 2482 ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
44139ed4
VK
2483 if (ret)
2484 pr_err("%s: cannot register global BOOST sysfs file\n",
2485 __func__);
2486
2487 return ret;
2488}
2489
2490static void remove_boost_sysfs_file(void)
2491{
2492 if (cpufreq_boost_supported())
c82bd444 2493 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
44139ed4
VK
2494}
2495
2496int cpufreq_enable_boost_support(void)
2497{
2498 if (!cpufreq_driver)
2499 return -EINVAL;
2500
2501 if (cpufreq_boost_supported())
2502 return 0;
2503
7a6c79f2 2504 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
44139ed4
VK
2505
2506 /* This will get removed on driver unregister */
2507 return create_boost_sysfs_file();
2508}
2509EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2510
6f19efc0
LM
2511int cpufreq_boost_enabled(void)
2512{
2513 return cpufreq_driver->boost_enabled;
2514}
2515EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2516
1da177e4
LT
2517/*********************************************************************
2518 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2519 *********************************************************************/
27622b06 2520static enum cpuhp_state hp_online;
1da177e4 2521
c4a3fa26
CY
2522static int cpuhp_cpufreq_online(unsigned int cpu)
2523{
2524 cpufreq_online(cpu);
2525
2526 return 0;
2527}
2528
2529static int cpuhp_cpufreq_offline(unsigned int cpu)
2530{
2531 cpufreq_offline(cpu);
2532
2533 return 0;
2534}
2535
1da177e4
LT
2536/**
2537 * cpufreq_register_driver - register a CPU Frequency driver
2538 * @driver_data: A struct cpufreq_driver containing the values#
2539 * submitted by the CPU Frequency driver.
2540 *
bb176f7d 2541 * Registers a CPU Frequency driver to this core code. This code
63af4055 2542 * returns zero on success, -EEXIST when another driver got here first
32ee8c3e 2543 * (and isn't unregistered in the meantime).
1da177e4
LT
2544 *
2545 */
221dee28 2546int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
2547{
2548 unsigned long flags;
2549 int ret;
2550
a7b422cd
KRW
2551 if (cpufreq_disabled())
2552 return -ENODEV;
2553
1da177e4 2554 if (!driver_data || !driver_data->verify || !driver_data->init ||
9c0ebcf7 2555 !(driver_data->setpolicy || driver_data->target_index ||
9832235f
RW
2556 driver_data->target) ||
2557 (driver_data->setpolicy && (driver_data->target_index ||
1c03a2d0 2558 driver_data->target)) ||
a9a22b57 2559 (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
91a12e91 2560 (!driver_data->online != !driver_data->offline))
1da177e4
LT
2561 return -EINVAL;
2562
2d06d8c4 2563 pr_debug("trying to register driver %s\n", driver_data->name);
1da177e4 2564
fdd320da 2565 /* Protect against concurrent CPU online/offline. */
a92551e4 2566 cpus_read_lock();
fdd320da 2567
0d1857a1 2568 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2569 if (cpufreq_driver) {
0d1857a1 2570 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
fdd320da
RW
2571 ret = -EEXIST;
2572 goto out;
1da177e4 2573 }
1c3d85dd 2574 cpufreq_driver = driver_data;
0d1857a1 2575 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 2576
bc68b7df
VK
2577 if (driver_data->setpolicy)
2578 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2579
7a6c79f2
RW
2580 if (cpufreq_boost_supported()) {
2581 ret = create_boost_sysfs_file();
2582 if (ret)
2583 goto err_null_driver;
2584 }
6f19efc0 2585
8a25a2fd 2586 ret = subsys_interface_register(&cpufreq_interface);
8f5bc2ab 2587 if (ret)
6f19efc0 2588 goto err_boost_unreg;
1da177e4 2589
ce1bcfe9
VK
2590 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2591 list_empty(&cpufreq_policy_list)) {
1da177e4 2592 /* if all ->init() calls failed, unregister */
6c770036 2593 ret = -ENODEV;
ce1bcfe9
VK
2594 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2595 driver_data->name);
2596 goto err_if_unreg;
1da177e4
LT
2597 }
2598
a92551e4
SAS
2599 ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2600 "cpufreq:online",
2601 cpuhp_cpufreq_online,
2602 cpuhp_cpufreq_offline);
27622b06
SAS
2603 if (ret < 0)
2604 goto err_if_unreg;
2605 hp_online = ret;
5372e054 2606 ret = 0;
27622b06 2607
2d06d8c4 2608 pr_debug("driver %s up and running\n", driver_data->name);
3834abb4 2609 goto out;
fdd320da 2610
8a25a2fd
KS
2611err_if_unreg:
2612 subsys_interface_unregister(&cpufreq_interface);
6f19efc0 2613err_boost_unreg:
44139ed4 2614 remove_boost_sysfs_file();
8f5bc2ab 2615err_null_driver:
0d1857a1 2616 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2617 cpufreq_driver = NULL;
0d1857a1 2618 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
3834abb4 2619out:
a92551e4 2620 cpus_read_unlock();
3834abb4 2621 return ret;
1da177e4
LT
2622}
2623EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2624
1da177e4
LT
2625/**
2626 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2627 *
bb176f7d 2628 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
2629 * the right to do so, i.e. if you have succeeded in initialising before!
2630 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2631 * currently not initialised.
2632 */
221dee28 2633int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
2634{
2635 unsigned long flags;
2636
1c3d85dd 2637 if (!cpufreq_driver || (driver != cpufreq_driver))
1da177e4 2638 return -EINVAL;
1da177e4 2639
2d06d8c4 2640 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 2641
454d3a25 2642 /* Protect against concurrent cpu hotplug */
a92551e4 2643 cpus_read_lock();
8a25a2fd 2644 subsys_interface_unregister(&cpufreq_interface);
44139ed4 2645 remove_boost_sysfs_file();
a92551e4 2646 cpuhp_remove_state_nocalls_cpuslocked(hp_online);
1da177e4 2647
0d1857a1 2648 write_lock_irqsave(&cpufreq_driver_lock, flags);
6eed9404 2649
1c3d85dd 2650 cpufreq_driver = NULL;
6eed9404 2651
0d1857a1 2652 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
a92551e4 2653 cpus_read_unlock();
1da177e4
LT
2654
2655 return 0;
2656}
2657EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8 2658
90de2a4a
DA
2659/*
2660 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2661 * or mutexes when secondary CPUs are halted.
2662 */
2663static struct syscore_ops cpufreq_syscore_ops = {
2664 .shutdown = cpufreq_suspend,
2665};
2666
c82bd444
VK
2667struct kobject *cpufreq_global_kobject;
2668EXPORT_SYMBOL(cpufreq_global_kobject);
2669
5a01f2e8
VP
2670static int __init cpufreq_core_init(void)
2671{
a7b422cd
KRW
2672 if (cpufreq_disabled())
2673 return -ENODEV;
2674
8eec1020 2675 cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
8aa84ad8
TR
2676 BUG_ON(!cpufreq_global_kobject);
2677
90de2a4a
DA
2678 register_syscore_ops(&cpufreq_syscore_ops);
2679
5a01f2e8
VP
2680 return 0;
2681}
d82f2692 2682module_param(off, int, 0444);
5a01f2e8 2683core_initcall(cpufreq_core_init);