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