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