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