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