Merge remote-tracking branch 'asoc/topic/88pm860x' into asoc-next
[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>
e00e56df 29#include <linux/syscore_ops.h>
5ff0a268 30#include <linux/tick.h>
6f4f2723
TR
31#include <trace/events/power.h>
32
1da177e4 33/**
cd878479 34 * The "cpufreq driver" - the arch- or hardware-dependent low
1da177e4
LT
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
1c3d85dd 38static struct cpufreq_driver *cpufreq_driver;
7a6aedfa 39static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
8414809c 40static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
bb176f7d
VK
41static DEFINE_RWLOCK(cpufreq_driver_lock);
42static DEFINE_MUTEX(cpufreq_governor_lock);
c88a1f8b 43static LIST_HEAD(cpufreq_policy_list);
bb176f7d 44
084f3493
TR
45#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
e77b89f1 47static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
084f3493 48#endif
1da177e4 49
5a01f2e8
VP
50/*
51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
52 * all cpufreq/hotplug/workqueue/etc related lock issues.
53 *
54 * The rules for this semaphore:
55 * - Any routine that wants to read from the policy structure will
56 * do a down_read on this semaphore.
57 * - Any routine that will write to the policy structure and/or may take away
58 * the policy altogether (eg. CPU hotplug), will hold this lock in write
59 * mode before doing so.
60 *
61 * Additional rules:
5a01f2e8
VP
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
395913d0
MD
64 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
5a01f2e8 66 */
5a01f2e8
VP
67static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
68
69#define lock_policy_rwsem(mode, cpu) \
fa1d8af4 70static int lock_policy_rwsem_##mode(int cpu) \
5a01f2e8 71{ \
474deff7
VK
72 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
73 BUG_ON(!policy); \
74 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
5a01f2e8
VP
75 \
76 return 0; \
77}
78
79lock_policy_rwsem(read, cpu);
5a01f2e8 80lock_policy_rwsem(write, cpu);
5a01f2e8 81
fa1d8af4
VK
82#define unlock_policy_rwsem(mode, cpu) \
83static void unlock_policy_rwsem_##mode(int cpu) \
84{ \
474deff7
VK
85 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
86 BUG_ON(!policy); \
87 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
5a01f2e8 88}
5a01f2e8 89
fa1d8af4
VK
90unlock_policy_rwsem(read, cpu);
91unlock_policy_rwsem(write, cpu);
5a01f2e8 92
6eed9404
VK
93/*
94 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
95 * sections
96 */
97static DECLARE_RWSEM(cpufreq_rwsem);
98
1da177e4 99/* internal prototypes */
29464f28
DJ
100static int __cpufreq_governor(struct cpufreq_policy *policy,
101 unsigned int event);
5a01f2e8 102static unsigned int __cpufreq_get(unsigned int cpu);
65f27f38 103static void handle_update(struct work_struct *work);
1da177e4
LT
104
105/**
32ee8c3e
DJ
106 * Two notifier lists: the "policy" list is involved in the
107 * validation process for a new CPU frequency policy; the
1da177e4
LT
108 * "transition" list for kernel code that needs to handle
109 * changes to devices when the CPU clock speed changes.
110 * The mutex locks both lists.
111 */
e041c683 112static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
b4dfdbb3 113static struct srcu_notifier_head cpufreq_transition_notifier_list;
1da177e4 114
74212ca4 115static bool init_cpufreq_transition_notifier_list_called;
b4dfdbb3
AS
116static int __init init_cpufreq_transition_notifier_list(void)
117{
118 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
74212ca4 119 init_cpufreq_transition_notifier_list_called = true;
b4dfdbb3
AS
120 return 0;
121}
b3438f82 122pure_initcall(init_cpufreq_transition_notifier_list);
1da177e4 123
a7b422cd 124static int off __read_mostly;
da584455 125static int cpufreq_disabled(void)
a7b422cd
KRW
126{
127 return off;
128}
129void disable_cpufreq(void)
130{
131 off = 1;
132}
1da177e4 133static LIST_HEAD(cpufreq_governor_list);
29464f28 134static DEFINE_MUTEX(cpufreq_governor_mutex);
1da177e4 135
4d5dcc42
VK
136bool have_governor_per_policy(void)
137{
1c3d85dd 138 return cpufreq_driver->have_governor_per_policy;
4d5dcc42 139}
3f869d6d 140EXPORT_SYMBOL_GPL(have_governor_per_policy);
4d5dcc42 141
944e9a03
VK
142struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
143{
144 if (have_governor_per_policy())
145 return &policy->kobj;
146 else
147 return cpufreq_global_kobject;
148}
149EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
150
72a4ce34
VK
151static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
152{
153 u64 idle_time;
154 u64 cur_wall_time;
155 u64 busy_time;
156
157 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
158
159 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
160 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
161 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
162 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
165
166 idle_time = cur_wall_time - busy_time;
167 if (wall)
168 *wall = cputime_to_usecs(cur_wall_time);
169
170 return cputime_to_usecs(idle_time);
171}
172
173u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
174{
175 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
176
177 if (idle_time == -1ULL)
178 return get_cpu_idle_time_jiffy(cpu, wall);
179 else if (!io_busy)
180 idle_time += get_cpu_iowait_time_us(cpu, wall);
181
182 return idle_time;
183}
184EXPORT_SYMBOL_GPL(get_cpu_idle_time);
185
6eed9404 186struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1da177e4 187{
6eed9404 188 struct cpufreq_policy *policy = NULL;
1da177e4
LT
189 unsigned long flags;
190
6eed9404
VK
191 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
192 return NULL;
193
194 if (!down_read_trylock(&cpufreq_rwsem))
195 return NULL;
1da177e4
LT
196
197 /* get the cpufreq driver */
1c3d85dd 198 read_lock_irqsave(&cpufreq_driver_lock, flags);
1da177e4 199
6eed9404
VK
200 if (cpufreq_driver) {
201 /* get the CPU */
202 policy = per_cpu(cpufreq_cpu_data, cpu);
203 if (policy)
204 kobject_get(&policy->kobj);
205 }
1da177e4 206
6eed9404 207 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 208
3a3e9e06 209 if (!policy)
6eed9404 210 up_read(&cpufreq_rwsem);
1da177e4 211
3a3e9e06 212 return policy;
a9144436 213}
1da177e4
LT
214EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
215
3a3e9e06 216void cpufreq_cpu_put(struct cpufreq_policy *policy)
1da177e4 217{
d5aaffa9
DB
218 if (cpufreq_disabled())
219 return;
220
6eed9404
VK
221 kobject_put(&policy->kobj);
222 up_read(&cpufreq_rwsem);
1da177e4
LT
223}
224EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
225
1da177e4
LT
226/*********************************************************************
227 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
228 *********************************************************************/
229
230/**
231 * adjust_jiffies - adjust the system "loops_per_jiffy"
232 *
233 * This function alters the system "loops_per_jiffy" for the clock
234 * speed change. Note that loops_per_jiffy cannot be updated on SMP
32ee8c3e 235 * systems as each CPU might be scaled differently. So, use the arch
1da177e4
LT
236 * per-CPU loops_per_jiffy value wherever possible.
237 */
238#ifndef CONFIG_SMP
239static unsigned long l_p_j_ref;
bb176f7d 240static unsigned int l_p_j_ref_freq;
1da177e4 241
858119e1 242static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1da177e4
LT
243{
244 if (ci->flags & CPUFREQ_CONST_LOOPS)
245 return;
246
247 if (!l_p_j_ref_freq) {
248 l_p_j_ref = loops_per_jiffy;
249 l_p_j_ref_freq = ci->old;
2d06d8c4 250 pr_debug("saving %lu as reference value for loops_per_jiffy; "
e08f5f5b 251 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
1da177e4 252 }
bb176f7d 253 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
42d4dc3f 254 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
e08f5f5b
GS
255 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
256 ci->new);
2d06d8c4 257 pr_debug("scaling loops_per_jiffy to %lu "
e08f5f5b 258 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
1da177e4
LT
259 }
260}
261#else
e08f5f5b
GS
262static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
263{
264 return;
265}
1da177e4
LT
266#endif
267
0956df9c 268static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
b43a7ffb 269 struct cpufreq_freqs *freqs, unsigned int state)
1da177e4
LT
270{
271 BUG_ON(irqs_disabled());
272
d5aaffa9
DB
273 if (cpufreq_disabled())
274 return;
275
1c3d85dd 276 freqs->flags = cpufreq_driver->flags;
2d06d8c4 277 pr_debug("notification %u of frequency transition to %u kHz\n",
e4472cb3 278 state, freqs->new);
1da177e4 279
1da177e4 280 switch (state) {
e4472cb3 281
1da177e4 282 case CPUFREQ_PRECHANGE:
32ee8c3e 283 /* detect if the driver reported a value as "old frequency"
e4472cb3
DJ
284 * which is not equal to what the cpufreq core thinks is
285 * "old frequency".
1da177e4 286 */
1c3d85dd 287 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e4472cb3
DJ
288 if ((policy) && (policy->cpu == freqs->cpu) &&
289 (policy->cur) && (policy->cur != freqs->old)) {
2d06d8c4 290 pr_debug("Warning: CPU frequency is"
e4472cb3
DJ
291 " %u, cpufreq assumed %u kHz.\n",
292 freqs->old, policy->cur);
293 freqs->old = policy->cur;
1da177e4
LT
294 }
295 }
b4dfdbb3 296 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 297 CPUFREQ_PRECHANGE, freqs);
1da177e4
LT
298 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
299 break;
e4472cb3 300
1da177e4
LT
301 case CPUFREQ_POSTCHANGE:
302 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
2d06d8c4 303 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
6f4f2723 304 (unsigned long)freqs->cpu);
25e41933 305 trace_cpu_frequency(freqs->new, freqs->cpu);
b4dfdbb3 306 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 307 CPUFREQ_POSTCHANGE, freqs);
e4472cb3
DJ
308 if (likely(policy) && likely(policy->cpu == freqs->cpu))
309 policy->cur = freqs->new;
1da177e4
LT
310 break;
311 }
1da177e4 312}
bb176f7d 313
b43a7ffb
VK
314/**
315 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
316 * on frequency transition.
317 *
318 * This function calls the transition notifiers and the "adjust_jiffies"
319 * function. It is called twice on all CPU frequency changes that have
320 * external effects.
321 */
322void cpufreq_notify_transition(struct cpufreq_policy *policy,
323 struct cpufreq_freqs *freqs, unsigned int state)
324{
325 for_each_cpu(freqs->cpu, policy->cpus)
326 __cpufreq_notify_transition(policy, freqs, state);
327}
1da177e4
LT
328EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
329
330
1da177e4
LT
331/*********************************************************************
332 * SYSFS INTERFACE *
333 *********************************************************************/
334
3bcb09a3
JF
335static struct cpufreq_governor *__find_governor(const char *str_governor)
336{
337 struct cpufreq_governor *t;
338
339 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
29464f28 340 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3bcb09a3
JF
341 return t;
342
343 return NULL;
344}
345
1da177e4
LT
346/**
347 * cpufreq_parse_governor - parse a governor string
348 */
905d77cd 349static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
1da177e4
LT
350 struct cpufreq_governor **governor)
351{
3bcb09a3 352 int err = -EINVAL;
1c3d85dd
RW
353
354 if (!cpufreq_driver)
3bcb09a3
JF
355 goto out;
356
1c3d85dd 357 if (cpufreq_driver->setpolicy) {
1da177e4
LT
358 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
359 *policy = CPUFREQ_POLICY_PERFORMANCE;
3bcb09a3 360 err = 0;
e08f5f5b
GS
361 } else if (!strnicmp(str_governor, "powersave",
362 CPUFREQ_NAME_LEN)) {
1da177e4 363 *policy = CPUFREQ_POLICY_POWERSAVE;
3bcb09a3 364 err = 0;
1da177e4 365 }
1c3d85dd 366 } else if (cpufreq_driver->target) {
1da177e4 367 struct cpufreq_governor *t;
3bcb09a3 368
3fc54d37 369 mutex_lock(&cpufreq_governor_mutex);
3bcb09a3
JF
370
371 t = __find_governor(str_governor);
372
ea714970 373 if (t == NULL) {
1a8e1463 374 int ret;
ea714970 375
1a8e1463
KC
376 mutex_unlock(&cpufreq_governor_mutex);
377 ret = request_module("cpufreq_%s", str_governor);
378 mutex_lock(&cpufreq_governor_mutex);
ea714970 379
1a8e1463
KC
380 if (ret == 0)
381 t = __find_governor(str_governor);
ea714970
JF
382 }
383
3bcb09a3
JF
384 if (t != NULL) {
385 *governor = t;
386 err = 0;
1da177e4 387 }
3bcb09a3 388
3fc54d37 389 mutex_unlock(&cpufreq_governor_mutex);
1da177e4 390 }
29464f28 391out:
3bcb09a3 392 return err;
1da177e4 393}
1da177e4 394
1da177e4 395/**
e08f5f5b
GS
396 * cpufreq_per_cpu_attr_read() / show_##file_name() -
397 * print out cpufreq information
1da177e4
LT
398 *
399 * Write out information from cpufreq_driver->policy[cpu]; object must be
400 * "unsigned int".
401 */
402
32ee8c3e
DJ
403#define show_one(file_name, object) \
404static ssize_t show_##file_name \
905d77cd 405(struct cpufreq_policy *policy, char *buf) \
32ee8c3e 406{ \
29464f28 407 return sprintf(buf, "%u\n", policy->object); \
1da177e4
LT
408}
409
410show_one(cpuinfo_min_freq, cpuinfo.min_freq);
411show_one(cpuinfo_max_freq, cpuinfo.max_freq);
ed129784 412show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
1da177e4
LT
413show_one(scaling_min_freq, min);
414show_one(scaling_max_freq, max);
415show_one(scaling_cur_freq, cur);
416
3a3e9e06
VK
417static int __cpufreq_set_policy(struct cpufreq_policy *policy,
418 struct cpufreq_policy *new_policy);
7970e08b 419
1da177e4
LT
420/**
421 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
422 */
423#define store_one(file_name, object) \
424static ssize_t store_##file_name \
905d77cd 425(struct cpufreq_policy *policy, const char *buf, size_t count) \
1da177e4 426{ \
5136fa56 427 int ret; \
1da177e4
LT
428 struct cpufreq_policy new_policy; \
429 \
430 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
431 if (ret) \
432 return -EINVAL; \
433 \
29464f28 434 ret = sscanf(buf, "%u", &new_policy.object); \
1da177e4
LT
435 if (ret != 1) \
436 return -EINVAL; \
437 \
7970e08b
TR
438 ret = __cpufreq_set_policy(policy, &new_policy); \
439 policy->user_policy.object = policy->object; \
1da177e4
LT
440 \
441 return ret ? ret : count; \
442}
443
29464f28
DJ
444store_one(scaling_min_freq, min);
445store_one(scaling_max_freq, max);
1da177e4
LT
446
447/**
448 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
449 */
905d77cd
DJ
450static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
451 char *buf)
1da177e4 452{
5a01f2e8 453 unsigned int cur_freq = __cpufreq_get(policy->cpu);
1da177e4
LT
454 if (!cur_freq)
455 return sprintf(buf, "<unknown>");
456 return sprintf(buf, "%u\n", cur_freq);
457}
458
1da177e4
LT
459/**
460 * show_scaling_governor - show the current policy for the specified CPU
461 */
905d77cd 462static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
1da177e4 463{
29464f28 464 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
1da177e4
LT
465 return sprintf(buf, "powersave\n");
466 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
467 return sprintf(buf, "performance\n");
468 else if (policy->governor)
4b972f0b 469 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
29464f28 470 policy->governor->name);
1da177e4
LT
471 return -EINVAL;
472}
473
1da177e4
LT
474/**
475 * store_scaling_governor - store policy for the specified CPU
476 */
905d77cd
DJ
477static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
478 const char *buf, size_t count)
1da177e4 479{
5136fa56 480 int ret;
1da177e4
LT
481 char str_governor[16];
482 struct cpufreq_policy new_policy;
483
484 ret = cpufreq_get_policy(&new_policy, policy->cpu);
485 if (ret)
486 return ret;
487
29464f28 488 ret = sscanf(buf, "%15s", str_governor);
1da177e4
LT
489 if (ret != 1)
490 return -EINVAL;
491
e08f5f5b
GS
492 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
493 &new_policy.governor))
1da177e4
LT
494 return -EINVAL;
495
bb176f7d
VK
496 /*
497 * Do not use cpufreq_set_policy here or the user_policy.max
498 * will be wrongly overridden
499 */
7970e08b
TR
500 ret = __cpufreq_set_policy(policy, &new_policy);
501
502 policy->user_policy.policy = policy->policy;
503 policy->user_policy.governor = policy->governor;
7970e08b 504
e08f5f5b
GS
505 if (ret)
506 return ret;
507 else
508 return count;
1da177e4
LT
509}
510
511/**
512 * show_scaling_driver - show the cpufreq driver currently loaded
513 */
905d77cd 514static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
1da177e4 515{
1c3d85dd 516 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
1da177e4
LT
517}
518
519/**
520 * show_scaling_available_governors - show the available CPUfreq governors
521 */
905d77cd
DJ
522static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
523 char *buf)
1da177e4
LT
524{
525 ssize_t i = 0;
526 struct cpufreq_governor *t;
527
1c3d85dd 528 if (!cpufreq_driver->target) {
1da177e4
LT
529 i += sprintf(buf, "performance powersave");
530 goto out;
531 }
532
533 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
29464f28
DJ
534 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
535 - (CPUFREQ_NAME_LEN + 2)))
1da177e4 536 goto out;
4b972f0b 537 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
1da177e4 538 }
7d5e350f 539out:
1da177e4
LT
540 i += sprintf(&buf[i], "\n");
541 return i;
542}
e8628dd0 543
f4fd3797 544ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
1da177e4
LT
545{
546 ssize_t i = 0;
547 unsigned int cpu;
548
835481d9 549 for_each_cpu(cpu, mask) {
1da177e4
LT
550 if (i)
551 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
552 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
553 if (i >= (PAGE_SIZE - 5))
29464f28 554 break;
1da177e4
LT
555 }
556 i += sprintf(&buf[i], "\n");
557 return i;
558}
f4fd3797 559EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
1da177e4 560
e8628dd0
DW
561/**
562 * show_related_cpus - show the CPUs affected by each transition even if
563 * hw coordination is in use
564 */
565static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
566{
f4fd3797 567 return cpufreq_show_cpus(policy->related_cpus, buf);
e8628dd0
DW
568}
569
570/**
571 * show_affected_cpus - show the CPUs affected by each transition
572 */
573static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
574{
f4fd3797 575 return cpufreq_show_cpus(policy->cpus, buf);
e8628dd0
DW
576}
577
9e76988e 578static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
905d77cd 579 const char *buf, size_t count)
9e76988e
VP
580{
581 unsigned int freq = 0;
582 unsigned int ret;
583
879000f9 584 if (!policy->governor || !policy->governor->store_setspeed)
9e76988e
VP
585 return -EINVAL;
586
587 ret = sscanf(buf, "%u", &freq);
588 if (ret != 1)
589 return -EINVAL;
590
591 policy->governor->store_setspeed(policy, freq);
592
593 return count;
594}
595
596static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
597{
879000f9 598 if (!policy->governor || !policy->governor->show_setspeed)
9e76988e
VP
599 return sprintf(buf, "<unsupported>\n");
600
601 return policy->governor->show_setspeed(policy, buf);
602}
1da177e4 603
e2f74f35 604/**
8bf1ac72 605 * show_bios_limit - show the current cpufreq HW/BIOS limitation
e2f74f35
TR
606 */
607static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
608{
609 unsigned int limit;
610 int ret;
1c3d85dd
RW
611 if (cpufreq_driver->bios_limit) {
612 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
e2f74f35
TR
613 if (!ret)
614 return sprintf(buf, "%u\n", limit);
615 }
616 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
617}
618
6dad2a29
BP
619cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
620cpufreq_freq_attr_ro(cpuinfo_min_freq);
621cpufreq_freq_attr_ro(cpuinfo_max_freq);
622cpufreq_freq_attr_ro(cpuinfo_transition_latency);
623cpufreq_freq_attr_ro(scaling_available_governors);
624cpufreq_freq_attr_ro(scaling_driver);
625cpufreq_freq_attr_ro(scaling_cur_freq);
626cpufreq_freq_attr_ro(bios_limit);
627cpufreq_freq_attr_ro(related_cpus);
628cpufreq_freq_attr_ro(affected_cpus);
629cpufreq_freq_attr_rw(scaling_min_freq);
630cpufreq_freq_attr_rw(scaling_max_freq);
631cpufreq_freq_attr_rw(scaling_governor);
632cpufreq_freq_attr_rw(scaling_setspeed);
1da177e4 633
905d77cd 634static struct attribute *default_attrs[] = {
1da177e4
LT
635 &cpuinfo_min_freq.attr,
636 &cpuinfo_max_freq.attr,
ed129784 637 &cpuinfo_transition_latency.attr,
1da177e4
LT
638 &scaling_min_freq.attr,
639 &scaling_max_freq.attr,
640 &affected_cpus.attr,
e8628dd0 641 &related_cpus.attr,
1da177e4
LT
642 &scaling_governor.attr,
643 &scaling_driver.attr,
644 &scaling_available_governors.attr,
9e76988e 645 &scaling_setspeed.attr,
1da177e4
LT
646 NULL
647};
648
29464f28
DJ
649#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
650#define to_attr(a) container_of(a, struct freq_attr, attr)
1da177e4 651
29464f28 652static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1da177e4 653{
905d77cd
DJ
654 struct cpufreq_policy *policy = to_policy(kobj);
655 struct freq_attr *fattr = to_attr(attr);
0db4a8a9 656 ssize_t ret = -EINVAL;
6eed9404
VK
657
658 if (!down_read_trylock(&cpufreq_rwsem))
659 goto exit;
5a01f2e8
VP
660
661 if (lock_policy_rwsem_read(policy->cpu) < 0)
6eed9404 662 goto up_read;
5a01f2e8 663
e08f5f5b
GS
664 if (fattr->show)
665 ret = fattr->show(policy, buf);
666 else
667 ret = -EIO;
668
5a01f2e8 669 unlock_policy_rwsem_read(policy->cpu);
6eed9404
VK
670
671up_read:
672 up_read(&cpufreq_rwsem);
673exit:
1da177e4
LT
674 return ret;
675}
676
905d77cd
DJ
677static ssize_t store(struct kobject *kobj, struct attribute *attr,
678 const char *buf, size_t count)
1da177e4 679{
905d77cd
DJ
680 struct cpufreq_policy *policy = to_policy(kobj);
681 struct freq_attr *fattr = to_attr(attr);
a07530b4 682 ssize_t ret = -EINVAL;
6eed9404 683
4f750c93
SB
684 get_online_cpus();
685
686 if (!cpu_online(policy->cpu))
687 goto unlock;
688
6eed9404 689 if (!down_read_trylock(&cpufreq_rwsem))
4f750c93 690 goto unlock;
5a01f2e8
VP
691
692 if (lock_policy_rwsem_write(policy->cpu) < 0)
6eed9404 693 goto up_read;
5a01f2e8 694
e08f5f5b
GS
695 if (fattr->store)
696 ret = fattr->store(policy, buf, count);
697 else
698 ret = -EIO;
699
5a01f2e8 700 unlock_policy_rwsem_write(policy->cpu);
6eed9404
VK
701
702up_read:
703 up_read(&cpufreq_rwsem);
4f750c93
SB
704unlock:
705 put_online_cpus();
706
1da177e4
LT
707 return ret;
708}
709
905d77cd 710static void cpufreq_sysfs_release(struct kobject *kobj)
1da177e4 711{
905d77cd 712 struct cpufreq_policy *policy = to_policy(kobj);
2d06d8c4 713 pr_debug("last reference is dropped\n");
1da177e4
LT
714 complete(&policy->kobj_unregister);
715}
716
52cf25d0 717static const struct sysfs_ops sysfs_ops = {
1da177e4
LT
718 .show = show,
719 .store = store,
720};
721
722static struct kobj_type ktype_cpufreq = {
723 .sysfs_ops = &sysfs_ops,
724 .default_attrs = default_attrs,
725 .release = cpufreq_sysfs_release,
726};
727
2361be23
VK
728struct kobject *cpufreq_global_kobject;
729EXPORT_SYMBOL(cpufreq_global_kobject);
730
731static int cpufreq_global_kobject_usage;
732
733int cpufreq_get_global_kobject(void)
734{
735 if (!cpufreq_global_kobject_usage++)
736 return kobject_add(cpufreq_global_kobject,
737 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
738
739 return 0;
740}
741EXPORT_SYMBOL(cpufreq_get_global_kobject);
742
743void cpufreq_put_global_kobject(void)
744{
745 if (!--cpufreq_global_kobject_usage)
746 kobject_del(cpufreq_global_kobject);
747}
748EXPORT_SYMBOL(cpufreq_put_global_kobject);
749
750int cpufreq_sysfs_create_file(const struct attribute *attr)
751{
752 int ret = cpufreq_get_global_kobject();
753
754 if (!ret) {
755 ret = sysfs_create_file(cpufreq_global_kobject, attr);
756 if (ret)
757 cpufreq_put_global_kobject();
758 }
759
760 return ret;
761}
762EXPORT_SYMBOL(cpufreq_sysfs_create_file);
763
764void cpufreq_sysfs_remove_file(const struct attribute *attr)
765{
766 sysfs_remove_file(cpufreq_global_kobject, attr);
767 cpufreq_put_global_kobject();
768}
769EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
770
19d6f7ec 771/* symlink affected CPUs */
308b60e7 772static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
19d6f7ec
DJ
773{
774 unsigned int j;
775 int ret = 0;
776
777 for_each_cpu(j, policy->cpus) {
8a25a2fd 778 struct device *cpu_dev;
19d6f7ec 779
308b60e7 780 if (j == policy->cpu)
19d6f7ec 781 continue;
19d6f7ec 782
e8fdde10 783 pr_debug("Adding link for CPU: %u\n", j);
8a25a2fd
KS
784 cpu_dev = get_cpu_device(j);
785 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
19d6f7ec 786 "cpufreq");
71c3461e
RW
787 if (ret)
788 break;
19d6f7ec
DJ
789 }
790 return ret;
791}
792
308b60e7 793static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8a25a2fd 794 struct device *dev)
909a694e
DJ
795{
796 struct freq_attr **drv_attr;
909a694e 797 int ret = 0;
909a694e
DJ
798
799 /* prepare interface data */
800 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8a25a2fd 801 &dev->kobj, "cpufreq");
909a694e
DJ
802 if (ret)
803 return ret;
804
805 /* set up files for this cpu device */
1c3d85dd 806 drv_attr = cpufreq_driver->attr;
909a694e
DJ
807 while ((drv_attr) && (*drv_attr)) {
808 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
809 if (ret)
1c3d85dd 810 goto err_out_kobj_put;
909a694e
DJ
811 drv_attr++;
812 }
1c3d85dd 813 if (cpufreq_driver->get) {
909a694e
DJ
814 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
815 if (ret)
1c3d85dd 816 goto err_out_kobj_put;
909a694e 817 }
1c3d85dd 818 if (cpufreq_driver->target) {
909a694e
DJ
819 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
820 if (ret)
1c3d85dd 821 goto err_out_kobj_put;
909a694e 822 }
1c3d85dd 823 if (cpufreq_driver->bios_limit) {
e2f74f35
TR
824 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
825 if (ret)
1c3d85dd 826 goto err_out_kobj_put;
e2f74f35 827 }
909a694e 828
308b60e7 829 ret = cpufreq_add_dev_symlink(policy);
ecf7e461
DJ
830 if (ret)
831 goto err_out_kobj_put;
832
e18f1682
SB
833 return ret;
834
835err_out_kobj_put:
836 kobject_put(&policy->kobj);
837 wait_for_completion(&policy->kobj_unregister);
838 return ret;
839}
840
841static void cpufreq_init_policy(struct cpufreq_policy *policy)
842{
843 struct cpufreq_policy new_policy;
844 int ret = 0;
845
d5b73cd8 846 memcpy(&new_policy, policy, sizeof(*policy));
ecf7e461
DJ
847 /* assure that the starting sequence is run in __cpufreq_set_policy */
848 policy->governor = NULL;
849
850 /* set default policy */
851 ret = __cpufreq_set_policy(policy, &new_policy);
852 policy->user_policy.policy = policy->policy;
853 policy->user_policy.governor = policy->governor;
854
855 if (ret) {
2d06d8c4 856 pr_debug("setting policy failed\n");
1c3d85dd
RW
857 if (cpufreq_driver->exit)
858 cpufreq_driver->exit(policy);
ecf7e461 859 }
909a694e
DJ
860}
861
fcf80582 862#ifdef CONFIG_HOTPLUG_CPU
d8d3b471
VK
863static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
864 unsigned int cpu, struct device *dev,
865 bool frozen)
fcf80582 866{
1c3d85dd 867 int ret = 0, has_target = !!cpufreq_driver->target;
fcf80582
VK
868 unsigned long flags;
869
3de9bdeb
VK
870 if (has_target) {
871 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
872 if (ret) {
873 pr_err("%s: Failed to stop governor\n", __func__);
874 return ret;
875 }
876 }
fcf80582 877
d8d3b471 878 lock_policy_rwsem_write(policy->cpu);
2eaa3e2d 879
0d1857a1 880 write_lock_irqsave(&cpufreq_driver_lock, flags);
2eaa3e2d 881
fcf80582
VK
882 cpumask_set_cpu(cpu, policy->cpus);
883 per_cpu(cpufreq_cpu_data, cpu) = policy;
0d1857a1 884 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
fcf80582 885
d8d3b471 886 unlock_policy_rwsem_write(policy->cpu);
2eaa3e2d 887
820c6ca2 888 if (has_target) {
3de9bdeb
VK
889 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
890 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
891 pr_err("%s: Failed to start governor\n", __func__);
892 return ret;
893 }
820c6ca2 894 }
fcf80582 895
a82fab29 896 /* Don't touch sysfs links during light-weight init */
71c3461e
RW
897 if (!frozen)
898 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
a82fab29
SB
899
900 return ret;
fcf80582
VK
901}
902#endif
1da177e4 903
8414809c
SB
904static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
905{
906 struct cpufreq_policy *policy;
907 unsigned long flags;
908
44871c9c 909 read_lock_irqsave(&cpufreq_driver_lock, flags);
8414809c
SB
910
911 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
912
44871c9c 913 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
8414809c
SB
914
915 return policy;
916}
917
e9698cc5
SB
918static struct cpufreq_policy *cpufreq_policy_alloc(void)
919{
920 struct cpufreq_policy *policy;
921
922 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
923 if (!policy)
924 return NULL;
925
926 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
927 goto err_free_policy;
928
929 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
930 goto err_free_cpumask;
931
c88a1f8b 932 INIT_LIST_HEAD(&policy->policy_list);
e9698cc5
SB
933 return policy;
934
935err_free_cpumask:
936 free_cpumask_var(policy->cpus);
937err_free_policy:
938 kfree(policy);
939
940 return NULL;
941}
942
943static void cpufreq_policy_free(struct cpufreq_policy *policy)
944{
945 free_cpumask_var(policy->related_cpus);
946 free_cpumask_var(policy->cpus);
947 kfree(policy);
948}
949
0d66b91e
SB
950static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
951{
cb38ed5c
SB
952 if (cpu == policy->cpu)
953 return;
954
8efd5765
VK
955 /*
956 * Take direct locks as lock_policy_rwsem_write wouldn't work here.
957 * Also lock for last cpu is enough here as contention will happen only
958 * after policy->cpu is changed and after it is changed, other threads
959 * will try to acquire lock for new cpu. And policy is already updated
960 * by then.
961 */
962 down_write(&per_cpu(cpu_policy_rwsem, policy->cpu));
963
0d66b91e
SB
964 policy->last_cpu = policy->cpu;
965 policy->cpu = cpu;
966
8efd5765
VK
967 up_write(&per_cpu(cpu_policy_rwsem, policy->last_cpu));
968
0d66b91e
SB
969#ifdef CONFIG_CPU_FREQ_TABLE
970 cpufreq_frequency_table_update_policy_cpu(policy);
971#endif
972 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
973 CPUFREQ_UPDATE_POLICY_CPU, policy);
974}
975
a82fab29
SB
976static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
977 bool frozen)
1da177e4 978{
fcf80582 979 unsigned int j, cpu = dev->id;
65922465 980 int ret = -ENOMEM;
1da177e4 981 struct cpufreq_policy *policy;
1da177e4 982 unsigned long flags;
90e41bac 983#ifdef CONFIG_HOTPLUG_CPU
1b274294 984 struct cpufreq_policy *tpolicy;
fcf80582 985 struct cpufreq_governor *gov;
90e41bac 986#endif
1da177e4 987
c32b6b8e
AR
988 if (cpu_is_offline(cpu))
989 return 0;
990
2d06d8c4 991 pr_debug("adding CPU %u\n", cpu);
1da177e4
LT
992
993#ifdef CONFIG_SMP
994 /* check whether a different CPU already registered this
995 * CPU because it is in the same boat. */
996 policy = cpufreq_cpu_get(cpu);
997 if (unlikely(policy)) {
8ff69732 998 cpufreq_cpu_put(policy);
1da177e4
LT
999 return 0;
1000 }
5025d628 1001#endif
fcf80582 1002
6eed9404
VK
1003 if (!down_read_trylock(&cpufreq_rwsem))
1004 return 0;
1005
fcf80582
VK
1006#ifdef CONFIG_HOTPLUG_CPU
1007 /* Check if this cpu was hot-unplugged earlier and has siblings */
0d1857a1 1008 read_lock_irqsave(&cpufreq_driver_lock, flags);
1b274294
VK
1009 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1010 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
0d1857a1 1011 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1b274294 1012 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
6eed9404
VK
1013 up_read(&cpufreq_rwsem);
1014 return ret;
2eaa3e2d 1015 }
fcf80582 1016 }
0d1857a1 1017 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
1018#endif
1019
8414809c
SB
1020 if (frozen)
1021 /* Restore the saved policy when doing light-weight init */
1022 policy = cpufreq_policy_restore(cpu);
1023 else
1024 policy = cpufreq_policy_alloc();
1025
059019a3 1026 if (!policy)
1da177e4 1027 goto nomem_out;
059019a3 1028
0d66b91e
SB
1029
1030 /*
1031 * In the resume path, since we restore a saved policy, the assignment
1032 * to policy->cpu is like an update of the existing policy, rather than
1033 * the creation of a brand new one. So we need to perform this update
1034 * by invoking update_policy_cpu().
1035 */
1036 if (frozen && cpu != policy->cpu)
1037 update_policy_cpu(policy, cpu);
1038 else
1039 policy->cpu = cpu;
1040
65922465 1041 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
835481d9 1042 cpumask_copy(policy->cpus, cpumask_of(cpu));
1da177e4 1043
1da177e4 1044 init_completion(&policy->kobj_unregister);
65f27f38 1045 INIT_WORK(&policy->update, handle_update);
1da177e4
LT
1046
1047 /* call driver. From then on the cpufreq must be able
1048 * to accept all calls to ->verify and ->setpolicy for this CPU
1049 */
1c3d85dd 1050 ret = cpufreq_driver->init(policy);
1da177e4 1051 if (ret) {
2d06d8c4 1052 pr_debug("initialization failed\n");
2eaa3e2d 1053 goto err_set_policy_cpu;
1da177e4 1054 }
643ae6e8 1055
fcf80582
VK
1056 /* related cpus should atleast have policy->cpus */
1057 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1058
643ae6e8
VK
1059 /*
1060 * affected cpus must always be the one, which are online. We aren't
1061 * managing offline cpus here.
1062 */
1063 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1064
187d9f4e
MC
1065 policy->user_policy.min = policy->min;
1066 policy->user_policy.max = policy->max;
1da177e4 1067
a1531acd
TR
1068 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1069 CPUFREQ_START, policy);
1070
fcf80582
VK
1071#ifdef CONFIG_HOTPLUG_CPU
1072 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1073 if (gov) {
1074 policy->governor = gov;
1075 pr_debug("Restoring governor %s for cpu %d\n",
1076 policy->governor->name, cpu);
4bfa042c 1077 }
fcf80582 1078#endif
1da177e4 1079
e18f1682 1080 write_lock_irqsave(&cpufreq_driver_lock, flags);
474deff7 1081 for_each_cpu(j, policy->cpus)
e18f1682 1082 per_cpu(cpufreq_cpu_data, j) = policy;
e18f1682
SB
1083 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1084
a82fab29 1085 if (!frozen) {
308b60e7 1086 ret = cpufreq_add_dev_interface(policy, dev);
a82fab29
SB
1087 if (ret)
1088 goto err_out_unregister;
1089 }
8ff69732 1090
9515f4d6
VK
1091 write_lock_irqsave(&cpufreq_driver_lock, flags);
1092 list_add(&policy->policy_list, &cpufreq_policy_list);
1093 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1094
e18f1682
SB
1095 cpufreq_init_policy(policy);
1096
038c5b3e 1097 kobject_uevent(&policy->kobj, KOBJ_ADD);
6eed9404
VK
1098 up_read(&cpufreq_rwsem);
1099
2d06d8c4 1100 pr_debug("initialization complete\n");
87c32271 1101
1da177e4
LT
1102 return 0;
1103
1da177e4 1104err_out_unregister:
0d1857a1 1105 write_lock_irqsave(&cpufreq_driver_lock, flags);
474deff7 1106 for_each_cpu(j, policy->cpus)
7a6aedfa 1107 per_cpu(cpufreq_cpu_data, j) = NULL;
0d1857a1 1108 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1109
2eaa3e2d 1110err_set_policy_cpu:
e9698cc5 1111 cpufreq_policy_free(policy);
1da177e4 1112nomem_out:
6eed9404
VK
1113 up_read(&cpufreq_rwsem);
1114
1da177e4
LT
1115 return ret;
1116}
1117
a82fab29
SB
1118/**
1119 * cpufreq_add_dev - add a CPU device
1120 *
1121 * Adds the cpufreq interface for a CPU device.
1122 *
1123 * The Oracle says: try running cpufreq registration/unregistration concurrently
1124 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1125 * mess up, but more thorough testing is needed. - Mathieu
1126 */
1127static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1128{
1129 return __cpufreq_add_dev(dev, sif, false);
1130}
1131
3a3e9e06 1132static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
a82fab29 1133 unsigned int old_cpu, bool frozen)
f9ba680d
SB
1134{
1135 struct device *cpu_dev;
f9ba680d
SB
1136 int ret;
1137
1138 /* first sibling now owns the new sysfs dir */
9c8f1ee4 1139 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
a82fab29
SB
1140
1141 /* Don't touch sysfs files during light-weight tear-down */
1142 if (frozen)
1143 return cpu_dev->id;
1144
f9ba680d 1145 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
3a3e9e06 1146 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
f9ba680d
SB
1147 if (ret) {
1148 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1149
1150 WARN_ON(lock_policy_rwsem_write(old_cpu));
3a3e9e06 1151 cpumask_set_cpu(old_cpu, policy->cpus);
f9ba680d
SB
1152 unlock_policy_rwsem_write(old_cpu);
1153
3a3e9e06 1154 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
f9ba680d
SB
1155 "cpufreq");
1156
1157 return -EINVAL;
1158 }
1159
1160 return cpu_dev->id;
1161}
1162
cedb70af
SB
1163static int __cpufreq_remove_dev_prepare(struct device *dev,
1164 struct subsys_interface *sif,
1165 bool frozen)
1da177e4 1166{
f9ba680d 1167 unsigned int cpu = dev->id, cpus;
3de9bdeb 1168 int new_cpu, ret;
1da177e4 1169 unsigned long flags;
3a3e9e06 1170 struct cpufreq_policy *policy;
1da177e4 1171
b8eed8af 1172 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1da177e4 1173
0d1857a1 1174 write_lock_irqsave(&cpufreq_driver_lock, flags);
2eaa3e2d 1175
3a3e9e06 1176 policy = per_cpu(cpufreq_cpu_data, cpu);
2eaa3e2d 1177
8414809c
SB
1178 /* Save the policy somewhere when doing a light-weight tear-down */
1179 if (frozen)
3a3e9e06 1180 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
8414809c 1181
0d1857a1 1182 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1183
3a3e9e06 1184 if (!policy) {
b8eed8af 1185 pr_debug("%s: No cpu_data found\n", __func__);
1da177e4
LT
1186 return -EINVAL;
1187 }
1da177e4 1188
3de9bdeb
VK
1189 if (cpufreq_driver->target) {
1190 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1191 if (ret) {
1192 pr_err("%s: Failed to stop governor\n", __func__);
1193 return ret;
1194 }
1195 }
1da177e4 1196
084f3493 1197#ifdef CONFIG_HOTPLUG_CPU
1c3d85dd 1198 if (!cpufreq_driver->setpolicy)
fa69e33f 1199 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
3a3e9e06 1200 policy->governor->name, CPUFREQ_NAME_LEN);
1da177e4
LT
1201#endif
1202
9c8f1ee4 1203 lock_policy_rwsem_read(cpu);
3a3e9e06 1204 cpus = cpumask_weight(policy->cpus);
9c8f1ee4 1205 unlock_policy_rwsem_read(cpu);
084f3493 1206
61173f25
SB
1207 if (cpu != policy->cpu) {
1208 if (!frozen)
1209 sysfs_remove_link(&dev->kobj, "cpufreq");
73bf0fc2 1210 } else if (cpus > 1) {
084f3493 1211
3a3e9e06 1212 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
f9ba680d 1213 if (new_cpu >= 0) {
3a3e9e06 1214 update_policy_cpu(policy, new_cpu);
a82fab29
SB
1215
1216 if (!frozen) {
1217 pr_debug("%s: policy Kobject moved to cpu: %d "
1218 "from: %d\n",__func__, new_cpu, cpu);
1219 }
1da177e4
LT
1220 }
1221 }
1da177e4 1222
cedb70af
SB
1223 return 0;
1224}
1225
1226static int __cpufreq_remove_dev_finish(struct device *dev,
1227 struct subsys_interface *sif,
1228 bool frozen)
1229{
1230 unsigned int cpu = dev->id, cpus;
1231 int ret;
1232 unsigned long flags;
1233 struct cpufreq_policy *policy;
1234 struct kobject *kobj;
1235 struct completion *cmp;
1236
1237 read_lock_irqsave(&cpufreq_driver_lock, flags);
1238 policy = per_cpu(cpufreq_cpu_data, cpu);
1239 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1240
1241 if (!policy) {
1242 pr_debug("%s: No cpu_data found\n", __func__);
1243 return -EINVAL;
1244 }
1245
9c8f1ee4 1246 WARN_ON(lock_policy_rwsem_write(cpu));
cedb70af 1247 cpus = cpumask_weight(policy->cpus);
9c8f1ee4
VK
1248
1249 if (cpus > 1)
1250 cpumask_clear_cpu(cpu, policy->cpus);
1251 unlock_policy_rwsem_write(cpu);
cedb70af 1252
b8eed8af
VK
1253 /* If cpu is last user of policy, free policy */
1254 if (cpus == 1) {
3de9bdeb
VK
1255 if (cpufreq_driver->target) {
1256 ret = __cpufreq_governor(policy,
1257 CPUFREQ_GOV_POLICY_EXIT);
1258 if (ret) {
1259 pr_err("%s: Failed to exit governor\n",
1260 __func__);
1261 return ret;
1262 }
edab2fbc 1263 }
2a998599 1264
8414809c
SB
1265 if (!frozen) {
1266 lock_policy_rwsem_read(cpu);
3a3e9e06
VK
1267 kobj = &policy->kobj;
1268 cmp = &policy->kobj_unregister;
8414809c
SB
1269 unlock_policy_rwsem_read(cpu);
1270 kobject_put(kobj);
1271
1272 /*
1273 * We need to make sure that the underlying kobj is
1274 * actually not referenced anymore by anybody before we
1275 * proceed with unloading.
1276 */
1277 pr_debug("waiting for dropping of refcount\n");
1278 wait_for_completion(cmp);
1279 pr_debug("wait complete\n");
1280 }
7d26e2d5 1281
8414809c
SB
1282 /*
1283 * Perform the ->exit() even during light-weight tear-down,
1284 * since this is a core component, and is essential for the
1285 * subsequent light-weight ->init() to succeed.
b8eed8af 1286 */
1c3d85dd 1287 if (cpufreq_driver->exit)
3a3e9e06 1288 cpufreq_driver->exit(policy);
27ecddc2 1289
9515f4d6
VK
1290 /* Remove policy from list of active policies */
1291 write_lock_irqsave(&cpufreq_driver_lock, flags);
1292 list_del(&policy->policy_list);
1293 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1294
8414809c 1295 if (!frozen)
3a3e9e06 1296 cpufreq_policy_free(policy);
2a998599 1297 } else {
2a998599 1298 if (cpufreq_driver->target) {
3de9bdeb
VK
1299 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1300 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1301 pr_err("%s: Failed to start governor\n",
1302 __func__);
1303 return ret;
1304 }
2a998599 1305 }
27ecddc2 1306 }
1da177e4 1307
474deff7 1308 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1da177e4
LT
1309 return 0;
1310}
1311
cedb70af
SB
1312/**
1313 * __cpufreq_remove_dev - remove a CPU device
1314 *
1315 * Removes the cpufreq interface for a CPU device.
1316 * Caller should already have policy_rwsem in write mode for this CPU.
1317 * This routine frees the rwsem before returning.
1318 */
1319static inline int __cpufreq_remove_dev(struct device *dev,
1320 struct subsys_interface *sif,
1321 bool frozen)
1322{
1323 int ret;
1324
1325 ret = __cpufreq_remove_dev_prepare(dev, sif, frozen);
1326
1327 if (!ret)
1328 ret = __cpufreq_remove_dev_finish(dev, sif, frozen);
1329
1330 return ret;
1331}
1332
8a25a2fd 1333static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
5a01f2e8 1334{
8a25a2fd 1335 unsigned int cpu = dev->id;
5a01f2e8 1336 int retval;
ec28297a
VP
1337
1338 if (cpu_is_offline(cpu))
1339 return 0;
1340
a82fab29 1341 retval = __cpufreq_remove_dev(dev, sif, false);
5a01f2e8
VP
1342 return retval;
1343}
1344
65f27f38 1345static void handle_update(struct work_struct *work)
1da177e4 1346{
65f27f38
DH
1347 struct cpufreq_policy *policy =
1348 container_of(work, struct cpufreq_policy, update);
1349 unsigned int cpu = policy->cpu;
2d06d8c4 1350 pr_debug("handle_update for cpu %u called\n", cpu);
1da177e4
LT
1351 cpufreq_update_policy(cpu);
1352}
1353
1354/**
bb176f7d
VK
1355 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1356 * in deep trouble.
1da177e4
LT
1357 * @cpu: cpu number
1358 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1359 * @new_freq: CPU frequency the CPU actually runs at
1360 *
29464f28
DJ
1361 * We adjust to current frequency first, and need to clean up later.
1362 * So either call to cpufreq_update_policy() or schedule handle_update()).
1da177e4 1363 */
e08f5f5b
GS
1364static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1365 unsigned int new_freq)
1da177e4 1366{
b43a7ffb 1367 struct cpufreq_policy *policy;
1da177e4 1368 struct cpufreq_freqs freqs;
b43a7ffb
VK
1369 unsigned long flags;
1370
2d06d8c4 1371 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1da177e4
LT
1372 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1373
1da177e4
LT
1374 freqs.old = old_freq;
1375 freqs.new = new_freq;
b43a7ffb
VK
1376
1377 read_lock_irqsave(&cpufreq_driver_lock, flags);
1378 policy = per_cpu(cpufreq_cpu_data, cpu);
1379 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1380
1381 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1382 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1da177e4
LT
1383}
1384
32ee8c3e 1385/**
4ab70df4 1386 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
95235ca2
VP
1387 * @cpu: CPU number
1388 *
1389 * This is the last known freq, without actually getting it from the driver.
1390 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1391 */
1392unsigned int cpufreq_quick_get(unsigned int cpu)
1393{
9e21ba8b 1394 struct cpufreq_policy *policy;
e08f5f5b 1395 unsigned int ret_freq = 0;
95235ca2 1396
1c3d85dd
RW
1397 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1398 return cpufreq_driver->get(cpu);
9e21ba8b
DB
1399
1400 policy = cpufreq_cpu_get(cpu);
95235ca2 1401 if (policy) {
e08f5f5b 1402 ret_freq = policy->cur;
95235ca2
VP
1403 cpufreq_cpu_put(policy);
1404 }
1405
4d34a67d 1406 return ret_freq;
95235ca2
VP
1407}
1408EXPORT_SYMBOL(cpufreq_quick_get);
1409
3d737108
JB
1410/**
1411 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1412 * @cpu: CPU number
1413 *
1414 * Just return the max possible frequency for a given CPU.
1415 */
1416unsigned int cpufreq_quick_get_max(unsigned int cpu)
1417{
1418 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1419 unsigned int ret_freq = 0;
1420
1421 if (policy) {
1422 ret_freq = policy->max;
1423 cpufreq_cpu_put(policy);
1424 }
1425
1426 return ret_freq;
1427}
1428EXPORT_SYMBOL(cpufreq_quick_get_max);
1429
5a01f2e8 1430static unsigned int __cpufreq_get(unsigned int cpu)
1da177e4 1431{
7a6aedfa 1432 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
e08f5f5b 1433 unsigned int ret_freq = 0;
5800043b 1434
1c3d85dd 1435 if (!cpufreq_driver->get)
4d34a67d 1436 return ret_freq;
1da177e4 1437
1c3d85dd 1438 ret_freq = cpufreq_driver->get(cpu);
1da177e4 1439
e08f5f5b 1440 if (ret_freq && policy->cur &&
1c3d85dd 1441 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e08f5f5b
GS
1442 /* verify no discrepancy between actual and
1443 saved value exists */
1444 if (unlikely(ret_freq != policy->cur)) {
1445 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1da177e4
LT
1446 schedule_work(&policy->update);
1447 }
1448 }
1449
4d34a67d 1450 return ret_freq;
5a01f2e8 1451}
1da177e4 1452
5a01f2e8
VP
1453/**
1454 * cpufreq_get - get the current CPU frequency (in kHz)
1455 * @cpu: CPU number
1456 *
1457 * Get the CPU current (static) CPU frequency
1458 */
1459unsigned int cpufreq_get(unsigned int cpu)
1460{
1461 unsigned int ret_freq = 0;
5a01f2e8 1462
26ca8694
VK
1463 if (cpufreq_disabled() || !cpufreq_driver)
1464 return -ENOENT;
1465
6eed9404
VK
1466 if (!down_read_trylock(&cpufreq_rwsem))
1467 return 0;
5a01f2e8
VP
1468
1469 if (unlikely(lock_policy_rwsem_read(cpu)))
1470 goto out_policy;
1471
1472 ret_freq = __cpufreq_get(cpu);
1473
1474 unlock_policy_rwsem_read(cpu);
1da177e4 1475
5a01f2e8 1476out_policy:
6eed9404
VK
1477 up_read(&cpufreq_rwsem);
1478
4d34a67d 1479 return ret_freq;
1da177e4
LT
1480}
1481EXPORT_SYMBOL(cpufreq_get);
1482
8a25a2fd
KS
1483static struct subsys_interface cpufreq_interface = {
1484 .name = "cpufreq",
1485 .subsys = &cpu_subsys,
1486 .add_dev = cpufreq_add_dev,
1487 .remove_dev = cpufreq_remove_dev,
e00e56df
RW
1488};
1489
42d4dc3f 1490/**
e00e56df
RW
1491 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1492 *
1493 * This function is only executed for the boot processor. The other CPUs
1494 * have been put offline by means of CPU hotplug.
42d4dc3f 1495 */
e00e56df 1496static int cpufreq_bp_suspend(void)
42d4dc3f 1497{
e08f5f5b 1498 int ret = 0;
4bc5d341 1499
e00e56df 1500 int cpu = smp_processor_id();
3a3e9e06 1501 struct cpufreq_policy *policy;
42d4dc3f 1502
2d06d8c4 1503 pr_debug("suspending cpu %u\n", cpu);
42d4dc3f 1504
e00e56df 1505 /* If there's no policy for the boot CPU, we have nothing to do. */
3a3e9e06
VK
1506 policy = cpufreq_cpu_get(cpu);
1507 if (!policy)
e00e56df 1508 return 0;
42d4dc3f 1509
1c3d85dd 1510 if (cpufreq_driver->suspend) {
3a3e9e06 1511 ret = cpufreq_driver->suspend(policy);
ce6c3997 1512 if (ret)
42d4dc3f 1513 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
3a3e9e06 1514 "step on CPU %u\n", policy->cpu);
42d4dc3f
BH
1515 }
1516
3a3e9e06 1517 cpufreq_cpu_put(policy);
c9060494 1518 return ret;
42d4dc3f
BH
1519}
1520
1da177e4 1521/**
e00e56df 1522 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1da177e4
LT
1523 *
1524 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
ce6c3997
DB
1525 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1526 * restored. It will verify that the current freq is in sync with
1527 * what we believe it to be. This is a bit later than when it
1528 * should be, but nonethteless it's better than calling
1529 * cpufreq_driver->get() here which might re-enable interrupts...
e00e56df
RW
1530 *
1531 * This function is only executed for the boot CPU. The other CPUs have not
1532 * been turned on yet.
1da177e4 1533 */
e00e56df 1534static void cpufreq_bp_resume(void)
1da177e4 1535{
e08f5f5b 1536 int ret = 0;
4bc5d341 1537
e00e56df 1538 int cpu = smp_processor_id();
3a3e9e06 1539 struct cpufreq_policy *policy;
1da177e4 1540
2d06d8c4 1541 pr_debug("resuming cpu %u\n", cpu);
1da177e4 1542
e00e56df 1543 /* If there's no policy for the boot CPU, we have nothing to do. */
3a3e9e06
VK
1544 policy = cpufreq_cpu_get(cpu);
1545 if (!policy)
e00e56df 1546 return;
1da177e4 1547
1c3d85dd 1548 if (cpufreq_driver->resume) {
3a3e9e06 1549 ret = cpufreq_driver->resume(policy);
1da177e4
LT
1550 if (ret) {
1551 printk(KERN_ERR "cpufreq: resume failed in ->resume "
3a3e9e06 1552 "step on CPU %u\n", policy->cpu);
c9060494 1553 goto fail;
1da177e4
LT
1554 }
1555 }
1556
3a3e9e06 1557 schedule_work(&policy->update);
ce6c3997 1558
c9060494 1559fail:
3a3e9e06 1560 cpufreq_cpu_put(policy);
1da177e4
LT
1561}
1562
e00e56df
RW
1563static struct syscore_ops cpufreq_syscore_ops = {
1564 .suspend = cpufreq_bp_suspend,
1565 .resume = cpufreq_bp_resume,
1da177e4
LT
1566};
1567
9d95046e
BP
1568/**
1569 * cpufreq_get_current_driver - return current driver's name
1570 *
1571 * Return the name string of the currently loaded cpufreq driver
1572 * or NULL, if none.
1573 */
1574const char *cpufreq_get_current_driver(void)
1575{
1c3d85dd
RW
1576 if (cpufreq_driver)
1577 return cpufreq_driver->name;
1578
1579 return NULL;
9d95046e
BP
1580}
1581EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1da177e4
LT
1582
1583/*********************************************************************
1584 * NOTIFIER LISTS INTERFACE *
1585 *********************************************************************/
1586
1587/**
1588 * cpufreq_register_notifier - register a driver with cpufreq
1589 * @nb: notifier function to register
1590 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1591 *
32ee8c3e 1592 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1593 * are notified about clock rate changes (once before and once after
1594 * the transition), or a list of drivers that are notified about
1595 * changes in cpufreq policy.
1596 *
1597 * This function may sleep, and has the same return conditions as
e041c683 1598 * blocking_notifier_chain_register.
1da177e4
LT
1599 */
1600int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1601{
1602 int ret;
1603
d5aaffa9
DB
1604 if (cpufreq_disabled())
1605 return -EINVAL;
1606
74212ca4
CEB
1607 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1608
1da177e4
LT
1609 switch (list) {
1610 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1611 ret = srcu_notifier_chain_register(
e041c683 1612 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1613 break;
1614 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1615 ret = blocking_notifier_chain_register(
1616 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1617 break;
1618 default:
1619 ret = -EINVAL;
1620 }
1da177e4
LT
1621
1622 return ret;
1623}
1624EXPORT_SYMBOL(cpufreq_register_notifier);
1625
1da177e4
LT
1626/**
1627 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1628 * @nb: notifier block to be unregistered
bb176f7d 1629 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1da177e4
LT
1630 *
1631 * Remove a driver from the CPU frequency notifier list.
1632 *
1633 * This function may sleep, and has the same return conditions as
e041c683 1634 * blocking_notifier_chain_unregister.
1da177e4
LT
1635 */
1636int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1637{
1638 int ret;
1639
d5aaffa9
DB
1640 if (cpufreq_disabled())
1641 return -EINVAL;
1642
1da177e4
LT
1643 switch (list) {
1644 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1645 ret = srcu_notifier_chain_unregister(
e041c683 1646 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1647 break;
1648 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1649 ret = blocking_notifier_chain_unregister(
1650 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1651 break;
1652 default:
1653 ret = -EINVAL;
1654 }
1da177e4
LT
1655
1656 return ret;
1657}
1658EXPORT_SYMBOL(cpufreq_unregister_notifier);
1659
1660
1661/*********************************************************************
1662 * GOVERNORS *
1663 *********************************************************************/
1664
1da177e4
LT
1665int __cpufreq_driver_target(struct cpufreq_policy *policy,
1666 unsigned int target_freq,
1667 unsigned int relation)
1668{
1669 int retval = -EINVAL;
7249924e 1670 unsigned int old_target_freq = target_freq;
c32b6b8e 1671
a7b422cd
KRW
1672 if (cpufreq_disabled())
1673 return -ENODEV;
1674
7249924e
VK
1675 /* Make sure that target_freq is within supported range */
1676 if (target_freq > policy->max)
1677 target_freq = policy->max;
1678 if (target_freq < policy->min)
1679 target_freq = policy->min;
1680
1681 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1682 policy->cpu, target_freq, relation, old_target_freq);
5a1c0228
VK
1683
1684 if (target_freq == policy->cur)
1685 return 0;
1686
1c3d85dd
RW
1687 if (cpufreq_driver->target)
1688 retval = cpufreq_driver->target(policy, target_freq, relation);
90d45d17 1689
1da177e4
LT
1690 return retval;
1691}
1692EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1693
1da177e4
LT
1694int cpufreq_driver_target(struct cpufreq_policy *policy,
1695 unsigned int target_freq,
1696 unsigned int relation)
1697{
f1829e4a 1698 int ret = -EINVAL;
1da177e4 1699
5a01f2e8 1700 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
f1829e4a 1701 goto fail;
1da177e4
LT
1702
1703 ret = __cpufreq_driver_target(policy, target_freq, relation);
1704
5a01f2e8 1705 unlock_policy_rwsem_write(policy->cpu);
1da177e4 1706
f1829e4a 1707fail:
1da177e4
LT
1708 return ret;
1709}
1710EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1711
153d7f3f 1712/*
153d7f3f
AV
1713 * when "event" is CPUFREQ_GOV_LIMITS
1714 */
1da177e4 1715
e08f5f5b
GS
1716static int __cpufreq_governor(struct cpufreq_policy *policy,
1717 unsigned int event)
1da177e4 1718{
cc993cab 1719 int ret;
6afde10c
TR
1720
1721 /* Only must be defined when default governor is known to have latency
1722 restrictions, like e.g. conservative or ondemand.
1723 That this is the case is already ensured in Kconfig
1724 */
1725#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1726 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1727#else
1728 struct cpufreq_governor *gov = NULL;
1729#endif
1c256245
TR
1730
1731 if (policy->governor->max_transition_latency &&
1732 policy->cpuinfo.transition_latency >
1733 policy->governor->max_transition_latency) {
6afde10c
TR
1734 if (!gov)
1735 return -EINVAL;
1736 else {
1737 printk(KERN_WARNING "%s governor failed, too long"
1738 " transition latency of HW, fallback"
1739 " to %s governor\n",
1740 policy->governor->name,
1741 gov->name);
1742 policy->governor = gov;
1743 }
1c256245 1744 }
1da177e4 1745
fe492f3f
VK
1746 if (event == CPUFREQ_GOV_POLICY_INIT)
1747 if (!try_module_get(policy->governor->owner))
1748 return -EINVAL;
1da177e4 1749
2d06d8c4 1750 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
e08f5f5b 1751 policy->cpu, event);
95731ebb
XC
1752
1753 mutex_lock(&cpufreq_governor_lock);
56d07db2 1754 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
f73d3933
VK
1755 || (!policy->governor_enabled
1756 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
95731ebb
XC
1757 mutex_unlock(&cpufreq_governor_lock);
1758 return -EBUSY;
1759 }
1760
1761 if (event == CPUFREQ_GOV_STOP)
1762 policy->governor_enabled = false;
1763 else if (event == CPUFREQ_GOV_START)
1764 policy->governor_enabled = true;
1765
1766 mutex_unlock(&cpufreq_governor_lock);
1767
1da177e4
LT
1768 ret = policy->governor->governor(policy, event);
1769
4d5dcc42
VK
1770 if (!ret) {
1771 if (event == CPUFREQ_GOV_POLICY_INIT)
1772 policy->governor->initialized++;
1773 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1774 policy->governor->initialized--;
95731ebb
XC
1775 } else {
1776 /* Restore original values */
1777 mutex_lock(&cpufreq_governor_lock);
1778 if (event == CPUFREQ_GOV_STOP)
1779 policy->governor_enabled = true;
1780 else if (event == CPUFREQ_GOV_START)
1781 policy->governor_enabled = false;
1782 mutex_unlock(&cpufreq_governor_lock);
4d5dcc42 1783 }
b394058f 1784
fe492f3f
VK
1785 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1786 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
1da177e4
LT
1787 module_put(policy->governor->owner);
1788
1789 return ret;
1790}
1791
1da177e4
LT
1792int cpufreq_register_governor(struct cpufreq_governor *governor)
1793{
3bcb09a3 1794 int err;
1da177e4
LT
1795
1796 if (!governor)
1797 return -EINVAL;
1798
a7b422cd
KRW
1799 if (cpufreq_disabled())
1800 return -ENODEV;
1801
3fc54d37 1802 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 1803
b394058f 1804 governor->initialized = 0;
3bcb09a3
JF
1805 err = -EBUSY;
1806 if (__find_governor(governor->name) == NULL) {
1807 err = 0;
1808 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 1809 }
1da177e4 1810
32ee8c3e 1811 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 1812 return err;
1da177e4
LT
1813}
1814EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1815
1da177e4
LT
1816void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1817{
90e41bac
PB
1818#ifdef CONFIG_HOTPLUG_CPU
1819 int cpu;
1820#endif
1821
1da177e4
LT
1822 if (!governor)
1823 return;
1824
a7b422cd
KRW
1825 if (cpufreq_disabled())
1826 return;
1827
90e41bac
PB
1828#ifdef CONFIG_HOTPLUG_CPU
1829 for_each_present_cpu(cpu) {
1830 if (cpu_online(cpu))
1831 continue;
1832 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1833 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1834 }
1835#endif
1836
3fc54d37 1837 mutex_lock(&cpufreq_governor_mutex);
1da177e4 1838 list_del(&governor->governor_list);
3fc54d37 1839 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1840 return;
1841}
1842EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1843
1844
1da177e4
LT
1845/*********************************************************************
1846 * POLICY INTERFACE *
1847 *********************************************************************/
1848
1849/**
1850 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
1851 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1852 * is written
1da177e4
LT
1853 *
1854 * Reads the current cpufreq policy.
1855 */
1856int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1857{
1858 struct cpufreq_policy *cpu_policy;
1859 if (!policy)
1860 return -EINVAL;
1861
1862 cpu_policy = cpufreq_cpu_get(cpu);
1863 if (!cpu_policy)
1864 return -EINVAL;
1865
d5b73cd8 1866 memcpy(policy, cpu_policy, sizeof(*policy));
1da177e4
LT
1867
1868 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
1869 return 0;
1870}
1871EXPORT_SYMBOL(cpufreq_get_policy);
1872
153d7f3f 1873/*
e08f5f5b
GS
1874 * data : current policy.
1875 * policy : policy to be set.
153d7f3f 1876 */
3a3e9e06
VK
1877static int __cpufreq_set_policy(struct cpufreq_policy *policy,
1878 struct cpufreq_policy *new_policy)
1da177e4 1879{
7bd353a9 1880 int ret = 0, failed = 1;
1da177e4 1881
3a3e9e06
VK
1882 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1883 new_policy->min, new_policy->max);
1da177e4 1884
d5b73cd8 1885 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1da177e4 1886
3a3e9e06 1887 if (new_policy->min > policy->max || new_policy->max < policy->min) {
9c9a43ed
MD
1888 ret = -EINVAL;
1889 goto error_out;
1890 }
1891
1da177e4 1892 /* verify the cpu speed can be set within this limit */
3a3e9e06 1893 ret = cpufreq_driver->verify(new_policy);
1da177e4
LT
1894 if (ret)
1895 goto error_out;
1896
1da177e4 1897 /* adjust if necessary - all reasons */
e041c683 1898 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1899 CPUFREQ_ADJUST, new_policy);
1da177e4
LT
1900
1901 /* adjust if necessary - hardware incompatibility*/
e041c683 1902 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1903 CPUFREQ_INCOMPATIBLE, new_policy);
1da177e4 1904
bb176f7d
VK
1905 /*
1906 * verify the cpu speed can be set within this limit, which might be
1907 * different to the first one
1908 */
3a3e9e06 1909 ret = cpufreq_driver->verify(new_policy);
e041c683 1910 if (ret)
1da177e4 1911 goto error_out;
1da177e4
LT
1912
1913 /* notification of the new policy */
e041c683 1914 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1915 CPUFREQ_NOTIFY, new_policy);
1da177e4 1916
3a3e9e06
VK
1917 policy->min = new_policy->min;
1918 policy->max = new_policy->max;
1da177e4 1919
2d06d8c4 1920 pr_debug("new min and max freqs are %u - %u kHz\n",
3a3e9e06 1921 policy->min, policy->max);
1da177e4 1922
1c3d85dd 1923 if (cpufreq_driver->setpolicy) {
3a3e9e06 1924 policy->policy = new_policy->policy;
2d06d8c4 1925 pr_debug("setting range\n");
3a3e9e06 1926 ret = cpufreq_driver->setpolicy(new_policy);
1da177e4 1927 } else {
3a3e9e06 1928 if (new_policy->governor != policy->governor) {
1da177e4 1929 /* save old, working values */
3a3e9e06 1930 struct cpufreq_governor *old_gov = policy->governor;
1da177e4 1931
2d06d8c4 1932 pr_debug("governor switch\n");
1da177e4
LT
1933
1934 /* end old governor */
3a3e9e06
VK
1935 if (policy->governor) {
1936 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1937 unlock_policy_rwsem_write(new_policy->cpu);
1938 __cpufreq_governor(policy,
7bd353a9 1939 CPUFREQ_GOV_POLICY_EXIT);
3a3e9e06 1940 lock_policy_rwsem_write(new_policy->cpu);
7bd353a9 1941 }
1da177e4
LT
1942
1943 /* start new governor */
3a3e9e06
VK
1944 policy->governor = new_policy->governor;
1945 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1946 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
7bd353a9 1947 failed = 0;
955ef483 1948 } else {
3a3e9e06
VK
1949 unlock_policy_rwsem_write(new_policy->cpu);
1950 __cpufreq_governor(policy,
7bd353a9 1951 CPUFREQ_GOV_POLICY_EXIT);
3a3e9e06 1952 lock_policy_rwsem_write(new_policy->cpu);
955ef483 1953 }
7bd353a9
VK
1954 }
1955
1956 if (failed) {
1da177e4 1957 /* new governor failed, so re-start old one */
2d06d8c4 1958 pr_debug("starting governor %s failed\n",
3a3e9e06 1959 policy->governor->name);
1da177e4 1960 if (old_gov) {
3a3e9e06
VK
1961 policy->governor = old_gov;
1962 __cpufreq_governor(policy,
7bd353a9 1963 CPUFREQ_GOV_POLICY_INIT);
3a3e9e06 1964 __cpufreq_governor(policy,
e08f5f5b 1965 CPUFREQ_GOV_START);
1da177e4
LT
1966 }
1967 ret = -EINVAL;
1968 goto error_out;
1969 }
1970 /* might be a policy change, too, so fall through */
1971 }
2d06d8c4 1972 pr_debug("governor: change or update limits\n");
3de9bdeb 1973 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1da177e4
LT
1974 }
1975
7d5e350f 1976error_out:
1da177e4
LT
1977 return ret;
1978}
1979
1da177e4
LT
1980/**
1981 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1982 * @cpu: CPU which shall be re-evaluated
1983 *
25985edc 1984 * Useful for policy notifiers which have different necessities
1da177e4
LT
1985 * at different times.
1986 */
1987int cpufreq_update_policy(unsigned int cpu)
1988{
3a3e9e06
VK
1989 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1990 struct cpufreq_policy new_policy;
f1829e4a 1991 int ret;
1da177e4 1992
3a3e9e06 1993 if (!policy) {
f1829e4a
JL
1994 ret = -ENODEV;
1995 goto no_policy;
1996 }
1da177e4 1997
f1829e4a
JL
1998 if (unlikely(lock_policy_rwsem_write(cpu))) {
1999 ret = -EINVAL;
2000 goto fail;
2001 }
1da177e4 2002
2d06d8c4 2003 pr_debug("updating policy for CPU %u\n", cpu);
d5b73cd8 2004 memcpy(&new_policy, policy, sizeof(*policy));
3a3e9e06
VK
2005 new_policy.min = policy->user_policy.min;
2006 new_policy.max = policy->user_policy.max;
2007 new_policy.policy = policy->user_policy.policy;
2008 new_policy.governor = policy->user_policy.governor;
1da177e4 2009
bb176f7d
VK
2010 /*
2011 * BIOS might change freq behind our back
2012 * -> ask driver for current freq and notify governors about a change
2013 */
1c3d85dd 2014 if (cpufreq_driver->get) {
3a3e9e06
VK
2015 new_policy.cur = cpufreq_driver->get(cpu);
2016 if (!policy->cur) {
2d06d8c4 2017 pr_debug("Driver did not initialize current freq");
3a3e9e06 2018 policy->cur = new_policy.cur;
a85f7bd3 2019 } else {
3a3e9e06
VK
2020 if (policy->cur != new_policy.cur && cpufreq_driver->target)
2021 cpufreq_out_of_sync(cpu, policy->cur,
2022 new_policy.cur);
a85f7bd3 2023 }
0961dd0d
TR
2024 }
2025
3a3e9e06 2026 ret = __cpufreq_set_policy(policy, &new_policy);
1da177e4 2027
5a01f2e8
VP
2028 unlock_policy_rwsem_write(cpu);
2029
f1829e4a 2030fail:
3a3e9e06 2031 cpufreq_cpu_put(policy);
f1829e4a 2032no_policy:
1da177e4
LT
2033 return ret;
2034}
2035EXPORT_SYMBOL(cpufreq_update_policy);
2036
2760984f 2037static int cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
2038 unsigned long action, void *hcpu)
2039{
2040 unsigned int cpu = (unsigned long)hcpu;
8a25a2fd 2041 struct device *dev;
5302c3fb 2042 bool frozen = false;
c32b6b8e 2043
8a25a2fd
KS
2044 dev = get_cpu_device(cpu);
2045 if (dev) {
5302c3fb
SB
2046
2047 if (action & CPU_TASKS_FROZEN)
2048 frozen = true;
2049
2050 switch (action & ~CPU_TASKS_FROZEN) {
c32b6b8e 2051 case CPU_ONLINE:
5302c3fb 2052 __cpufreq_add_dev(dev, NULL, frozen);
23d32899 2053 cpufreq_update_policy(cpu);
c32b6b8e 2054 break;
5302c3fb 2055
c32b6b8e 2056 case CPU_DOWN_PREPARE:
cedb70af 2057 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
1aee40ac
SB
2058 break;
2059
2060 case CPU_POST_DEAD:
cedb70af 2061 __cpufreq_remove_dev_finish(dev, NULL, frozen);
c32b6b8e 2062 break;
5302c3fb 2063
5a01f2e8 2064 case CPU_DOWN_FAILED:
5302c3fb 2065 __cpufreq_add_dev(dev, NULL, frozen);
c32b6b8e
AR
2066 break;
2067 }
2068 }
2069 return NOTIFY_OK;
2070}
2071
9c36f746 2072static struct notifier_block __refdata cpufreq_cpu_notifier = {
bb176f7d 2073 .notifier_call = cpufreq_cpu_callback,
c32b6b8e 2074};
1da177e4
LT
2075
2076/*********************************************************************
2077 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2078 *********************************************************************/
2079
2080/**
2081 * cpufreq_register_driver - register a CPU Frequency driver
2082 * @driver_data: A struct cpufreq_driver containing the values#
2083 * submitted by the CPU Frequency driver.
2084 *
bb176f7d 2085 * Registers a CPU Frequency driver to this core code. This code
1da177e4 2086 * returns zero on success, -EBUSY when another driver got here first
32ee8c3e 2087 * (and isn't unregistered in the meantime).
1da177e4
LT
2088 *
2089 */
221dee28 2090int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
2091{
2092 unsigned long flags;
2093 int ret;
2094
a7b422cd
KRW
2095 if (cpufreq_disabled())
2096 return -ENODEV;
2097
1da177e4
LT
2098 if (!driver_data || !driver_data->verify || !driver_data->init ||
2099 ((!driver_data->setpolicy) && (!driver_data->target)))
2100 return -EINVAL;
2101
2d06d8c4 2102 pr_debug("trying to register driver %s\n", driver_data->name);
1da177e4
LT
2103
2104 if (driver_data->setpolicy)
2105 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2106
0d1857a1 2107 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2108 if (cpufreq_driver) {
0d1857a1 2109 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4dea5806 2110 return -EEXIST;
1da177e4 2111 }
1c3d85dd 2112 cpufreq_driver = driver_data;
0d1857a1 2113 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 2114
8a25a2fd 2115 ret = subsys_interface_register(&cpufreq_interface);
8f5bc2ab
JS
2116 if (ret)
2117 goto err_null_driver;
1da177e4 2118
1c3d85dd 2119 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1da177e4
LT
2120 int i;
2121 ret = -ENODEV;
2122
2123 /* check for at least one working CPU */
7a6aedfa
MT
2124 for (i = 0; i < nr_cpu_ids; i++)
2125 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1da177e4 2126 ret = 0;
7a6aedfa
MT
2127 break;
2128 }
1da177e4
LT
2129
2130 /* if all ->init() calls failed, unregister */
2131 if (ret) {
2d06d8c4 2132 pr_debug("no CPU initialized for driver %s\n",
e08f5f5b 2133 driver_data->name);
8a25a2fd 2134 goto err_if_unreg;
1da177e4
LT
2135 }
2136 }
2137
8f5bc2ab 2138 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2d06d8c4 2139 pr_debug("driver %s up and running\n", driver_data->name);
1da177e4 2140
8f5bc2ab 2141 return 0;
8a25a2fd
KS
2142err_if_unreg:
2143 subsys_interface_unregister(&cpufreq_interface);
8f5bc2ab 2144err_null_driver:
0d1857a1 2145 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2146 cpufreq_driver = NULL;
0d1857a1 2147 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4d34a67d 2148 return ret;
1da177e4
LT
2149}
2150EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2151
1da177e4
LT
2152/**
2153 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2154 *
bb176f7d 2155 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
2156 * the right to do so, i.e. if you have succeeded in initialising before!
2157 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2158 * currently not initialised.
2159 */
221dee28 2160int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
2161{
2162 unsigned long flags;
2163
1c3d85dd 2164 if (!cpufreq_driver || (driver != cpufreq_driver))
1da177e4 2165 return -EINVAL;
1da177e4 2166
2d06d8c4 2167 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 2168
8a25a2fd 2169 subsys_interface_unregister(&cpufreq_interface);
65edc68c 2170 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4 2171
6eed9404 2172 down_write(&cpufreq_rwsem);
0d1857a1 2173 write_lock_irqsave(&cpufreq_driver_lock, flags);
6eed9404 2174
1c3d85dd 2175 cpufreq_driver = NULL;
6eed9404 2176
0d1857a1 2177 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
6eed9404 2178 up_write(&cpufreq_rwsem);
1da177e4
LT
2179
2180 return 0;
2181}
2182EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8
VP
2183
2184static int __init cpufreq_core_init(void)
2185{
2186 int cpu;
2187
a7b422cd
KRW
2188 if (cpufreq_disabled())
2189 return -ENODEV;
2190
474deff7 2191 for_each_possible_cpu(cpu)
5a01f2e8 2192 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
8aa84ad8 2193
2361be23 2194 cpufreq_global_kobject = kobject_create();
8aa84ad8 2195 BUG_ON(!cpufreq_global_kobject);
e00e56df 2196 register_syscore_ops(&cpufreq_syscore_ops);
8aa84ad8 2197
5a01f2e8
VP
2198 return 0;
2199}
5a01f2e8 2200core_initcall(cpufreq_core_init);
This page took 0.899592 seconds and 5 git commands to generate.