cpufreq: Get rid of cpufreq_cpu_data_fallback
[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
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 */
0d1857a1 1216 read_lock_irqsave(&cpufreq_driver_lock, flags);
f963735a 1217 for_each_active_policy(policy) {
7f0c020a 1218 if (cpumask_test_cpu(cpu, policy->related_cpus)) {
0d1857a1 1219 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
7f0c020a 1220 ret = cpufreq_add_policy_cpu(policy, cpu, dev);
6eed9404
VK
1221 up_read(&cpufreq_rwsem);
1222 return ret;
2eaa3e2d 1223 }
fcf80582 1224 }
0d1857a1 1225 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1226
72368d12
RW
1227 /*
1228 * Restore the saved policy when doing light-weight init and fall back
1229 * to the full init if that fails.
1230 */
96bbbe4a 1231 policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
72368d12 1232 if (!policy) {
96bbbe4a 1233 recover_policy = false;
8414809c 1234 policy = cpufreq_policy_alloc();
72368d12
RW
1235 if (!policy)
1236 goto nomem_out;
1237 }
0d66b91e
SB
1238
1239 /*
1240 * In the resume path, since we restore a saved policy, the assignment
1241 * to policy->cpu is like an update of the existing policy, rather than
1242 * the creation of a brand new one. So we need to perform this update
1243 * by invoking update_policy_cpu().
1244 */
1bfb425b
VK
1245 if (recover_policy && cpu != policy->cpu)
1246 WARN_ON(update_policy_cpu(policy, cpu, dev));
1247 else
0d66b91e
SB
1248 policy->cpu = cpu;
1249
835481d9 1250 cpumask_copy(policy->cpus, cpumask_of(cpu));
1da177e4 1251
1da177e4
LT
1252 /* call driver. From then on the cpufreq must be able
1253 * to accept all calls to ->verify and ->setpolicy for this CPU
1254 */
1c3d85dd 1255 ret = cpufreq_driver->init(policy);
1da177e4 1256 if (ret) {
2d06d8c4 1257 pr_debug("initialization failed\n");
2eaa3e2d 1258 goto err_set_policy_cpu;
1da177e4 1259 }
643ae6e8 1260
6d4e81ed
TV
1261 down_write(&policy->rwsem);
1262
5a7e56a5
VK
1263 /* related cpus should atleast have policy->cpus */
1264 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1265
1266 /*
1267 * affected cpus must always be the one, which are online. We aren't
1268 * managing offline cpus here.
1269 */
1270 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1271
96bbbe4a 1272 if (!recover_policy) {
5a7e56a5
VK
1273 policy->user_policy.min = policy->min;
1274 policy->user_policy.max = policy->max;
6d4e81ed
TV
1275
1276 /* prepare interface data */
1277 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1278 &dev->kobj, "cpufreq");
1279 if (ret) {
1280 pr_err("%s: failed to init policy->kobj: %d\n",
1281 __func__, ret);
1282 goto err_init_policy_kobj;
1283 }
5a7e56a5 1284
988bed09
VK
1285 write_lock_irqsave(&cpufreq_driver_lock, flags);
1286 for_each_cpu(j, policy->related_cpus)
1287 per_cpu(cpufreq_cpu_data, j) = policy;
1288 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1289 }
652ed95d 1290
2ed99e39 1291 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
da60ce9f
VK
1292 policy->cur = cpufreq_driver->get(policy->cpu);
1293 if (!policy->cur) {
1294 pr_err("%s: ->get() failed\n", __func__);
1295 goto err_get_freq;
1296 }
1297 }
1298
d3916691
VK
1299 /*
1300 * Sometimes boot loaders set CPU frequency to a value outside of
1301 * frequency table present with cpufreq core. In such cases CPU might be
1302 * unstable if it has to run on that frequency for long duration of time
1303 * and so its better to set it to a frequency which is specified in
1304 * freq-table. This also makes cpufreq stats inconsistent as
1305 * cpufreq-stats would fail to register because current frequency of CPU
1306 * isn't found in freq-table.
1307 *
1308 * Because we don't want this change to effect boot process badly, we go
1309 * for the next freq which is >= policy->cur ('cur' must be set by now,
1310 * otherwise we will end up setting freq to lowest of the table as 'cur'
1311 * is initialized to zero).
1312 *
1313 * We are passing target-freq as "policy->cur - 1" otherwise
1314 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1315 * equal to target-freq.
1316 */
1317 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1318 && has_target()) {
1319 /* Are we running at unknown frequency ? */
1320 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1321 if (ret == -EINVAL) {
1322 /* Warn user and fix it */
1323 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1324 __func__, policy->cpu, policy->cur);
1325 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1326 CPUFREQ_RELATION_L);
1327
1328 /*
1329 * Reaching here after boot in a few seconds may not
1330 * mean that system will remain stable at "unknown"
1331 * frequency for longer duration. Hence, a BUG_ON().
1332 */
1333 BUG_ON(ret);
1334 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1335 __func__, policy->cpu, policy->cur);
1336 }
1337 }
1338
a1531acd
TR
1339 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1340 CPUFREQ_START, policy);
1341
96bbbe4a 1342 if (!recover_policy) {
308b60e7 1343 ret = cpufreq_add_dev_interface(policy, dev);
a82fab29
SB
1344 if (ret)
1345 goto err_out_unregister;
fcd7af91
VK
1346 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1347 CPUFREQ_CREATE_POLICY, policy);
8ff69732 1348
988bed09
VK
1349 write_lock_irqsave(&cpufreq_driver_lock, flags);
1350 list_add(&policy->policy_list, &cpufreq_policy_list);
1351 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1352 }
9515f4d6 1353
e18f1682
SB
1354 cpufreq_init_policy(policy);
1355
96bbbe4a 1356 if (!recover_policy) {
08fd8c1c
VK
1357 policy->user_policy.policy = policy->policy;
1358 policy->user_policy.governor = policy->governor;
1359 }
4e97b631 1360 up_write(&policy->rwsem);
08fd8c1c 1361
038c5b3e 1362 kobject_uevent(&policy->kobj, KOBJ_ADD);
7c45cf31 1363
6eed9404
VK
1364 up_read(&cpufreq_rwsem);
1365
7c45cf31
VK
1366 /* Callback for handling stuff after policy is ready */
1367 if (cpufreq_driver->ready)
1368 cpufreq_driver->ready(policy);
1369
2d06d8c4 1370 pr_debug("initialization complete\n");
87c32271 1371
1da177e4
LT
1372 return 0;
1373
1da177e4 1374err_out_unregister:
652ed95d 1375err_get_freq:
6d4e81ed
TV
1376 if (!recover_policy) {
1377 kobject_put(&policy->kobj);
1378 wait_for_completion(&policy->kobj_unregister);
1379 }
1380err_init_policy_kobj:
7106e02b
PB
1381 up_write(&policy->rwsem);
1382
da60ce9f
VK
1383 if (cpufreq_driver->exit)
1384 cpufreq_driver->exit(policy);
2eaa3e2d 1385err_set_policy_cpu:
3914d379 1386 if (recover_policy)
42f921a6 1387 cpufreq_policy_put_kobj(policy);
e9698cc5 1388 cpufreq_policy_free(policy);
42f921a6 1389
1da177e4 1390nomem_out:
6eed9404
VK
1391 up_read(&cpufreq_rwsem);
1392
1da177e4
LT
1393 return ret;
1394}
1395
cedb70af 1396static int __cpufreq_remove_dev_prepare(struct device *dev,
96bbbe4a 1397 struct subsys_interface *sif)
1da177e4 1398{
f9ba680d 1399 unsigned int cpu = dev->id, cpus;
1bfb425b 1400 int ret;
3a3e9e06 1401 struct cpufreq_policy *policy;
1da177e4 1402
b8eed8af 1403 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1da177e4 1404
988bed09 1405 policy = cpufreq_cpu_get_raw(cpu);
3a3e9e06 1406 if (!policy) {
b8eed8af 1407 pr_debug("%s: No cpu_data found\n", __func__);
1da177e4
LT
1408 return -EINVAL;
1409 }
1da177e4 1410
9c0ebcf7 1411 if (has_target()) {
3de9bdeb
VK
1412 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1413 if (ret) {
1414 pr_err("%s: Failed to stop governor\n", __func__);
1415 return ret;
1416 }
1da177e4 1417
fa69e33f 1418 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
3a3e9e06 1419 policy->governor->name, CPUFREQ_NAME_LEN);
db5f2995 1420 }
1da177e4 1421
ad7722da 1422 down_read(&policy->rwsem);
3a3e9e06 1423 cpus = cpumask_weight(policy->cpus);
ad7722da 1424 up_read(&policy->rwsem);
084f3493 1425
61173f25 1426 if (cpu != policy->cpu) {
6964d91d 1427 sysfs_remove_link(&dev->kobj, "cpufreq");
73bf0fc2 1428 } else if (cpus > 1) {
1bfb425b
VK
1429 /* Nominate new CPU */
1430 int new_cpu = cpumask_any_but(policy->cpus, cpu);
1431 struct device *cpu_dev = get_cpu_device(new_cpu);
a82fab29 1432
1bfb425b
VK
1433 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1434 ret = update_policy_cpu(policy, new_cpu, cpu_dev);
1435 if (ret) {
1436 if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1437 "cpufreq"))
1438 pr_err("%s: Failed to restore kobj link to cpu:%d\n",
1439 __func__, cpu_dev->id);
1440 return ret;
1da177e4 1441 }
1bfb425b
VK
1442
1443 if (!cpufreq_suspended)
1444 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1445 __func__, new_cpu, cpu);
789ca243 1446 } else if (cpufreq_driver->stop_cpu) {
367dc4aa 1447 cpufreq_driver->stop_cpu(policy);
1da177e4 1448 }
1da177e4 1449
cedb70af
SB
1450 return 0;
1451}
1452
1453static int __cpufreq_remove_dev_finish(struct device *dev,
96bbbe4a 1454 struct subsys_interface *sif)
cedb70af 1455{
988bed09 1456 unsigned int cpu = dev->id;
cedb70af 1457 int ret;
988bed09 1458 struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
cedb70af
SB
1459
1460 if (!policy) {
1461 pr_debug("%s: No cpu_data found\n", __func__);
1462 return -EINVAL;
1463 }
1464
ad7722da 1465 down_write(&policy->rwsem);
303ae723 1466 cpumask_clear_cpu(cpu, policy->cpus);
ad7722da 1467 up_write(&policy->rwsem);
cedb70af 1468
b8eed8af 1469 /* If cpu is last user of policy, free policy */
988bed09 1470 if (policy_is_inactive(policy)) {
9c0ebcf7 1471 if (has_target()) {
3de9bdeb
VK
1472 ret = __cpufreq_governor(policy,
1473 CPUFREQ_GOV_POLICY_EXIT);
1474 if (ret) {
1475 pr_err("%s: Failed to exit governor\n",
e837f9b5 1476 __func__);
3de9bdeb
VK
1477 return ret;
1478 }
edab2fbc 1479 }
2a998599 1480
96bbbe4a 1481 if (!cpufreq_suspended)
42f921a6 1482 cpufreq_policy_put_kobj(policy);
7d26e2d5 1483
8414809c
SB
1484 /*
1485 * Perform the ->exit() even during light-weight tear-down,
1486 * since this is a core component, and is essential for the
1487 * subsequent light-weight ->init() to succeed.
b8eed8af 1488 */
1c3d85dd 1489 if (cpufreq_driver->exit)
3a3e9e06 1490 cpufreq_driver->exit(policy);
27ecddc2 1491
96bbbe4a 1492 if (!cpufreq_suspended)
3a3e9e06 1493 cpufreq_policy_free(policy);
e5c87b76
SK
1494 } else if (has_target()) {
1495 ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1496 if (!ret)
1497 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1498
1499 if (ret) {
1500 pr_err("%s: Failed to start governor\n", __func__);
1501 return ret;
2a998599 1502 }
27ecddc2 1503 }
1da177e4 1504
1da177e4
LT
1505 return 0;
1506}
1507
cedb70af 1508/**
27a862e9 1509 * cpufreq_remove_dev - remove a CPU device
cedb70af
SB
1510 *
1511 * Removes the cpufreq interface for a CPU device.
cedb70af 1512 */
8a25a2fd 1513static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
5a01f2e8 1514{
8a25a2fd 1515 unsigned int cpu = dev->id;
27a862e9 1516 int ret;
ec28297a
VP
1517
1518 if (cpu_is_offline(cpu))
1519 return 0;
1520
96bbbe4a 1521 ret = __cpufreq_remove_dev_prepare(dev, sif);
27a862e9
VK
1522
1523 if (!ret)
96bbbe4a 1524 ret = __cpufreq_remove_dev_finish(dev, sif);
27a862e9
VK
1525
1526 return ret;
5a01f2e8
VP
1527}
1528
65f27f38 1529static void handle_update(struct work_struct *work)
1da177e4 1530{
65f27f38
DH
1531 struct cpufreq_policy *policy =
1532 container_of(work, struct cpufreq_policy, update);
1533 unsigned int cpu = policy->cpu;
2d06d8c4 1534 pr_debug("handle_update for cpu %u called\n", cpu);
1da177e4
LT
1535 cpufreq_update_policy(cpu);
1536}
1537
1538/**
bb176f7d
VK
1539 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1540 * in deep trouble.
a1e1dc41 1541 * @policy: policy managing CPUs
1da177e4
LT
1542 * @new_freq: CPU frequency the CPU actually runs at
1543 *
29464f28
DJ
1544 * We adjust to current frequency first, and need to clean up later.
1545 * So either call to cpufreq_update_policy() or schedule handle_update()).
1da177e4 1546 */
a1e1dc41 1547static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
e08f5f5b 1548 unsigned int new_freq)
1da177e4
LT
1549{
1550 struct cpufreq_freqs freqs;
b43a7ffb 1551
e837f9b5 1552 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
a1e1dc41 1553 policy->cur, new_freq);
1da177e4 1554
a1e1dc41 1555 freqs.old = policy->cur;
1da177e4 1556 freqs.new = new_freq;
b43a7ffb 1557
8fec051e
VK
1558 cpufreq_freq_transition_begin(policy, &freqs);
1559 cpufreq_freq_transition_end(policy, &freqs, 0);
1da177e4
LT
1560}
1561
32ee8c3e 1562/**
4ab70df4 1563 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
95235ca2
VP
1564 * @cpu: CPU number
1565 *
1566 * This is the last known freq, without actually getting it from the driver.
1567 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1568 */
1569unsigned int cpufreq_quick_get(unsigned int cpu)
1570{
9e21ba8b 1571 struct cpufreq_policy *policy;
e08f5f5b 1572 unsigned int ret_freq = 0;
95235ca2 1573
1c3d85dd
RW
1574 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1575 return cpufreq_driver->get(cpu);
9e21ba8b
DB
1576
1577 policy = cpufreq_cpu_get(cpu);
95235ca2 1578 if (policy) {
e08f5f5b 1579 ret_freq = policy->cur;
95235ca2
VP
1580 cpufreq_cpu_put(policy);
1581 }
1582
4d34a67d 1583 return ret_freq;
95235ca2
VP
1584}
1585EXPORT_SYMBOL(cpufreq_quick_get);
1586
3d737108
JB
1587/**
1588 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1589 * @cpu: CPU number
1590 *
1591 * Just return the max possible frequency for a given CPU.
1592 */
1593unsigned int cpufreq_quick_get_max(unsigned int cpu)
1594{
1595 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1596 unsigned int ret_freq = 0;
1597
1598 if (policy) {
1599 ret_freq = policy->max;
1600 cpufreq_cpu_put(policy);
1601 }
1602
1603 return ret_freq;
1604}
1605EXPORT_SYMBOL(cpufreq_quick_get_max);
1606
d92d50a4 1607static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1da177e4 1608{
e08f5f5b 1609 unsigned int ret_freq = 0;
5800043b 1610
1c3d85dd 1611 if (!cpufreq_driver->get)
4d34a67d 1612 return ret_freq;
1da177e4 1613
d92d50a4 1614 ret_freq = cpufreq_driver->get(policy->cpu);
1da177e4 1615
e08f5f5b 1616 if (ret_freq && policy->cur &&
1c3d85dd 1617 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e08f5f5b
GS
1618 /* verify no discrepancy between actual and
1619 saved value exists */
1620 if (unlikely(ret_freq != policy->cur)) {
a1e1dc41 1621 cpufreq_out_of_sync(policy, ret_freq);
1da177e4
LT
1622 schedule_work(&policy->update);
1623 }
1624 }
1625
4d34a67d 1626 return ret_freq;
5a01f2e8 1627}
1da177e4 1628
5a01f2e8
VP
1629/**
1630 * cpufreq_get - get the current CPU frequency (in kHz)
1631 * @cpu: CPU number
1632 *
1633 * Get the CPU current (static) CPU frequency
1634 */
1635unsigned int cpufreq_get(unsigned int cpu)
1636{
999976e0 1637 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
5a01f2e8 1638 unsigned int ret_freq = 0;
5a01f2e8 1639
999976e0
AP
1640 if (policy) {
1641 down_read(&policy->rwsem);
d92d50a4 1642 ret_freq = __cpufreq_get(policy);
999976e0 1643 up_read(&policy->rwsem);
5a01f2e8 1644
999976e0
AP
1645 cpufreq_cpu_put(policy);
1646 }
6eed9404 1647
4d34a67d 1648 return ret_freq;
1da177e4
LT
1649}
1650EXPORT_SYMBOL(cpufreq_get);
1651
8a25a2fd
KS
1652static struct subsys_interface cpufreq_interface = {
1653 .name = "cpufreq",
1654 .subsys = &cpu_subsys,
1655 .add_dev = cpufreq_add_dev,
1656 .remove_dev = cpufreq_remove_dev,
e00e56df
RW
1657};
1658
e28867ea
VK
1659/*
1660 * In case platform wants some specific frequency to be configured
1661 * during suspend..
1662 */
1663int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1664{
1665 int ret;
1666
1667 if (!policy->suspend_freq) {
1668 pr_err("%s: suspend_freq can't be zero\n", __func__);
1669 return -EINVAL;
1670 }
1671
1672 pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1673 policy->suspend_freq);
1674
1675 ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1676 CPUFREQ_RELATION_H);
1677 if (ret)
1678 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1679 __func__, policy->suspend_freq, ret);
1680
1681 return ret;
1682}
1683EXPORT_SYMBOL(cpufreq_generic_suspend);
1684
42d4dc3f 1685/**
2f0aea93 1686 * cpufreq_suspend() - Suspend CPUFreq governors
e00e56df 1687 *
2f0aea93
VK
1688 * Called during system wide Suspend/Hibernate cycles for suspending governors
1689 * as some platforms can't change frequency after this point in suspend cycle.
1690 * Because some of the devices (like: i2c, regulators, etc) they use for
1691 * changing frequency are suspended quickly after this point.
42d4dc3f 1692 */
2f0aea93 1693void cpufreq_suspend(void)
42d4dc3f 1694{
3a3e9e06 1695 struct cpufreq_policy *policy;
42d4dc3f 1696
2f0aea93
VK
1697 if (!cpufreq_driver)
1698 return;
42d4dc3f 1699
2f0aea93 1700 if (!has_target())
b1b12bab 1701 goto suspend;
42d4dc3f 1702
2f0aea93
VK
1703 pr_debug("%s: Suspending Governors\n", __func__);
1704
f963735a 1705 for_each_active_policy(policy) {
2f0aea93
VK
1706 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1707 pr_err("%s: Failed to stop governor for policy: %p\n",
1708 __func__, policy);
1709 else if (cpufreq_driver->suspend
1710 && cpufreq_driver->suspend(policy))
1711 pr_err("%s: Failed to suspend driver: %p\n", __func__,
1712 policy);
42d4dc3f 1713 }
b1b12bab
VK
1714
1715suspend:
1716 cpufreq_suspended = true;
42d4dc3f
BH
1717}
1718
1da177e4 1719/**
2f0aea93 1720 * cpufreq_resume() - Resume CPUFreq governors
1da177e4 1721 *
2f0aea93
VK
1722 * Called during system wide Suspend/Hibernate cycle for resuming governors that
1723 * are suspended with cpufreq_suspend().
1da177e4 1724 */
2f0aea93 1725void cpufreq_resume(void)
1da177e4 1726{
3a3e9e06 1727 struct cpufreq_policy *policy;
1da177e4 1728
2f0aea93
VK
1729 if (!cpufreq_driver)
1730 return;
1da177e4 1731
8e30444e
LT
1732 cpufreq_suspended = false;
1733
2f0aea93 1734 if (!has_target())
e00e56df 1735 return;
1da177e4 1736
2f0aea93 1737 pr_debug("%s: Resuming Governors\n", __func__);
1da177e4 1738
f963735a 1739 for_each_active_policy(policy) {
0c5aa405
VK
1740 if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
1741 pr_err("%s: Failed to resume driver: %p\n", __func__,
1742 policy);
1743 else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
2f0aea93
VK
1744 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1745 pr_err("%s: Failed to start governor for policy: %p\n",
1746 __func__, policy);
2f0aea93 1747 }
c75de0ac
VK
1748
1749 /*
1750 * schedule call cpufreq_update_policy() for first-online CPU, as that
1751 * wouldn't be hotplugged-out on suspend. It will verify that the
1752 * current freq is in sync with what we believe it to be.
1753 */
1754 policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1755 if (WARN_ON(!policy))
1756 return;
1757
1758 schedule_work(&policy->update);
2f0aea93 1759}
1da177e4 1760
9d95046e
BP
1761/**
1762 * cpufreq_get_current_driver - return current driver's name
1763 *
1764 * Return the name string of the currently loaded cpufreq driver
1765 * or NULL, if none.
1766 */
1767const char *cpufreq_get_current_driver(void)
1768{
1c3d85dd
RW
1769 if (cpufreq_driver)
1770 return cpufreq_driver->name;
1771
1772 return NULL;
9d95046e
BP
1773}
1774EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1da177e4 1775
51315cdf
TP
1776/**
1777 * cpufreq_get_driver_data - return current driver data
1778 *
1779 * Return the private data of the currently loaded cpufreq
1780 * driver, or NULL if no cpufreq driver is loaded.
1781 */
1782void *cpufreq_get_driver_data(void)
1783{
1784 if (cpufreq_driver)
1785 return cpufreq_driver->driver_data;
1786
1787 return NULL;
1788}
1789EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1790
1da177e4
LT
1791/*********************************************************************
1792 * NOTIFIER LISTS INTERFACE *
1793 *********************************************************************/
1794
1795/**
1796 * cpufreq_register_notifier - register a driver with cpufreq
1797 * @nb: notifier function to register
1798 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1799 *
32ee8c3e 1800 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1801 * are notified about clock rate changes (once before and once after
1802 * the transition), or a list of drivers that are notified about
1803 * changes in cpufreq policy.
1804 *
1805 * This function may sleep, and has the same return conditions as
e041c683 1806 * blocking_notifier_chain_register.
1da177e4
LT
1807 */
1808int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1809{
1810 int ret;
1811
d5aaffa9
DB
1812 if (cpufreq_disabled())
1813 return -EINVAL;
1814
74212ca4
CEB
1815 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1816
1da177e4
LT
1817 switch (list) {
1818 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1819 ret = srcu_notifier_chain_register(
e041c683 1820 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1821 break;
1822 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1823 ret = blocking_notifier_chain_register(
1824 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1825 break;
1826 default:
1827 ret = -EINVAL;
1828 }
1da177e4
LT
1829
1830 return ret;
1831}
1832EXPORT_SYMBOL(cpufreq_register_notifier);
1833
1da177e4
LT
1834/**
1835 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1836 * @nb: notifier block to be unregistered
bb176f7d 1837 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1da177e4
LT
1838 *
1839 * Remove a driver from the CPU frequency notifier list.
1840 *
1841 * This function may sleep, and has the same return conditions as
e041c683 1842 * blocking_notifier_chain_unregister.
1da177e4
LT
1843 */
1844int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1845{
1846 int ret;
1847
d5aaffa9
DB
1848 if (cpufreq_disabled())
1849 return -EINVAL;
1850
1da177e4
LT
1851 switch (list) {
1852 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1853 ret = srcu_notifier_chain_unregister(
e041c683 1854 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1855 break;
1856 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1857 ret = blocking_notifier_chain_unregister(
1858 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1859 break;
1860 default:
1861 ret = -EINVAL;
1862 }
1da177e4
LT
1863
1864 return ret;
1865}
1866EXPORT_SYMBOL(cpufreq_unregister_notifier);
1867
1868
1869/*********************************************************************
1870 * GOVERNORS *
1871 *********************************************************************/
1872
1c03a2d0
VK
1873/* Must set freqs->new to intermediate frequency */
1874static int __target_intermediate(struct cpufreq_policy *policy,
1875 struct cpufreq_freqs *freqs, int index)
1876{
1877 int ret;
1878
1879 freqs->new = cpufreq_driver->get_intermediate(policy, index);
1880
1881 /* We don't need to switch to intermediate freq */
1882 if (!freqs->new)
1883 return 0;
1884
1885 pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1886 __func__, policy->cpu, freqs->old, freqs->new);
1887
1888 cpufreq_freq_transition_begin(policy, freqs);
1889 ret = cpufreq_driver->target_intermediate(policy, index);
1890 cpufreq_freq_transition_end(policy, freqs, ret);
1891
1892 if (ret)
1893 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1894 __func__, ret);
1895
1896 return ret;
1897}
1898
8d65775d
VK
1899static int __target_index(struct cpufreq_policy *policy,
1900 struct cpufreq_frequency_table *freq_table, int index)
1901{
1c03a2d0
VK
1902 struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1903 unsigned int intermediate_freq = 0;
8d65775d
VK
1904 int retval = -EINVAL;
1905 bool notify;
1906
1907 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
8d65775d 1908 if (notify) {
1c03a2d0
VK
1909 /* Handle switching to intermediate frequency */
1910 if (cpufreq_driver->get_intermediate) {
1911 retval = __target_intermediate(policy, &freqs, index);
1912 if (retval)
1913 return retval;
1914
1915 intermediate_freq = freqs.new;
1916 /* Set old freq to intermediate */
1917 if (intermediate_freq)
1918 freqs.old = freqs.new;
1919 }
8d65775d 1920
1c03a2d0 1921 freqs.new = freq_table[index].frequency;
8d65775d
VK
1922 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1923 __func__, policy->cpu, freqs.old, freqs.new);
1924
1925 cpufreq_freq_transition_begin(policy, &freqs);
1926 }
1927
1928 retval = cpufreq_driver->target_index(policy, index);
1929 if (retval)
1930 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1931 retval);
1932
1c03a2d0 1933 if (notify) {
8d65775d
VK
1934 cpufreq_freq_transition_end(policy, &freqs, retval);
1935
1c03a2d0
VK
1936 /*
1937 * Failed after setting to intermediate freq? Driver should have
1938 * reverted back to initial frequency and so should we. Check
1939 * here for intermediate_freq instead of get_intermediate, in
1940 * case we have't switched to intermediate freq at all.
1941 */
1942 if (unlikely(retval && intermediate_freq)) {
1943 freqs.old = intermediate_freq;
1944 freqs.new = policy->restore_freq;
1945 cpufreq_freq_transition_begin(policy, &freqs);
1946 cpufreq_freq_transition_end(policy, &freqs, 0);
1947 }
1948 }
1949
8d65775d
VK
1950 return retval;
1951}
1952
1da177e4
LT
1953int __cpufreq_driver_target(struct cpufreq_policy *policy,
1954 unsigned int target_freq,
1955 unsigned int relation)
1956{
7249924e 1957 unsigned int old_target_freq = target_freq;
8d65775d 1958 int retval = -EINVAL;
c32b6b8e 1959
a7b422cd
KRW
1960 if (cpufreq_disabled())
1961 return -ENODEV;
1962
7249924e
VK
1963 /* Make sure that target_freq is within supported range */
1964 if (target_freq > policy->max)
1965 target_freq = policy->max;
1966 if (target_freq < policy->min)
1967 target_freq = policy->min;
1968
1969 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
e837f9b5 1970 policy->cpu, target_freq, relation, old_target_freq);
5a1c0228 1971
9c0ebcf7
VK
1972 /*
1973 * This might look like a redundant call as we are checking it again
1974 * after finding index. But it is left intentionally for cases where
1975 * exactly same freq is called again and so we can save on few function
1976 * calls.
1977 */
5a1c0228
VK
1978 if (target_freq == policy->cur)
1979 return 0;
1980
1c03a2d0
VK
1981 /* Save last value to restore later on errors */
1982 policy->restore_freq = policy->cur;
1983
1c3d85dd
RW
1984 if (cpufreq_driver->target)
1985 retval = cpufreq_driver->target(policy, target_freq, relation);
9c0ebcf7
VK
1986 else if (cpufreq_driver->target_index) {
1987 struct cpufreq_frequency_table *freq_table;
1988 int index;
90d45d17 1989
9c0ebcf7
VK
1990 freq_table = cpufreq_frequency_get_table(policy->cpu);
1991 if (unlikely(!freq_table)) {
1992 pr_err("%s: Unable to find freq_table\n", __func__);
1993 goto out;
1994 }
1995
1996 retval = cpufreq_frequency_table_target(policy, freq_table,
1997 target_freq, relation, &index);
1998 if (unlikely(retval)) {
1999 pr_err("%s: Unable to find matching freq\n", __func__);
2000 goto out;
2001 }
2002
d4019f0a 2003 if (freq_table[index].frequency == policy->cur) {
9c0ebcf7 2004 retval = 0;
d4019f0a
VK
2005 goto out;
2006 }
2007
8d65775d 2008 retval = __target_index(policy, freq_table, index);
9c0ebcf7
VK
2009 }
2010
2011out:
1da177e4
LT
2012 return retval;
2013}
2014EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
2015
1da177e4
LT
2016int cpufreq_driver_target(struct cpufreq_policy *policy,
2017 unsigned int target_freq,
2018 unsigned int relation)
2019{
f1829e4a 2020 int ret = -EINVAL;
1da177e4 2021
ad7722da 2022 down_write(&policy->rwsem);
1da177e4
LT
2023
2024 ret = __cpufreq_driver_target(policy, target_freq, relation);
2025
ad7722da 2026 up_write(&policy->rwsem);
1da177e4 2027
1da177e4
LT
2028 return ret;
2029}
2030EXPORT_SYMBOL_GPL(cpufreq_driver_target);
2031
e08f5f5b
GS
2032static int __cpufreq_governor(struct cpufreq_policy *policy,
2033 unsigned int event)
1da177e4 2034{
cc993cab 2035 int ret;
6afde10c
TR
2036
2037 /* Only must be defined when default governor is known to have latency
2038 restrictions, like e.g. conservative or ondemand.
2039 That this is the case is already ensured in Kconfig
2040 */
2041#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
2042 struct cpufreq_governor *gov = &cpufreq_gov_performance;
2043#else
2044 struct cpufreq_governor *gov = NULL;
2045#endif
1c256245 2046
2f0aea93
VK
2047 /* Don't start any governor operations if we are entering suspend */
2048 if (cpufreq_suspended)
2049 return 0;
cb57720b
EZ
2050 /*
2051 * Governor might not be initiated here if ACPI _PPC changed
2052 * notification happened, so check it.
2053 */
2054 if (!policy->governor)
2055 return -EINVAL;
2f0aea93 2056
1c256245
TR
2057 if (policy->governor->max_transition_latency &&
2058 policy->cpuinfo.transition_latency >
2059 policy->governor->max_transition_latency) {
6afde10c
TR
2060 if (!gov)
2061 return -EINVAL;
2062 else {
e837f9b5
JP
2063 pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2064 policy->governor->name, gov->name);
6afde10c
TR
2065 policy->governor = gov;
2066 }
1c256245 2067 }
1da177e4 2068
fe492f3f
VK
2069 if (event == CPUFREQ_GOV_POLICY_INIT)
2070 if (!try_module_get(policy->governor->owner))
2071 return -EINVAL;
1da177e4 2072
2d06d8c4 2073 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
e837f9b5 2074 policy->cpu, event);
95731ebb
XC
2075
2076 mutex_lock(&cpufreq_governor_lock);
56d07db2 2077 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
f73d3933
VK
2078 || (!policy->governor_enabled
2079 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
95731ebb
XC
2080 mutex_unlock(&cpufreq_governor_lock);
2081 return -EBUSY;
2082 }
2083
2084 if (event == CPUFREQ_GOV_STOP)
2085 policy->governor_enabled = false;
2086 else if (event == CPUFREQ_GOV_START)
2087 policy->governor_enabled = true;
2088
2089 mutex_unlock(&cpufreq_governor_lock);
2090
1da177e4
LT
2091 ret = policy->governor->governor(policy, event);
2092
4d5dcc42
VK
2093 if (!ret) {
2094 if (event == CPUFREQ_GOV_POLICY_INIT)
2095 policy->governor->initialized++;
2096 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2097 policy->governor->initialized--;
95731ebb
XC
2098 } else {
2099 /* Restore original values */
2100 mutex_lock(&cpufreq_governor_lock);
2101 if (event == CPUFREQ_GOV_STOP)
2102 policy->governor_enabled = true;
2103 else if (event == CPUFREQ_GOV_START)
2104 policy->governor_enabled = false;
2105 mutex_unlock(&cpufreq_governor_lock);
4d5dcc42 2106 }
b394058f 2107
fe492f3f
VK
2108 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2109 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
1da177e4
LT
2110 module_put(policy->governor->owner);
2111
2112 return ret;
2113}
2114
1da177e4
LT
2115int cpufreq_register_governor(struct cpufreq_governor *governor)
2116{
3bcb09a3 2117 int err;
1da177e4
LT
2118
2119 if (!governor)
2120 return -EINVAL;
2121
a7b422cd
KRW
2122 if (cpufreq_disabled())
2123 return -ENODEV;
2124
3fc54d37 2125 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 2126
b394058f 2127 governor->initialized = 0;
3bcb09a3 2128 err = -EBUSY;
42f91fa1 2129 if (!find_governor(governor->name)) {
3bcb09a3
JF
2130 err = 0;
2131 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 2132 }
1da177e4 2133
32ee8c3e 2134 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 2135 return err;
1da177e4
LT
2136}
2137EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2138
1da177e4
LT
2139void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2140{
90e41bac 2141 int cpu;
90e41bac 2142
1da177e4
LT
2143 if (!governor)
2144 return;
2145
a7b422cd
KRW
2146 if (cpufreq_disabled())
2147 return;
2148
90e41bac
PB
2149 for_each_present_cpu(cpu) {
2150 if (cpu_online(cpu))
2151 continue;
2152 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
2153 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
2154 }
90e41bac 2155
3fc54d37 2156 mutex_lock(&cpufreq_governor_mutex);
1da177e4 2157 list_del(&governor->governor_list);
3fc54d37 2158 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
2159 return;
2160}
2161EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2162
2163
1da177e4
LT
2164/*********************************************************************
2165 * POLICY INTERFACE *
2166 *********************************************************************/
2167
2168/**
2169 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
2170 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2171 * is written
1da177e4
LT
2172 *
2173 * Reads the current cpufreq policy.
2174 */
2175int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2176{
2177 struct cpufreq_policy *cpu_policy;
2178 if (!policy)
2179 return -EINVAL;
2180
2181 cpu_policy = cpufreq_cpu_get(cpu);
2182 if (!cpu_policy)
2183 return -EINVAL;
2184
d5b73cd8 2185 memcpy(policy, cpu_policy, sizeof(*policy));
1da177e4
LT
2186
2187 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
2188 return 0;
2189}
2190EXPORT_SYMBOL(cpufreq_get_policy);
2191
153d7f3f 2192/*
037ce839
VK
2193 * policy : current policy.
2194 * new_policy: policy to be set.
153d7f3f 2195 */
037ce839 2196static int cpufreq_set_policy(struct cpufreq_policy *policy,
3a3e9e06 2197 struct cpufreq_policy *new_policy)
1da177e4 2198{
d9a789c7
RW
2199 struct cpufreq_governor *old_gov;
2200 int ret;
1da177e4 2201
e837f9b5
JP
2202 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2203 new_policy->cpu, new_policy->min, new_policy->max);
1da177e4 2204
d5b73cd8 2205 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1da177e4 2206
d9a789c7
RW
2207 if (new_policy->min > policy->max || new_policy->max < policy->min)
2208 return -EINVAL;
9c9a43ed 2209
1da177e4 2210 /* verify the cpu speed can be set within this limit */
3a3e9e06 2211 ret = cpufreq_driver->verify(new_policy);
1da177e4 2212 if (ret)
d9a789c7 2213 return ret;
1da177e4 2214
1da177e4 2215 /* adjust if necessary - all reasons */
e041c683 2216 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2217 CPUFREQ_ADJUST, new_policy);
1da177e4
LT
2218
2219 /* adjust if necessary - hardware incompatibility*/
e041c683 2220 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2221 CPUFREQ_INCOMPATIBLE, new_policy);
1da177e4 2222
bb176f7d
VK
2223 /*
2224 * verify the cpu speed can be set within this limit, which might be
2225 * different to the first one
2226 */
3a3e9e06 2227 ret = cpufreq_driver->verify(new_policy);
e041c683 2228 if (ret)
d9a789c7 2229 return ret;
1da177e4
LT
2230
2231 /* notification of the new policy */
e041c683 2232 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 2233 CPUFREQ_NOTIFY, new_policy);
1da177e4 2234
3a3e9e06
VK
2235 policy->min = new_policy->min;
2236 policy->max = new_policy->max;
1da177e4 2237
2d06d8c4 2238 pr_debug("new min and max freqs are %u - %u kHz\n",
e837f9b5 2239 policy->min, policy->max);
1da177e4 2240
1c3d85dd 2241 if (cpufreq_driver->setpolicy) {
3a3e9e06 2242 policy->policy = new_policy->policy;
2d06d8c4 2243 pr_debug("setting range\n");
d9a789c7
RW
2244 return cpufreq_driver->setpolicy(new_policy);
2245 }
1da177e4 2246
d9a789c7
RW
2247 if (new_policy->governor == policy->governor)
2248 goto out;
7bd353a9 2249
d9a789c7
RW
2250 pr_debug("governor switch\n");
2251
2252 /* save old, working values */
2253 old_gov = policy->governor;
2254 /* end old governor */
2255 if (old_gov) {
2256 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2257 up_write(&policy->rwsem);
e5c87b76 2258 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
d9a789c7 2259 down_write(&policy->rwsem);
1da177e4
LT
2260 }
2261
d9a789c7
RW
2262 /* start new governor */
2263 policy->governor = new_policy->governor;
2264 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2265 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2266 goto out;
2267
2268 up_write(&policy->rwsem);
2269 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2270 down_write(&policy->rwsem);
2271 }
2272
2273 /* new governor failed, so re-start old one */
2274 pr_debug("starting governor %s failed\n", policy->governor->name);
2275 if (old_gov) {
2276 policy->governor = old_gov;
2277 __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2278 __cpufreq_governor(policy, CPUFREQ_GOV_START);
2279 }
2280
2281 return -EINVAL;
2282
2283 out:
2284 pr_debug("governor: change or update limits\n");
2285 return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1da177e4
LT
2286}
2287
1da177e4
LT
2288/**
2289 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2290 * @cpu: CPU which shall be re-evaluated
2291 *
25985edc 2292 * Useful for policy notifiers which have different necessities
1da177e4
LT
2293 * at different times.
2294 */
2295int cpufreq_update_policy(unsigned int cpu)
2296{
3a3e9e06
VK
2297 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2298 struct cpufreq_policy new_policy;
f1829e4a 2299 int ret;
1da177e4 2300
fefa8ff8
AP
2301 if (!policy)
2302 return -ENODEV;
1da177e4 2303
ad7722da 2304 down_write(&policy->rwsem);
1da177e4 2305
2d06d8c4 2306 pr_debug("updating policy for CPU %u\n", cpu);
d5b73cd8 2307 memcpy(&new_policy, policy, sizeof(*policy));
3a3e9e06
VK
2308 new_policy.min = policy->user_policy.min;
2309 new_policy.max = policy->user_policy.max;
2310 new_policy.policy = policy->user_policy.policy;
2311 new_policy.governor = policy->user_policy.governor;
1da177e4 2312
bb176f7d
VK
2313 /*
2314 * BIOS might change freq behind our back
2315 * -> ask driver for current freq and notify governors about a change
2316 */
2ed99e39 2317 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
3a3e9e06 2318 new_policy.cur = cpufreq_driver->get(cpu);
bd0fa9bb
VK
2319 if (WARN_ON(!new_policy.cur)) {
2320 ret = -EIO;
fefa8ff8 2321 goto unlock;
bd0fa9bb
VK
2322 }
2323
3a3e9e06 2324 if (!policy->cur) {
e837f9b5 2325 pr_debug("Driver did not initialize current freq\n");
3a3e9e06 2326 policy->cur = new_policy.cur;
a85f7bd3 2327 } else {
9c0ebcf7 2328 if (policy->cur != new_policy.cur && has_target())
a1e1dc41 2329 cpufreq_out_of_sync(policy, new_policy.cur);
a85f7bd3 2330 }
0961dd0d
TR
2331 }
2332
037ce839 2333 ret = cpufreq_set_policy(policy, &new_policy);
1da177e4 2334
fefa8ff8 2335unlock:
ad7722da 2336 up_write(&policy->rwsem);
5a01f2e8 2337
3a3e9e06 2338 cpufreq_cpu_put(policy);
1da177e4
LT
2339 return ret;
2340}
2341EXPORT_SYMBOL(cpufreq_update_policy);
2342
2760984f 2343static int cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
2344 unsigned long action, void *hcpu)
2345{
2346 unsigned int cpu = (unsigned long)hcpu;
8a25a2fd 2347 struct device *dev;
c32b6b8e 2348
8a25a2fd
KS
2349 dev = get_cpu_device(cpu);
2350 if (dev) {
5302c3fb 2351 switch (action & ~CPU_TASKS_FROZEN) {
c32b6b8e 2352 case CPU_ONLINE:
23faf0b7 2353 cpufreq_add_dev(dev, NULL);
c32b6b8e 2354 break;
5302c3fb 2355
c32b6b8e 2356 case CPU_DOWN_PREPARE:
96bbbe4a 2357 __cpufreq_remove_dev_prepare(dev, NULL);
1aee40ac
SB
2358 break;
2359
2360 case CPU_POST_DEAD:
96bbbe4a 2361 __cpufreq_remove_dev_finish(dev, NULL);
c32b6b8e 2362 break;
5302c3fb 2363
5a01f2e8 2364 case CPU_DOWN_FAILED:
23faf0b7 2365 cpufreq_add_dev(dev, NULL);
c32b6b8e
AR
2366 break;
2367 }
2368 }
2369 return NOTIFY_OK;
2370}
2371
9c36f746 2372static struct notifier_block __refdata cpufreq_cpu_notifier = {
bb176f7d 2373 .notifier_call = cpufreq_cpu_callback,
c32b6b8e 2374};
1da177e4 2375
6f19efc0
LM
2376/*********************************************************************
2377 * BOOST *
2378 *********************************************************************/
2379static int cpufreq_boost_set_sw(int state)
2380{
2381 struct cpufreq_frequency_table *freq_table;
2382 struct cpufreq_policy *policy;
2383 int ret = -EINVAL;
2384
f963735a 2385 for_each_active_policy(policy) {
6f19efc0
LM
2386 freq_table = cpufreq_frequency_get_table(policy->cpu);
2387 if (freq_table) {
2388 ret = cpufreq_frequency_table_cpuinfo(policy,
2389 freq_table);
2390 if (ret) {
2391 pr_err("%s: Policy frequency update failed\n",
2392 __func__);
2393 break;
2394 }
2395 policy->user_policy.max = policy->max;
2396 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2397 }
2398 }
2399
2400 return ret;
2401}
2402
2403int cpufreq_boost_trigger_state(int state)
2404{
2405 unsigned long flags;
2406 int ret = 0;
2407
2408 if (cpufreq_driver->boost_enabled == state)
2409 return 0;
2410
2411 write_lock_irqsave(&cpufreq_driver_lock, flags);
2412 cpufreq_driver->boost_enabled = state;
2413 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2414
2415 ret = cpufreq_driver->set_boost(state);
2416 if (ret) {
2417 write_lock_irqsave(&cpufreq_driver_lock, flags);
2418 cpufreq_driver->boost_enabled = !state;
2419 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2420
e837f9b5
JP
2421 pr_err("%s: Cannot %s BOOST\n",
2422 __func__, state ? "enable" : "disable");
6f19efc0
LM
2423 }
2424
2425 return ret;
2426}
2427
2428int cpufreq_boost_supported(void)
2429{
2430 if (likely(cpufreq_driver))
2431 return cpufreq_driver->boost_supported;
2432
2433 return 0;
2434}
2435EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2436
2437int cpufreq_boost_enabled(void)
2438{
2439 return cpufreq_driver->boost_enabled;
2440}
2441EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2442
1da177e4
LT
2443/*********************************************************************
2444 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2445 *********************************************************************/
2446
2447/**
2448 * cpufreq_register_driver - register a CPU Frequency driver
2449 * @driver_data: A struct cpufreq_driver containing the values#
2450 * submitted by the CPU Frequency driver.
2451 *
bb176f7d 2452 * Registers a CPU Frequency driver to this core code. This code
1da177e4 2453 * returns zero on success, -EBUSY when another driver got here first
32ee8c3e 2454 * (and isn't unregistered in the meantime).
1da177e4
LT
2455 *
2456 */
221dee28 2457int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
2458{
2459 unsigned long flags;
2460 int ret;
2461
a7b422cd
KRW
2462 if (cpufreq_disabled())
2463 return -ENODEV;
2464
1da177e4 2465 if (!driver_data || !driver_data->verify || !driver_data->init ||
9c0ebcf7 2466 !(driver_data->setpolicy || driver_data->target_index ||
9832235f
RW
2467 driver_data->target) ||
2468 (driver_data->setpolicy && (driver_data->target_index ||
1c03a2d0
VK
2469 driver_data->target)) ||
2470 (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
1da177e4
LT
2471 return -EINVAL;
2472
2d06d8c4 2473 pr_debug("trying to register driver %s\n", driver_data->name);
1da177e4 2474
0d1857a1 2475 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2476 if (cpufreq_driver) {
0d1857a1 2477 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4dea5806 2478 return -EEXIST;
1da177e4 2479 }
1c3d85dd 2480 cpufreq_driver = driver_data;
0d1857a1 2481 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 2482
bc68b7df
VK
2483 if (driver_data->setpolicy)
2484 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2485
6f19efc0
LM
2486 if (cpufreq_boost_supported()) {
2487 /*
2488 * Check if driver provides function to enable boost -
2489 * if not, use cpufreq_boost_set_sw as default
2490 */
2491 if (!cpufreq_driver->set_boost)
2492 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2493
2494 ret = cpufreq_sysfs_create_file(&boost.attr);
2495 if (ret) {
2496 pr_err("%s: cannot register global BOOST sysfs file\n",
e837f9b5 2497 __func__);
6f19efc0
LM
2498 goto err_null_driver;
2499 }
2500 }
2501
8a25a2fd 2502 ret = subsys_interface_register(&cpufreq_interface);
8f5bc2ab 2503 if (ret)
6f19efc0 2504 goto err_boost_unreg;
1da177e4 2505
ce1bcfe9
VK
2506 if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2507 list_empty(&cpufreq_policy_list)) {
1da177e4 2508 /* if all ->init() calls failed, unregister */
ce1bcfe9
VK
2509 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2510 driver_data->name);
2511 goto err_if_unreg;
1da177e4
LT
2512 }
2513
8f5bc2ab 2514 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2d06d8c4 2515 pr_debug("driver %s up and running\n", driver_data->name);
1da177e4 2516
8f5bc2ab 2517 return 0;
8a25a2fd
KS
2518err_if_unreg:
2519 subsys_interface_unregister(&cpufreq_interface);
6f19efc0
LM
2520err_boost_unreg:
2521 if (cpufreq_boost_supported())
2522 cpufreq_sysfs_remove_file(&boost.attr);
8f5bc2ab 2523err_null_driver:
0d1857a1 2524 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2525 cpufreq_driver = NULL;
0d1857a1 2526 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4d34a67d 2527 return ret;
1da177e4
LT
2528}
2529EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2530
1da177e4
LT
2531/**
2532 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2533 *
bb176f7d 2534 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
2535 * the right to do so, i.e. if you have succeeded in initialising before!
2536 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2537 * currently not initialised.
2538 */
221dee28 2539int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
2540{
2541 unsigned long flags;
2542
1c3d85dd 2543 if (!cpufreq_driver || (driver != cpufreq_driver))
1da177e4 2544 return -EINVAL;
1da177e4 2545
2d06d8c4 2546 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 2547
8a25a2fd 2548 subsys_interface_unregister(&cpufreq_interface);
6f19efc0
LM
2549 if (cpufreq_boost_supported())
2550 cpufreq_sysfs_remove_file(&boost.attr);
2551
65edc68c 2552 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4 2553
6eed9404 2554 down_write(&cpufreq_rwsem);
0d1857a1 2555 write_lock_irqsave(&cpufreq_driver_lock, flags);
6eed9404 2556
1c3d85dd 2557 cpufreq_driver = NULL;
6eed9404 2558
0d1857a1 2559 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
6eed9404 2560 up_write(&cpufreq_rwsem);
1da177e4
LT
2561
2562 return 0;
2563}
2564EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8 2565
90de2a4a
DA
2566/*
2567 * Stop cpufreq at shutdown to make sure it isn't holding any locks
2568 * or mutexes when secondary CPUs are halted.
2569 */
2570static struct syscore_ops cpufreq_syscore_ops = {
2571 .shutdown = cpufreq_suspend,
2572};
2573
5a01f2e8
VP
2574static int __init cpufreq_core_init(void)
2575{
a7b422cd
KRW
2576 if (cpufreq_disabled())
2577 return -ENODEV;
2578
2361be23 2579 cpufreq_global_kobject = kobject_create();
8aa84ad8
TR
2580 BUG_ON(!cpufreq_global_kobject);
2581
90de2a4a
DA
2582 register_syscore_ops(&cpufreq_syscore_ops);
2583
5a01f2e8
VP
2584 return 0;
2585}
5a01f2e8 2586core_initcall(cpufreq_core_init);
This page took 1.591704 seconds and 5 git commands to generate.