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