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