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