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