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