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