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